Skip to content

Commit c7ea7a0

Browse files
committed
tx metadata insert custom key names
1 parent dc4d4fa commit c7ea7a0

File tree

7 files changed

+47
-19
lines changed

7 files changed

+47
-19
lines changed

cardano-chain-gen/test/Test/Cardano/Db/Mock/Config.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -268,6 +268,7 @@ mkSyncNodeParams staticDir mutableDir CommandLineArgs {..} = do
268268
, enpHasShelley = True
269269
, enpHasMultiAssets = claHasMultiAssets
270270
, enpHasMetadata = claHasMetadata
271+
, enpKeepMetadataNames = []
271272
, enpHasPlutusExtra = True
272273
, enpHasGov = True
273274
, enpHasOffChainPoolData = True

cardano-db-sync/app/cardano-db-sync.hs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ pRunDbSyncNode =
8787
<*> pHasShelley
8888
<*> pHasMultiAssets
8989
<*> pHasMetadata
90+
<*> pKeepTxMetadata
9091
<*> pHasPlutusExtra
9192
<*> pHasGov
9293
<*> pHasOffChainPoolData
@@ -228,6 +229,15 @@ pSlotNo =
228229
<> Opt.metavar "WORD"
229230
)
230231

232+
pKeepTxMetadata :: Parser [Text]
233+
pKeepTxMetadata =
234+
Opt.many
235+
( Opt.strOption
236+
( Opt.long "keep-tx-metadata"
237+
<> Opt.help "Insert a specific set of tx metadata, based on the tx metadata key names"
238+
)
239+
)
240+
231241
pHasInOut :: Parser Bool
232242
pHasInOut =
233243
Opt.flag

cardano-db-sync/src/Cardano/DbSync.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -235,7 +235,7 @@ extractSyncOptions snp aop =
235235
iopts
236236
| enpOnlyGov snp = onlyGovInsertOptions useLedger
237237
| enpOnlyUTxO snp = onlyUTxOInsertOptions
238-
| enpFullMode snp = fullInsertOptions useLedger
238+
| enpFullMode snp = fullInsertOptions useLedger (enpKeepMetadataNames snp)
239239
| enpDisableAllMode snp = disableAllInsertOptions useLedger
240240
| otherwise =
241241
InsertOptions
@@ -245,6 +245,7 @@ extractSyncOptions snp aop =
245245
, ioRewards = True
246246
, ioMultiAssets = enpHasMultiAssets snp
247247
, ioMetadata = enpHasMetadata snp
248+
, ioKeepMetadataNames = enpKeepMetadataNames snp
248249
, ioPlutusExtra = enpHasPlutusExtra snp
249250
, ioOffChainPoolData = enpHasOffChainPoolData snp
250251
, ioGov = enpHasGov snp

cardano-db-sync/src/Cardano/DbSync/Api.hs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -199,8 +199,8 @@ getPrunes :: SyncEnv -> Bool
199199
getPrunes = do
200200
DB.pcmPruneTxOut . getPruneConsume
201201

202-
fullInsertOptions :: Bool -> InsertOptions
203-
fullInsertOptions useLedger = InsertOptions True useLedger True True True True True True True
202+
fullInsertOptions :: Bool -> [Text] -> InsertOptions
203+
fullInsertOptions useLedger keepTxMDNames = InsertOptions True useLedger True True True True keepTxMDNames True True True
204204

205205
onlyUTxOInsertOptions :: InsertOptions
206206
onlyUTxOInsertOptions =
@@ -211,6 +211,7 @@ onlyUTxOInsertOptions =
211211
, ioRewards = False
212212
, ioMultiAssets = True
213213
, ioMetadata = False
214+
, ioKeepMetadataNames = []
214215
, ioPlutusExtra = False
215216
, ioOffChainPoolData = False
216217
, ioGov = False
@@ -220,7 +221,7 @@ onlyGovInsertOptions :: Bool -> InsertOptions
220221
onlyGovInsertOptions useLedger = (disableAllInsertOptions useLedger) {ioGov = True}
221222

222223
disableAllInsertOptions :: Bool -> InsertOptions
223-
disableAllInsertOptions useLedger = InsertOptions False useLedger False False False False False False False
224+
disableAllInsertOptions useLedger = InsertOptions False useLedger False False False False [] False False False
224225

225226
initEpochState :: EpochState
226227
initEpochState =

cardano-db-sync/src/Cardano/DbSync/Api/Types.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ import Cardano.DbSync.Types (
2323
OffChainVoteResult,
2424
OffChainVoteWorkQueue,
2525
)
26-
import Cardano.Prelude (Bool, Eq, IO, Show, Word64)
26+
import Cardano.Prelude (Bool, Eq, IO, Show, Text, Word64)
2727
import Cardano.Slotting.Slot (EpochNo (..))
2828
import Control.Concurrent.Class.MonadSTM.Strict (
2929
StrictTVar,
@@ -78,6 +78,7 @@ data InsertOptions = InsertOptions
7878
, ioRewards :: !Bool
7979
, ioMultiAssets :: !Bool
8080
, ioMetadata :: !Bool
81+
, ioKeepMetadataNames :: ![Text]
8182
, ioPlutusExtra :: !Bool
8283
, ioOffChainPoolData :: !Bool
8384
, ioGov :: !Bool

cardano-db-sync/src/Cardano/DbSync/Config/Types.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,7 @@ data SyncNodeParams = SyncNodeParams
7272
, enpHasShelley :: !Bool
7373
, enpHasMultiAssets :: !Bool
7474
, enpHasMetadata :: !Bool
75+
, enpKeepMetadataNames :: ![Text]
7576
, enpHasPlutusExtra :: !Bool
7677
, enpHasGov :: !Bool
7778
, enpHasOffChainPoolData :: !Bool

cardano-db-sync/src/Cardano/DbSync/Era/Shelley/Insert.hs

Lines changed: 27 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -80,18 +80,20 @@ import qualified Cardano.Ledger.Shelley.API.Wallet as Shelley
8080
import qualified Cardano.Ledger.Shelley.TxBody as Shelley
8181
import Cardano.Ledger.Shelley.TxCert
8282
import Cardano.Prelude
83-
import Control.Monad.Extra (whenJust)
83+
import Control.Monad.Extra (mapMaybeM, whenJust)
8484
import Control.Monad.Trans.Control (MonadBaseControl)
8585
import Control.Monad.Trans.Except.Extra (newExceptT)
8686
import qualified Data.Aeson as Aeson
8787
import qualified Data.ByteString.Lazy.Char8 as LBS
8888
import Data.Either.Extra (eitherToMaybe)
8989
import Data.Group (invert)
9090
import qualified Data.Map.Strict as Map
91+
import qualified Data.Text as Text
9192
import qualified Data.Text.Encoding as Text
9293
import Database.Persist.Sql (SqlBackend)
9394
import Lens.Micro
9495
import Ouroboros.Consensus.Cardano.Block (StandardConway, StandardCrypto)
96+
import Prelude (read)
9597

9698
{- HLINT ignore "Reduce duplication" -}
9799

@@ -350,6 +352,7 @@ insertTx syncEnv isMember blkId epochNo slotNo applyResult blockIndex tx grouped
350352
prepareTxMetadata
351353
tracer
352354
txId
355+
iopts
353356
(Generic.txMetadata tx)
354357
mapM_
355358
(insertCertificate syncEnv isMember blkId txId epochNo slotNo redeemers)
@@ -1205,28 +1208,38 @@ prepareTxMetadata ::
12051208
(MonadBaseControl IO m, MonadIO m) =>
12061209
Trace IO Text ->
12071210
DB.TxId ->
1211+
InsertOptions ->
12081212
Maybe (Map Word64 TxMetadataValue) ->
12091213
ExceptT SyncNodeError (ReaderT SqlBackend m) [DB.TxMetadata]
1210-
prepareTxMetadata tracer txId mmetadata =
1214+
prepareTxMetadata tracer txId inOpts mmetadata = do
12111215
case mmetadata of
12121216
Nothing -> pure []
1213-
Just metadata -> mapM prepare $ Map.toList metadata
1217+
Just metadata -> mapMaybeM prepare $ Map.toList metadata
12141218
where
12151219
prepare ::
12161220
(MonadBaseControl IO m, MonadIO m) =>
12171221
(Word64, TxMetadataValue) ->
1218-
ExceptT SyncNodeError (ReaderT SqlBackend m) DB.TxMetadata
1222+
ExceptT SyncNodeError (ReaderT SqlBackend m) (Maybe DB.TxMetadata)
12191223
prepare (key, md) = do
1220-
let jsonbs = LBS.toStrict $ Aeson.encode (metadataValueToJsonNoSchema md)
1221-
singleKeyCBORMetadata = serialiseTxMetadataToCbor $ Map.singleton key md
1222-
mjson <- safeDecodeToJson tracer "prepareTxMetadata" jsonbs
1223-
pure
1224-
DB.TxMetadata
1225-
{ DB.txMetadataKey = DbWord64 key
1226-
, DB.txMetadataJson = mjson
1227-
, DB.txMetadataBytes = singleKeyCBORMetadata
1228-
, DB.txMetadataTxId = txId
1229-
}
1224+
let metadataNames = ioKeepMetadataNames inOpts
1225+
isMatchingKey = key `elem` map (read . Text.unpack) metadataNames
1226+
isMetadataNamesEmpty = null metadataNames
1227+
-- if the metadata names list is empty then nothing was passed to the command line flag
1228+
-- so we just return all metadata as normal overiding isMatchingKey.
1229+
if isMetadataNamesEmpty || isMatchingKey
1230+
then do
1231+
let jsonbs = LBS.toStrict $ Aeson.encode (metadataValueToJsonNoSchema md)
1232+
singleKeyCBORMetadata = serialiseTxMetadataToCbor $ Map.singleton key md
1233+
mjson <- safeDecodeToJson tracer "prepareTxMetadata" jsonbs
1234+
pure $
1235+
Just $
1236+
DB.TxMetadata
1237+
{ DB.txMetadataKey = DbWord64 key
1238+
, DB.txMetadataJson = mjson
1239+
, DB.txMetadataBytes = singleKeyCBORMetadata
1240+
, DB.txMetadataTxId = txId
1241+
}
1242+
else pure Nothing
12301243

12311244
insertCostModel ::
12321245
(MonadBaseControl IO m, MonadIO m) =>

0 commit comments

Comments
 (0)