Skip to content

Commit 6b3e580

Browse files
committed
Extendend whitelist options
1 parent b0f0134 commit 6b3e580

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

47 files changed

+1711
-1179
lines changed

Diff for: cardano-chain-gen/cardano-chain-gen.cabal

+2
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@ library
6161
build-depends: base >= 4.14 && < 5
6262
, async
6363
, aeson
64+
, base16-bytestring
6465
, bytestring
6566
, cardano-binary
6667
, cardano-crypto-class
@@ -166,6 +167,7 @@ test-suite cardano-chain-gen
166167
Test.Cardano.Db.Mock.Unit.Conway.Simple
167168
Test.Cardano.Db.Mock.Unit.Conway.Stake
168169
Test.Cardano.Db.Mock.Unit.Conway.Tx
170+
Test.Cardano.Db.Mock.Unit.Conway.Whitelist
169171
Test.Cardano.Db.Mock.UnifiedApi
170172
Test.Cardano.Db.Mock.Validate
171173

Diff for: cardano-chain-gen/src/Cardano/Mock/Forging/Tx/Alonzo/ScriptsExamples.hs

+11
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ module Cardano.Mock.Forging.Tx.Alonzo.ScriptsExamples (
2323
alwaysMintScriptHash,
2424
alwaysMintScriptAddr,
2525
alwaysMintScriptStake,
26+
alwaysMintScriptHashRandomPolicyVal,
2627
scriptHash,
2728
assetNames,
2829
plutusData2,
@@ -47,6 +48,7 @@ import Codec.Serialise
4748
import Codec.Serialise.Encoding
4849
import Data.ByteString.Short
4950
import Data.Maybe
51+
import Numeric.Natural (Natural)
5052
import Ouroboros.Consensus.Cardano.Block (StandardAlonzo)
5153
import qualified PlutusCore.Data as Plutus
5254
import qualified PlutusLedgerApi.Test.Examples as Plutus
@@ -103,6 +105,15 @@ alwaysMintScriptStake = ScriptHashObj alwaysMintScriptHash
103105
mkPlutusScriptEra :: AlonzoEraScript era => PlutusBinary -> AlonzoScript era
104106
mkPlutusScriptEra sh = PlutusScript $ fromJust $ mkBinaryPlutusScript PlutusV1 sh
105107

108+
alwaysMintScriptHashRandomPolicyVal :: Natural -> ScriptHash StandardCrypto
109+
alwaysMintScriptHashRandomPolicyVal n = scriptHash @StandardAlonzo $ alwaysMintRandomScript n
110+
111+
alwaysMintRandomScript :: AlonzoEraScript era => Natural -> AlonzoScript era
112+
alwaysMintRandomScript n = mkPlutusScriptEra $ alwaysMintRandomPlutusBinary n
113+
114+
alwaysMintRandomPlutusBinary :: Natural -> PlutusBinary
115+
alwaysMintRandomPlutusBinary n = PlutusBinary $ Plutus.alwaysFailingNAryFunction n
116+
106117
scriptHash ::
107118
forall era.
108119
( EraCrypto era ~ StandardCrypto

Diff for: cardano-chain-gen/src/Cardano/Mock/Forging/Tx/Conway.hs

+16
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ module Cardano.Mock.Forging.Tx.Conway (
3838
mkNewConstitutionTx,
3939
mkDummyRegisterTx,
4040
mkDummyTxBody,
41+
mkDummyTxBodyWithFee,
4142
mkTxDelegCert,
4243
mkRegTxCert,
4344
mkUnRegTxCert,
@@ -589,6 +590,21 @@ mkDummyTxBody =
589590
mempty
590591
(Withdrawals mempty)
591592

593+
mkDummyTxBodyWithFee ::
594+
Coin ->
595+
ConwayTxBody StandardConway
596+
mkDummyTxBodyWithFee coin' =
597+
consTxBody
598+
mempty
599+
mempty
600+
mempty
601+
mempty
602+
SNothing
603+
coin'
604+
mempty
605+
mempty
606+
(Withdrawals mempty)
607+
592608
mkFullTx ::
593609
Int ->
594610
Integer ->

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

+68-1
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,20 @@ module Cardano.Mock.Query (
1111
queryGovActionCounts,
1212
queryConstitutionAnchor,
1313
queryRewardRests,
14+
queryMultiAssetMetadataPolicy,
15+
queryStakeAddressHashRaw,
16+
queryStakeAddressCount,
17+
queryCollateralTxOutCount,
18+
queryPoolUpdateCount,
19+
queryStakeDeRegCount,
20+
queryStakeRegCount,
21+
countTxOutNonNullStakeAddrIds,
1422
) where
1523

1624
import qualified Cardano.Db as Db
17-
import Cardano.Prelude hiding (from, on)
25+
import Cardano.Prelude hiding (from, isNothing, on)
26+
import qualified Data.ByteString.Base16 as Base16
27+
import Data.ByteString.Short (ShortByteString, toShort)
1828
import Database.Esqueleto.Experimental
1929
import Prelude ()
2030

@@ -150,3 +160,60 @@ queryRewardRests = do
150160
pure (reward ^. Db.RewardRestType, reward ^. Db.RewardRestAmount)
151161

152162
pure $ map (bimap unValue (Db.unDbLovelace . unValue)) res
163+
164+
queryMultiAssetMetadataPolicy :: MonadIO io => ReaderT SqlBackend io (Maybe ShortByteString)
165+
queryMultiAssetMetadataPolicy = do
166+
res <- selectOne $ do
167+
metadataPolicy <- from $ table @Db.MultiAsset
168+
pure $ metadataPolicy ^. Db.MultiAssetPolicy
169+
pure $ toShort . Base16.encode . unValue <$> res
170+
171+
queryStakeAddressHashRaw :: MonadIO io => ReaderT SqlBackend io (Maybe ShortByteString)
172+
queryStakeAddressHashRaw = do
173+
res <- selectOne $ do
174+
stakeAddress <- from $ table @Db.StakeAddress
175+
pure $ stakeAddress ^. Db.StakeAddressHashRaw
176+
pure $ toShort . Base16.encode . unValue <$> res
177+
178+
queryStakeAddressCount :: MonadIO io => ReaderT SqlBackend io Word
179+
queryStakeAddressCount = do
180+
res <- selectOne $ do
181+
_ <- from (table @Db.StakeAddress)
182+
pure countRows
183+
pure $ maybe 0 unValue res
184+
185+
queryCollateralTxOutCount :: MonadIO io => ReaderT SqlBackend io Word
186+
queryCollateralTxOutCount = do
187+
res <- selectOne $ do
188+
_ <- from (table @Db.CollateralTxOut)
189+
pure countRows
190+
pure $ maybe 0 unValue res
191+
192+
queryPoolUpdateCount :: MonadIO io => ReaderT SqlBackend io Word
193+
queryPoolUpdateCount = do
194+
res <- selectOne $ do
195+
_ <- from (table @Db.PoolUpdate)
196+
pure countRows
197+
pure $ maybe 0 unValue res
198+
199+
queryStakeDeRegCount :: MonadIO io => ReaderT SqlBackend io Word
200+
queryStakeDeRegCount = do
201+
res <- selectOne $ do
202+
_ <- from (table @Db.StakeDeregistration)
203+
pure countRows
204+
pure $ maybe 0 unValue res
205+
206+
queryStakeRegCount :: MonadIO io => ReaderT SqlBackend io Word
207+
queryStakeRegCount = do
208+
res <- selectOne $ do
209+
_ <- from (table @Db.StakeRegistration)
210+
pure countRows
211+
pure $ maybe 0 unValue res
212+
213+
countTxOutNonNullStakeAddrIds :: (MonadIO m) => SqlPersistT m Word
214+
countTxOutNonNullStakeAddrIds = do
215+
result <- selectOne $ do
216+
txOut <- from $ table @Db.TxOut
217+
where_ $ not_ (isNothing $ txOut ^. Db.TxOutStakeAddressId)
218+
pure countRows
219+
pure $ maybe 0 unValue result

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

+15-1
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import qualified Test.Cardano.Db.Mock.Unit.Conway.Rollback as Rollback
1616
import qualified Test.Cardano.Db.Mock.Unit.Conway.Simple as Simple
1717
import qualified Test.Cardano.Db.Mock.Unit.Conway.Stake as Stake
1818
import qualified Test.Cardano.Db.Mock.Unit.Conway.Tx as Tx
19+
import qualified Test.Cardano.Db.Mock.Unit.Conway.Whitelist as Whitelist
1920
import Test.Cardano.Db.Mock.Validate (expectFailSilent)
2021
import Test.Tasty (TestTree (), testGroup)
2122
import Test.Tasty.HUnit (Assertion (), testCase)
@@ -42,6 +43,12 @@ unitTests iom knownMigrations =
4243
"remove jsonb from schema and add back"
4344
Config.configJsonbInSchemaShouldRemoveThenAdd
4445
]
46+
, testGroup
47+
"invalid whitelist hashes"
48+
[ testCase "Fail if Shelley stake address hash is invalid" Config.invalidShelleyStkAddrHash
49+
, testCase "Fail if multi-asset policies hash is invalid" Config.invalidMultiAssetPoliciesHash
50+
, testCase "Fail if Plutus script hash invalid" Config.invalidPlutusScriptHash
51+
]
4552
, testGroup
4653
"tx-out"
4754
[ test "consumed_by_tx_id column check" MigrateConsumedPruneTxOut.txConsumedColumnCheck
@@ -112,7 +119,6 @@ unitTests iom knownMigrations =
112119
, test "consume utxo same block" Tx.consumeSameBlock
113120
, test "tx with metadata" Tx.addTxMetadata
114121
, test "tx with metadata disabled" Tx.addTxMetadataDisabled
115-
, test "tx with metadata whitelist" Tx.addTxMetadataWhitelist
116122
]
117123
, testGroup
118124
"stake addresses"
@@ -174,6 +180,14 @@ unitTests iom knownMigrations =
174180
, test "swap many multi assets" Plutus.swapMultiAssets
175181
, test "swap with multi assets disabled" Plutus.swapMultiAssetsDisabled
176182
]
183+
, testGroup
184+
"Whitelist"
185+
[ test "add tx with whitelist" Whitelist.addTxMultiAssetsWhitelist
186+
, test "tx with metadata whitelist" Whitelist.addTxMetadataWhitelist
187+
, test "tx with metadata whitelist multiple" Whitelist.addTxMetadataWhitelistMultiple
188+
, test "add simple tx, whitelist tx address" Whitelist.addSimpleTxStakeAddrsWhitelist
189+
, test "add full tx, with stake address whitelist" Whitelist.fullTxStakeAddressWhitelist
190+
]
177191
, testGroup
178192
"Pools and smash"
179193
[ test "pool registration" Other.poolReg

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

+33-2
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,29 @@ module Test.Cardano.Db.Mock.Unit.Conway.Config.Parse (
88
wrongConwayGenesisHash,
99
insertConfig,
1010
defaultInsertConfig,
11-
) where
11+
invalidShelleyStkAddrHash,
12+
invalidMultiAssetPoliciesHash,
13+
invalidPlutusScriptHash,
14+
)
15+
where
1216

1317
import Cardano.DbSync.Config
1418
import Cardano.DbSync.Config.Types
1519
import Cardano.DbSync.Error
1620
import Cardano.Prelude hiding (from, isNothing)
1721
import qualified Data.Aeson as Aeson
1822
import Data.Default.Class (Default (..))
23+
import Data.String (String)
24+
import Data.Text (pack)
1925
import Test.Cardano.Db.Mock.Config
2026
import Test.Tasty.HUnit (Assertion (), assertBool, (@?=))
2127
import Prelude ()
2228

2329
conwayGenesis :: Assertion
2430
conwayGenesis =
2531
mkSyncNodeConfig configDir initCommandLineArgs
26-
>>= void . mkConfig configDir mutableDir cmdLineArgs
32+
>>= void
33+
. mkConfig configDir mutableDir cmdLineArgs
2734
where
2835
configDir = "config-conway"
2936
mutableDir = mkMutableDir "conwayConfigSimple"
@@ -108,3 +115,27 @@ insertConfig = do
108115
dncInsertOptions cfg @?= expected
109116
where
110117
configDir = "config-conway-insert-options"
118+
119+
invalidShelleyStkAddrHash :: Assertion
120+
invalidShelleyStkAddrHash =
121+
let invalidJson = "{ \"enable\": true, \"stake_addresses\": " <> invalidHash <> " }"
122+
decodedResult :: Either String ShelleyInsertConfig
123+
decodedResult = Aeson.eitherDecodeStrict $ encodeUtf8 $ pack invalidJson
124+
in assertBool "Decoding should fail for invalid Shelley stake address hash" (isLeft decodedResult)
125+
126+
invalidMultiAssetPoliciesHash :: Assertion
127+
invalidMultiAssetPoliciesHash =
128+
let invalidJson = "{ \"enable\": true, \"policies\": " <> invalidHash <> " }"
129+
decodedResult :: Either String MultiAssetConfig
130+
decodedResult = Aeson.eitherDecodeStrict $ encodeUtf8 $ pack invalidJson
131+
in assertBool "Decoding should fail for invalid MultiAsset policies hash" (isLeft decodedResult)
132+
133+
invalidPlutusScriptHash :: Assertion
134+
invalidPlutusScriptHash =
135+
let invalidJson = "{ \"enable\": true, \"script_hashes\": " <> invalidHash <> " }"
136+
decodedResult :: Either String PlutusConfig
137+
decodedResult = Aeson.eitherDecodeStrict $ encodeUtf8 $ pack invalidJson
138+
in assertBool "Decoding should fail for invalid Plutus script hash" (isLeft decodedResult)
139+
140+
invalidHash :: String
141+
invalidHash = "[\"\\xe0758b08dea05dabd1cd3510689ebd9efb6a49316acb30eead750e2e9e\"]"

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

+42-31
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
{-# LANGUAGE NumericUnderscores #-}
2+
{-# LANGUAGE OverloadedStrings #-}
23
{-# LANGUAGE TypeApplications #-}
34

45
module Test.Cardano.Db.Mock.Unit.Conway.Plutus (
@@ -32,6 +33,8 @@ module Test.Cardano.Db.Mock.Unit.Conway.Plutus (
3233

3334
import Cardano.Crypto.Hash.Class (hashToBytes)
3435
import qualified Cardano.Db as DB
36+
import Cardano.DbSync.Config (SyncNodeConfig (..))
37+
import Cardano.DbSync.Config.Types (MultiAssetConfig (..), SyncInsertOptions (..))
3538
import Cardano.DbSync.Era.Shelley.Generic.Util (renderAddress)
3639
import Cardano.Ledger.Coin (Coin (..))
3740
import Cardano.Ledger.Mary.Value (MaryValue (..), MultiAsset (..), PolicyID (..))
@@ -763,41 +766,49 @@ swapMultiAssets =
763766
testLabel = "conwaySwapMultiAssets"
764767

765768
swapMultiAssetsDisabled :: IOManager -> [(Text, Text)] -> Assertion
766-
swapMultiAssetsDisabled =
767-
withCustomConfig args Nothing cfgDir testLabel $ \interpreter mockServer dbSync -> do
768-
startDBSync dbSync
769+
swapMultiAssetsDisabled ioManager metadata = do
770+
syncNodeConfig <- mksNodeConfig
771+
withCustomConfig cmdlArgs (Just syncNodeConfig) cfgDir testLabel action ioManager metadata
772+
where
773+
action = \interpreter mockServer dbSync -> do
774+
startDBSync dbSync
775+
776+
-- Forge a block with multiple multi-asset scripts
777+
void $ Api.withConwayFindLeaderAndSubmit interpreter mockServer $ \state' -> do
778+
let policy = PolicyID Examples.alwaysMintScriptHash
779+
assets = Map.singleton (Prelude.head Examples.assetNames) 1
780+
mintedValue = MultiAsset $ Map.singleton policy assets
781+
outValue = MaryValue (Coin 20) (MultiAsset $ Map.singleton policy assets)
782+
783+
-- Forge a multi-asset script
784+
tx0 <-
785+
Conway.mkMultiAssetsScriptTx
786+
[UTxOIndex 0]
787+
(UTxOIndex 1)
788+
[(UTxOAddress Examples.alwaysSucceedsScriptAddr, outValue)]
789+
[]
790+
mintedValue
791+
True
792+
100
793+
state'
769794

770-
-- Forge a block with multiple multi-asset scripts
771-
void $ Api.withConwayFindLeaderAndSubmit interpreter mockServer $ \state' -> do
772-
let policy = PolicyID Examples.alwaysMintScriptHash
773-
assets = Map.singleton (Prelude.head Examples.assetNames) 1
774-
mintedValue = MultiAsset $ Map.singleton policy assets
775-
outValue = MaryValue (Coin 20) (MultiAsset $ Map.singleton policy assets)
795+
pure [tx0]
776796

777-
-- Forge a multi-asset script
778-
tx0 <-
779-
Conway.mkMultiAssetsScriptTx
780-
[UTxOIndex 0]
781-
(UTxOIndex 1)
782-
[(UTxOAddress Examples.alwaysSucceedsScriptAddr, outValue)]
783-
[]
784-
mintedValue
785-
True
786-
100
787-
state'
797+
-- Wait for it to sync
798+
assertBlockNoBackoff dbSync 1
799+
-- Verify multi-assets
800+
assertEqBackoff dbSync queryMultiAssetCount 0 [] "Unexpected multi-assets"
788801

789-
pure [tx0]
802+
cmdlArgs = initCommandLineArgs {claFullMode = False}
790803

791-
-- Wait for it to sync
792-
assertBlockNoBackoff dbSync 1
793-
-- Verify multi-assets
794-
assertEqBackoff dbSync queryMultiAssetCount 0 [] "Unexpected multi-assets"
795-
where
796-
args =
797-
initCommandLineArgs
798-
{ claConfigFilename = "test-db-sync-config-no-multi-assets.json"
799-
, claFullMode = False
800-
}
804+
mksNodeConfig :: IO SyncNodeConfig
805+
mksNodeConfig = do
806+
initConfigFile <- mkSyncNodeConfig cfgDir cmdlArgs
807+
let dncInsertOptions' = dncInsertOptions initConfigFile
808+
pure $
809+
initConfigFile
810+
{ dncInsertOptions = dncInsertOptions' {sioMultiAsset = MultiAssetDisable}
811+
}
801812

802813
testLabel = "conwayConfigMultiAssetsDisabled"
803814
cfgDir = conwayConfigDir

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ registerStakeCreds = do
412412

413413
registerStakeCredsNoShelley :: IOManager -> [(Text, Text)] -> Assertion
414414
registerStakeCredsNoShelley = do
415-
withCustomConfig args Nothing cfgDir testLabel $ \interpreter mockServer dbSync -> do
415+
withCustomConfigAndDropDB args Nothing cfgDir testLabel $ \interpreter mockServer dbSync -> do
416416
startDBSync dbSync
417417

418418
-- These should not be saved when shelley is disabled

0 commit comments

Comments
 (0)