Skip to content

Commit 59f8eed

Browse files
sgillespiekderme
authored andcommitted
fix(cardano-chain-gen): Fix insert options issues
* Validate state-dir/ledger config * Lookup in/out from config * Disable rewards in some presets
1 parent 5f7eaf7 commit 59f8eed

File tree

11 files changed

+118
-44
lines changed

11 files changed

+118
-44
lines changed

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ insertConfig = do
2424
{ sioTxOut = TxOutDisable
2525
, sioLedger = LedgerDisable
2626
, sioShelley = ShelleyDisable
27+
, sioRewards = RewardsConfig True
2728
, sioMultiAsset = MultiAssetDisable
2829
, sioMetadata = MetadataDisable
2930
, sioPlutus = PlutusDisable

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ insertConfig = do
2424
{ sioTxOut = TxOutDisable
2525
, sioLedger = LedgerDisable
2626
, sioShelley = ShelleyDisable
27+
, sioRewards = RewardsConfig True
2728
, sioMultiAsset = MultiAssetDisable
2829
, sioMetadata = MetadataDisable
2930
, sioPlutus = PlutusDisable

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,7 @@ insertConfig = do
9494
{ sioTxOut = TxOutDisable
9595
, sioLedger = LedgerDisable
9696
, sioShelley = ShelleyDisable
97+
, sioRewards = RewardsConfig True
9798
, sioMultiAsset = MultiAssetDisable
9899
, sioMetadata = MetadataDisable
99100
, sioPlutus = PlutusDisable

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

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import Cardano.Db (MigrationDir (..), PGPassSource (PGPassDefaultEnv, PGPassEnv), gitRev)
55
import Cardano.DbSync (runDbSyncNode)
66
import Cardano.DbSync.Config
7+
import Cardano.DbSync.Config.Types
78
import Cardano.DbSync.Metrics (withMetricSetters)
89
import Cardano.Prelude
910
import Cardano.Slotting.Slot (SlotNo (..))
@@ -15,6 +16,7 @@ import Options.Applicative (Parser, ParserInfo)
1516
import qualified Options.Applicative as Opt
1617
import Paths_cardano_db_sync (version)
1718
import System.Info (arch, compilerName, compilerVersion, os)
19+
import Prelude (error)
1820

1921
main :: IO ()
2022
main = do
@@ -23,26 +25,36 @@ main = do
2325
CmdVersion -> runVersionCommand
2426
CmdRun params -> run params
2527
where
26-
-- TODO[sgillespie]: This check needs to come after we parse the config file
27-
--
28-
-- (Nothing, True) -> error stateDirErrorMsg
29-
--
30-
-- stateDirErrorMsg :: [Char]
31-
-- stateDirErrorMsg =
32-
-- "Error: If not using --state-dir then make sure to have --disable-ledger. "
33-
-- <> "For more details view https://github.com/IntersectMBO/cardano-db-sync/blob/master/doc/syncing-and-rollbacks.md#ledger-state"
34-
3528
knownMigrationsPlain :: [(Text, Text)]
3629
knownMigrationsPlain = (\x -> (hash x, filepath x)) <$> knownMigrations
3730

3831
run :: SyncNodeParams -> IO ()
3932
run prms = do
4033
-- read the config file as early as possible
4134
syncNodeConfigFromFile <- readSyncNodeConfig (enpConfigFile prms)
35+
36+
-- Validate state-dir/ledger
37+
let maybeLedgerStateDir = enpMaybeLedgerStateDir prms
38+
ledgerCfg = sioLedger (dncInsertOptions syncNodeConfigFromFile)
39+
40+
void $ case (maybeLedgerStateDir, ledgerCfg) of
41+
-- It is an error to enable ledger and not specify the state
42+
(Nothing, LedgerEnable) -> error stateDirErrorMsg
43+
-- Or to ignore ledger and not specify the state
44+
(Nothing, LedgerIgnore) -> error stateDirErrorMsg
45+
-- Otherwise, it's OK
46+
_ -> pure ()
47+
4248
let prometheusPort = dncPrometheusPort syncNodeConfigFromFile
4349
withMetricSetters prometheusPort $ \metricsSetters ->
4450
runDbSyncNode metricsSetters knownMigrationsPlain prms syncNodeConfigFromFile
4551

52+
stateDirErrorMsg :: [Char]
53+
stateDirErrorMsg =
54+
"Error: If not using --state-dir then make sure to have ledger disabled. "
55+
<> "For more details view https://github.com/IntersectMBO/cardano-db-sync/blob"
56+
<> "/master/doc/syncing-and-rollbacks.md#ledger-state"
57+
4658
-- -------------------------------------------------------------------------------------------------
4759

4860
opts :: ParserInfo SyncCommand

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -253,10 +253,11 @@ extractSyncOptions snp aop snc =
253253

254254
iopts =
255255
InsertOptions
256-
{ ioInOut = enpHasInOut snp
256+
{ ioInOut = isTxOutEnabled'
257257
, ioUseLedger = useLedger
258258
, ioShelley = isShelleyEnabled (sioShelley (dncInsertOptions snc))
259-
, ioRewards = True
259+
, -- Rewards are only disabled on "disable_all" and "only_gov" presets
260+
ioRewards = True
260261
, ioMultiAssets = isMultiAssetEnabled (sioMultiAsset (dncInsertOptions snc))
261262
, ioMetadata = isMetadataEnabled (sioMetadata (dncInsertOptions snc))
262263
, ioKeepMetadataNames = maybeKeepMNames
@@ -274,6 +275,7 @@ extractSyncOptions snp aop snc =
274275
isTxOutConsumed' = isTxOutConsumed . sioTxOut . dncInsertOptions $ snc
275276
isTxOutPrune' = isTxOutPrune . sioTxOut . dncInsertOptions $ snc
276277
isTxOutBootstrap' = isTxOutBootstrap . sioTxOut . dncInsertOptions $ snc
278+
isTxOutEnabled' = isTxOutEnabled . sioTxOut . dncInsertOptions $ snc
277279
forceTxIn' = forceTxIn . sioTxOut . dncInsertOptions $ snc
278280

279281
startupReport :: Trace IO Text -> Bool -> SyncNodeParams -> IO ()

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

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ fullInsertOptions =
215215
{ sioTxOut = TxOutEnable
216216
, sioLedger = LedgerEnable
217217
, sioShelley = ShelleyEnable
218+
, sioRewards = RewardsConfig True
218219
, sioMultiAsset = MultiAssetEnable
219220
, sioMetadata = MetadataEnable
220221
, sioPlutus = PlutusEnable
@@ -229,6 +230,7 @@ onlyUTxOInsertOptions =
229230
{ sioTxOut = TxOutEnable
230231
, sioLedger = LedgerIgnore
231232
, sioShelley = ShelleyDisable
233+
, sioRewards = RewardsConfig True
232234
, sioMultiAsset = MultiAssetDisable
233235
, sioMetadata = MetadataDisable
234236
, sioPlutus = PlutusDisable
@@ -248,8 +250,9 @@ disableAllInsertOptions :: SyncInsertOptions
248250
disableAllInsertOptions =
249251
SyncInsertOptions
250252
{ sioTxOut = TxOutDisable
251-
, sioLedger = LedgerIgnore
253+
, sioLedger = LedgerDisable
252254
, sioShelley = ShelleyDisable
255+
, sioRewards = RewardsConfig False
253256
, sioMultiAsset = MultiAssetDisable
254257
, sioMetadata = MetadataDisable
255258
, sioPlutus = PlutusDisable
@@ -408,8 +411,8 @@ mkSyncEnv trce backend connectionString syncOptions protoInfo nw nwMagic systemS
408411
(Nothing, False) -> NoLedger <$> mkNoLedgerEnv trce protoInfo nw systemStart
409412
(Just _, False) -> do
410413
logWarning trce $
411-
"Using `--disable-ledger` doesn't require having a --state-dir."
412-
<> " For more details view https://github.com/IntersectMBO/cardano-db-sync/blob/master/doc/configuration.md#--disable-ledger"
414+
"Disabling the ledger doesn't require having a --state-dir."
415+
<> " For more details view https://github.com/IntersectMBO/cardano-db-sync/blob/master/doc/configuration.md#ledger"
413416
NoLedger <$> mkNoLedgerEnv trce protoInfo nw systemStart
414417
-- This won't ever call because we error out this combination at parse time
415418
(Nothing, True) -> NoLedger <$> mkNoLedgerEnv trce protoInfo nw systemStart

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ module Cardano.DbSync.Config.Types (
2626
ForceTxIn (..),
2727
LedgerInsertConfig (..),
2828
ShelleyInsertConfig (..),
29+
RewardsConfig (..),
2930
MultiAssetConfig (..),
3031
MetadataConfig (..),
3132
PlutusConfig (..),
@@ -157,6 +158,7 @@ data SyncInsertOptions = SyncInsertOptions
157158
{ sioTxOut :: TxOutConfig
158159
, sioLedger :: LedgerInsertConfig
159160
, sioShelley :: ShelleyInsertConfig
161+
, sioRewards :: RewardsConfig
160162
, sioMultiAsset :: MultiAssetConfig
161163
, sioMetadata :: MetadataConfig
162164
, sioPlutus :: PlutusConfig
@@ -190,6 +192,10 @@ data ShelleyInsertConfig
190192
| ShelleyStakeAddrs (NonEmpty ShortByteString)
191193
deriving (Eq, Show)
192194

195+
newtype RewardsConfig = RewardsConfig
196+
{areRewardsEnabled :: Bool}
197+
deriving (Eq, Show)
198+
193199
data MultiAssetConfig
194200
= MultiAssetEnable
195201
| MultiAssetDisable
@@ -381,6 +387,7 @@ instance FromJSON SyncInsertOptions where
381387
<$> obj .:? "tx_out" .!= sioTxOut def
382388
<*> obj .:? "ledger" .!= sioLedger def
383389
<*> obj .:? "shelley" .!= sioShelley def
390+
<*> pure (sioRewards def)
384391
<*> obj .:? "multi_asset" .!= sioMultiAsset def
385392
<*> obj .:? "metadata" .!= sioMetadata def
386393
<*> obj .:? "plutus" .!= sioPlutus def
@@ -571,6 +578,7 @@ instance Default SyncInsertOptions where
571578
{ sioTxOut = TxOutEnable
572579
, sioLedger = LedgerEnable
573580
, sioShelley = ShelleyEnable
581+
, sioRewards = RewardsConfig True
574582
, sioMultiAsset = MultiAssetEnable
575583
, sioMetadata = MetadataEnable
576584
, sioPlutus = PlutusEnable

cardano-db-sync/test/Cardano/DbSync/ApiTest.hs

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,16 @@ tests =
1414
checkParallel $
1515
Group
1616
"Cardano.DbSync.Api"
17-
[("extractInsertOptions", prop_extractInsertOptions)]
17+
[ ("extractInsertOptions", prop_extractInsertOptions)
18+
, ("extractInsertOptions rewards", prop_extractInsertOptionsRewards)
19+
]
1820

1921
prop_extractInsertOptions :: Property
2022
prop_extractInsertOptions = property $ do
2123
cfg <- forAll Gen.syncPreConfig
2224

2325
let insertOpts = pcInsertConfig cfg
24-
25-
isSyncInsertConfig =
26-
case insertOpts of
27-
SyncInsertConfig _ -> True
28-
_ -> False
29-
30-
cover 5 "full" (insertOpts == FullInsertOptions)
31-
cover 5 "only utxo" (insertOpts == OnlyUTxOInsertOptions)
32-
cover 5 "only gov" (insertOpts == OnlyGovInsertOptions)
33-
cover 5 "disable all" (insertOpts == DisableAllInsertOptions)
34-
cover 5 "config" isSyncInsertConfig
26+
coverInsertCfg insertOpts
3527

3628
case insertOpts of
3729
FullInsertOptions ->
@@ -44,3 +36,32 @@ prop_extractInsertOptions = property $ do
4436
extractInsertOptions cfg === disableAllInsertOptions
4537
SyncInsertConfig cfg' ->
4638
extractInsertOptions cfg === cfg'
39+
40+
prop_extractInsertOptionsRewards :: Property
41+
prop_extractInsertOptionsRewards = property $ do
42+
cfg <- forAll Gen.syncPreConfig
43+
44+
let insertOpts = pcInsertConfig cfg
45+
coverInsertCfg insertOpts
46+
47+
let areRewardsEnabled' = areRewardsEnabled $ sioRewards (extractInsertOptions cfg)
48+
49+
case insertOpts of
50+
OnlyGovInsertOptions ->
51+
assert $ not areRewardsEnabled'
52+
DisableAllInsertOptions ->
53+
assert $ not areRewardsEnabled'
54+
_ -> assert areRewardsEnabled'
55+
56+
coverInsertCfg :: MonadTest m => SyncInsertConfig -> m ()
57+
coverInsertCfg insertOpts = do
58+
cover 5 "full" (insertOpts == FullInsertOptions)
59+
cover 5 "only utxo" (insertOpts == OnlyUTxOInsertOptions)
60+
cover 5 "only gov" (insertOpts == OnlyGovInsertOptions)
61+
cover 5 "disable all" (insertOpts == DisableAllInsertOptions)
62+
cover 5 "config" isSyncInsertConfig
63+
where
64+
isSyncInsertConfig =
65+
case insertOpts of
66+
SyncInsertConfig _ -> True
67+
_ -> False

cardano-db-sync/test/Cardano/DbSync/Gen.hs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,7 @@ syncInsertOptions =
118118
<$> txOutConfig
119119
<*> Gen.element [LedgerEnable, LedgerDisable, LedgerIgnore]
120120
<*> shelleyConfig
121+
<*> pure (RewardsConfig True)
121122
<*> multiAssetConfig
122123
<*> metadataConfig
123124
<*> plutusConfig

cardano-db-sync/test/Cardano/DbSyncTest.hs

Lines changed: 39 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,20 @@ tests =
1818
checkParallel $
1919
Group
2020
"Cardano.DbSync"
21-
[ ("extractSyncOptions passes tx out", prop_extractSyncOptionsTxOut)
21+
[
22+
( "extractSyncOptions passes prune consume migration"
23+
, prop_extractSyncOptionsPruneConsumeMigration
24+
)
25+
, ("extractSyncOptions passes tx out disable", prop_extractSyncOptionsDisable)
2226
,
2327
( "extractSyncOptions passes offchain pool data"
2428
, prop_extractSyncOptionsOffchainPoolData
2529
)
2630
, ("extractSyncOptions passes governance", prop_extractSyncOptionsGov)
2731
]
2832

29-
prop_extractSyncOptionsTxOut :: Property
30-
prop_extractSyncOptionsTxOut = property $ do
33+
prop_extractSyncOptionsPruneConsumeMigration :: Property
34+
prop_extractSyncOptionsPruneConsumeMigration = property $ do
3135
loggingCfg <- liftIO Logging.empty
3236
syncNodeParams <- forAll Gen.syncNodeParams
3337
abortOnPanic <- forAll Gen.bool
@@ -36,17 +40,7 @@ prop_extractSyncOptionsTxOut = property $ do
3640
(const "SyncNodeConfig") -- SyncNodeConfig does not have Show
3741
$ Gen.syncNodeConfig loggingCfg
3842

39-
let isTxOutEnabled' = isTxOutEnabled . sioTxOut . dncInsertOptions $ syncNodeConfig
40-
isTxOutDisabled' = isTxOutEnabled . sioTxOut . dncInsertOptions $ syncNodeConfig
41-
isTxOutBootstrap' = isTxOutBootstrap . sioTxOut . dncInsertOptions $ syncNodeConfig
42-
isTxOutPrune' = isTxOutPrune . sioTxOut . dncInsertOptions $ syncNodeConfig
43-
isTxOutConsumed' = isTxOutConsumed . sioTxOut . dncInsertOptions $ syncNodeConfig
44-
45-
cover 5 "tx out enabled" isTxOutEnabled'
46-
cover 5 "tx out disabled" isTxOutDisabled'
47-
cover 5 "tx out bootstrap" isTxOutBootstrap'
48-
cover 5 "tx out prune" isTxOutPrune'
49-
cover 5 "tx out consumed" isTxOutConsumed'
43+
coverTxOut syncNodeConfig
5044

5145
let syncOptions = extractSyncOptions syncNodeParams abortOnPanic syncNodeConfig
5246
expectedPruneConsume =
@@ -74,6 +68,23 @@ prop_extractSyncOptionsOffchainPoolData = property $ do
7468
ioOffChainPoolData (soptInsertOptions syncOptions)
7569
=== isOffchainPoolDataEnabled (sioOffchainPoolData (dncInsertOptions syncNodeConfig))
7670

71+
prop_extractSyncOptionsDisable :: Property
72+
prop_extractSyncOptionsDisable = property $ do
73+
loggingCfg <- liftIO Logging.empty
74+
syncNodeParams <- forAll Gen.syncNodeParams
75+
abortOnPanic <- forAll Gen.bool
76+
syncNodeConfig <-
77+
forAllWith
78+
(const "SyncNodeConfig") -- SyncNodeConfig does not have Show
79+
$ Gen.syncNodeConfig loggingCfg
80+
81+
coverTxOut syncNodeConfig
82+
83+
let isTxOutDisabled' = isTxOutEnabled . sioTxOut . dncInsertOptions $ syncNodeConfig
84+
syncOptions = extractSyncOptions syncNodeParams abortOnPanic syncNodeConfig
85+
86+
ioInOut (soptInsertOptions syncOptions) === isTxOutDisabled'
87+
7788
prop_extractSyncOptionsGov :: Property
7889
prop_extractSyncOptionsGov = property $ do
7990
loggingCfg <- liftIO Logging.empty
@@ -88,3 +99,17 @@ prop_extractSyncOptionsGov = property $ do
8899

89100
ioGov (soptInsertOptions syncOptions)
90101
=== isGovernanceEnabled (sioGovernance (dncInsertOptions syncNodeConfig))
102+
103+
coverTxOut :: MonadTest m => SyncNodeConfig -> m ()
104+
coverTxOut syncNodeConfig = do
105+
let isTxOutEnabled' = isTxOutEnabled . sioTxOut . dncInsertOptions $ syncNodeConfig
106+
isTxOutDisabled' = isTxOutEnabled . sioTxOut . dncInsertOptions $ syncNodeConfig
107+
isTxOutBootstrap' = isTxOutBootstrap . sioTxOut . dncInsertOptions $ syncNodeConfig
108+
isTxOutPrune' = isTxOutPrune . sioTxOut . dncInsertOptions $ syncNodeConfig
109+
isTxOutConsumed' = isTxOutConsumed . sioTxOut . dncInsertOptions $ syncNodeConfig
110+
111+
cover 5 "tx out enabled" isTxOutEnabled'
112+
cover 5 "tx out disabled" isTxOutDisabled'
113+
cover 5 "tx out bootstrap" isTxOutBootstrap'
114+
cover 5 "tx out prune" isTxOutPrune'
115+
cover 5 "tx out consumed" isTxOutConsumed'

0 commit comments

Comments
 (0)