Skip to content

Commit

Permalink
add minting policies check to isMAPoliciesInWhitelist
Browse files Browse the repository at this point in the history
  • Loading branch information
Cmdv committed Jun 25, 2024
1 parent 26622eb commit 92ab49f
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 23 deletions.
12 changes: 6 additions & 6 deletions cardano-db-sync/src/Cardano/DbSync/Era/Universal/Insert/Tx.hs
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@
module Cardano.DbSync.Era.Universal.Insert.Tx (
insertTx,
insertTxOut,
) where
)
where

import Cardano.BM.Trace (Trace)
import Cardano.Db (DbLovelace (..), DbWord64 (..))
import qualified Cardano.Db as DB
import Cardano.DbSync.Api
import Cardano.DbSync.Api.Types (InsertOptions (..), SyncEnv (..))
import Cardano.DbSync.Cache.Types (CacheStatus (..))

import Cardano.DbSync.Config.Types (MetadataConfig (..), MultiAssetConfig (..), PlutusConfig (..), isPlutusModeActive, isShelleyModeActive)
import qualified Cardano.DbSync.Era.Shelley.Generic as Generic
import Cardano.DbSync.Era.Shelley.Generic.Metadata (TxMetadataValue (..), metadataValueToJsonNoSchema)
Expand Down Expand Up @@ -124,8 +124,7 @@ insertTx syncEnv isMember blkId epochNo slotNo applyResult blockIndex tx grouped
if not (Generic.txValidContract tx)
then do
!txOutsGrouped <- do
let txOuts = Generic.txOutputs tx
if plutusMultiAssetWhitelistCheck syncEnv txOuts
if plutusMultiAssetWhitelistCheck syncEnv txMints txOuts
then mapMaybeM (insertTxOut syncEnv cache iopts (txId, txHash)) txOuts
else pure mempty

Expand All @@ -137,8 +136,7 @@ insertTx syncEnv isMember blkId epochNo slotNo applyResult blockIndex tx grouped
-- The following operations only happen if the script passes stage 2 validation (or the tx has
-- no script).
!txOutsGrouped <- do
let txOuts = Generic.txOutputs tx
if plutusMultiAssetWhitelistCheck syncEnv txOuts
if plutusMultiAssetWhitelistCheck syncEnv txMints txOuts
then mapMaybeM (insertTxOut syncEnv cache iopts (txId, txHash)) txOuts
else pure mempty

Expand Down Expand Up @@ -183,6 +181,8 @@ insertTx syncEnv isMember blkId epochNo slotNo applyResult blockIndex tx grouped
let !txIns = map (prepareTxIn txId redeemers) resolvedInputs
pure (grouped <> BlockGroupedData txIns txOutsGrouped txMetadata maTxMint resolvedFees outSum)
where
txOuts = Generic.txOutputs tx
txMints = Generic.txMint tx
tracer = getTrace syncEnv
cache = envCache syncEnv
iopts = getInsertOptions syncEnv
Expand Down
50 changes: 34 additions & 16 deletions cardano-db-sync/src/Cardano/DbSync/Util/Whitelist.hs
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,22 @@ import Cardano.DbSync.Error (shortBsBase16Encode)
import qualified Cardano.Ledger.Address as Ledger
import qualified Cardano.Ledger.Credential as Ledger
import Cardano.Ledger.Crypto (StandardCrypto)
import Cardano.Ledger.Mary.Value (PolicyID (..))
import Cardano.Ledger.Mary.Value (MultiAsset (..), PolicyID (..))
import Cardano.Prelude (NonEmpty)
import Data.ByteString (ByteString)
import Data.ByteString.Short (ShortByteString, toShort)
import Data.Map (keys)

-- check both whitelist but also checking plutus Maybes first
plutusMultiAssetWhitelistCheck :: SyncEnv -> [Generic.TxOut] -> Bool
plutusMultiAssetWhitelistCheck syncEnv txOuts =
isPlutusScriptHashesInWhitelist syncEnv txOuts || isMAPoliciesInWhitelist syncEnv txOuts
plutusMultiAssetWhitelistCheck ::
SyncEnv ->
-- | TxMint
MultiAsset StandardCrypto ->
-- | TxOuts
[Generic.TxOut] ->
Bool
plutusMultiAssetWhitelistCheck syncEnv txMints txOuts =
isPlutusScriptHashesInWhitelist syncEnv txOuts || isMAPoliciesInWhitelist syncEnv txMints txOuts

isPlutusScriptHashesInWhitelist :: SyncEnv -> [Generic.TxOut] -> Bool
isPlutusScriptHashesInWhitelist syncEnv txOuts = do
Expand All @@ -45,32 +51,44 @@ isSimplePlutusScriptHashInWhitelist :: SyncEnv -> ByteString -> Bool
isSimplePlutusScriptHashInWhitelist syncEnv scriptHash = do
case ioPlutus iopts of
PlutusEnable -> True
PlutusDisable -> False
PlutusDisable -> True
PlutusScripts plutusWhitelist -> toShort scriptHash `elem` plutusWhitelist
where
iopts = soptInsertOptions $ envOptions syncEnv

isMAPoliciesInWhitelist :: SyncEnv -> [Generic.TxOut] -> Bool
isMAPoliciesInWhitelist syncEnv txOuts = do
isMAPoliciesInWhitelist ::
SyncEnv ->
-- | TxMint
MultiAsset StandardCrypto ->
-- | TxOuts
[Generic.TxOut] ->
Bool
isMAPoliciesInWhitelist syncEnv (MultiAsset mintMap) txOuts = do
let iopts = soptInsertOptions $ envOptions syncEnv
case ioMultiAssets iopts of
MultiAssetEnable -> True
MultiAssetDisable -> False
MultiAssetDisable -> True
MultiAssetPolicies multiAssetWhitelist ->
or multiAssetwhitelistCheck
mintPoliciesCheck || txOutPoliciesCheck
where
-- txOutMaValue is a Map and we want to check if any of the keys match our whitelist
multiAssetwhitelistCheck :: [Bool]
multiAssetwhitelistCheck =
( \txout ->
any (checkMAValueMap multiAssetWhitelist) (keys $ Generic.txOutMaValue txout)
)
<$> txOuts
mintPoliciesCheck :: Bool
mintPoliciesCheck = any (checkMAValueMap multiAssetWhitelist) mintPolicies

txOutPoliciesCheck :: Bool
txOutPoliciesCheck =
any
( \txout ->
any (checkMAValueMap multiAssetWhitelist) (keys $ Generic.txOutMaValue txout)
)
txOuts

checkMAValueMap :: NonEmpty ShortByteString -> PolicyID StandardCrypto -> Bool
checkMAValueMap maWhitelist policyId =
toShort (Generic.unScriptHash (policyID policyId)) `elem` maWhitelist

mintPolicies :: [PolicyID StandardCrypto]
mintPolicies = keys mintMap

shelleyStkAddrWhitelistCheckWithAddr ::
SyncEnv ->
Ledger.Addr StandardCrypto ->
Expand Down
4 changes: 3 additions & 1 deletion doc/configuration.md
Original file line number Diff line number Diff line change
Expand Up @@ -427,9 +427,11 @@ Be mindfull that whitelisting policies will omit data that does not match whitel
| table name |
| :-------------------- |
| datum |
| ma_tx_mint |
| ma_tx_out |
| multi_assets |
| script |
| tx_out |
| multi_assets |

#### Example

Expand Down

0 comments on commit 92ab49f

Please sign in to comment.