Skip to content

Commit 96358db

Browse files
Less things in the Types module (#297)
* Less things in the Types module * remove Utils, clean up Types (#298) --------- Co-authored-by: Brendan Brown <brendan.brown@iohk.io>
1 parent 650f9b0 commit 96358db

File tree

32 files changed

+330
-440
lines changed

32 files changed

+330
-440
lines changed

doc/read-the-docs-site/doc/marconi-as-a-library/tutorials/BasicApp.hs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,9 @@ module Main where
2121

2222
import Control.Concurrent (MVar)
2323
import Control.Concurrent.Async (race_)
24+
import Control.Exception (throwIO)
2425
import Control.Lens ((^.))
25-
import Control.Monad.Except (ExceptT, MonadError)
26+
import Control.Monad.Except (ExceptT, MonadError, runExceptT)
2627
import Control.Monad.IO.Class (MonadIO, liftIO)
2728
import Data.Aeson (FromJSON, ToJSON, (.=))
2829
import Data.Aeson qualified as Aeson
@@ -40,14 +41,13 @@ import Cardano.Api qualified as C
4041
import Cardano.BM.Setup qualified as Trace
4142
import Cardano.BM.Tracing qualified as Trace
4243
import Data.Void (Void)
43-
import Marconi.Cardano.ChainIndex.Utils qualified as Utils
44-
import Marconi.Cardano.Core.Logger (mkMarconiTrace)
44+
import Marconi.Cardano.ChainIndex.SecurityParam qualified as SecurityParam
45+
import Marconi.Cardano.Core.Logger (MarconiTrace, mkMarconiTrace)
4546
import Marconi.Cardano.Core.Node.Client.Retry qualified as Core
4647
import Marconi.Cardano.Core.Orphans ()
4748
import Marconi.Cardano.Core.Runner qualified as Core
4849
import Marconi.Cardano.Core.Types (
4950
BlockEvent (BlockEvent),
50-
MarconiTrace,
5151
RetryConfig (RetryConfig),
5252
SecurityParam,
5353
)
@@ -104,7 +104,8 @@ withIndexerBuildEnv worker action = do
104104
-- We query the local node for the security parameter with a retry mechanism
105105
-- in case the node has not been started.
106106
k <- Core.withNodeConnectRetry marconiTrace (RetryConfig 30 Nothing) socketFilePath $ do
107-
Utils.toException $ Utils.querySecurityParam @Void networkId socketFilePath
107+
runExceptT (SecurityParam.querySecurityParam @Void networkId socketFilePath)
108+
>>= either throwIO pure
108109

109110
action (Env marconiTrace socketFilePath networkId k worker)
110111

@@ -307,7 +308,7 @@ mkBlockInfoSqliteIndexerWorker
307308
-- Storage type of the indexer
308309
)
309310
mkBlockInfoSqliteIndexerWorker dbPath = do
310-
ix <- Utils.toException $ mkBlockInfoSqliteIndexer dbPath
311+
ix <- either throwIO pure =<< runExceptT (mkBlockInfoSqliteIndexer dbPath)
311312
Core.createWorker "BlockInfo" getEventsFromBlock ix
312313

313314
-- | Creation of 'Core.SQLiteIndexer' for the 'BlockInfoEvent' indexer.

marconi-cardano-chain-index/marconi-cardano-chain-index.cabal

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -89,8 +89,8 @@ library
8989
Marconi.Cardano.ChainIndex.Git.RevFromGit
9090
Marconi.Cardano.ChainIndex.Indexers
9191
Marconi.Cardano.ChainIndex.Run
92+
Marconi.Cardano.ChainIndex.SecurityParam
9293
Marconi.Cardano.ChainIndex.Snapshot.Run
93-
Marconi.Cardano.ChainIndex.Utils
9494

9595
other-modules: Paths_marconi_cardano_chain_index
9696
autogen-modules: Paths_marconi_cardano_chain_index
@@ -111,7 +111,6 @@ library
111111
, cardano-api ^>=8.20
112112
, cardano-crypto-class
113113
, cardano-ledger-shelley
114-
, cardano-slotting ^>=0.1.1.1
115114
, iohk-monitoring
116115
, ouroboros-consensus
117116

@@ -189,7 +188,6 @@ test-suite marconi-cardano-chain-index-test
189188
, cardano-ledger-core
190189
, cardano-ledger-shelley
191190
, cardano-node-emulator
192-
, cardano-slotting ^>=0.1.1.1
193191
, hedgehog-extras
194192
, iohk-monitoring
195193
, ouroboros-consensus
@@ -302,7 +300,7 @@ library marconi-cardano-chain-index-test-lib
302300
, aeson
303301
, aeson-pretty
304302
, async
305-
, base >=4.9 && <5
303+
, base >=4.9 && <5
306304
, bytestring
307305
, containers
308306
, contra-tracer
@@ -313,6 +311,7 @@ library marconi-cardano-chain-index-test-lib
313311
, hedgehog-extras
314312
, lens
315313
, mtl
314+
, nonempty-containers
316315
, process
317316
, streaming
318317
, temporary

marconi-cardano-chain-index/src/Marconi/Cardano/ChainIndex/Api/JsonRpc/Endpoint/EpochState.hs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,6 @@ import Marconi.Cardano.ChainIndex.Indexers (
2626
queryableEpochNonce,
2727
queryableEpochSDD,
2828
)
29-
import Marconi.Cardano.ChainIndex.Utils qualified as Util
3029
import Marconi.Cardano.Indexers.EpochNonce qualified as EpochState
3130
import Marconi.Cardano.Indexers.EpochSDD qualified as EpochState
3231
import Marconi.Core qualified as Core
@@ -122,8 +121,8 @@ getEpochStakePoolDelegationHandler epochNo
122121
ActiveSDDResult
123122
(Lens.view EpochState.sddPoolId epochSDD)
124123
(Lens.view EpochState.sddLovelace epochSDD)
125-
(Util.chainPointToSlotNo chainPoint)
126-
(Util.chainPointToHash chainPoint)
124+
(C.chainPointToSlotNo chainPoint)
125+
(C.chainPointToHeaderHash chainPoint)
127126
(Lens.view EpochState.sddBlockNo epochSDD)
128127
(Lens.view EpochState.sddEpochNo epochSDD)
129128

@@ -151,10 +150,10 @@ getEpochNonceHandler epochNo
151150
toEpochNonceResult Nothing = EpochNonceResult Nothing Nothing Nothing Nothing Nothing
152151
toEpochNonceResult (Just (Core.Timed chainPoint epochNonce)) =
153152
EpochNonceResult
154-
(Util.chainPointToHash chainPoint)
153+
(C.chainPointToHeaderHash chainPoint)
155154
(Just $ Lens.view EpochState.nonceBlockNo epochNonce)
156155
(Just $ Lens.view EpochState.nonceEpochNo epochNonce)
157-
(Util.chainPointToSlotNo chainPoint)
156+
(C.chainPointToSlotNo chainPoint)
158157
(nonceToMaybe $ Lens.view EpochState.nonceNonce epochNonce)
159158

160159
nonceToMaybe :: Ledger.Nonce -> Maybe (Crypto.Hash Crypto.Blake2b_256 Ledger.Nonce)

marconi-cardano-chain-index/src/Marconi/Cardano/ChainIndex/CLI.hs

Lines changed: 32 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import Data.ByteString.Char8 qualified as C8
1313
import Data.List (nub)
1414
import Data.List.NonEmpty (NonEmpty)
1515
import Data.Proxy (Proxy (Proxy))
16+
import Data.Set.NonEmpty (NESet)
1617
import Data.Text (Text)
1718
import Data.Text qualified as Text
1819
import Data.Text.Encoding qualified as Text
@@ -30,17 +31,9 @@ import GHC.Generics (Generic)
3031
import Marconi.Cardano.ChainIndex.Git.Rev (gitRev)
3132
import Marconi.Cardano.Core.Orphans ()
3233
import Marconi.Cardano.Core.Types (
33-
BlockRange,
3434
RetryConfig (RetryConfig),
35-
TargetAddresses,
36-
UtxoIndexerConfig (UtxoIndexerConfig),
37-
addressDatumDbName,
38-
epochStateDbName,
39-
mintBurnDbName,
40-
mkBlockRange,
41-
scriptTxDbName,
42-
utxoDbName,
4335
)
36+
import Marconi.Cardano.Indexers.SnapshotBlockEvent (BlockRange, mkBlockRange)
4437
import Options.Applicative (ReadM, eitherReader, execParserPure)
4538
import Paths_marconi_cardano_chain_index (version)
4639

@@ -370,6 +363,9 @@ commonBatchSizeParser =
370363
<> Opt.help "Number of blocks sent as a batch to the indexers"
371364
<> Opt.showDefault
372365

366+
-- | Type represents non empty set of Bech32 Shelley compatible addresses
367+
type TargetAddresses = NESet (C.Address C.ShelleyAddr)
368+
373369
{- | Parse the addresses to index. Addresses should be given in Bech32 format
374370
Several addresses can be given in a single string, if they are separated by a space
375371
-}
@@ -473,9 +469,17 @@ commonRetryConfigParser =
473469
<> Opt.value 1_800
474470
)
475471

472+
-- | Type defining the shape of UtxoIndexerConfig from CLI arguments.
473+
data UtxoIndexerCLIConfig = UtxoIndexerCLIConfig
474+
{ ucTargetAddresses :: Maybe TargetAddresses
475+
-- ^ List of address utxo indexer to follow
476+
, ucEnableUtxoTxOutRef :: Bool
477+
-- ^ enable utxo indexer to store txOut refScript
478+
}
479+
476480
-- | Extract UtxoIndexerConfig from CLI Options
477-
mkUtxoIndexerConfig :: Options -> UtxoIndexerConfig
478-
mkUtxoIndexerConfig o = UtxoIndexerConfig (optionsTargetAddresses o) (optionsEnableUtxoTxOutRef o)
481+
mkUtxoIndexerConfig :: Options -> UtxoIndexerCLIConfig
482+
mkUtxoIndexerConfig o = UtxoIndexerCLIConfig (optionsTargetAddresses o) (optionsEnableUtxoTxOutRef o)
479483

480484
-- | CL options for the marconi-chain-snapshot executable.
481485
data SnapshotOptions = SnapshotOptions
@@ -536,3 +540,20 @@ readBlockRange rawBlockRange =
536540
"Block range is not formatted correctly. "
537541
<> "Please provide a pair of positive numbers, "
538542
<> "without any spaces."
543+
544+
-- * Database file names
545+
546+
utxoDbName :: FilePath
547+
utxoDbName = "utxo.db"
548+
549+
addressDatumDbName :: FilePath
550+
addressDatumDbName = "addressdatum.db"
551+
552+
scriptTxDbName :: FilePath
553+
scriptTxDbName = "scripttx.db"
554+
555+
epochStateDbName :: FilePath
556+
epochStateDbName = "epochstate.db"
557+
558+
mintBurnDbName :: FilePath
559+
mintBurnDbName = "mintburn.db"

marconi-cardano-chain-index/src/Marconi/Cardano/ChainIndex/Indexers.hs

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,16 +27,14 @@ import Marconi.Cardano.Core.Indexer.Worker (
2727
StandardWorker (StandardWorker),
2828
StandardWorkerConfig (StandardWorkerConfig, eventExtractor, logger, workerName),
2929
)
30+
import Marconi.Cardano.Core.Logger (MarconiTrace)
3031
import Marconi.Cardano.Core.Transformer.WithSyncStats (WithSyncStats)
3132
import Marconi.Cardano.Core.Types (
3233
AnyTxBody (AnyTxBody),
3334
BlockEvent (BlockEvent),
34-
BlockRange,
35-
MarconiTrace,
3635
SecurityParam,
3736
TipAndBlock (TipAndBlock),
3837
TxIndexInBlock,
39-
blockRangeFst,
4038
)
4139
import Marconi.Cardano.Indexers.BlockInfo qualified as BlockInfo
4240
import Marconi.Cardano.Indexers.ChainTip qualified as ChainTip
@@ -309,7 +307,7 @@ snapshotBlockEventBuilder
309307
-> Core.CatchupConfig
310308
-> BM.Trace m Text
311309
-> FilePath
312-
-> BlockRange
310+
-> SnapshotBlockEvent.BlockRange
313311
-> FilePath
314312
-> n
315313
( Core.WorkerIndexer
@@ -353,7 +351,7 @@ buildIndexersForSnapshot
353351
-> BM.Trace IO Text
354352
-> MarconiTrace IO
355353
-> FilePath
356-
-> [BlockRange]
354+
-> [SnapshotBlockEvent.BlockRange]
357355
-> FilePath
358356
-> ExceptT
359357
Core.IndexerError
@@ -421,7 +419,7 @@ snapshotExtLedgerStateEventBuilder
421419
-> Core.CatchupConfig
422420
-> BM.Trace m Text
423421
-> FilePath
424-
-> BlockRange
422+
-> SnapshotBlockEvent.BlockRange
425423
-> FilePath
426424
-> n
427425
( Core.WorkerIndexer
@@ -474,7 +472,10 @@ snapshotExtLedgerStateEventWorker standardWorkerConfig snapshotBlockEventWorkerC
474472
Core.createWorkerWithPreprocessing (workerName standardWorkerConfig) preprocessor indexer
475473

476474
justBeforeBlockRangePreprocessor
477-
:: (Monad m) => (a -> C.BlockNo) -> BlockRange -> Core.Preprocessor m C.ChainPoint a a
475+
:: (Monad m)
476+
=> (a -> C.BlockNo)
477+
-> SnapshotBlockEvent.BlockRange
478+
-> Core.Preprocessor m C.ChainPoint a a
478479
justBeforeBlockRangePreprocessor toBlockNo br =
479480
Core.scanMaybeEvent filterJustBeforeBlockRange Nothing
480481
where
@@ -486,5 +487,5 @@ justBeforeBlockRangePreprocessor toBlockNo br =
486487
offset
487488
| leftRange == 0 = 0
488489
| otherwise = 1
489-
leftRange = Lens.view blockRangeFst br
490+
leftRange = Lens.view SnapshotBlockEvent.blockRangeFst br
490491
inputWord = C.unBlockNo . toBlockNo $ input

marconi-cardano-chain-index/src/Marconi/Cardano/ChainIndex/Run.hs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ import Cardano.BM.Setup qualified as BM
88
import Cardano.BM.Trace (logError, logInfo)
99
import Cardano.BM.Tracing qualified as BM
1010
import Control.Concurrent.Async (race_)
11-
import Control.Exception (finally)
12-
import Control.Monad (guard, unless)
11+
import Control.Exception (finally, throwIO)
12+
import Control.Monad (guard, unless, (>=>))
1313
import Control.Monad.Except (runExceptT)
1414
import Control.Monad.Reader (runReaderT)
1515
import Data.Aeson (toJSON)
@@ -27,11 +27,11 @@ import Marconi.Cardano.ChainIndex.Api.Types (
2727
)
2828
import Marconi.Cardano.ChainIndex.CLI qualified as Cli
2929
import Marconi.Cardano.ChainIndex.Indexers (buildIndexers)
30-
import Marconi.Cardano.ChainIndex.Utils qualified as Utils
30+
import Marconi.Cardano.ChainIndex.SecurityParam qualified as SecurityParam
3131
import Marconi.Cardano.Core.Logger (defaultStdOutLogger, mkMarconiTrace)
3232
import Marconi.Cardano.Core.Node.Client.Retry (withNodeConnectRetry)
3333
import Marconi.Cardano.Core.Runner qualified as Runner
34-
import Marconi.Cardano.Core.Types (SecurityParam (SecurityParam), TargetAddresses)
34+
import Marconi.Cardano.Core.Types (SecurityParam (SecurityParam))
3535
import Marconi.Cardano.Indexers.MintTokenEvent qualified as MintTokenEvent
3636
import Marconi.Cardano.Indexers.Utxo qualified as Utxo
3737
import Marconi.Core qualified as Core
@@ -117,8 +117,8 @@ run appName = withGracefulTermination_ $ do
117117

118118
securityParam <-
119119
withNodeConnectRetry marconiTrace retryConfig socketPath $
120-
Utils.toException $
121-
Utils.querySecurityParam @Void networkId socketPath
120+
(runExceptT >=> either throwIO pure) $
121+
SecurityParam.querySecurityParam @Void networkId socketPath
122122

123123
let SecurityParam stopCatchupDistance = securityParam
124124
extLedgerStateAsEvent previousLedgerStateEvent ledgerStateEvent _blockEvent = do
@@ -184,7 +184,7 @@ run appName = withGracefulTermination_ $ do
184184
runHttpServer'
185185
`finally` BM.shutdown sb
186186

187-
shelleyAddressesToAddressAny :: Maybe TargetAddresses -> [C.AddressAny]
187+
shelleyAddressesToAddressAny :: Maybe Cli.TargetAddresses -> [C.AddressAny]
188188
shelleyAddressesToAddressAny Nothing = []
189189
shelleyAddressesToAddressAny (Just targetAddresses) =
190190
fmap C.AddressShelley $ NEList.toList $ NESet.toList targetAddresses
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
{-# LANGUAGE DerivingStrategies #-}
2+
{-# LANGUAGE OverloadedStrings #-}
3+
4+
{- |
5+
Query the 'SecurityParam' from a running local node.
6+
-}
7+
module Marconi.Cardano.ChainIndex.SecurityParam (
8+
querySecurityParam,
9+
querySecurityParamEra,
10+
) where
11+
12+
import Cardano.Api qualified as C
13+
import Cardano.Api.Extended.IPC qualified as CE
14+
import Cardano.Api.Extended.Shelley qualified as CE
15+
import Control.Monad.Except (
16+
ExceptT,
17+
throwError,
18+
)
19+
import Control.Monad.Trans (MonadTrans (lift))
20+
import Data.Text (pack)
21+
import Marconi.Cardano.ChainIndex.Error (IndexerError (CantStartIndexer))
22+
import Marconi.Cardano.Core.Types (
23+
SecurityParam,
24+
)
25+
import Ouroboros.Consensus.HardFork.Combinator.AcrossEras (EraMismatch)
26+
27+
{- | Query security param from the local node.
28+
It queries the current era first, and uses that to query the security parameter.
29+
-}
30+
querySecurityParam
31+
:: C.NetworkId
32+
-> FilePath
33+
-- ^ Node socket file path
34+
-> ExceptT (IndexerError err) IO SecurityParam
35+
querySecurityParam networkId socketPath = do
36+
(C.AnyCardanoEra era) <- queryCurrentEra networkId socketPath
37+
case CE.cardanoEraToShelleyBasedEra era of
38+
Nothing -> throwError $ CantStartIndexer "The security parameter can only be queried in shelley based era."
39+
Just shelleyBasedEra -> querySecurityParamEra shelleyBasedEra networkId socketPath
40+
41+
-- | Query the current era of the local node's current state.
42+
queryCurrentEra
43+
:: C.NetworkId
44+
-> FilePath
45+
-- ^ Node socket file path
46+
-> ExceptT (IndexerError err) IO C.AnyCardanoEra
47+
queryCurrentEra networkId socketPath = do
48+
let localNodeConnectInfo :: C.LocalNodeConnectInfo C.CardanoMode
49+
localNodeConnectInfo = CE.mkLocalNodeConnectInfo networkId socketPath
50+
51+
queryInMode :: C.QueryInMode C.CardanoMode C.AnyCardanoEra
52+
queryInMode = C.QueryCurrentEra C.CardanoModeIsMultiEra
53+
54+
result <- lift $ C.queryNodeLocalState localNodeConnectInfo Nothing queryInMode
55+
case result of
56+
Left err -> toError err
57+
Right x -> pure x
58+
59+
-- | Query security param from the local node given a Shelley based era.
60+
querySecurityParamEra
61+
:: forall era err
62+
. C.ShelleyBasedEra era
63+
-> C.NetworkId
64+
-> FilePath
65+
-- ^ Node socket file path
66+
-> ExceptT (IndexerError err) IO SecurityParam
67+
querySecurityParamEra shelleyBasedEra networkId socketPath = do
68+
result <- lift $ C.queryNodeLocalState localNodeConnectInfo Nothing queryInMode
69+
genesisParameters <- case result of
70+
Left err -> toError err
71+
Right (Left err) -> toError err
72+
Right (Right x) -> pure x
73+
pure $ fromIntegral $ C.protocolParamSecurity genesisParameters
74+
where
75+
localNodeConnectInfo :: C.LocalNodeConnectInfo C.CardanoMode
76+
localNodeConnectInfo = CE.mkLocalNodeConnectInfo networkId socketPath
77+
78+
queryInMode :: C.QueryInMode C.CardanoMode (Either EraMismatch (C.GenesisParameters C.ShelleyEra))
79+
queryInMode =
80+
C.QueryInEra (CE.toShelleyEraInCardanoMode shelleyBasedEra) $
81+
C.QueryInShelleyBasedEra shelleyBasedEra C.QueryGenesisParameters
82+
83+
toError :: (Show a) => a -> ExceptT (IndexerError err) IO b
84+
toError = throwError . CantStartIndexer . pack . show

0 commit comments

Comments
 (0)