Skip to content

Commit 5064616

Browse files
committed
test(cardano-chain-gen): Add a more realistic hard-fork test
Create a hard fork test from Babbage -> Conway by using a parameter update proposal, rather than hardcoding the epoch number
1 parent 50f6a2b commit 5064616

File tree

7 files changed

+179
-6
lines changed

7 files changed

+179
-6
lines changed

cardano-chain-gen/src/Cardano/Mock/Forging/Tx/Babbage.hs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
{-# LANGUAGE GADTs #-}
55
{-# LANGUAGE OverloadedStrings #-}
66
{-# LANGUAGE ScopedTypeVariables #-}
7+
{-# LANGUAGE TupleSections #-}
78
{-# LANGUAGE TypeApplications #-}
89
{-# LANGUAGE TypeOperators #-}
910
{-# OPTIONS_GHC -Wno-orphans #-}
@@ -44,6 +45,7 @@ module Cardano.Mock.Forging.Tx.Babbage (
4445
mkWitnesses,
4546
mkUTxOBabbage,
4647
mkUTxOCollBabbage,
48+
mkParamUpdateTx,
4749
mkFullTx,
4850
emptyTxBody,
4951
emptyTx,
@@ -90,6 +92,7 @@ import Lens.Micro
9092
import Ouroboros.Consensus.Cardano.Block (LedgerState)
9193
import Ouroboros.Consensus.Shelley.Eras (StandardBabbage, StandardCrypto)
9294
import Ouroboros.Consensus.Shelley.Ledger (ShelleyBlock)
95+
import Prelude hiding (map)
9396

9497
type BabbageUTxOIndex = UTxOIndex StandardBabbage
9598

@@ -515,6 +518,37 @@ emptyTx =
515518
, auxiliaryData = maybeToStrictMaybe Nothing
516519
}
517520

521+
mkParamUpdateTx :: Either ForgingError (AlonzoTx StandardBabbage)
522+
mkParamUpdateTx = Right (mkSimpleTx True txBody)
523+
where
524+
txBody =
525+
BabbageTxBody
526+
{ btbInputs = mempty
527+
, btbCollateral = mempty
528+
, btbReferenceInputs = mempty
529+
, btbOutputs = mempty
530+
, btbCollateralReturn = SNothing
531+
, btbTotalCollateral = SNothing
532+
, btbCerts = mempty
533+
, btbWithdrawals = Withdrawals mempty
534+
, btbTxFee = Coin 0
535+
, btbValidityInterval = ValidityInterval SNothing SNothing
536+
, btbUpdate = SJust $ Update update (EpochNo 1)
537+
, btbReqSignerHashes = mempty
538+
, btbMint = mempty
539+
, btbScriptIntegrityHash = SNothing
540+
, btbAuxDataHash = SNothing
541+
, btbTxNetworkId = SJust Testnet
542+
}
543+
update =
544+
ProposedPPUpdates $
545+
Map.fromList $
546+
map (,paramsUpdate) registeredShelleyGenesisKeys
547+
paramsUpdate =
548+
Core.emptyPParamsUpdate
549+
& ppuProtocolVersionL
550+
.~ SJust (ProtVer (natVersion @9) 0)
551+
518552
mkFullTx ::
519553
Int ->
520554
Integer ->

cardano-chain-gen/src/Cardano/Mock/Forging/Tx/Conway.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ module Cardano.Mock.Forging.Tx.Conway (
3232
mkRegTxCert,
3333
mkUnRegTxCert,
3434
mkDelegTxCert,
35+
Babbage.mkParamUpdateTx,
3536
mkFullTx,
3637
mkScriptMint,
3738
Babbage.mkScriptInp,

cardano-chain-gen/src/Cardano/Mock/Forging/Tx/Generic.hs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@ module Cardano.Mock.Forging.Tx.Generic (
2222
unregisteredAddresses,
2323
unregisteredStakeCredentials,
2424
unregisteredPools,
25+
registeredByronGenesisKeys,
26+
registeredShelleyGenesisKeys,
2527
consPoolParams,
2628
getPoolStakeCreds,
2729
) where
@@ -249,6 +251,17 @@ unregisteredGenesisKeys =
249251
, KeyHash "33323876542397465497834256329487563428975634827956348975"
250252
]
251253

254+
registeredByronGenesisKeys :: [KeyHash 'Genesis StandardCrypto]
255+
registeredByronGenesisKeys =
256+
[ KeyHash "1a3e49767796fd99b057ad54db3310fd640806fcb0927399bbca7b43"
257+
]
258+
259+
registeredShelleyGenesisKeys :: [KeyHash 'Genesis StandardCrypto]
260+
registeredShelleyGenesisKeys =
261+
[ KeyHash "30c3083efd794227fde2351a04500349d1b467556c30e35d6794a501"
262+
, KeyHash "471cc34983f6a2fd7b4018e3147532185d69a448d6570d46019e58e6"
263+
]
264+
252265
createStakeCredentials :: Int -> [StakeCredential StandardCrypto]
253266
createStakeCredentials n =
254267
fmap (KeyHashObj . KeyHash . mkDummyHash (Proxy @(ADDRHASH StandardCrypto))) [1 .. n]

cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -202,6 +202,7 @@ unitTests iom knownMigrations =
202202
"Hard Fork"
203203
[ test "fork from Babbage to Conway fixed epoch" Other.forkFixedEpoch
204204
, test "fork from Babbage to Conway and rollback" Other.rollbackFork
205+
, test "fork with protocol change proposal" Other.forkParam
205206
]
206207
]
207208
where

cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/Other.hs

Lines changed: 117 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,10 @@
11
{-# LANGUAGE DataKinds #-}
2+
{-# LANGUAGE ExplicitNamespaces #-}
3+
{-# LANGUAGE FlexibleContexts #-}
24
{-# LANGUAGE NumericUnderscores #-}
5+
{-# LANGUAGE OverloadedStrings #-}
6+
{-# LANGUAGE ScopedTypeVariables #-}
7+
{-# LANGUAGE TypeApplications #-}
38

49
module Test.Cardano.Db.Mock.Unit.Conway.Other (
510
-- * Different configs
@@ -16,25 +21,47 @@ module Test.Cardano.Db.Mock.Unit.Conway.Other (
1621
-- * Hard fork
1722
forkFixedEpoch,
1823
rollbackFork,
24+
forkParam,
1925
) where
2026

27+
import Cardano.Db (
28+
EntityField (
29+
EpochParamEpochNo,
30+
EpochParamProtocolMajor,
31+
ParamProposalEpochNo,
32+
ParamProposalProtocolMajor
33+
),
34+
EpochParam (),
35+
ParamProposal (),
36+
)
2137
import Cardano.DbSync.Era.Shelley.Generic.Util (unKeyHashRaw)
22-
import Cardano.Ledger.BaseTypes (EpochNo ())
38+
import Cardano.Ledger.BaseTypes (EpochNo (..))
2339
import Cardano.Ledger.Conway.TxCert (ConwayTxCert (..))
2440
import Cardano.Ledger.Core (PoolCert (..))
2541
import Cardano.Ledger.Credential (StakeCredential ())
2642
import Cardano.Ledger.Crypto (StandardCrypto ())
2743
import Cardano.Ledger.Keys (KeyHash (), KeyRole (..))
2844
import Cardano.Mock.ChainSync.Server (IOManager (), addBlock, rollback)
29-
import Cardano.Mock.Forging.Interpreter (forgeNext)
45+
import Cardano.Mock.Forging.Interpreter (Interpreter (), forgeNext, getCurrentEpoch)
3046
import qualified Cardano.Mock.Forging.Tx.Babbage as Babbage
3147
import qualified Cardano.Mock.Forging.Tx.Conway as Conway
3248
import Cardano.Mock.Forging.Tx.Generic (resolvePool)
3349
import Cardano.Mock.Forging.Types
34-
import Cardano.Prelude
50+
import Cardano.Prelude hiding (from)
3551
import Cardano.SMASH.Server.PoolDataLayer (PoolDataLayer (..), dbToServantPoolId)
3652
import Cardano.SMASH.Server.Types (DBFail (..))
3753
import Data.List (last)
54+
import Database.Esqueleto.Experimental (
55+
from,
56+
selectOne,
57+
table,
58+
unValue,
59+
val,
60+
where_,
61+
(==.),
62+
(^.),
63+
)
64+
import Database.Persist.Sql (SqlBackend ())
3865
import Ouroboros.Consensus.Shelley.Eras (StandardConway ())
3966
import Ouroboros.Network.Block (blockPoint)
4067
import Test.Cardano.Db.Mock.Config
@@ -436,3 +463,90 @@ rollbackFork =
436463
where
437464
configDir = "config-conway-hf-epoch1"
438465
testLabel = "conwayRollbackFork"
466+
467+
forkParam :: IOManager -> [(Text, Text)] -> Assertion
468+
forkParam =
469+
withFullConfig configDir testLabel $ \interpreter mockServer dbSync -> do
470+
startDBSync dbSync
471+
472+
-- Forge a block with stake credentials
473+
void $ Api.registerAllStakeCreds interpreter mockServer
474+
-- Protocol params aren't added to the DB until the following epoch
475+
epoch0 <- Api.fillUntilNextEpoch interpreter mockServer
476+
-- Wait for it to sync
477+
assertBlockNoBackoff dbSync (1 + length epoch0)
478+
-- Protocol major version should still match config
479+
assertEqBackoff
480+
dbSync
481+
(queryCurrentMajVer interpreter)
482+
(Just 7)
483+
[]
484+
"Unexpected protocol major version"
485+
486+
-- Propose a parameter update
487+
void $
488+
Api.withBabbageFindLeaderAndSubmitTx interpreter mockServer $
489+
const Babbage.mkParamUpdateTx
490+
-- Wait for it to sync
491+
assertBlockNoBackoff dbSync (2 + length epoch0)
492+
-- Query protocol param proposals
493+
assertEqBackoff
494+
dbSync
495+
(queryMajVerProposal interpreter)
496+
(Just 9)
497+
[]
498+
"Unexpected protocol major version proposal"
499+
500+
-- The fork will be applied on the first block of the next epoch
501+
epoch1 <- Api.fillUntilNextEpoch interpreter mockServer
502+
-- Wait for it to sync
503+
assertBlockNoBackoff dbSync $ 2 + length (epoch0 <> epoch1)
504+
-- Protocol major version should now be updated
505+
assertEqBackoff
506+
dbSync
507+
(queryCurrentMajVer interpreter)
508+
(Just 9)
509+
[]
510+
"Unexpected protocol major version"
511+
512+
-- Add a simple Conway tx
513+
void $
514+
Api.withConwayFindLeaderAndSubmitTx interpreter mockServer $
515+
Conway.mkPaymentTx (UTxOIndex 0) (UTxOIndex 1) 10_000 500
516+
-- Wait for it to sync
517+
assertBlockNoBackoff dbSync $ 3 + length (epoch0 <> epoch1)
518+
where
519+
testLabel = "conwayForkParam"
520+
configDir = babbageConfigDir
521+
522+
queryCurrentMajVer ::
523+
MonadIO m =>
524+
Interpreter ->
525+
ReaderT SqlBackend m (Maybe Word16)
526+
queryCurrentMajVer interpreter = do
527+
-- Look up current epoch from ledger
528+
EpochNo currentEpoch <- liftIO $ getCurrentEpoch interpreter
529+
530+
-- Query epoch params from database
531+
res <- selectOne $ do
532+
param <- from $ table @EpochParam
533+
where_ (param ^. EpochParamEpochNo ==. val currentEpoch)
534+
pure (param ^. EpochParamProtocolMajor)
535+
536+
pure $ unValue <$> res
537+
538+
queryMajVerProposal ::
539+
MonadIO m =>
540+
Interpreter ->
541+
ReaderT SqlBackend m (Maybe Word16)
542+
queryMajVerProposal interpreter = do
543+
-- Look up current epoch from ledger
544+
EpochNo currentEpoch <- liftIO $ getCurrentEpoch interpreter
545+
546+
-- Query proposals from database
547+
res <- selectOne $ do
548+
prop <- from $ table @ParamProposal
549+
where_ $ prop ^. ParamProposalEpochNo ==. val (Just currentEpoch)
550+
pure (prop ^. ParamProposalProtocolMajor)
551+
552+
pure $ join (unValue <$> res)

cardano-chain-gen/test/testfiles/config/genesis.json

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"maxLovelaceSupply": 60000000,
33
"securityParam": 10,
44
"slotsPerKESPeriod": 129600,
5-
"updateQuorum": 5,
5+
"updateQuorum": 2,
66
"activeSlotsCoeff": 0.2,
77
"protocolParams": {
88
"minUTxOValue": 0,
@@ -43,7 +43,16 @@
4343
},
4444
"networkId": "Testnet",
4545
"maxKESEvolutions": 60,
46-
"genDelegs": { },
46+
"genDelegs": {
47+
"30c3083efd794227fde2351a04500349d1b467556c30e35d6794a501": {
48+
"delegate": "3af50e522694318e8856d34021140817aa21d47ff62a4d0ddcba1924",
49+
"vrf": "2846ed605f6629976ec92e9e65140c1cb3a95c563c996592c926a2ae7e5a461f"
50+
},
51+
"471cc34983f6a2fd7b4018e3147532185d69a448d6570d46019e58e6": {
52+
"delegate": "1683652d05e83e25eb648a9543db33cf8ff1f7a2af9129d3a901dd4a",
53+
"vrf": "4df7c5a5b30469756bd09db60cbe9b0cf1b2447e0c41122d130768c179b7d166"
54+
}
55+
},
4756
"slotLength": 1,
4857
"systemStart": "2021-11-18T20:22:02Z",
4958
"epochLength": 500,
@@ -106,4 +115,4 @@
106115
"95be61304693df94ba89ff989e6542f174bb10d5ff49e8e8b1292519": "9f1b441b9b781b3c3abb43b25679dc17dbaaf116dddca1ad09dc1de0"
107116
}
108117
}
109-
}
118+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[2,7,10,12,19,24,26,27,35,37,39,61,70,74,75,84,99,101,103,112,120,125,136,137,138,141,146,151,166,180,188,205,207,215,216,226,234,235,246,253,261,272,275,279,284,285,287,299,307,311,314,315,318,321,341,345,353,355,361,362,366,367,372,377,379,389,391,393,396,401,403,410,427,435,455,460,465,470,484,486,487,488,497,499,506,510,511,512,516,519,520,523,530,532,537,540,550,558,559,571,579,581,583,600,604,605,607,610,613,621,630,631,651,658,665,669,678,679,687,692,693,696,698,699,712,725,728,742,758,765,774,775,778,787,789,792,794,796,801,804,808,815,816,818,821,828,837,841,842,851,853,863,866,873,880,883,885,886,896,901,903,904,909,911,914,915,918,920,927,931,932,938,949,956,966,976,981,983,988,994,996,999,1000,1002]

0 commit comments

Comments
 (0)