Skip to content

Commit 572095c

Browse files
authored
Merge pull request #1775 from IntersectMBO/1641-config-overide-presets
1641 - single configs can override presets
2 parents a67e9fd + e833345 commit 572095c

File tree

5 files changed

+189
-129
lines changed

5 files changed

+189
-129
lines changed

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

Lines changed: 1 addition & 72 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,6 @@
99

1010
module Cardano.DbSync.Api (
1111
extractInsertOptions,
12-
fullInsertOptions,
13-
onlyUTxOInsertOptions,
14-
onlyGovInsertOptions,
15-
disableAllInsertOptions,
1612
setConsistentLevel,
1713
getConsistentLevel,
1814
isConsistent,
@@ -211,74 +207,7 @@ getPrunes = do
211207
DB.pcmPruneTxOut . getPruneConsume
212208

213209
extractInsertOptions :: SyncPreConfig -> SyncInsertOptions
214-
extractInsertOptions cfg =
215-
case pcInsertConfig cfg of
216-
FullInsertOptions -> fullInsertOptions
217-
OnlyUTxOInsertOptions -> onlyUTxOInsertOptions
218-
OnlyGovInsertOptions -> onlyGovInsertOptions
219-
DisableAllInsertOptions -> disableAllInsertOptions
220-
SyncInsertConfig opts -> opts
221-
222-
fullInsertOptions :: SyncInsertOptions
223-
fullInsertOptions =
224-
SyncInsertOptions
225-
{ sioTxCBOR = TxCBORConfig False
226-
, sioTxOut = TxOutEnable
227-
, sioLedger = LedgerEnable
228-
, sioShelley = ShelleyEnable
229-
, sioRewards = RewardsConfig True
230-
, sioMultiAsset = MultiAssetEnable
231-
, sioMetadata = MetadataEnable
232-
, sioPlutus = PlutusEnable
233-
, sioGovernance = GovernanceConfig True
234-
, sioOffchainPoolData = OffchainPoolDataConfig True
235-
, sioPoolStats = PoolStatsConfig True
236-
, sioJsonType = JsonTypeText
237-
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
238-
}
239-
240-
onlyUTxOInsertOptions :: SyncInsertOptions
241-
onlyUTxOInsertOptions =
242-
SyncInsertOptions
243-
{ sioTxCBOR = TxCBORConfig False
244-
, sioTxOut = TxOutBootstrap (ForceTxIn False)
245-
, sioLedger = LedgerIgnore
246-
, sioShelley = ShelleyDisable
247-
, sioRewards = RewardsConfig True
248-
, sioMultiAsset = MultiAssetDisable
249-
, sioMetadata = MetadataDisable
250-
, sioPlutus = PlutusDisable
251-
, sioGovernance = GovernanceConfig False
252-
, sioOffchainPoolData = OffchainPoolDataConfig False
253-
, sioPoolStats = PoolStatsConfig False
254-
, sioJsonType = JsonTypeText
255-
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
256-
}
257-
258-
onlyGovInsertOptions :: SyncInsertOptions
259-
onlyGovInsertOptions =
260-
disableAllInsertOptions
261-
{ sioLedger = LedgerEnable
262-
, sioGovernance = GovernanceConfig True
263-
}
264-
265-
disableAllInsertOptions :: SyncInsertOptions
266-
disableAllInsertOptions =
267-
SyncInsertOptions
268-
{ sioTxCBOR = TxCBORConfig False
269-
, sioTxOut = TxOutDisable
270-
, sioLedger = LedgerDisable
271-
, sioShelley = ShelleyDisable
272-
, sioRewards = RewardsConfig False
273-
, sioMultiAsset = MultiAssetDisable
274-
, sioMetadata = MetadataDisable
275-
, sioPlutus = PlutusDisable
276-
, sioOffchainPoolData = OffchainPoolDataConfig False
277-
, sioPoolStats = PoolStatsConfig False
278-
, sioGovernance = GovernanceConfig False
279-
, sioJsonType = JsonTypeText
280-
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
281-
}
210+
extractInsertOptions = sicOptions . pcInsertConfig
282211

283212
initCurrentEpochNo :: CurrentEpochNo
284213
initCurrentEpochNo =

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

Lines changed: 147 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ module Cardano.DbSync.Config.Types (
2222
SyncNodeConfig (..),
2323
SyncPreConfig (..),
2424
SyncInsertConfig (..),
25+
SyncInsertPreset (..),
2526
SyncInsertOptions (..),
2627
TxCBORConfig (..),
2728
PoolStatsConfig (..),
@@ -55,6 +56,10 @@ module Cardano.DbSync.Config.Types (
5556
isTxOutConsumed,
5657
isTxOutPrune,
5758
forceTxIn,
59+
fullInsertOptions,
60+
onlyUTxOInsertOptions,
61+
onlyGovInsertOptions,
62+
disableAllInsertOptions,
5863
) where
5964

6065
import qualified Cardano.BM.Configuration as Logging
@@ -68,7 +73,8 @@ import Cardano.Slotting.Slot (SlotNo (..))
6873
import Control.Monad (fail)
6974
import Data.Aeson (FromJSON (..), ToJSON (..), Value (..), (.!=), (.:), (.:?), (.=))
7075
import qualified Data.Aeson as Aeson
71-
import Data.Aeson.Types (Parser, typeMismatch)
76+
import Data.Aeson.Key (fromText)
77+
import Data.Aeson.Types (Pair, Parser, typeMismatch)
7278
import Data.ByteString.Short (ShortByteString (), fromShort, toShort)
7379
import Data.Default.Class (Default (..))
7480
import Ouroboros.Consensus.Cardano.CanHardFork (TriggerHardFork (..))
@@ -150,12 +156,17 @@ data SyncPreConfig = SyncPreConfig
150156
}
151157
deriving (Show)
152158

153-
data SyncInsertConfig
154-
= FullInsertOptions
155-
| OnlyUTxOInsertOptions
156-
| OnlyGovInsertOptions
157-
| DisableAllInsertOptions
158-
| SyncInsertConfig SyncInsertOptions
159+
data SyncInsertConfig = SyncInsertConfig
160+
{ sicPreset :: Maybe SyncInsertPreset
161+
, sicOptions :: SyncInsertOptions
162+
}
163+
deriving (Eq, Show)
164+
165+
data SyncInsertPreset
166+
= FullInsertPreset
167+
| OnlyUTxOInsertPreset
168+
| OnlyGovInsertPreset
169+
| DisableAllInsertPreset
159170
deriving (Eq, Show)
160171

161172
data SyncInsertOptions = SyncInsertOptions
@@ -386,23 +397,73 @@ instance FromJSON SyncProtocol where
386397
String "Cardano" -> pure SyncProtocolCardano
387398
x -> typeMismatch "Protocol" x
388399

400+
instance FromJSON SyncInsertPreset where
401+
parseJSON = Aeson.withText "SyncInsertPreset" $ \case
402+
"full" -> pure FullInsertPreset
403+
"only_utxo" -> pure OnlyUTxOInsertPreset
404+
"only_governance" -> pure OnlyGovInsertPreset
405+
"disable_all" -> pure DisableAllInsertPreset
406+
other -> fail $ "unexpected preset: " <> show other
407+
408+
instance ToJSON SyncInsertPreset where
409+
toJSON FullInsertPreset = "full"
410+
toJSON OnlyUTxOInsertPreset = "only_utxo"
411+
toJSON OnlyGovInsertPreset = "only_governance"
412+
toJSON DisableAllInsertPreset = "disable_all"
413+
389414
instance FromJSON SyncInsertConfig where
390415
parseJSON = Aeson.withObject "SyncInsertConfig" $ \obj -> do
391416
preset <- obj .:? "preset"
392-
case preset :: Maybe Text of
393-
Nothing -> SyncInsertConfig <$> parseJSON (Aeson.Object obj)
394-
Just "full" -> pure FullInsertOptions
395-
Just "only_utxo" -> pure OnlyUTxOInsertOptions
396-
Just "only_gov" -> pure OnlyGovInsertOptions
397-
Just "disable_all" -> pure DisableAllInsertOptions
398-
Just other -> fail $ "unexpected preset: " <> show other
417+
baseOptions <- case preset of
418+
Just FullInsertPreset -> pure fullInsertOptions
419+
Just OnlyUTxOInsertPreset -> pure onlyUTxOInsertOptions
420+
Just OnlyGovInsertPreset -> pure onlyGovInsertOptions
421+
Just DisableAllInsertPreset -> pure disableAllInsertOptions
422+
Nothing -> pure def -- Default options
423+
options <- parseOverrides obj baseOptions
424+
pure $ SyncInsertConfig preset options
425+
426+
parseOverrides :: Aeson.Object -> SyncInsertOptions -> Parser SyncInsertOptions
427+
parseOverrides obj baseOptions = do
428+
SyncInsertOptions
429+
<$> obj .:? "tx_cbor" .!= sioTxCBOR baseOptions
430+
<*> obj .:? "tx_out" .!= sioTxOut baseOptions
431+
<*> obj .:? "ledger" .!= sioLedger baseOptions
432+
<*> obj .:? "shelley" .!= sioShelley baseOptions
433+
<*> pure (sioRewards baseOptions)
434+
<*> obj .:? "multi_asset" .!= sioMultiAsset baseOptions
435+
<*> obj .:? "metadata" .!= sioMetadata baseOptions
436+
<*> obj .:? "plutus" .!= sioPlutus baseOptions
437+
<*> obj .:? "governance" .!= sioGovernance baseOptions
438+
<*> obj .:? "offchain_pool_data" .!= sioOffchainPoolData baseOptions
439+
<*> obj .:? "pool_stats" .!= sioPoolStats baseOptions
440+
<*> obj .:? "json_type" .!= sioJsonType baseOptions
441+
<*> obj .:? "remove_jsonb_from_schema" .!= sioRemoveJsonbFromSchema baseOptions
399442

400443
instance ToJSON SyncInsertConfig where
401-
toJSON (SyncInsertConfig opts) = toJSON opts
402-
toJSON FullInsertOptions = Aeson.object ["preset" .= ("full" :: Text)]
403-
toJSON OnlyUTxOInsertOptions = Aeson.object ["preset" .= ("only_utxo" :: Text)]
404-
toJSON OnlyGovInsertOptions = Aeson.object ["preset" .= ("only_gov" :: Text)]
405-
toJSON DisableAllInsertOptions = Aeson.object ["preset" .= ("disable_all" :: Text)]
444+
toJSON (SyncInsertConfig preset options) =
445+
Aeson.object $ maybe [] (\p -> [fromText "preset" .= p]) preset ++ optionsToList options
446+
447+
optionsToList :: SyncInsertOptions -> [Pair]
448+
optionsToList SyncInsertOptions {..} =
449+
catMaybes
450+
[ toJsonIfSet "tx_cbor" sioTxCBOR
451+
, toJsonIfSet "tx_out" sioTxOut
452+
, toJsonIfSet "ledger" sioLedger
453+
, toJsonIfSet "shelley" sioShelley
454+
, toJsonIfSet "rewards" sioRewards
455+
, toJsonIfSet "multi_asset" sioMultiAsset
456+
, toJsonIfSet "metadata" sioMetadata
457+
, toJsonIfSet "plutus" sioPlutus
458+
, toJsonIfSet "governance" sioGovernance
459+
, toJsonIfSet "offchain_pool_data" sioOffchainPoolData
460+
, toJsonIfSet "pool_stats" sioPoolStats
461+
, toJsonIfSet "json_type" sioJsonType
462+
, toJsonIfSet "remove_jsonb_from_schema" sioRemoveJsonbFromSchema
463+
]
464+
465+
toJsonIfSet :: ToJSON a => Text -> a -> Maybe Pair
466+
toJsonIfSet key value = Just $ fromText key .= value
406467

407468
instance FromJSON SyncInsertOptions where
408469
parseJSON = Aeson.withObject "SyncInsertOptions" $ \obj ->
@@ -433,11 +494,14 @@ instance ToJSON SyncInsertOptions where
433494
, "plutus" .= sioPlutus
434495
, "governance" .= sioGovernance
435496
, "offchain_pool_data" .= sioOffchainPoolData
436-
, "pool_stat" .= sioPoolStats
497+
, "pool_stats" .= sioPoolStats
437498
, "json_type" .= sioJsonType
438499
, "remove_jsonb_from_schema" .= sioRemoveJsonbFromSchema
439500
]
440501

502+
instance ToJSON RewardsConfig where
503+
toJSON (RewardsConfig enabled) = Aeson.Bool enabled
504+
441505
instance ToJSON TxCBORConfig where
442506
toJSON = boolToEnableDisable . isTxCBOREnabled
443507

@@ -626,7 +690,7 @@ instance FromJSON JsonTypeConfig where
626690
other -> fail $ "unexpected json_type: " <> show other
627691

628692
instance Default SyncInsertConfig where
629-
def = SyncInsertConfig def
693+
def = SyncInsertConfig Nothing def
630694

631695
instance Default SyncInsertOptions where
632696
def =
@@ -646,6 +710,68 @@ instance Default SyncInsertOptions where
646710
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
647711
}
648712

713+
fullInsertOptions :: SyncInsertOptions
714+
fullInsertOptions =
715+
SyncInsertOptions
716+
{ sioTxCBOR = TxCBORConfig False
717+
, sioTxOut = TxOutEnable
718+
, sioLedger = LedgerEnable
719+
, sioShelley = ShelleyEnable
720+
, sioRewards = RewardsConfig True
721+
, sioMultiAsset = MultiAssetEnable
722+
, sioMetadata = MetadataEnable
723+
, sioPlutus = PlutusEnable
724+
, sioGovernance = GovernanceConfig True
725+
, sioOffchainPoolData = OffchainPoolDataConfig True
726+
, sioPoolStats = PoolStatsConfig True
727+
, sioJsonType = JsonTypeText
728+
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
729+
}
730+
731+
onlyUTxOInsertOptions :: SyncInsertOptions
732+
onlyUTxOInsertOptions =
733+
SyncInsertOptions
734+
{ sioTxCBOR = TxCBORConfig False
735+
, sioTxOut = TxOutBootstrap (ForceTxIn False)
736+
, sioLedger = LedgerIgnore
737+
, sioShelley = ShelleyDisable
738+
, sioRewards = RewardsConfig True
739+
, sioMultiAsset = MultiAssetDisable
740+
, sioMetadata = MetadataDisable
741+
, sioPlutus = PlutusDisable
742+
, sioGovernance = GovernanceConfig False
743+
, sioOffchainPoolData = OffchainPoolDataConfig False
744+
, sioPoolStats = PoolStatsConfig False
745+
, sioJsonType = JsonTypeText
746+
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
747+
}
748+
749+
onlyGovInsertOptions :: SyncInsertOptions
750+
onlyGovInsertOptions =
751+
disableAllInsertOptions
752+
{ sioLedger = LedgerEnable
753+
, sioGovernance = GovernanceConfig True
754+
, sioPoolStats = PoolStatsConfig True
755+
}
756+
757+
disableAllInsertOptions :: SyncInsertOptions
758+
disableAllInsertOptions =
759+
SyncInsertOptions
760+
{ sioTxCBOR = TxCBORConfig False
761+
, sioTxOut = TxOutDisable
762+
, sioLedger = LedgerDisable
763+
, sioShelley = ShelleyDisable
764+
, sioRewards = RewardsConfig False
765+
, sioMultiAsset = MultiAssetDisable
766+
, sioMetadata = MetadataDisable
767+
, sioPlutus = PlutusDisable
768+
, sioOffchainPoolData = OffchainPoolDataConfig False
769+
, sioPoolStats = PoolStatsConfig False
770+
, sioGovernance = GovernanceConfig False
771+
, sioJsonType = JsonTypeText
772+
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
773+
}
774+
649775
boolToEnableDisable :: IsString s => Bool -> s
650776
boolToEnableDisable True = "enable"
651777
boolToEnableDisable False = "disable"

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

Lines changed: 22 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -22,46 +22,47 @@ prop_extractInsertOptions :: Property
2222
prop_extractInsertOptions = property $ do
2323
cfg <- forAll Gen.syncPreConfig
2424

25-
let insertOpts = pcInsertConfig cfg
26-
coverInsertCfg insertOpts
25+
let insertCfg = pcInsertConfig cfg
26+
coverInsertCfg insertCfg
2727

28-
case insertOpts of
29-
FullInsertOptions ->
28+
case insertCfg of
29+
SyncInsertConfig (Just FullInsertPreset) _ ->
3030
extractInsertOptions cfg === fullInsertOptions
31-
OnlyUTxOInsertOptions ->
31+
SyncInsertConfig (Just OnlyUTxOInsertPreset) _ ->
3232
extractInsertOptions cfg === onlyUTxOInsertOptions
33-
OnlyGovInsertOptions ->
33+
SyncInsertConfig (Just OnlyGovInsertPreset) _ ->
3434
extractInsertOptions cfg === onlyGovInsertOptions
35-
DisableAllInsertOptions ->
35+
SyncInsertConfig (Just DisableAllInsertPreset) _ ->
3636
extractInsertOptions cfg === disableAllInsertOptions
37-
SyncInsertConfig cfg' ->
38-
extractInsertOptions cfg === cfg'
37+
SyncInsertConfig Nothing opts ->
38+
extractInsertOptions cfg === opts
3939

4040
prop_extractInsertOptionsRewards :: Property
4141
prop_extractInsertOptionsRewards = property $ do
4242
cfg <- forAll Gen.syncPreConfig
4343

44-
let insertOpts = pcInsertConfig cfg
45-
coverInsertCfg insertOpts
44+
let insertCfg = pcInsertConfig cfg
45+
coverInsertCfg insertCfg
4646

4747
let areRewardsEnabled' = areRewardsEnabled $ sioRewards (extractInsertOptions cfg)
4848

49-
case insertOpts of
50-
OnlyGovInsertOptions ->
49+
case insertCfg of
50+
SyncInsertConfig (Just OnlyGovInsertPreset) _ ->
5151
assert $ not areRewardsEnabled'
52-
DisableAllInsertOptions ->
52+
SyncInsertConfig (Just DisableAllInsertPreset) _ ->
5353
assert $ not areRewardsEnabled'
54-
_ -> assert areRewardsEnabled'
54+
_other -> assert areRewardsEnabled'
5555

5656
coverInsertCfg :: MonadTest m => SyncInsertConfig -> m ()
5757
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)
58+
let preset = sicPreset insertOpts
59+
cover 5 "full" (preset == Just FullInsertPreset)
60+
cover 5 "only utxo" (preset == Just OnlyUTxOInsertPreset)
61+
cover 5 "only gov" (preset == Just OnlyGovInsertPreset)
62+
cover 5 "disable all" (preset == Just DisableAllInsertPreset)
6263
cover 5 "config" isSyncInsertConfig
6364
where
6465
isSyncInsertConfig =
6566
case insertOpts of
66-
SyncInsertConfig _ -> True
67-
_ -> False
67+
(SyncInsertConfig Nothing _) -> True
68+
_other -> False

0 commit comments

Comments
 (0)