Skip to content

Commit efa38c2

Browse files
committed
add test for shelley whitelist
1 parent 791fcd2 commit efa38c2

File tree

5 files changed

+96
-35
lines changed

5 files changed

+96
-35
lines changed

Diff for: cardano-chain-gen/src/Cardano/Mock/Query.hs

+60-1
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,16 @@ module Cardano.Mock.Query (
88
queryTxMetadataCount,
99
queryMultiAssetMetadataPolicy,
1010
queryStakeAddressHashRaw,
11+
queryStakeAddressCount,
12+
queryCollateralTxOutCount,
13+
queryPoolUpdateCount,
14+
queryStakeDeRegCount,
15+
queryStakeRegCount,
16+
countTxOutNonNullStakeAddrIds,
1117
) where
1218

1319
import qualified Cardano.Db as Db
14-
import Cardano.Prelude hiding (from)
20+
import Cardano.Prelude hiding (isNothing, from)
1521
import qualified Data.ByteString.Base16 as Base16
1622
import Data.ByteString.Short (ShortByteString, toShort)
1723
import Database.Esqueleto.Experimental
@@ -86,3 +92,56 @@ queryStakeAddressHashRaw = do
8692
stakeAddress <- from $ table @Db.StakeAddress
8793
pure $ stakeAddress ^. Db.StakeAddressHashRaw
8894
pure $ toShort . Base16.encode . unValue <$> res
95+
96+
-- queryTableCount ::
97+
-- (MonadIO m, PersistEntity table, PersistEntityBackend table ~ SqlBackend)
98+
-- => (SqlExpr (Entity table) -> SqlQuery ())
99+
-- -> m Word
100+
-- queryTableCount _ = do
101+
-- res <- selectOne $ do
102+
-- _ <- from (table @table) -- using TypeApplications to generalize the table
103+
-- pure countRows
104+
-- pure $ maybe 0 unValue res
105+
106+
queryStakeAddressCount :: MonadIO io => ReaderT SqlBackend io Word
107+
queryStakeAddressCount = do
108+
res <- selectOne $ do
109+
_ <- from (table @Db.StakeAddress)
110+
pure countRows
111+
pure $ maybe 0 unValue res
112+
113+
queryCollateralTxOutCount :: MonadIO io => ReaderT SqlBackend io Word
114+
queryCollateralTxOutCount = do
115+
res <- selectOne $ do
116+
_ <- from (table @Db.CollateralTxOut)
117+
pure countRows
118+
pure $ maybe 0 unValue res
119+
120+
queryPoolUpdateCount :: MonadIO io => ReaderT SqlBackend io Word
121+
queryPoolUpdateCount = do
122+
res <- selectOne $ do
123+
_ <- from (table @Db.PoolUpdate)
124+
pure countRows
125+
pure $ maybe 0 unValue res
126+
127+
queryStakeDeRegCount :: MonadIO io => ReaderT SqlBackend io Word
128+
queryStakeDeRegCount = do
129+
res <- selectOne $ do
130+
_ <- from (table @Db.StakeDeregistration)
131+
pure countRows
132+
pure $ maybe 0 unValue res
133+
134+
queryStakeRegCount :: MonadIO io => ReaderT SqlBackend io Word
135+
queryStakeRegCount = do
136+
res <- selectOne $ do
137+
_ <- from (table @Db.StakeRegistration)
138+
pure countRows
139+
pure $ maybe 0 unValue res
140+
141+
countTxOutNonNullStakeAddrIds :: (MonadIO m) => SqlPersistT m Word
142+
countTxOutNonNullStakeAddrIds = do
143+
result <- selectOne $ do
144+
txOut <- from $ table @Db.TxOut
145+
where_ $ not_ (isNothing $ txOut ^. Db.TxOutStakeAddressId)
146+
pure countRows
147+
pure $ maybe 0 unValue result

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

-1
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,6 @@ unitTests iom knownMigrations =
169169
, test "tx with metadata whitelist multiple" Whitelist.addTxMetadataWhitelistMultiple
170170
, test "add simple tx, whitelist tx address" Whitelist.addSimpleTxStakeAddrsWhitelist
171171
, test "add full tx, with stake address whitelist" Whitelist.fullTxStakeAddressWhitelist
172-
173172
]
174173
, testGroup
175174
"Pools and smash"

Diff for: cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/Whitelist.hs

+35-24
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,18 @@ import Cardano.Mock.ChainSync.Server (IOManager ())
1919
import qualified Cardano.Mock.Forging.Tx.Alonzo.ScriptsExamples as Examples
2020
import qualified Cardano.Mock.Forging.Tx.Conway as Conway
2121
import Cardano.Mock.Forging.Types
22-
import Cardano.Mock.Query (queryMultiAssetCount, queryMultiAssetMetadataPolicy, queryStakeAddressHashRaw, queryTxMetadataCount)
22+
import qualified Cardano.Mock.Query as MockQ
2323
import Cardano.Prelude hiding (head)
2424
import Data.ByteString.Short (toShort)
2525
import Data.List.NonEmpty (fromList)
2626
import qualified Data.Map as Map
2727
import Test.Cardano.Db.Mock.Config
28+
import Test.Cardano.Db.Mock.UnifiedApi (withConwayFindLeaderAndSubmit)
2829
import qualified Test.Cardano.Db.Mock.UnifiedApi as Api
2930
import qualified Test.Cardano.Db.Mock.UnifiedApi as UnifiedApi
3031
import Test.Cardano.Db.Mock.Validate
3132
import Test.Tasty.HUnit (Assertion ())
3233
import Prelude (head, (!!))
33-
import Test.Cardano.Db.Mock.UnifiedApi (withConwayFindLeaderAndSubmit)
3434

3535
addTxMultiAssetsWhitelist :: IOManager -> [(Text, Text)] -> Assertion
3636
addTxMultiAssetsWhitelist ioManager metadata = do
@@ -91,9 +91,9 @@ addTxMultiAssetsWhitelist ioManager metadata = do
9191
assertBlockNoBackoff dbSync 1
9292
assertAlonzoCounts dbSync (2, 4, 1, 2, 4, 2, 0, 0)
9393
-- create 4 multi-assets but only 2 should be added due to the whitelist
94-
assertEqBackoff dbSync queryMultiAssetCount 2 [] "Expected 2 multi-assets"
94+
assertEqBackoff dbSync MockQ.queryMultiAssetCount 2 [] "Expected 2 multi-assets"
9595
-- do the policy match the whitelist
96-
assertEqBackoff dbSync queryMultiAssetMetadataPolicy (Just policyShortBs) [] "Expected correct policy in db"
96+
assertEqBackoff dbSync MockQ.queryMultiAssetMetadataPolicy (Just policyShortBs) [] "Expected correct policy in db"
9797

9898
args = initCommandLineArgs {claFullMode = False}
9999
testLabel = "conwayConfigMultiAssetsWhitelist"
@@ -140,7 +140,7 @@ addTxMetadataWhitelist ioManager metadata = do
140140

141141
assertBlockNoBackoff dbSync 2
142142
-- Should have first block's tx metadata
143-
assertEqBackoff dbSync queryTxMetadataCount 4 [] "Expected tx metadata"
143+
assertEqBackoff dbSync MockQ.queryTxMetadataCount 4 [] "Expected tx metadata"
144144

145145
args = initCommandLineArgs {claFullMode = False}
146146
testLabel = "conwayConfigMetadataWhitelist"
@@ -181,7 +181,7 @@ addTxMetadataWhitelistMultiple ioManager metadata = do
181181

182182
assertBlockNoBackoff dbSync 2
183183
-- Should have both block's tx metadata
184-
assertEqBackoff dbSync queryTxMetadataCount 8 [] "Expected tx metadata"
184+
assertEqBackoff dbSync MockQ.queryTxMetadataCount 8 [] "Expected tx metadata"
185185

186186
args = initCommandLineArgs {claFullMode = False}
187187
testLabel = "conwayConfigMetadataWhitelist"
@@ -214,7 +214,7 @@ addSimpleTxStakeAddrsWhitelist ioManager metadata = do
214214
assertBlockNoBackoff dbSync 1
215215
assertTxCount dbSync 12
216216

217-
assertEqBackoff dbSync queryStakeAddressHashRaw (Just shelleyStakeAddrShortBs) [] "Expected matching stake address"
217+
assertEqBackoff dbSync MockQ.queryStakeAddressHashRaw (Just shelleyStakeAddrShortBs) [] "Expected matching stake address"
218218

219219
testLabel = "conwayAddSimpleTx"
220220
args = initCommandLineArgs {claFullMode = False}
@@ -235,33 +235,45 @@ addSimpleTxStakeAddrsWhitelist ioManager metadata = do
235235
}
236236
}
237237

238-
239238
fullTxStakeAddressWhitelist :: IOManager -> [(Text, Text)] -> Assertion
240239
fullTxStakeAddressWhitelist ioManager metadata = do
241240
syncNodeConfig <- mksNodeConfig
242-
withCustomConfigAndDropDB args (Just syncNodeConfig) cfgDir testLabel action ioManager metadata
241+
withCustomConfig args (Just syncNodeConfig) cfgDir testLabel action ioManager metadata
243242
where
244243
action =
245-
\interpreter mockServer dbSync -> do
244+
\interpreter mockServer dbSync -> do
246245
startDBSync dbSync
247246
-- Add some blocks with transactions
248247
void $ withConwayFindLeaderAndSubmit interpreter mockServer $ \state' ->
249-
sequence
250-
[ Conway.mkFullTx 0 100 state'
251-
-- , Conway.mkFullTx 1 200 state'
252-
]
248+
sequence
249+
[ Conway.mkFullTx 0 100 state'
250+
, Conway.mkFullTx 1 200 state'
251+
]
253252
-- Wait for them to sync
254253
assertBlockNoBackoff dbSync 1
255-
assertTxCount dbSync 12
256-
-- assertTxCount dbSync 13
254+
assertTxCount dbSync 13
255+
-- Check all tables that stake addresses effect
256+
assertEqBackoff dbSync MockQ.queryStakeAddressCount 5 [] "Expected 5 stake addresses"
257+
assertEqBackoff dbSync MockQ.queryCollateralTxOutCount 2 [] "Expected 1 collateral tx out"
258+
assertEqBackoff dbSync MockQ.queryPoolUpdateCount 5 [] "Expected 3 pool updates"
259+
assertEqBackoff dbSync MockQ.queryStakeDeRegCount 2 [] "Expected 1 stake deregistration"
260+
assertEqBackoff dbSync MockQ.queryStakeRegCount 2 [] "Expected 1 stake registration"
261+
assertEqBackoff dbSync MockQ.countTxOutNonNullStakeAddrIds 2 [] "Expected 1 non-null stake address id"
262+
-- TODO: Cmdv: Missing tables that are currently blank:
263+
-- delegation, epoch_stake, pool_owner, stake_deregistration
264+
-- One's that are needed for sanchonet:
265+
-- delegation_vote, gov_action_proposal, instant_reward, reserve,
266+
-- treasury, treasury_withdrawl.
257267

258268
testLabel = "fullTxStakeAddressWhitelist"
259-
args = initCommandLineArgs {claFullMode = False}
269+
args = initCommandLineArgs {claFullMode = True}
260270
cfgDir = conwayConfigDir
261-
-- shelleyStakeAddr0 = toShort "e0addfa484e8095ff53f45b25cf337923cf79abe6ec192fdf288d621f9"
262-
-- shelleyStakeAddr1 = toShort "e0921c25093b263793a1baf36166b819543f5822c62f72571111111111"
263-
-- shelleyStakeAddr2 = toShort "e0921c25093b263793a1baf36166b819543f5822c62f72573333333333"
264-
-- match all metadata keys of value 1
271+
shelleyStakeAddr0 = toShort "e0addfa484e8095ff53f45b25cf337923cf79abe6ec192fdf288d621f9"
272+
shelleyStakeAddr1 = toShort "e0921c25093b263793a1baf36166b819543f5822c62f72571111111111"
273+
shelleyStakeAddr2 = toShort "e0921c25093b263793a1baf36166b819543f5822c62f72573333333333"
274+
shelleyStakeAddr3 = toShort "e0000131350ac206583290486460934394208654903261221230945870"
275+
shelleyStakeAddr4 = toShort "e022236827154873624578632414768234573268457923654973246472"
276+
265277
mksNodeConfig :: IO SyncNodeConfig
266278
mksNodeConfig = do
267279
initConfigFile <- mkSyncNodeConfig cfgDir args
@@ -271,8 +283,7 @@ fullTxStakeAddressWhitelist ioManager metadata = do
271283
{ dncInsertOptions =
272284
dncInsertOptions'
273285
{ sioShelley =
274-
ShelleyEnable
275-
-- ShelleyStakeAddrs $
276-
-- fromList [shelleyStakeAddr0, shelleyStakeAddr1, shelleyStakeAddr2]
286+
ShelleyStakeAddrs $
287+
fromList [shelleyStakeAddr0, shelleyStakeAddr1, shelleyStakeAddr2, shelleyStakeAddr3, shelleyStakeAddr4]
277288
}
278289
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
[5]

Diff for: cardano-db-sync/src/Cardano/DbSync/Cache.hs

-9
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@ import Control.Concurrent.Class.MonadSTM.Strict (
5151
writeTVar,
5252
)
5353
import Control.Monad.Trans.Control (MonadBaseControl)
54-
import qualified Data.ByteString.Base16 as Base16
55-
import Data.ByteString.Short (toShort)
5654
import Data.Either.Combinators
5755
import qualified Data.Map.Strict as Map
5856
import Database.Persist.Postgresql (SqlBackend)
@@ -95,13 +93,6 @@ queryOrInsertRewardAccount ::
9593
Ledger.RewardAcnt StandardCrypto ->
9694
ReaderT SqlBackend m (Maybe DB.StakeAddressId)
9795
queryOrInsertRewardAccount syncEnv cache cacheNew rewardAddr = do
98-
liftIO $
99-
logInfo (getTrace syncEnv) $
100-
mconcat
101-
[ "queryOrInsertRewardAccount: "
102-
, show $ toShort . Base16.encode $ laBs
103-
]
104-
-- check if the stake address is in the whitelist
10596
if shelleyInsertWhitelistCheck (ioShelley iopts) laBs
10697
then do
10798
eiAddrId <- queryRewardAccountWithCacheRetBs cache cacheNew rewardAddr

0 commit comments

Comments
 (0)