Skip to content

Commit 6f29b18

Browse files
authored
Merge pull request #1590 from IntersectMBO/1574-txmetadata-cmdline
Flag to keep specified Tx Metadata by key names
2 parents bfe1133 + 66757c9 commit 6f29b18

File tree

9 files changed

+89
-13
lines changed

9 files changed

+89
-13
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: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Cardano.DbSync.Config
77
import Cardano.DbSync.Metrics (withMetricSetters)
88
import Cardano.Prelude
99
import Cardano.Slotting.Slot (SlotNo (..))
10+
import Data.List.Split (splitOn)
1011
import Data.String (String)
1112
import qualified Data.Text as Text
1213
import Data.Version (showVersion)
@@ -87,6 +88,7 @@ pRunDbSyncNode =
8788
<*> pHasShelley
8889
<*> pHasMultiAssets
8990
<*> pHasMetadata
91+
<*> pKeepTxMetadata
9092
<*> pHasPlutusExtra
9193
<*> pHasGov
9294
<*> pHasOffChainPoolData
@@ -228,6 +230,20 @@ pSlotNo =
228230
<> Opt.metavar "WORD"
229231
)
230232

233+
pKeepTxMetadata :: Parser [Word64]
234+
pKeepTxMetadata =
235+
Opt.option
236+
(parseCommaSeparated <$> Opt.str)
237+
( Opt.long "keep-tx-metadata"
238+
<> Opt.help "Insert a specific set of tx metadata, based on the tx metadata key names"
239+
)
240+
where
241+
parseCommaSeparated :: String -> [Word64]
242+
parseCommaSeparated str =
243+
case traverse readMaybe (splitOn "," str) of
244+
Just values -> values
245+
Nothing -> error "Failed to parse comma-separated values"
246+
231247
pHasInOut :: Parser Bool
232248
pHasInOut =
233249
Opt.flag

cardano-db-sync/cardano-db-sync.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,7 @@ executable cardano-db-sync
254254
, cardano-prelude
255255
, cardano-slotting
256256
, optparse-applicative
257+
, split
257258
, text
258259

259260
executable http-get-json-metadata

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

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,7 @@ import Cardano.Prelude hiding (Nat, (%))
5656
import Cardano.Slotting.Slot (EpochNo (..))
5757
import Control.Concurrent.Async
5858
import Control.Monad.Extra (whenJust)
59+
import qualified Data.Strict.Maybe as Strict
5960
import qualified Data.Text as Text
6061
import Data.Version (showVersion)
6162
import Database.Persist.Postgresql (ConnectionString, withPostgresqlConn)
@@ -232,6 +233,11 @@ extractSyncOptions snp aop =
232233
, snapshotEveryLagging = enpSnEveryLagging snp
233234
}
234235
where
236+
maybeKeepMNames =
237+
if null (enpKeepMetadataNames snp)
238+
then Strict.Nothing
239+
else Strict.Just (enpKeepMetadataNames snp)
240+
235241
iopts
236242
| enpOnlyGov snp = onlyGovInsertOptions useLedger
237243
| enpOnlyUTxO snp = onlyUTxOInsertOptions
@@ -245,6 +251,7 @@ extractSyncOptions snp aop =
245251
, ioRewards = True
246252
, ioMultiAssets = enpHasMultiAssets snp
247253
, ioMetadata = enpHasMetadata snp
254+
, ioKeepMetadataNames = maybeKeepMNames
248255
, ioPlutusExtra = enpHasPlutusExtra snp
249256
, ioOffChainPoolData = enpHasOffChainPoolData snp
250257
, ioGov = enpHasGov snp

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

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,19 @@ getPrunes = do
200200
DB.pcmPruneTxOut . getPruneConsume
201201

202202
fullInsertOptions :: Bool -> InsertOptions
203-
fullInsertOptions useLedger = InsertOptions True useLedger True True True True True True True
203+
fullInsertOptions useLedger =
204+
InsertOptions
205+
{ ioInOut = True
206+
, ioUseLedger = useLedger
207+
, ioShelley = True
208+
, ioRewards = True
209+
, ioMultiAssets = True
210+
, ioMetadata = True
211+
, ioKeepMetadataNames = Strict.Nothing
212+
, ioPlutusExtra = True
213+
, ioOffChainPoolData = True
214+
, ioGov = True
215+
}
204216

205217
onlyUTxOInsertOptions :: InsertOptions
206218
onlyUTxOInsertOptions =
@@ -211,6 +223,7 @@ onlyUTxOInsertOptions =
211223
, ioRewards = False
212224
, ioMultiAssets = True
213225
, ioMetadata = False
226+
, ioKeepMetadataNames = Strict.Nothing
214227
, ioPlutusExtra = False
215228
, ioOffChainPoolData = False
216229
, ioGov = False
@@ -220,7 +233,19 @@ onlyGovInsertOptions :: Bool -> InsertOptions
220233
onlyGovInsertOptions useLedger = (disableAllInsertOptions useLedger) {ioGov = True}
221234

222235
disableAllInsertOptions :: Bool -> InsertOptions
223-
disableAllInsertOptions useLedger = InsertOptions False useLedger False False False False False False False
236+
disableAllInsertOptions useLedger =
237+
InsertOptions
238+
{ ioInOut = False
239+
, ioUseLedger = useLedger
240+
, ioShelley = False
241+
, ioRewards = False
242+
, ioMultiAssets = False
243+
, ioMetadata = False
244+
, ioKeepMetadataNames = Strict.Nothing
245+
, ioPlutusExtra = False
246+
, ioOffChainPoolData = False
247+
, ioGov = False
248+
}
224249

225250
initEpochState :: EpochState
226251
initEpochState =

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,7 @@ data InsertOptions = InsertOptions
7878
, ioRewards :: !Bool
7979
, ioMultiAssets :: !Bool
8080
, ioMetadata :: !Bool
81+
, ioKeepMetadataNames :: Strict.Maybe [Word64]
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 :: ![Word64]
7576
, enpHasPlutusExtra :: !Bool
7677
, enpHasGov :: !Bool
7778
, enpHasOffChainPoolData :: !Bool

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

Lines changed: 29 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,15 @@ 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.Strict.Maybe as Strict
9192
import qualified Data.Text.Encoding as Text
9293
import Database.Persist.Sql (SqlBackend)
9394
import Lens.Micro
@@ -350,6 +351,7 @@ insertTx syncEnv isMember blkId epochNo slotNo applyResult blockIndex tx grouped
350351
prepareTxMetadata
351352
tracer
352353
txId
354+
iopts
353355
(Generic.txMetadata tx)
354356
mapM_
355357
(insertCertificate syncEnv isMember blkId txId epochNo slotNo redeemers)
@@ -1205,28 +1207,44 @@ prepareTxMetadata ::
12051207
(MonadBaseControl IO m, MonadIO m) =>
12061208
Trace IO Text ->
12071209
DB.TxId ->
1210+
InsertOptions ->
12081211
Maybe (Map Word64 TxMetadataValue) ->
12091212
ExceptT SyncNodeError (ReaderT SqlBackend m) [DB.TxMetadata]
1210-
prepareTxMetadata tracer txId mmetadata =
1213+
prepareTxMetadata tracer txId inOpts mmetadata = do
12111214
case mmetadata of
12121215
Nothing -> pure []
1213-
Just metadata -> mapM prepare $ Map.toList metadata
1216+
Just metadata -> mapMaybeM prepare $ Map.toList metadata
12141217
where
12151218
prepare ::
12161219
(MonadBaseControl IO m, MonadIO m) =>
12171220
(Word64, TxMetadataValue) ->
1218-
ExceptT SyncNodeError (ReaderT SqlBackend m) DB.TxMetadata
1221+
ExceptT SyncNodeError (ReaderT SqlBackend m) (Maybe DB.TxMetadata)
12191222
prepare (key, md) = do
1223+
case ioKeepMetadataNames inOpts of
1224+
Strict.Just metadataNames -> do
1225+
let isMatchingKey = key `elem` metadataNames
1226+
if isMatchingKey
1227+
then mkDbTxMetadata (key, md)
1228+
else pure Nothing
1229+
-- if we have TxMetadata and keepMetadataNames is Nothing then we want to keep all metadata
1230+
Strict.Nothing -> mkDbTxMetadata (key, md)
1231+
1232+
mkDbTxMetadata ::
1233+
(MonadBaseControl IO m, MonadIO m) =>
1234+
(Word64, TxMetadataValue) ->
1235+
ExceptT SyncNodeError (ReaderT SqlBackend m) (Maybe DB.TxMetadata)
1236+
mkDbTxMetadata (key, md) = do
12201237
let jsonbs = LBS.toStrict $ Aeson.encode (metadataValueToJsonNoSchema md)
12211238
singleKeyCBORMetadata = serialiseTxMetadataToCbor $ Map.singleton key md
12221239
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-
}
1240+
pure $
1241+
Just $
1242+
DB.TxMetadata
1243+
{ DB.txMetadataKey = DbWord64 key
1244+
, DB.txMetadataJson = mjson
1245+
, DB.txMetadataBytes = singleKeyCBORMetadata
1246+
, DB.txMetadataTxId = txId
1247+
}
12301248

12311249
insertCostModel ::
12321250
(MonadBaseControl IO m, MonadIO m) =>

doc/configuration.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,3 +154,9 @@ Some field are left empty when using this flag, like
154154
- `redeemer.script_hash` is left Null
155155

156156
Until the ledger state migration happens any restart requires reusing the `--bootstrap-tx-out` flag. After it's completed the flag can be omitted on restarts.
157+
158+
### --keep-tx-metadata
159+
160+
This flag was introduced in v.13.2.0.0 as all postgres field with the type jsonb were removed to improve insertion performance.
161+
If they are required and you have database queries against jsonb then activate this flag to re-introduce the type jsonb.
162+
You can pass multiple values to the flag eg: `--keep-tx-metadata 1,2,3` make sure you are using commas between each key.

0 commit comments

Comments
 (0)