@@ -20,22 +20,24 @@ module Test.Cardano.Db.Mock.UnifiedApi
20
20
21
21
import Data.Word (Word64 )
22
22
23
- import Cardano.Slotting.Slot (SlotNo (.. ))
24
-
23
+ import Cardano.Ledger.Alonzo (AlonzoEra )
25
24
import qualified Cardano.Ledger.Core as Core
26
25
27
- import Ouroboros.Consensus.Cardano.Block (StandardAlonzo , StandardBabbage ,
28
- StandardShelley )
29
- import Ouroboros.Consensus.Ledger.Basics (LedgerState )
30
- import Ouroboros.Consensus.Shelley.Ledger (ShelleyBlock )
31
-
32
26
import Cardano.Mock.ChainSync.Server
33
27
import Cardano.Mock.Forging.Interpreter
34
28
import Cardano.Mock.Forging.Types
35
29
30
+ import Cardano.Slotting.Slot (SlotNo (.. ))
31
+
36
32
import Control.Monad (forM , replicateM )
37
33
import Control.Monad.Class.MonadSTM.Strict (atomically )
38
34
35
+ import Ouroboros.Consensus.Cardano.Block (ShelleyEra , StandardAlonzo , StandardBabbage ,
36
+ StandardCrypto )
37
+ import Ouroboros.Consensus.Ledger.Basics (LedgerState )
38
+ import Ouroboros.Consensus.Shelley.Ledger (ShelleyBlock )
39
+
40
+
39
41
40
42
forgeNextAndSubmit :: Interpreter -> ServerHandle IO CardanoBlock -> MockBlock -> IO CardanoBlock
41
43
forgeNextAndSubmit inter mockServer testBlock = do
@@ -45,9 +47,9 @@ forgeNextAndSubmit inter mockServer testBlock = do
45
47
46
48
forgeNextFindLeaderAndSubmit :: Interpreter -> ServerHandle IO CardanoBlock -> [TxEra ] -> IO CardanoBlock
47
49
forgeNextFindLeaderAndSubmit interpreter mockServer txs' = do
48
- blk <- forgeNextFindLeader interpreter txs'
49
- atomically $ addBlock mockServer blk
50
- pure blk
50
+ blk <- forgeNextFindLeader interpreter txs'
51
+ atomically $ addBlock mockServer blk
52
+ pure blk
51
53
52
54
forgeNextSkipSlotsFindLeaderAndSubmit :: Interpreter -> ServerHandle IO CardanoBlock -> Word64 -> [TxEra ] -> IO CardanoBlock
53
55
forgeNextSkipSlotsFindLeaderAndSubmit interpreter mockServer skipSlots txs' = do
@@ -56,12 +58,13 @@ forgeNextSkipSlotsFindLeaderAndSubmit interpreter mockServer skipSlots txs' = d
56
58
pure blk
57
59
58
60
forgeAndSubmitBlocks :: Interpreter -> ServerHandle IO CardanoBlock -> Int -> IO [CardanoBlock ]
59
- forgeAndSubmitBlocks interpreter mockServer blocksToCreate = do
61
+ forgeAndSubmitBlocks interpreter mockServer blocksToCreate =
60
62
forM [1 .. blocksToCreate] $ \ _ -> forgeNextFindLeaderAndSubmit interpreter mockServer []
61
63
62
64
withAlonzoFindLeaderAndSubmit
63
65
:: Interpreter -> ServerHandle IO CardanoBlock
64
- -> (LedgerState (ShelleyBlock TPraosStandard StandardAlonzo ) -> Either ForgingError [Core. Tx StandardAlonzo ])
66
+ -> (LedgerState (ShelleyBlock TPraosStandard StandardAlonzo )
67
+ -> Either ForgingError [Core. Tx (AlonzoEra StandardCrypto )])
65
68
-> IO CardanoBlock
66
69
withAlonzoFindLeaderAndSubmit interpreter mockServer mkTxs = do
67
70
alTxs <- withAlonzoLedgerState interpreter mkTxs
@@ -77,7 +80,8 @@ withBabbageFindLeaderAndSubmit interpreter mockServer mkTxs = do
77
80
78
81
withAlonzoFindLeaderAndSubmitTx
79
82
:: Interpreter -> ServerHandle IO CardanoBlock
80
- -> (LedgerState (ShelleyBlock TPraosStandard StandardAlonzo ) -> Either ForgingError (Core. Tx StandardAlonzo ))
83
+ -> (LedgerState (ShelleyBlock TPraosStandard StandardAlonzo )
84
+ -> Either ForgingError (Core. Tx (AlonzoEra StandardCrypto )))
81
85
-> IO CardanoBlock
82
86
withAlonzoFindLeaderAndSubmitTx interpreter mockServer mkTxs = do
83
87
withAlonzoFindLeaderAndSubmit interpreter mockServer $ \ st -> do
@@ -95,20 +99,22 @@ withBabbageFindLeaderAndSubmitTx interpreter mockServer mkTxs = do
95
99
96
100
withShelleyFindLeaderAndSubmit
97
101
:: Interpreter -> ServerHandle IO CardanoBlock
98
- -> (LedgerState (ShelleyBlock TPraosStandard StandardShelley ) -> Either ForgingError [Core. Tx StandardShelley ])
102
+ -> (LedgerState (ShelleyBlock TPraosStandard (ShelleyEra StandardCrypto ))
103
+ -> Either ForgingError [Core. Tx (ShelleyEra StandardCrypto )])
99
104
-> IO CardanoBlock
100
105
withShelleyFindLeaderAndSubmit interpreter mockServer mkTxs = do
101
106
alTxs <- withShelleyLedgerState interpreter mkTxs
102
107
forgeNextFindLeaderAndSubmit interpreter mockServer (TxShelley <$> alTxs)
103
108
104
109
withShelleyFindLeaderAndSubmitTx
105
110
:: Interpreter -> ServerHandle IO CardanoBlock
106
- -> (LedgerState (ShelleyBlock TPraosStandard StandardShelley ) -> Either ForgingError (Core. Tx StandardShelley ))
111
+ -> (LedgerState (ShelleyBlock TPraosStandard (ShelleyEra StandardCrypto ))
112
+ -> Either ForgingError (Core. Tx (ShelleyEra StandardCrypto )))
107
113
-> IO CardanoBlock
108
- withShelleyFindLeaderAndSubmitTx interpreter mockServer mkTxs = do
109
- withShelleyFindLeaderAndSubmit interpreter mockServer $ \ st -> do
110
- tx <- mkTxs st
111
- pure [tx]
114
+ withShelleyFindLeaderAndSubmitTx interpreter mockServer mkTxs =
115
+ withShelleyFindLeaderAndSubmit interpreter mockServer $ \ st -> do
116
+ tx <- mkTxs st
117
+ pure [tx]
112
118
113
119
getAlonzoLedgerState :: Interpreter -> IO (LedgerState (ShelleyBlock TPraosStandard StandardAlonzo ))
114
120
getAlonzoLedgerState interpreter = withAlonzoLedgerState interpreter Right
@@ -118,44 +124,44 @@ getBabbageLedgerState interpreter = withBabbageLedgerState interpreter Right
118
124
119
125
skipUntilNextEpoch :: Interpreter -> ServerHandle IO CardanoBlock -> [TxEra ] -> IO CardanoBlock
120
126
skipUntilNextEpoch interpreter mockServer txsEra = do
121
- slot <- getCurrentSlot interpreter
122
- let skipSlots = 500 - mod (unSlotNo slot) 500
123
- blk <- forgeNextAfter interpreter skipSlots txsEra
124
- atomically $ addBlock mockServer blk
125
- pure blk
127
+ slot <- getCurrentSlot interpreter
128
+ let skipSlots = 500 - mod (unSlotNo slot) 500
129
+ blk <- forgeNextAfter interpreter skipSlots txsEra
130
+ atomically $ addBlock mockServer blk
131
+ pure blk
126
132
127
133
-- First block of next epoch is also submited
128
134
fillUntilNextEpoch :: Interpreter -> ServerHandle IO CardanoBlock -> IO [CardanoBlock ]
129
135
fillUntilNextEpoch interpreter mockServer = do
130
- startingEpochNo <- getCurrentEpoch interpreter
131
- let
132
- go n blks = do
133
- blk <- forgeNextFindLeader interpreter []
134
- atomically $ addBlock mockServer blk
135
- epochNo' <- getCurrentEpoch interpreter
136
- if epochNo' == startingEpochNo
136
+ startingEpochNo <- getCurrentEpoch interpreter
137
+ let
138
+ go :: Int -> [CardanoBlock ] -> IO [CardanoBlock ]
139
+ go n blks = do
140
+ blk <- forgeNextFindLeader interpreter []
141
+ atomically $ addBlock mockServer blk
142
+ epochNo' <- getCurrentEpoch interpreter
143
+ if epochNo' == startingEpochNo
137
144
then go (n + 1 ) (blk : blks)
138
145
else pure $ reverse (blk : blks)
139
- go (0 :: Int ) []
146
+ go (0 :: Int ) []
140
147
141
148
-- | Returns number of blocks submitted
142
149
fillEpochs :: Interpreter -> ServerHandle IO CardanoBlock -> Int -> IO [CardanoBlock ]
143
- fillEpochs interpreter mockServer epochs = do
144
- blks <- replicateM epochs $ fillUntilNextEpoch interpreter mockServer
145
- pure $ concat blks
150
+ fillEpochs interpreter mockServer epochs =
151
+ concat <$> replicateM epochs (fillUntilNextEpoch interpreter mockServer)
146
152
147
153
-- | Providing 30 in percentage will create blocks that approximately fill 30% of epoch.
148
154
-- Returns number of blocks submitted
149
155
fillEpochPercentage :: Interpreter -> ServerHandle IO CardanoBlock -> Int -> IO [CardanoBlock ]
150
156
fillEpochPercentage interpreter mockServer percentage = do
151
- let blocksToCreate = div (percentage * blocksPerEpoch) 100
152
- replicateM blocksToCreate $ forgeNextFindLeaderAndSubmit interpreter mockServer []
157
+ let blocksToCreate = div (percentage * blocksPerEpoch) 100
158
+ replicateM blocksToCreate $ forgeNextFindLeaderAndSubmit interpreter mockServer []
153
159
154
160
registerAllStakeCreds :: Interpreter -> ServerHandle IO CardanoBlock -> IO CardanoBlock
155
161
registerAllStakeCreds interpreter mockServer = do
156
- blk <- forgeWithStakeCreds interpreter
157
- atomically $ addBlock mockServer blk
158
- pure blk
162
+ blk <- forgeWithStakeCreds interpreter
163
+ atomically $ addBlock mockServer blk
164
+ pure blk
159
165
160
166
-- Expected number. This should be taken from the parameters, instead of hardcoded.
161
167
blocksPerEpoch :: Int
0 commit comments