Skip to content

Commit

Permalink
Add hashes of genesis.json to node configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
eyeinsky committed Nov 29, 2022
1 parent 15556f3 commit 5dec75a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 1 deletion.
2 changes: 2 additions & 0 deletions cardano-testnet/cardano-testnet.cabal
Expand Up @@ -33,7 +33,9 @@ library
, bytestring
, cardano-api
, cardano-cli
, cardano-crypto-class
, cardano-git-rev
, cardano-ledger-byron
, cardano-node
, containers
, directory
Expand Down
32 changes: 31 additions & 1 deletion cardano-testnet/src/Testnet/Cardano.hs
Expand Up @@ -19,20 +19,26 @@ module Testnet.Cardano

import Prelude
import Control.Monad
import Control.Monad.IO.Class (liftIO)
import Control.Monad.IO.Class (liftIO, MonadIO)
import Data.Aeson ((.=))
import Data.ByteString.Lazy (ByteString)
import Data.List ((\\))
import Data.Maybe
import Data.String
import qualified Hedgehog as H
import Hedgehog.Extras.Stock.IO.Network.Sprocket (Sprocket (..))
import Hedgehog.Extras.Stock.Time (formatIso8601, showUTCTimeSeconds)
import Ouroboros.Network.PeerSelection.LedgerPeers (UseLedgerAfter (..))
import Ouroboros.Network.PeerSelection.RelayAccessPoint (RelayAccessPoint (..))
import System.FilePath.Posix ((</>))
import Control.Monad.Trans.Except
import qualified Data.ByteString as BS
import qualified Cardano.Crypto.Hash.Class
import qualified Cardano.Crypto.Hash.Blake2b

import qualified Cardano.Node.Configuration.Topology as NonP2P
import qualified Cardano.Node.Configuration.TopologyP2P as P2P
import Cardano.Chain.Genesis (readGenesisData, GenesisHash(unGenesisHash))
import qualified Data.Aeson as J
import qualified Data.HashMap.Lazy as HM
import qualified Data.List as L
Expand Down Expand Up @@ -706,6 +712,15 @@ cardanoTestnet testnetOptions H.Conf {..} = do
-- Generated a signed 'do it all' transaction:
H.assertIO . IO.doesFileExist $ tempAbsPath </> "tx1.tx"

-- Add Byron, Shelley and Alonzo genesis hashes to node configuration
byronGenesisHash <- getByronGenesisHash $ tempAbsPath </> "byron/genesis.json"
shelleyGenesisHash <- getShelleyGenesisHash $ tempAbsPath </> "shelley/genesis.json"
alonzoGenesisHash <- getShelleyGenesisHash $ tempAbsPath </> "shelley/genesis.alonzo.json"
H.rewriteYamlFile (tempAbsPath </> "configuration.yaml") . J.rewriteObject
$ HM.insert "ByronGenesisHash" byronGenesisHash
. HM.insert "ShelleyGenesisHash" shelleyGenesisHash
. HM.insert "AlonzoGenesisHash" alonzoGenesisHash

--------------------------------
-- Launch cluster of three nodes

Expand Down Expand Up @@ -761,3 +776,18 @@ cardanoTestnet testnetOptions H.Conf {..} = do
, wallets
, delegators = [] -- TODO this should be populated
}

-- * Generate hashes for genesis.json files

getByronGenesisHash :: (H.MonadTest m, MonadIO m) => FilePath -> m J.Value
getByronGenesisHash path = do
e <- runExceptT $ readGenesisData path
(_, genesisHash) <- H.leftFail e
let genesisHash' = J.toJSON $ unGenesisHash genesisHash
pure genesisHash'

getShelleyGenesisHash :: (H.MonadTest m, MonadIO m) => FilePath -> m J.Value
getShelleyGenesisHash path = do
content <- liftIO $ BS.readFile path
let genesisHash = Cardano.Crypto.Hash.Class.hashWith id content :: Cardano.Crypto.Hash.Class.Hash Cardano.Crypto.Hash.Blake2b.Blake2b_256 BS.ByteString
pure $ J.toJSON genesisHash

0 comments on commit 5dec75a

Please sign in to comment.