Skip to content

Commit

Permalink
Update Testnet.Babbage with path construction functions
Browse files Browse the repository at this point in the history
  • Loading branch information
Jimbo4350 committed Jun 2, 2023
1 parent 9401c46 commit 4e65614
Showing 1 changed file with 70 additions and 66 deletions.
136 changes: 70 additions & 66 deletions cardano-testnet/src/Testnet/Babbage.hs
@@ -1,7 +1,6 @@
{-# LANGUAGE CPP #-}
{-# LANGUAGE NamedFieldPuns #-}
{-# LANGUAGE OverloadedStrings #-}
{-# LANGUAGE RecordWildCards #-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE TypeApplications #-}

Expand Down Expand Up @@ -36,7 +35,8 @@ import Testnet.Options
import qualified Testnet.Util.Assert as H
import Testnet.Util.Process (execCli_)
import Testnet.Util.Runtime (Delegator (..), PaymentKeyPair (..), PoolNode (PoolNode),
PoolNodeKeys (..), StakingKeyPair (..), TestnetRuntime (..), startNode)
PoolNodeKeys (..), StakingKeyPair (..), TestnetRuntime (..),
TmpAbsolutePath (..), makeLogDir, startNode)
import Testnet.Utils

import qualified Hedgehog as H
Expand All @@ -53,8 +53,12 @@ startTimeOffsetSeconds :: DTC.NominalDiffTime
startTimeOffsetSeconds = if OS.isWin32 then 90 else 15

babbageTestnet :: BabbageTestnetOptions -> H.Conf -> H.Integration TestnetRuntime
babbageTestnet testnetOptions H.Conf {..} = do
H.lbsWriteFile (tempAbsPath </> "byron.genesis.spec.json")
babbageTestnet testnetOptions H.Conf {H.configurationTemplate, H.tempAbsPath, H.testnetMagic} = do
let logDir = makeLogDir tempAbsPath
tempAbsPath' = unTmpAbsPath tempAbsPath
H.createDirectoryIfMissing_ logDir

H.lbsWriteFile (tempAbsPath' </> "byron.genesis.spec.json")
. encode $ defaultByronProtocolParamsJsonValue

void $ H.note OS.os
Expand All @@ -65,28 +69,28 @@ babbageTestnet testnetOptions H.Conf {..} = do
testnetMagic
startTime
testnetOptions
(tempAbsPath </> "byron.genesis.spec.json")
(tempAbsPath </> "byron-gen-command")
(tempAbsPath' </> "byron.genesis.spec.json")
(tempAbsPath' </> "byron-gen-command")


-- Because in Babbage the overlay schedule and decentralization parameter
-- are deprecated, we must use the "create-staked" cli command to create
-- SPOs in the ShelleyGenesis

alonzoBabbageTestGenesisJsonTargetFile <- H.noteShow $ tempAbsPath </> "genesis.alonzo.spec.json"
alonzoBabbageTestGenesisJsonTargetFile <- H.noteShow $ tempAbsPath' </> "genesis.alonzo.spec.json"
gen <- H.evalEither $ first displayError defaultAlonzoGenesis
H.evalIO $ LBS.writeFile alonzoBabbageTestGenesisJsonTargetFile $ encode gen

conwayBabbageTestGenesisJsonTargetFile <- H.noteShow $ tempAbsPath </> "genesis.conway.spec.json"
conwayBabbageTestGenesisJsonTargetFile <- H.noteShow $ tempAbsPath' </> "genesis.conway.spec.json"
H.evalIO $ LBS.writeFile conwayBabbageTestGenesisJsonTargetFile $ encode defaultConwayGenesis

configurationFile <- H.noteShow $ tempAbsPath </> "configuration.yaml"
configurationFile <- H.noteShow $ tempAbsPath' </> "configuration.yaml"

let numPoolNodes = 3 :: Int

execCli_
[ "genesis", "create-staked"
, "--genesis-dir", tempAbsPath
, "--genesis-dir", tempAbsPath'
, "--testnet-magic", show @Int testnetMagic
, "--gen-pools", show @Int 3
, "--supply", "1000000000000"
Expand All @@ -97,29 +101,29 @@ babbageTestnet testnetOptions H.Conf {..} = do

poolKeys <- H.noteShow $ flip fmap [1..numPoolNodes] $ \n ->
PoolNodeKeys
{ poolNodeKeysColdVkey = tempAbsPath </> "pools" </> "cold" <> show n <> ".vkey"
, poolNodeKeysColdSkey = tempAbsPath </> "pools" </> "cold" <> show n <> ".skey"
, poolNodeKeysVrfVkey = tempAbsPath </> "node-spo" <> show n </> "vrf.vkey"
, poolNodeKeysVrfSkey = tempAbsPath </> "node-spo" <> show n </> "vrf.skey"
, poolNodeKeysStakingVkey = tempAbsPath </> "pools" </> "staking-reward" <> show n <> ".vkey"
, poolNodeKeysStakingSkey = tempAbsPath </> "pools" </> "staking-reward" <> show n <> ".skey"
{ poolNodeKeysColdVkey = tempAbsPath' </> "pools" </> "cold" <> show n <> ".vkey"
, poolNodeKeysColdSkey = tempAbsPath' </> "pools" </> "cold" <> show n <> ".skey"
, poolNodeKeysVrfVkey = tempAbsPath' </> "node-spo" <> show n </> "vrf.vkey"
, poolNodeKeysVrfSkey = tempAbsPath' </> "node-spo" <> show n </> "vrf.skey"
, poolNodeKeysStakingVkey = tempAbsPath' </> "pools" </> "staking-reward" <> show n <> ".vkey"
, poolNodeKeysStakingSkey = tempAbsPath' </> "pools" </> "staking-reward" <> show n <> ".skey"
}

wallets <- forM [1..3] $ \idx -> do
pure $ PaymentKeyPair
{ paymentSKey = tempAbsPath </> "utxo-keys/utxo" <> show @Int idx <> ".skey"
, paymentVKey = tempAbsPath </> "utxo-keys/utxo" <> show @Int idx <> ".vkey"
{ paymentSKey = tempAbsPath' </> "utxo-keys/utxo" <> show @Int idx <> ".skey"
, paymentVKey = tempAbsPath' </> "utxo-keys/utxo" <> show @Int idx <> ".vkey"
}

delegators <- forM [1..3] $ \idx -> do
pure $ Delegator
{ paymentKeyPair = PaymentKeyPair
{ paymentSKey = tempAbsPath </> "stake-delegator-keys/payment" <> show @Int idx <> ".skey"
, paymentVKey = tempAbsPath </> "stake-delegator-keys/payment" <> show @Int idx <> ".vkey"
{ paymentSKey = tempAbsPath' </> "stake-delegator-keys/payment" <> show @Int idx <> ".skey"
, paymentVKey = tempAbsPath' </> "stake-delegator-keys/payment" <> show @Int idx <> ".vkey"
}
, stakingKeyPair = StakingKeyPair
{ stakingSKey = tempAbsPath </> "stake-delegator-keys/staking" <> show @Int idx <> ".skey"
, stakingVKey = tempAbsPath </> "stake-delegator-keys/staking" <> show @Int idx <> ".vkey"
{ stakingSKey = tempAbsPath' </> "stake-delegator-keys/staking" <> show @Int idx <> ".skey"
, stakingVKey = tempAbsPath' </> "stake-delegator-keys/staking" <> show @Int idx <> ".vkey"
}
}

Expand All @@ -128,24 +132,24 @@ babbageTestnet testnetOptions H.Conf {..} = do
-- Create the node directories

forM_ spoNodes $ \node -> do
H.createDirectoryIfMissing_ (tempAbsPath </> node)
H.createDirectoryIfMissing_ (tempAbsPath' </> node)

-- Here we move all of the keys etc generated by create-staked
-- for the nodes to use

-- Move all genesis related files

genesisByronDir <- H.createDirectoryIfMissing $ tempAbsPath </> "byron"
genesisShelleyDir <- H.createDirectoryIfMissing $ tempAbsPath </> "shelley"
genesisByronDir <- H.createDirectoryIfMissing $ tempAbsPath' </> "byron"
genesisShelleyDir <- H.createDirectoryIfMissing $ tempAbsPath' </> "shelley"

files <- H.listDirectory tempAbsPath
files <- H.listDirectory tempAbsPath'
forM_ files $ \file -> do
H.note file

H.renameFile (tempAbsPath </> "byron-gen-command/genesis.json") (genesisByronDir </> "genesis.json")
H.renameFile (tempAbsPath </> "genesis.alonzo.json") (genesisShelleyDir </> "genesis.alonzo.json")
H.renameFile (tempAbsPath </> "genesis.conway.json") (genesisShelleyDir </> "genesis.conway.json")
H.renameFile (tempAbsPath </> "genesis.json") (genesisShelleyDir </> "genesis.json")
H.renameFile (tempAbsPath' </> "byron-gen-command/genesis.json") (genesisByronDir </> "genesis.json")
H.renameFile (tempAbsPath' </> "genesis.alonzo.json") (genesisShelleyDir </> "genesis.alonzo.json")
H.renameFile (tempAbsPath' </> "genesis.conway.json") (genesisShelleyDir </> "genesis.conway.json")
H.renameFile (tempAbsPath' </> "genesis.json") (genesisShelleyDir </> "genesis.json")

H.rewriteJsonFile (genesisByronDir </> "genesis.json") $ J.rewriteObject
$ flip HM.adjust "protocolConsts"
Expand Down Expand Up @@ -179,10 +183,10 @@ babbageTestnet testnetOptions H.Conf {..} = do
-- rather as an executable will allow us to get the genesis files paths in a more
-- direct fashion.

byronGenesisHash <- getByronGenesisHash $ tempAbsPath </> "byron/genesis.json"
shelleyGenesisHash <- getShelleyGenesisHash (tempAbsPath </> "shelley/genesis.json") "ShelleyGenesisHash"
alonzoGenesisHash <- getShelleyGenesisHash (tempAbsPath </> "shelley/genesis.alonzo.json") "AlonzoGenesisHash"
conwayGenesisHash <- getShelleyGenesisHash (tempAbsPath </> "shelley/genesis.conway.json") "ConwayGenesisHash"
byronGenesisHash <- getByronGenesisHash $ tempAbsPath' </> "byron/genesis.json"
shelleyGenesisHash <- getShelleyGenesisHash (tempAbsPath' </> "shelley/genesis.json") "ShelleyGenesisHash"
alonzoGenesisHash <- getShelleyGenesisHash (tempAbsPath' </> "shelley/genesis.alonzo.json") "AlonzoGenesisHash"
conwayGenesisHash <- getShelleyGenesisHash (tempAbsPath' </> "shelley/genesis.conway.json") "ConwayGenesisHash"


let finalYamlConfig :: LBS.ByteString
Expand All @@ -193,40 +197,40 @@ babbageTestnet testnetOptions H.Conf {..} = do
, conwayGenesisHash
, defaultYamlHardforkViaConfig (AnyCardanoEra BabbageEra)]

H.evalIO $ LBS.writeFile (tempAbsPath </> "configuration.yaml") finalYamlConfig
H.evalIO $ LBS.writeFile (tempAbsPath' </> "configuration.yaml") finalYamlConfig


H.renameFile (tempAbsPath </> "pools/vrf1.skey") (tempAbsPath </> "node-spo1/vrf.skey")
H.renameFile (tempAbsPath </> "pools/vrf2.skey") (tempAbsPath </> "node-spo2/vrf.skey")
H.renameFile (tempAbsPath </> "pools/vrf3.skey") (tempAbsPath </> "node-spo3/vrf.skey")
H.renameFile (tempAbsPath' </> "pools/vrf1.skey") (tempAbsPath' </> "node-spo1/vrf.skey")
H.renameFile (tempAbsPath' </> "pools/vrf2.skey") (tempAbsPath' </> "node-spo2/vrf.skey")
H.renameFile (tempAbsPath' </> "pools/vrf3.skey") (tempAbsPath' </> "node-spo3/vrf.skey")

H.renameFile (tempAbsPath </> "pools/opcert1.cert") (tempAbsPath </> "node-spo1/opcert.cert")
H.renameFile (tempAbsPath </> "pools/opcert2.cert") (tempAbsPath </> "node-spo2/opcert.cert")
H.renameFile (tempAbsPath </> "pools/opcert3.cert") (tempAbsPath </> "node-spo3/opcert.cert")
H.renameFile (tempAbsPath' </> "pools/opcert1.cert") (tempAbsPath' </> "node-spo1/opcert.cert")
H.renameFile (tempAbsPath' </> "pools/opcert2.cert") (tempAbsPath' </> "node-spo2/opcert.cert")
H.renameFile (tempAbsPath' </> "pools/opcert3.cert") (tempAbsPath' </> "node-spo3/opcert.cert")

H.renameFile (tempAbsPath </> "pools/kes1.skey") (tempAbsPath </> "node-spo1/kes.skey")
H.renameFile (tempAbsPath </> "pools/kes2.skey") (tempAbsPath </> "node-spo2/kes.skey")
H.renameFile (tempAbsPath </> "pools/kes3.skey") (tempAbsPath </> "node-spo3/kes.skey")
H.renameFile (tempAbsPath' </> "pools/kes1.skey") (tempAbsPath' </> "node-spo1/kes.skey")
H.renameFile (tempAbsPath' </> "pools/kes2.skey") (tempAbsPath' </> "node-spo2/kes.skey")
H.renameFile (tempAbsPath' </> "pools/kes3.skey") (tempAbsPath' </> "node-spo3/kes.skey")

-- Byron related

H.renameFile (tempAbsPath </> "byron-gen-command/delegate-keys.000.key") (tempAbsPath </> "node-spo1/byron-delegate.key")
H.renameFile (tempAbsPath </> "byron-gen-command/delegate-keys.001.key") (tempAbsPath </> "node-spo2/byron-delegate.key")
H.renameFile (tempAbsPath </> "byron-gen-command/delegate-keys.002.key") (tempAbsPath </> "node-spo3/byron-delegate.key")
H.renameFile (tempAbsPath' </> "byron-gen-command/delegate-keys.000.key") (tempAbsPath' </> "node-spo1/byron-delegate.key")
H.renameFile (tempAbsPath' </> "byron-gen-command/delegate-keys.001.key") (tempAbsPath' </> "node-spo2/byron-delegate.key")
H.renameFile (tempAbsPath' </> "byron-gen-command/delegate-keys.002.key") (tempAbsPath' </> "node-spo3/byron-delegate.key")

H.renameFile (tempAbsPath </> "byron-gen-command/delegation-cert.000.json") (tempAbsPath </> "node-spo1/byron-delegation.cert")
H.renameFile (tempAbsPath </> "byron-gen-command/delegation-cert.001.json") (tempAbsPath </> "node-spo2/byron-delegation.cert")
H.renameFile (tempAbsPath </> "byron-gen-command/delegation-cert.002.json") (tempAbsPath </> "node-spo3/byron-delegation.cert")
H.renameFile (tempAbsPath' </> "byron-gen-command/delegation-cert.000.json") (tempAbsPath' </> "node-spo1/byron-delegation.cert")
H.renameFile (tempAbsPath' </> "byron-gen-command/delegation-cert.001.json") (tempAbsPath' </> "node-spo2/byron-delegation.cert")
H.renameFile (tempAbsPath' </> "byron-gen-command/delegation-cert.002.json") (tempAbsPath' </> "node-spo3/byron-delegation.cert")

H.writeFile (tempAbsPath </> "node-spo1/port") "3001"
H.writeFile (tempAbsPath </> "node-spo2/port") "3002"
H.writeFile (tempAbsPath </> "node-spo3/port") "3003"
H.writeFile (tempAbsPath' </> "node-spo1/port") "3001"
H.writeFile (tempAbsPath' </> "node-spo2/port") "3002"
H.writeFile (tempAbsPath' </> "node-spo3/port") "3003"


-- Make topology files
-- TODO generalise this over the N BFT nodes and pool nodes

H.lbsWriteFile (tempAbsPath </> "node-spo1/topology.json") $ encode $
H.lbsWriteFile (tempAbsPath' </> "node-spo1/topology.json") $ encode $
object
[ "Producers" .= toJSON
[ object
Expand All @@ -242,7 +246,7 @@ babbageTestnet testnetOptions H.Conf {..} = do
]
]

H.lbsWriteFile (tempAbsPath </> "node-spo2/topology.json") $ encode $
H.lbsWriteFile (tempAbsPath' </> "node-spo2/topology.json") $ encode $
object
[ "Producers" .= toJSON
[ object
Expand All @@ -258,7 +262,7 @@ babbageTestnet testnetOptions H.Conf {..} = do
]
]

H.lbsWriteFile (tempAbsPath </> "node-spo3/topology.json") $ encode $
H.lbsWriteFile (tempAbsPath' </> "node-spo3/topology.json") $ encode $
object
[ "Producers" .= toJSON
[ object
Expand All @@ -275,24 +279,24 @@ babbageTestnet testnetOptions H.Conf {..} = do
]

poolNodes <- forM (L.zip spoNodes poolKeys) $ \(node,key) -> do
runtime <- startNode tempBaseAbsPath tempAbsPath logDir socketDir node
runtime <- startNode (TmpAbsolutePath tempAbsPath') node
[ "run"
, "--config", tempAbsPath </> "configuration.yaml"
, "--topology", tempAbsPath </> node </> "topology.json"
, "--database-path", tempAbsPath </> node </> "db"
, "--shelley-kes-key", tempAbsPath </> node </> "kes.skey"
, "--shelley-vrf-key", tempAbsPath </> node </> "vrf.skey"
, "--byron-delegation-certificate", tempAbsPath </> node </> "byron-delegation.cert"
, "--byron-signing-key", tempAbsPath </> node </> "byron-delegate.key"
, "--shelley-operational-certificate", tempAbsPath </> node </> "opcert.cert"
, "--config", tempAbsPath' </> "configuration.yaml"
, "--topology", tempAbsPath' </> node </> "topology.json"
, "--database-path", tempAbsPath' </> node </> "db"
, "--shelley-kes-key", tempAbsPath' </> node </> "kes.skey"
, "--shelley-vrf-key", tempAbsPath' </> node </> "vrf.skey"
, "--byron-delegation-certificate", tempAbsPath' </> node </> "byron-delegation.cert"
, "--byron-signing-key", tempAbsPath' </> node </> "byron-delegate.key"
, "--shelley-operational-certificate", tempAbsPath' </> node </> "opcert.cert"
]
return $ PoolNode runtime key

now <- H.noteShowIO DTC.getCurrentTime
deadline <- H.noteShow $ DTC.addUTCTime 90 now

forM_ spoNodes $ \node -> do
nodeStdoutFile <- H.noteTempFile logDir $ node <> ".stdout.log"
nodeStdoutFile <- H.noteTempFile (makeLogDir $ TmpAbsolutePath tempAbsPath') $ node <> ".stdout.log"
H.assertChainExtended deadline (babbageNodeLoggingFormat testnetOptions) nodeStdoutFile

H.noteShowIO_ DTC.getCurrentTime
Expand Down

0 comments on commit 4e65614

Please sign in to comment.