Skip to content

Commit a9b6eee

Browse files
committed
convert to Word64 at Parser level
1 parent c7ea7a0 commit a9b6eee

File tree

6 files changed

+44
-32
lines changed

6 files changed

+44
-32
lines changed

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

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -229,10 +229,11 @@ pSlotNo =
229229
<> Opt.metavar "WORD"
230230
)
231231

232-
pKeepTxMetadata :: Parser [Text]
232+
pKeepTxMetadata :: Parser [Word64]
233233
pKeepTxMetadata =
234234
Opt.many
235-
( Opt.strOption
235+
( Opt.option
236+
Opt.auto
236237
( Opt.long "keep-tx-metadata"
237238
<> Opt.help "Insert a specific set of tx metadata, based on the tx metadata key names"
238239
)

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

Lines changed: 8 additions & 2 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,10 +233,15 @@ extractSyncOptions snp aop =
232233
, snapshotEveryLagging = enpSnEveryLagging snp
233234
}
234235
where
236+
maybeKeepMNames =
237+
if null (enpKeepMetadataNames snp)
238+
then Strict.Just (enpKeepMetadataNames snp)
239+
else Strict.Nothing
240+
235241
iopts
236242
| enpOnlyGov snp = onlyGovInsertOptions useLedger
237243
| enpOnlyUTxO snp = onlyUTxOInsertOptions
238-
| enpFullMode snp = fullInsertOptions useLedger (enpKeepMetadataNames snp)
244+
| enpFullMode snp = fullInsertOptions useLedger maybeKeepMNames
239245
| enpDisableAllMode snp = disableAllInsertOptions useLedger
240246
| otherwise =
241247
InsertOptions
@@ -245,7 +251,7 @@ extractSyncOptions snp aop =
245251
, ioRewards = True
246252
, ioMultiAssets = enpHasMultiAssets snp
247253
, ioMetadata = enpHasMetadata snp
248-
, ioKeepMetadataNames = enpKeepMetadataNames snp
254+
, ioKeepMetadataNames = maybeKeepMNames
249255
, ioPlutusExtra = enpHasPlutusExtra snp
250256
, ioOffChainPoolData = enpHasOffChainPoolData snp
251257
, ioGov = enpHasGov snp

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

Lines changed: 4 additions & 4 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 -> [Text] -> InsertOptions
203-
fullInsertOptions useLedger keepTxMDNames = InsertOptions True useLedger True True True True keepTxMDNames True True True
202+
fullInsertOptions :: Bool -> Strict.Maybe [Word64] -> InsertOptions
203+
fullInsertOptions useLedger maybeKeepMNames = InsertOptions True useLedger True True True True maybeKeepMNames True True True
204204

205205
onlyUTxOInsertOptions :: InsertOptions
206206
onlyUTxOInsertOptions =
@@ -211,7 +211,7 @@ onlyUTxOInsertOptions =
211211
, ioRewards = False
212212
, ioMultiAssets = True
213213
, ioMetadata = False
214-
, ioKeepMetadataNames = []
214+
, ioKeepMetadataNames = Strict.Nothing
215215
, ioPlutusExtra = False
216216
, ioOffChainPoolData = False
217217
, ioGov = False
@@ -221,7 +221,7 @@ onlyGovInsertOptions :: Bool -> InsertOptions
221221
onlyGovInsertOptions useLedger = (disableAllInsertOptions useLedger) {ioGov = True}
222222

223223
disableAllInsertOptions :: Bool -> InsertOptions
224-
disableAllInsertOptions useLedger = InsertOptions False useLedger False False False False [] False False False
224+
disableAllInsertOptions useLedger = InsertOptions False useLedger False False False False Strict.Nothing False False False
225225

226226
initEpochState :: EpochState
227227
initEpochState =

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

Lines changed: 2 additions & 2 deletions
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, Text, Word64)
26+
import Cardano.Prelude (Bool, Eq, IO, Show, Word64)
2727
import Cardano.Slotting.Slot (EpochNo (..))
2828
import Control.Concurrent.Class.MonadSTM.Strict (
2929
StrictTVar,
@@ -78,7 +78,7 @@ data InsertOptions = InsertOptions
7878
, ioRewards :: !Bool
7979
, ioMultiAssets :: !Bool
8080
, ioMetadata :: !Bool
81-
, ioKeepMetadataNames :: ![Text]
81+
, ioKeepMetadataNames :: Strict.Maybe [Word64]
8282
, ioPlutusExtra :: !Bool
8383
, ioOffChainPoolData :: !Bool
8484
, ioGov :: !Bool

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ data SyncNodeParams = SyncNodeParams
7272
, enpHasShelley :: !Bool
7373
, enpHasMultiAssets :: !Bool
7474
, enpHasMetadata :: !Bool
75-
, enpKeepMetadataNames :: ![Text]
75+
, enpKeepMetadataNames :: ![Word64]
7676
, enpHasPlutusExtra :: !Bool
7777
, enpHasGov :: !Bool
7878
, enpHasOffChainPoolData :: !Bool

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

Lines changed: 26 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -88,12 +88,11 @@ 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
91+
import qualified Data.Strict.Maybe as Strict
9292
import qualified Data.Text.Encoding as Text
9393
import Database.Persist.Sql (SqlBackend)
9494
import Lens.Micro
9595
import Ouroboros.Consensus.Cardano.Block (StandardConway, StandardCrypto)
96-
import Prelude (read)
9796

9897
{- HLINT ignore "Reduce duplication" -}
9998

@@ -1221,25 +1220,31 @@ prepareTxMetadata tracer txId inOpts mmetadata = do
12211220
(Word64, TxMetadataValue) ->
12221221
ExceptT SyncNodeError (ReaderT SqlBackend m) (Maybe DB.TxMetadata)
12231222
prepare (key, md) = do
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
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
1237+
let jsonbs = LBS.toStrict $ Aeson.encode (metadataValueToJsonNoSchema md)
1238+
singleKeyCBORMetadata = serialiseTxMetadataToCbor $ Map.singleton key md
1239+
mjson <- safeDecodeToJson tracer "prepareTxMetadata" jsonbs
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+
}
12431248

12441249
insertCostModel ::
12451250
(MonadBaseControl IO m, MonadIO m) =>

0 commit comments

Comments
 (0)