Skip to content

Commit

Permalink
#4928 Add reading genesis data in test using FromJSON instances
Browse files Browse the repository at this point in the history
  • Loading branch information
carbolymer committed May 22, 2023
1 parent 220e7dc commit efffbac
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 12 deletions.
1 change: 1 addition & 0 deletions cardano-testnet/cardano-testnet.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ library
, cardano-api ^>= 8.1.0.1
, cardano-cli
, cardano-crypto-class
, cardano-crypto-wrapper
, cardano-git-rev
, cardano-ledger-core
, cardano-ledger-byron
Expand Down
37 changes: 25 additions & 12 deletions cardano-testnet/src/Testnet/Util/Runtime.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,11 @@ import Control.Monad
import Control.Monad.Error.Class
import Control.Monad.IO.Class
import Control.Monad.Trans.Except
import Data.Aeson ((.:))
import qualified Data.Aeson as A
import qualified Data.Aeson.Types as A
import qualified Data.List as L
import Data.Monoid
import Data.Text (Text)
import Data.Time.Clock (UTCTime)
import Data.Time.Clock.POSIX (posixSecondsToUTCTime)
import GHC.Generics (Generic)
import GHC.Stack
import qualified Hedgehog as H
Expand All @@ -55,8 +53,14 @@ import qualified System.IO as IO
import qualified System.Process as IO

import Cardano.Api
import qualified Cardano.Chain.Genesis as G
import Cardano.Crypto.ProtocolMagic (RequiresNetworkMagic (..))
import Cardano.Ledger.Crypto (StandardCrypto)
import Cardano.Ledger.Shelley.Genesis
import Cardano.Node.Configuration.POM
import Cardano.Node.Handlers.Shutdown
import qualified Cardano.Node.Protocol.Byron as Byron
import Cardano.Node.Types
import qualified Testnet.Util.Process as H

data NodeLoggingFormat = NodeLoggingFormatAsJson | NodeLoggingFormatAsText deriving (Eq, Show)
Expand Down Expand Up @@ -129,15 +133,24 @@ shelleyGenesis TestnetRuntime{shelleyGenesisFile} = withFrozenCallStack $

getStartTime :: (H.MonadTest m, MonadIO m, HasCallStack) => FilePath -> TestnetRuntime -> m UTCTime
getStartTime tempRootPath TestnetRuntime{configurationFile} = withFrozenCallStack $ H.evalEither <=< H.evalIO . runExceptT $ do
testnetConfig <- ExceptT $ A.eitherDecodeFileStrict' configurationFile
byronGenesisFile <- liftEither $ A.parseEither
(A.withObject ("testnet configuration file: " <> configurationFile) (.: "ByronGenesisFile"))
testnetConfig
let byronGenesisFilePath = tempRootPath </> byronGenesisFile
genesisConfig <- ExceptT $ A.eitherDecodeFileStrict' byronGenesisFilePath
fmap posixSecondsToUTCTime . liftEither $ A.parseEither
(A.withObject ("byron genesis file: " <> byronGenesisFilePath) (.: "startTime"))
genesisConfig
NodeConfiguration{ncProtocolConfig} <- decodeNodeConfiguration configurationFile
byronGenesisFile <- case ncProtocolConfig of
NodeProtocolConfigurationCardano NodeByronProtocolConfiguration{npcByronGenesisFile} _ _ _ _ ->
pure $ unGenesisFile npcByronGenesisFile
NodeProtocolConfigurationByron NodeByronProtocolConfiguration{npcByronGenesisFile} ->
pure $ unGenesisFile npcByronGenesisFile
unsupported ->
throwError $ "cannot find byron configuration path in " <> configurationFile <> " - found instead: " <> show unsupported
let byronGenesisFilePath = GenesisFile $ tempRootPath </> byronGenesisFile
G.gdStartTime . G.configGenesisData <$> decodeGenesisFile byronGenesisFilePath
where
decodeNodeConfiguration =
(liftEither . makeNodeConfiguration) <=< (fmap (defaultPnc <>) . ExceptT . A.eitherDecodeFileStrict')
decodeGenesisFile fp = withExceptT displayError $ Byron.readGenesis fp Nothing RequiresNoMagic
defaultPnc = defaultPartialNodeConfiguration
{ pncValidateDB = Last $ Just True
, pncShutdownConfig = Last . Just $ ShutdownConfig Nothing (Just . ASlot $ SlotNo 42)
}

readNodeLoggingFormat :: String -> Either String NodeLoggingFormat
readNodeLoggingFormat = \case
Expand Down

0 comments on commit efffbac

Please sign in to comment.