Skip to content

Commit

Permalink
add some tests for whitelist
Browse files Browse the repository at this point in the history
  • Loading branch information
Cmdv committed Apr 4, 2024
1 parent 627cf65 commit 78418bf
Show file tree
Hide file tree
Showing 9 changed files with 307 additions and 189 deletions.
1 change: 1 addition & 0 deletions cardano-chain-gen/cardano-chain-gen.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,7 @@ test-suite cardano-chain-gen
Test.Cardano.Db.Mock.Unit.Conway.Simple
Test.Cardano.Db.Mock.Unit.Conway.Stake
Test.Cardano.Db.Mock.Unit.Conway.Tx
Test.Cardano.Db.Mock.Unit.Conway.Whitelist
Test.Cardano.Db.Mock.UnifiedApi
Test.Cardano.Db.Mock.Validate

Expand Down
8 changes: 8 additions & 0 deletions cardano-chain-gen/src/Cardano/Mock/Query.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Cardano.Mock.Query (
queryMultiAssetCount,
queryTxMetadataCount,
queryMultiAssetMetadataPolicy,
queryStakeAddressHashRaw,
) where

import qualified Cardano.Db as Db
Expand Down Expand Up @@ -78,3 +79,10 @@ queryMultiAssetMetadataPolicy = do
metadataPolicy <- from $ table @Db.MultiAsset
pure $ metadataPolicy ^. Db.MultiAssetPolicy
pure $ toShort . Base16.encode . unValue <$> res

queryStakeAddressHashRaw :: MonadIO io => ReaderT SqlBackend io (Maybe ShortByteString)
queryStakeAddressHashRaw = do
res <- selectOne $ do
stakeAddress <- from $ table @Db.StakeAddress
pure $ stakeAddress ^. Db.StakeAddressHashRaw
pure $ toShort . Base16.encode . unValue <$> res
11 changes: 8 additions & 3 deletions cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import qualified Test.Cardano.Db.Mock.Unit.Conway.Rollback as Rollback
import qualified Test.Cardano.Db.Mock.Unit.Conway.Simple as Simple
import qualified Test.Cardano.Db.Mock.Unit.Conway.Stake as Stake
import qualified Test.Cardano.Db.Mock.Unit.Conway.Tx as Tx
import qualified Test.Cardano.Db.Mock.Unit.Conway.Whitelist as Whitelist
import Test.Tasty (TestTree (), testGroup)
import Test.Tasty.HUnit (Assertion (), testCase)
import Prelude (String ())
Expand Down Expand Up @@ -100,8 +101,6 @@ unitTests iom knownMigrations =
, test "consume utxo same block" Tx.consumeSameBlock
, 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 Expand Up @@ -162,7 +161,13 @@ unitTests iom knownMigrations =
, test "mint many multi assets" Plutus.mintMultiAssets
, test "swap many multi assets" Plutus.swapMultiAssets
, test "swap with multi assets disabled" Plutus.swapMultiAssetsDisabled
, test "add multi assets with whitelist" Plutus.addTxMultiAssetsWhitelist
]
, testGroup
"Whitelist"
[ test "add tx with whitelist" Whitelist.addTxMultiAssetsWhitelist
, test "tx with metadata whitelist" Whitelist.addTxMetadataWhitelist
, test "tx with metadata whitelist multiple" Whitelist.addTxMetadataWhitelistMultiple
, test "add simple tx, whitelist tx address" Whitelist.addSimpleTxStakeAddrsWhitelist
]
, testGroup
"Pools and smash"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,6 @@ module Test.Cardano.Db.Mock.Unit.Conway.Plutus (
mintMultiAssets,
swapMultiAssets,
swapMultiAssetsDisabled,
addTxMultiAssetsWhitelist,
) where

import Cardano.Crypto.Hash.Class (hashToBytes)
Expand All @@ -46,10 +45,8 @@ import Cardano.Mock.Forging.Interpreter (withConwayLedgerState)
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)
import Cardano.Mock.Query (queryMultiAssetCount)
import Cardano.Prelude hiding (head)
import Data.ByteString.Short (toShort)
import Data.List.NonEmpty (fromList)
import qualified Data.Map as Map
import Data.Maybe.Strict (StrictMaybe (..))
import Ouroboros.Consensus.Shelley.Eras (StandardConway ())
Expand Down Expand Up @@ -815,87 +812,3 @@ swapMultiAssetsDisabled ioManager metadata = do

testLabel = "conwayConfigMultiAssetsDisabled"
cfgDir = conwayConfigDir

addTxMultiAssetsWhitelist :: IOManager -> [(Text, Text)] -> Assertion
addTxMultiAssetsWhitelist ioManager metadata = do
syncNodeConfig <- mksNodeConfig
withCustomConfig args (Just syncNodeConfig) cfgDir testLabel action ioManager metadata
where
action = \interpreter mockServer dbSync -> do
startDBSync dbSync
-- Forge a block with multiple multi-asset scripts
void $ Api.withConwayFindLeaderAndSubmit interpreter mockServer $ \state' -> do
let assetsMinted =
Map.fromList [(head Examples.assetNames, 10), (Examples.assetNames !! 1, 4)]
policy0 = PolicyID $ Examples.alwaysMintScriptHashRandomPolicyVal 1
policy1 = PolicyID $ Examples.alwaysMintScriptHashRandomPolicyVal 2
mintValue =
MultiAsset $
Map.fromList [(policy0, assetsMinted), (policy1, assetsMinted)]
assets =
Map.fromList [(head Examples.assetNames, 5), (Examples.assetNames !! 1, 2)]
outValue =
MaryValue (Coin 20) $
MultiAsset $
Map.fromList [(policy0, assets), (policy1, assets)]

-- Forge a multi-asset script
tx0 <-
Conway.mkMultiAssetsScriptTx
[UTxOIndex 0]
(UTxOIndex 1)
[ (UTxOAddress Examples.alwaysSucceedsScriptAddr, outValue)
, (UTxOAddress Examples.alwaysMintScriptAddr, outValue)
]
[]
mintValue
True
100
state'

-- Consume the outputs from tx0
let utxos = Conway.mkUTxOConway tx0
tx1 <-
Conway.mkMultiAssetsScriptTx
[UTxOPair (head utxos), UTxOPair (utxos !! 1), UTxOIndex 2]
(UTxOIndex 3)
[ (UTxOAddress Examples.alwaysSucceedsScriptAddr, outValue)
, (UTxOAddress Examples.alwaysMintScriptAddr, outValue)
, (UTxOAddressNew 0, outValue)
, (UTxOAddressNew 0, outValue)
]
[]
mintValue
True
200
state'
pure [tx0, tx1]

-- Verify script counts
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"
-- do the policy match the whitelist
assertEqBackoff dbSync queryMultiAssetMetadataPolicy (Just policyShortBs) [] "Expected correct policy in db"

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

cfgDir = conwayConfigDir

policyShortBs = toShort "4509cdddad21412c22c9164e10bc6071340ba235562f1575a35ded4d"

mksNodeConfig :: IO SyncNodeConfig
mksNodeConfig = do
initConfigFile <- mkSyncNodeConfig cfgDir args
let dncInsertOptions' = dncInsertOptions initConfigFile
pure $
initConfigFile
{ dncInsertOptions =
dncInsertOptions'
{ sioMultiAsset =
MultiAssetPolicies $
fromList [policyShortBs]
}
}
86 changes: 0 additions & 86 deletions cardano-chain-gen/test/Test/Cardano/Db/Mock/Unit/Conway/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,8 @@ module Test.Cardano.Db.Mock.Unit.Conway.Tx (
consumeSameBlock,
addTxMetadata,
addTxMetadataDisabled,
addTxMetadataWhitelist,
addTxMetadataWhitelistMultiple,
) where

import Cardano.Api.Ledger (Coin (..))
import Cardano.DbSync.Config (SyncNodeConfig (..))
import Cardano.DbSync.Config.Types (MetadataConfig (..), SyncInsertOptions (..))
import Cardano.Ledger.Shelley.TxAuxData (Metadatum (..))
Expand All @@ -21,7 +18,6 @@ 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
Expand Down Expand Up @@ -167,85 +163,3 @@ addTxMetadataDisabled ioManager metadata = do
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.mkDummyTxBodyWithFee $ 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.mkDummyTxBodyWithFee $ 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"

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]}
}

-- 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
action = \interpreter mockServer dbSync -> do
startDBSync dbSync
-- Add transactions with metadata
void $ do
UnifiedApi.withConwayFindLeaderAndSubmitTx interpreter mockServer $ \_ ->
let txBody = Conway.mkDummyTxBodyWithFee $ 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.mkDummyTxBodyWithFee $ 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]}
}
Loading

0 comments on commit 78418bf

Please sign in to comment.