From efa38c28ce7ebe666d2649299e38c4701dbca495 Mon Sep 17 00:00:00 2001 From: Cmdv Date: Tue, 9 Apr 2024 19:22:34 +0100 Subject: [PATCH] add test for shelley whitelist --- cardano-chain-gen/src/Cardano/Mock/Query.hs | 61 ++++++++++++++++++- .../test/Test/Cardano/Db/Mock/Unit/Conway.hs | 1 - .../Cardano/Db/Mock/Unit/Conway/Whitelist.hs | 59 ++++++++++-------- .../fingerprint/fullTxStakeAddressWhitelist | 1 + cardano-db-sync/src/Cardano/DbSync/Cache.hs | 9 --- 5 files changed, 96 insertions(+), 35 deletions(-) create mode 100644 cardano-chain-gen/test/testfiles/fingerprint/fullTxStakeAddressWhitelist diff --git a/cardano-chain-gen/src/Cardano/Mock/Query.hs b/cardano-chain-gen/src/Cardano/Mock/Query.hs index 60dfd2300..93e1edcb5 100644 --- a/cardano-chain-gen/src/Cardano/Mock/Query.hs +++ b/cardano-chain-gen/src/Cardano/Mock/Query.hs @@ -8,10 +8,16 @@ module Cardano.Mock.Query ( queryTxMetadataCount, queryMultiAssetMetadataPolicy, queryStakeAddressHashRaw, + queryStakeAddressCount, + queryCollateralTxOutCount, + queryPoolUpdateCount, + queryStakeDeRegCount, + queryStakeRegCount, + countTxOutNonNullStakeAddrIds, ) where import qualified Cardano.Db as Db -import Cardano.Prelude hiding (from) +import Cardano.Prelude hiding (isNothing, from) import qualified Data.ByteString.Base16 as Base16 import Data.ByteString.Short (ShortByteString, toShort) import Database.Esqueleto.Experimental @@ -86,3 +92,56 @@ queryStakeAddressHashRaw = do stakeAddress <- from $ table @Db.StakeAddress pure $ stakeAddress ^. Db.StakeAddressHashRaw pure $ toShort . Base16.encode . unValue <$> res + +-- queryTableCount :: +-- (MonadIO m, PersistEntity table, PersistEntityBackend table ~ SqlBackend) +-- => (SqlExpr (Entity table) -> SqlQuery ()) +-- -> m Word +-- queryTableCount _ = do +-- res <- selectOne $ do +-- _ <- from (table @table) -- using TypeApplications to generalize the table +-- pure countRows +-- pure $ maybe 0 unValue res + +queryStakeAddressCount :: MonadIO io => ReaderT SqlBackend io Word +queryStakeAddressCount = do + res <- selectOne $ do + _ <- from (table @Db.StakeAddress) + pure countRows + pure $ maybe 0 unValue res + +queryCollateralTxOutCount :: MonadIO io => ReaderT SqlBackend io Word +queryCollateralTxOutCount = do + res <- selectOne $ do + _ <- from (table @Db.CollateralTxOut) + pure countRows + pure $ maybe 0 unValue res + +queryPoolUpdateCount :: MonadIO io => ReaderT SqlBackend io Word +queryPoolUpdateCount = do + res <- selectOne $ do + _ <- from (table @Db.PoolUpdate) + pure countRows + pure $ maybe 0 unValue res + +queryStakeDeRegCount :: MonadIO io => ReaderT SqlBackend io Word +queryStakeDeRegCount = do + res <- selectOne $ do + _ <- from (table @Db.StakeDeregistration) + pure countRows + pure $ maybe 0 unValue res + +queryStakeRegCount :: MonadIO io => ReaderT SqlBackend io Word +queryStakeRegCount = do + res <- selectOne $ do + _ <- from (table @Db.StakeRegistration) + pure countRows + pure $ maybe 0 unValue res + +countTxOutNonNullStakeAddrIds :: (MonadIO m) => SqlPersistT m Word +countTxOutNonNullStakeAddrIds = do + result <- selectOne $ do + txOut <- from $ table @Db.TxOut + where_ $ not_ (isNothing $ txOut ^. Db.TxOutStakeAddressId) + pure countRows + pure $ maybe 0 unValue result diff --git a/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway.hs b/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway.hs index 66dd3771e..ebdfd5d67 100644 --- a/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway.hs +++ b/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway.hs @@ -169,7 +169,6 @@ unitTests iom knownMigrations = , test "tx with metadata whitelist multiple" Whitelist.addTxMetadataWhitelistMultiple , test "add simple tx, whitelist tx address" Whitelist.addSimpleTxStakeAddrsWhitelist , test "add full tx, with stake address whitelist" Whitelist.fullTxStakeAddressWhitelist - ] , testGroup "Pools and smash" diff --git a/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/Whitelist.hs b/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/Whitelist.hs index 491189813..7bcc491a7 100644 --- a/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/Whitelist.hs +++ b/cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/Whitelist.hs @@ -19,18 +19,18 @@ import Cardano.Mock.ChainSync.Server (IOManager ()) import qualified Cardano.Mock.Forging.Tx.Alonzo.ScriptsExamples as Examples import qualified Cardano.Mock.Forging.Tx.Conway as Conway import Cardano.Mock.Forging.Types -import Cardano.Mock.Query (queryMultiAssetCount, queryMultiAssetMetadataPolicy, queryStakeAddressHashRaw, queryTxMetadataCount) +import qualified Cardano.Mock.Query as MockQ import Cardano.Prelude hiding (head) import Data.ByteString.Short (toShort) import Data.List.NonEmpty (fromList) import qualified Data.Map as Map import Test.Cardano.Db.Mock.Config +import Test.Cardano.Db.Mock.UnifiedApi (withConwayFindLeaderAndSubmit) import qualified Test.Cardano.Db.Mock.UnifiedApi as Api import qualified Test.Cardano.Db.Mock.UnifiedApi as UnifiedApi import Test.Cardano.Db.Mock.Validate import Test.Tasty.HUnit (Assertion ()) import Prelude (head, (!!)) -import Test.Cardano.Db.Mock.UnifiedApi (withConwayFindLeaderAndSubmit) addTxMultiAssetsWhitelist :: IOManager -> [(Text, Text)] -> Assertion addTxMultiAssetsWhitelist ioManager metadata = do @@ -91,9 +91,9 @@ addTxMultiAssetsWhitelist ioManager metadata = do assertBlockNoBackoff dbSync 1 assertAlonzoCounts dbSync (2, 4, 1, 2, 4, 2, 0, 0) -- create 4 multi-assets but only 2 should be added due to the whitelist - assertEqBackoff dbSync queryMultiAssetCount 2 [] "Expected 2 multi-assets" + assertEqBackoff dbSync MockQ.queryMultiAssetCount 2 [] "Expected 2 multi-assets" -- do the policy match the whitelist - assertEqBackoff dbSync queryMultiAssetMetadataPolicy (Just policyShortBs) [] "Expected correct policy in db" + assertEqBackoff dbSync MockQ.queryMultiAssetMetadataPolicy (Just policyShortBs) [] "Expected correct policy in db" args = initCommandLineArgs {claFullMode = False} testLabel = "conwayConfigMultiAssetsWhitelist" @@ -140,7 +140,7 @@ addTxMetadataWhitelist ioManager metadata = do assertBlockNoBackoff dbSync 2 -- Should have first block's tx metadata - assertEqBackoff dbSync queryTxMetadataCount 4 [] "Expected tx metadata" + assertEqBackoff dbSync MockQ.queryTxMetadataCount 4 [] "Expected tx metadata" args = initCommandLineArgs {claFullMode = False} testLabel = "conwayConfigMetadataWhitelist" @@ -181,7 +181,7 @@ addTxMetadataWhitelistMultiple ioManager metadata = do assertBlockNoBackoff dbSync 2 -- Should have both block's tx metadata - assertEqBackoff dbSync queryTxMetadataCount 8 [] "Expected tx metadata" + assertEqBackoff dbSync MockQ.queryTxMetadataCount 8 [] "Expected tx metadata" args = initCommandLineArgs {claFullMode = False} testLabel = "conwayConfigMetadataWhitelist" @@ -214,7 +214,7 @@ addSimpleTxStakeAddrsWhitelist ioManager metadata = do assertBlockNoBackoff dbSync 1 assertTxCount dbSync 12 - assertEqBackoff dbSync queryStakeAddressHashRaw (Just shelleyStakeAddrShortBs) [] "Expected matching stake address" + assertEqBackoff dbSync MockQ.queryStakeAddressHashRaw (Just shelleyStakeAddrShortBs) [] "Expected matching stake address" testLabel = "conwayAddSimpleTx" args = initCommandLineArgs {claFullMode = False} @@ -235,33 +235,45 @@ addSimpleTxStakeAddrsWhitelist ioManager metadata = do } } - fullTxStakeAddressWhitelist :: IOManager -> [(Text, Text)] -> Assertion fullTxStakeAddressWhitelist ioManager metadata = do syncNodeConfig <- mksNodeConfig - withCustomConfigAndDropDB args (Just syncNodeConfig) cfgDir testLabel action ioManager metadata + withCustomConfig args (Just syncNodeConfig) cfgDir testLabel action ioManager metadata where action = - \interpreter mockServer dbSync -> do + \interpreter mockServer dbSync -> do startDBSync dbSync -- Add some blocks with transactions void $ withConwayFindLeaderAndSubmit interpreter mockServer $ \state' -> - sequence - [ Conway.mkFullTx 0 100 state' - -- , Conway.mkFullTx 1 200 state' - ] + sequence + [ Conway.mkFullTx 0 100 state' + , Conway.mkFullTx 1 200 state' + ] -- Wait for them to sync assertBlockNoBackoff dbSync 1 - assertTxCount dbSync 12 - -- assertTxCount dbSync 13 + assertTxCount dbSync 13 + -- Check all tables that stake addresses effect + assertEqBackoff dbSync MockQ.queryStakeAddressCount 5 [] "Expected 5 stake addresses" + assertEqBackoff dbSync MockQ.queryCollateralTxOutCount 2 [] "Expected 1 collateral tx out" + assertEqBackoff dbSync MockQ.queryPoolUpdateCount 5 [] "Expected 3 pool updates" + assertEqBackoff dbSync MockQ.queryStakeDeRegCount 2 [] "Expected 1 stake deregistration" + assertEqBackoff dbSync MockQ.queryStakeRegCount 2 [] "Expected 1 stake registration" + assertEqBackoff dbSync MockQ.countTxOutNonNullStakeAddrIds 2 [] "Expected 1 non-null stake address id" + -- TODO: Cmdv: Missing tables that are currently blank: + -- delegation, epoch_stake, pool_owner, stake_deregistration + -- One's that are needed for sanchonet: + -- delegation_vote, gov_action_proposal, instant_reward, reserve, + -- treasury, treasury_withdrawl. testLabel = "fullTxStakeAddressWhitelist" - args = initCommandLineArgs {claFullMode = False} + args = initCommandLineArgs {claFullMode = True} cfgDir = conwayConfigDir - -- shelleyStakeAddr0 = toShort "e0addfa484e8095ff53f45b25cf337923cf79abe6ec192fdf288d621f9" - -- shelleyStakeAddr1 = toShort "e0921c25093b263793a1baf36166b819543f5822c62f72571111111111" - -- shelleyStakeAddr2 = toShort "e0921c25093b263793a1baf36166b819543f5822c62f72573333333333" - -- match all metadata keys of value 1 + shelleyStakeAddr0 = toShort "e0addfa484e8095ff53f45b25cf337923cf79abe6ec192fdf288d621f9" + shelleyStakeAddr1 = toShort "e0921c25093b263793a1baf36166b819543f5822c62f72571111111111" + shelleyStakeAddr2 = toShort "e0921c25093b263793a1baf36166b819543f5822c62f72573333333333" + shelleyStakeAddr3 = toShort "e0000131350ac206583290486460934394208654903261221230945870" + shelleyStakeAddr4 = toShort "e022236827154873624578632414768234573268457923654973246472" + mksNodeConfig :: IO SyncNodeConfig mksNodeConfig = do initConfigFile <- mkSyncNodeConfig cfgDir args @@ -271,8 +283,7 @@ fullTxStakeAddressWhitelist ioManager metadata = do { dncInsertOptions = dncInsertOptions' { sioShelley = - ShelleyEnable - -- ShelleyStakeAddrs $ - -- fromList [shelleyStakeAddr0, shelleyStakeAddr1, shelleyStakeAddr2] + ShelleyStakeAddrs $ + fromList [shelleyStakeAddr0, shelleyStakeAddr1, shelleyStakeAddr2, shelleyStakeAddr3, shelleyStakeAddr4] } } diff --git a/cardano-chain-gen/test/testfiles/fingerprint/fullTxStakeAddressWhitelist b/cardano-chain-gen/test/testfiles/fingerprint/fullTxStakeAddressWhitelist new file mode 100644 index 000000000..4d176701b --- /dev/null +++ b/cardano-chain-gen/test/testfiles/fingerprint/fullTxStakeAddressWhitelist @@ -0,0 +1 @@ +[5] \ No newline at end of file diff --git a/cardano-db-sync/src/Cardano/DbSync/Cache.hs b/cardano-db-sync/src/Cardano/DbSync/Cache.hs index 98e739eb4..7e2bd95bd 100644 --- a/cardano-db-sync/src/Cardano/DbSync/Cache.hs +++ b/cardano-db-sync/src/Cardano/DbSync/Cache.hs @@ -51,8 +51,6 @@ import Control.Concurrent.Class.MonadSTM.Strict ( writeTVar, ) import Control.Monad.Trans.Control (MonadBaseControl) -import qualified Data.ByteString.Base16 as Base16 -import Data.ByteString.Short (toShort) import Data.Either.Combinators import qualified Data.Map.Strict as Map import Database.Persist.Postgresql (SqlBackend) @@ -95,13 +93,6 @@ queryOrInsertRewardAccount :: Ledger.RewardAcnt StandardCrypto -> ReaderT SqlBackend m (Maybe DB.StakeAddressId) queryOrInsertRewardAccount syncEnv cache cacheNew rewardAddr = do - liftIO $ - logInfo (getTrace syncEnv) $ - mconcat - [ "queryOrInsertRewardAccount: " - , show $ toShort . Base16.encode $ laBs - ] - -- check if the stake address is in the whitelist if shelleyInsertWhitelistCheck (ioShelley iopts) laBs then do eiAddrId <- queryRewardAccountWithCacheRetBs cache cacheNew rewardAddr