Skip to content

Commit

Permalink
add more tests for metadata whitelist
Browse files Browse the repository at this point in the history
  • Loading branch information
Cmdv committed Mar 27, 2024
1 parent e723143 commit c14e3fa
Show file tree
Hide file tree
Showing 6 changed files with 200 additions and 327 deletions.
16 changes: 16 additions & 0 deletions cardano-chain-gen/src/Cardano/Mock/Forging/Tx/Conway.hs
Expand Up @@ -31,6 +31,7 @@ module Cardano.Mock.Forging.Tx.Conway (
mkDepositTxPools,
mkDummyRegisterTx,
mkDummyTxBody,
mkDummyTxBodyWithCoin,
mkTxDelegCert,
mkRegTxCert,
mkUnRegTxCert,
Expand Down Expand Up @@ -483,6 +484,21 @@ mkDummyTxBody =
mempty
(Withdrawals mempty)

mkDummyTxBodyWithCoin ::
Coin ->
ConwayTxBody StandardConway
mkDummyTxBodyWithCoin coin' =
consTxBody
mempty
mempty
mempty
mempty
SNothing
coin'
mempty
mempty
(Withdrawals mempty)

mkFullTx ::
Int ->
Integer ->
Expand Down
1 change: 1 addition & 0 deletions cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway.hs
Expand Up @@ -113,6 +113,7 @@ unitTests iom knownMigrations =
, test "tx with metadata" Tx.addTxMetadata
, test "tx with metadata disabled" Tx.addTxMetadataDisabled
, test "tx with metadata whitelist" Tx.addTxMetadataWhitelist
, test "tx with metadata whitelist multiple" Tx.addTxMetadataWhitelistMultiple
]
, testGroup
"stake addresses"
Expand Down
199 changes: 140 additions & 59 deletions cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/Tx.hs
Expand Up @@ -8,21 +8,26 @@ module Test.Cardano.Db.Mock.Unit.Conway.Tx (
addTxMetadata,
addTxMetadataDisabled,
addTxMetadataWhitelist,
addTxMetadataWhitelistMultiple,
) where

import Cardano.DbSync.Config (SyncNodeConfig (..))
import Cardano.DbSync.Config.Types (MetadataConfig (..), SyncInsertOptions (..))
import Cardano.Ledger.Shelley.TxAuxData (Metadatum (..))
import Cardano.Mock.ChainSync.Server (IOManager ())
import qualified Cardano.Mock.Forging.Tx.Conway as Conway
import qualified Cardano.Mock.Forging.Tx.Shelley as Shelley
import Cardano.Mock.Forging.Types (UTxOIndex (..))
import Cardano.Mock.Query (queryNullTxDepositExists, queryTxMetadataCount)
import Cardano.Prelude hiding (head)
import Data.List.NonEmpty (fromList)
import qualified Data.Map as Map
import Test.Cardano.Db.Mock.Config
import qualified Test.Cardano.Db.Mock.UnifiedApi as UnifiedApi
import Test.Cardano.Db.Mock.Validate
import Test.Tasty.HUnit (Assertion ())
import Prelude (head)
import Cardano.Api.Ledger (Coin(..))

addSimpleTx :: IOManager -> [(Text, Text)] -> Assertion
addSimpleTx =
Expand Down Expand Up @@ -98,75 +103,151 @@ consumeSameBlock =
testLabel = "conwayConsumeSameBlock"

addTxMetadata :: IOManager -> [(Text, Text)] -> Assertion
addTxMetadata = do
withCustomConfigAndDropDB args Nothing cfgDir testLabel $ \interpreter mockServer dbSync -> do
startDBSync dbSync

-- Add blocks with transactions
void $
UnifiedApi.withConwayFindLeaderAndSubmitTx interpreter mockServer $ \_ ->
let txBody = Conway.mkDummyTxBody
auxData = Map.fromList [(1, I 1), (2, I 2)]
in Right (Conway.mkAuxDataTx True txBody auxData)

-- Wait for it to sync
assertBlockNoBackoff dbSync 1
-- Should have tx metadata
assertEqBackoff dbSync queryTxMetadataCount 2 [] "Expected tx metadata"
addTxMetadata ioManager metadata = do
syncNodeConfig <- mksNodeConfig
withCustomConfigAndDropDB args (Just syncNodeConfig) cfgDir testLabel action ioManager metadata
where
args =
initCommandLineArgs
{ claFullMode = False
}
action = \interpreter mockServer dbSync -> do
startDBSync dbSync
-- Add blocks with transactions
void $
UnifiedApi.withConwayFindLeaderAndSubmitTx interpreter mockServer $ \_ ->
let txBody = Conway.mkDummyTxBody
auxData = Map.fromList [(1, I 1), (2, I 2)]
in Right (Conway.mkAuxDataTx True txBody auxData)

-- Wait for it to sync
assertBlockNoBackoff dbSync 1
-- Should have tx metadata
assertEqBackoff dbSync queryTxMetadataCount 2 [] "Expected tx metadata"

args = initCommandLineArgs {claFullMode = False}
testLabel = "conwayConfigMetadataEnabled"

cfgDir = conwayConfigDir

addTxMetadataWhitelist :: IOManager -> [(Text, Text)] -> Assertion
addTxMetadataWhitelist = do
withCustomConfigAndDropDB args Nothing cfgDir testLabel $ \interpreter mockServer dbSync -> do
startDBSync dbSync
mksNodeConfig :: IO SyncNodeConfig
mksNodeConfig = do
initConfigFile <- mkSyncNodeConfig cfgDir args
let dncInsertOptions' = dncInsertOptions initConfigFile
pure $
initConfigFile
{ dncInsertOptions = dncInsertOptions' {sioMetadata = MetadataEnable}
}

-- Add blocks with transactions
void $ do
UnifiedApi.withConwayFindLeaderAndSubmitTx interpreter mockServer $ \_ ->
let txBody = Conway.mkDummyTxBody
auxData = Map.fromList [(1, I 1), (2, I 2)]
in Right (Conway.mkAuxDataTx True txBody auxData)

-- Wait for it to sync
assertBlockNoBackoff dbSync 1
-- Should have tx metadata
assertEqBackoff dbSync queryTxMetadataCount 1 [] "Expected tx metadata"
addTxMetadataDisabled :: IOManager -> [(Text, Text)] -> Assertion
addTxMetadataDisabled ioManager metadata = do
syncNodeConfig <- mksNodeConfig
withCustomConfigAndDropDB args (Just syncNodeConfig) cfgDir testLabel action ioManager metadata
where
args =
initCommandLineArgs
{ claConfigFilename = "test-db-sync-config-keep-metadata.json"
, claFullMode = False
}
testLabel = "conwayConfigMetadataKeep"
action = \interpreter mockServer dbSync -> do
startDBSync dbSync
-- Add blocks with transactions
void $
UnifiedApi.withConwayFindLeaderAndSubmitTx interpreter mockServer $ \_ ->
let txBody = Conway.mkDummyTxBody
auxData = Map.fromList [(1, I 1), (2, I 2)]
in Right (Conway.mkAuxDataTx True txBody auxData)

-- Wait for it to sync
assertBlockNoBackoff dbSync 1
-- Should have tx metadata
assertEqBackoff dbSync queryTxMetadataCount 0 [] "Expected tx metadata"

args = initCommandLineArgs {claFullMode = False}
testLabel = "conwayConfigMetadataDisabled"

cfgDir = conwayConfigDir

addTxMetadataDisabled :: IOManager -> [(Text, Text)] -> Assertion
addTxMetadataDisabled = do
withCustomConfigAndDropDB args Nothing cfgDir testLabel $ \interpreter mockServer dbSync -> do
startDBSync dbSync
mksNodeConfig :: IO SyncNodeConfig
mksNodeConfig = do
initConfigFile <- mkSyncNodeConfig cfgDir args
let dncInsertOptions' = dncInsertOptions initConfigFile
pure $
initConfigFile
{ dncInsertOptions = dncInsertOptions' {sioMetadata = MetadataDisable}
}

-- 2 blocks each with 4 metadata entries.
-- The whitelist has one tx metadata key which is in the first block
-- so only the TX in the first block should have tx metadata kept.
addTxMetadataWhitelist :: IOManager -> [(Text, Text)] -> Assertion
addTxMetadataWhitelist ioManager metadata = do
syncNodeConfig <- mksNodeConfig
withCustomConfigAndDropDB args (Just syncNodeConfig) cfgDir testLabel action ioManager metadata
where
action = \interpreter mockServer dbSync -> do
startDBSync dbSync
-- Add transactions with metadata
void $ do
UnifiedApi.withConwayFindLeaderAndSubmitTx interpreter mockServer $ \_ ->
let txBody = Conway.mkDummyTxBodyWithCoin $ Coin 1_000
auxData = Map.fromList [(1, I 1), (2, I 2), (3, I 3), (4, I 4)]
in Right (Conway.mkAuxDataTx True txBody auxData)
void $ do
UnifiedApi.withConwayFindLeaderAndSubmitTx interpreter mockServer $ \_ ->
let txBody = Conway.mkDummyTxBodyWithCoin $ Coin 2_000
auxData = Map.fromList [(5, I 5), (6, I 6), (7, I 7), (8, I 8)]
in Right (Conway.mkAuxDataTx True txBody auxData)

assertBlockNoBackoff dbSync 2
-- Should have first block's tx metadata
assertEqBackoff dbSync queryTxMetadataCount 4 [] "Expected tx metadata"

args = initCommandLineArgs {claFullMode = False}
testLabel = "conwayConfigMetadataWhitelist"

-- Add blocks with transactions
void $
UnifiedApi.withConwayFindLeaderAndSubmitTx interpreter mockServer $ \_ ->
let txBody = Conway.mkDummyTxBody
auxData = Map.singleton 1 (I 1)
in Right (Conway.mkAuxDataTx True txBody auxData)
cfgDir = conwayConfigDir

-- Wait for it to sync
assertBlockNoBackoff dbSync 1
-- Should have tx metadata
assertEqBackoff dbSync queryTxMetadataCount 0 [] "Expected tx metadata"
-- match all metadata keys of value 1
mksNodeConfig :: IO SyncNodeConfig
mksNodeConfig = do
initConfigFile <- mkSyncNodeConfig cfgDir args
let dncInsertOptions' = dncInsertOptions initConfigFile
pure $
initConfigFile
{ dncInsertOptions = dncInsertOptions' {sioMetadata = MetadataKeys $ fromList [1]}
}

-- 2 blocks each with 4 metadata entries
-- The whitelist is set to keys [1,6] each key in in different TX
-- so all TxMetadata should be kept from both blocks.
addTxMetadataWhitelistMultiple :: IOManager -> [(Text, Text)] -> Assertion
addTxMetadataWhitelistMultiple ioManager metadata = do
syncNodeConfig <- mksNodeConfig
withCustomConfigAndDropDB args (Just syncNodeConfig) cfgDir testLabel action ioManager metadata
where
args =
initCommandLineArgs
{ claConfigFilename = "test-db-sync-config-no-metadata.json"
, claFullMode = False
}
testLabel = "conwayConfigMetadataDisabled"
action = \interpreter mockServer dbSync -> do
startDBSync dbSync
-- Add transactions with metadata
void $ do
UnifiedApi.withConwayFindLeaderAndSubmitTx interpreter mockServer $ \_ ->
let txBody = Conway.mkDummyTxBodyWithCoin $ Coin 1_000
auxData = Map.fromList [(1, I 1), (2, I 2), (3, I 3), (4, I 4)]
in Right (Conway.mkAuxDataTx True txBody auxData)
void $ do
UnifiedApi.withConwayFindLeaderAndSubmitTx interpreter mockServer $ \_ ->
let txBody = Conway.mkDummyTxBodyWithCoin $ Coin 2_000
auxData = Map.fromList [(5, I 5), (6, I 6), (7, I 7), (8, I 8)]
in Right (Conway.mkAuxDataTx True txBody auxData)

assertBlockNoBackoff dbSync 2
-- Should have both block's tx metadata
assertEqBackoff dbSync queryTxMetadataCount 8 [] "Expected tx metadata"

args = initCommandLineArgs {claFullMode = False}
testLabel = "conwayConfigMetadataWhitelist"

cfgDir = conwayConfigDir

-- match all metadata keys of value 1
mksNodeConfig :: IO SyncNodeConfig
mksNodeConfig = do
initConfigFile <- mkSyncNodeConfig cfgDir args
let dncInsertOptions' = dncInsertOptions initConfigFile
pure $
initConfigFile
{ dncInsertOptions = dncInsertOptions' {sioMetadata = MetadataKeys $ fromList [1,6]}
}

This file was deleted.

0 comments on commit c14e3fa

Please sign in to comment.