Skip to content

Commit 9857461

Browse files
committed
1641 - config can overide presets
1 parent d21895f commit 9857461

File tree

5 files changed

+183
-133
lines changed

5 files changed

+183
-133
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: 132 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ module Cardano.DbSync.Config.Types (
5555
isTxOutConsumed,
5656
isTxOutPrune,
5757
forceTxIn,
58+
fullInsertOptions,
59+
onlyUTxOInsertOptions,
60+
onlyGovInsertOptions,
61+
disableAllInsertOptions,
5862
) where
5963

6064
import qualified Cardano.BM.Configuration as Logging
@@ -68,10 +72,11 @@ import Cardano.Slotting.Slot (SlotNo (..))
6872
import Control.Monad (fail)
6973
import Data.Aeson (FromJSON (..), ToJSON (..), Value (..), (.!=), (.:), (.:?), (.=))
7074
import qualified Data.Aeson as Aeson
71-
import Data.Aeson.Types (Parser, typeMismatch)
75+
import Data.Aeson.Types (Parser, typeMismatch, Pair)
7276
import Data.ByteString.Short (ShortByteString (), fromShort, toShort)
7377
import Data.Default.Class (Default (..))
7478
import Ouroboros.Consensus.Cardano.CanHardFork (TriggerHardFork (..))
79+
import Data.Aeson.Key (fromText)
7580

7681
newtype LogFileDir = LogFileDir
7782
{ unLogFileDir :: FilePath
@@ -150,14 +155,20 @@ data SyncPreConfig = SyncPreConfig
150155
}
151156
deriving (Show)
152157

153-
data SyncInsertConfig
154-
= FullInsertOptions
155-
| OnlyUTxOInsertOptions
156-
| OnlyGovInsertOptions
157-
| DisableAllInsertOptions
158-
| SyncInsertConfig SyncInsertOptions
158+
data SyncInsertConfig = SyncInsertConfig
159+
{ sicPreset :: Maybe Text
160+
, sicOptions :: SyncInsertOptions
161+
}
159162
deriving (Eq, Show)
160163

164+
-- data SyncInsertConfig
165+
-- = FullInsertOptions
166+
-- | OnlyUTxOInsertOptions
167+
-- | OnlyGovInsertOptions
168+
-- | DisableAllInsertOptions
169+
-- | SyncInsertConfig SyncInsertOptions
170+
-- deriving (Eq, Show)
171+
161172
data SyncInsertOptions = SyncInsertOptions
162173
{ sioTxCBOR :: TxCBORConfig
163174
, sioTxOut :: TxOutConfig
@@ -389,20 +400,57 @@ instance FromJSON SyncProtocol where
389400
instance FromJSON SyncInsertConfig where
390401
parseJSON = Aeson.withObject "SyncInsertConfig" $ \obj -> do
391402
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
403+
baseOptions <- case preset of
404+
Just "full" -> pure fullInsertOptions
405+
Just "only_utxo" -> pure onlyUTxOInsertOptions
406+
Just "only_gov" -> pure onlyGovInsertOptions
407+
Just "disable_all" -> pure disableAllInsertOptions
398408
Just other -> fail $ "unexpected preset: " <> show other
409+
Nothing -> pure def -- Default options
410+
options <- parseOverrides obj baseOptions
411+
pure $ SyncInsertConfig preset options
412+
413+
parseOverrides :: Aeson.Object -> SyncInsertOptions -> Parser SyncInsertOptions
414+
parseOverrides obj baseOptions = do
415+
SyncInsertOptions
416+
<$> obj .:? "tx_cbor" .!= sioTxCBOR baseOptions
417+
<*> obj .:? "tx_out" .!= sioTxOut baseOptions
418+
<*> obj .:? "ledger" .!= sioLedger baseOptions
419+
<*> obj .:? "shelley" .!= sioShelley baseOptions
420+
<*> pure (sioRewards baseOptions)
421+
<*> obj .:? "multi_asset" .!= sioMultiAsset baseOptions
422+
<*> obj .:? "metadata" .!= sioMetadata baseOptions
423+
<*> obj .:? "plutus" .!= sioPlutus baseOptions
424+
<*> obj .:? "governance" .!= sioGovernance baseOptions
425+
<*> obj .:? "offchain_pool_data" .!= sioOffchainPoolData baseOptions
426+
<*> obj .:? "pool_stats" .!= sioPoolStats baseOptions
427+
<*> obj .:? "json_type" .!= sioJsonType baseOptions
428+
<*> obj .:? "remove_jsonb_from_schema" .!= sioRemoveJsonbFromSchema baseOptions
429+
399430

400431
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)]
432+
toJSON (SyncInsertConfig preset options) =
433+
Aeson.object $ maybe [] (\p -> [fromText "preset" .= p]) preset ++ optionsToList options
434+
435+
optionsToList :: SyncInsertOptions -> [Pair]
436+
optionsToList SyncInsertOptions{..} = catMaybes
437+
[ toJsonIfSet "tx_cbor" sioTxCBOR
438+
, toJsonIfSet "tx_out" sioTxOut
439+
, toJsonIfSet "ledger" sioLedger
440+
, toJsonIfSet "shelley" sioShelley
441+
, toJsonIfSet "rewards" sioRewards
442+
, toJsonIfSet "multi_asset" sioMultiAsset
443+
, toJsonIfSet "metadata" sioMetadata
444+
, toJsonIfSet "plutus" sioPlutus
445+
, toJsonIfSet "governance" sioGovernance
446+
, toJsonIfSet "offchain_pool_data" sioOffchainPoolData
447+
, toJsonIfSet "pool_stats" sioPoolStats
448+
, toJsonIfSet "json_type" sioJsonType
449+
, toJsonIfSet "remove_jsonb_from_schema" sioRemoveJsonbFromSchema
450+
]
451+
452+
toJsonIfSet :: ToJSON a => Text -> a -> Maybe Pair
453+
toJsonIfSet key value = Just $ fromText key .= value
406454

407455
instance FromJSON SyncInsertOptions where
408456
parseJSON = Aeson.withObject "SyncInsertOptions" $ \obj ->
@@ -433,11 +481,14 @@ instance ToJSON SyncInsertOptions where
433481
, "plutus" .= sioPlutus
434482
, "governance" .= sioGovernance
435483
, "offchain_pool_data" .= sioOffchainPoolData
436-
, "pool_stat" .= sioPoolStats
484+
, "pool_stats" .= sioPoolStats
437485
, "json_type" .= sioJsonType
438486
, "remove_jsonb_from_schema" .= sioRemoveJsonbFromSchema
439487
]
440488

489+
instance ToJSON RewardsConfig where
490+
toJSON (RewardsConfig enabled) = Aeson.Bool enabled
491+
441492
instance ToJSON TxCBORConfig where
442493
toJSON = boolToEnableDisable . isTxCBOREnabled
443494

@@ -626,7 +677,7 @@ instance FromJSON JsonTypeConfig where
626677
other -> fail $ "unexpected json_type: " <> show other
627678

628679
instance Default SyncInsertConfig where
629-
def = SyncInsertConfig def
680+
def = SyncInsertConfig Nothing def
630681

631682
instance Default SyncInsertOptions where
632683
def =
@@ -646,6 +697,67 @@ instance Default SyncInsertOptions where
646697
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
647698
}
648699

700+
fullInsertOptions :: SyncInsertOptions
701+
fullInsertOptions =
702+
SyncInsertOptions
703+
{ sioTxCBOR = TxCBORConfig False
704+
, sioTxOut = TxOutEnable
705+
, sioLedger = LedgerEnable
706+
, sioShelley = ShelleyEnable
707+
, sioRewards = RewardsConfig True
708+
, sioMultiAsset = MultiAssetEnable
709+
, sioMetadata = MetadataEnable
710+
, sioPlutus = PlutusEnable
711+
, sioGovernance = GovernanceConfig True
712+
, sioOffchainPoolData = OffchainPoolDataConfig True
713+
, sioPoolStats = PoolStatsConfig True
714+
, sioJsonType = JsonTypeText
715+
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
716+
}
717+
718+
onlyUTxOInsertOptions :: SyncInsertOptions
719+
onlyUTxOInsertOptions =
720+
SyncInsertOptions
721+
{ sioTxCBOR = TxCBORConfig False
722+
, sioTxOut = TxOutBootstrap (ForceTxIn False)
723+
, sioLedger = LedgerIgnore
724+
, sioShelley = ShelleyDisable
725+
, sioRewards = RewardsConfig True
726+
, sioMultiAsset = MultiAssetDisable
727+
, sioMetadata = MetadataDisable
728+
, sioPlutus = PlutusDisable
729+
, sioGovernance = GovernanceConfig False
730+
, sioOffchainPoolData = OffchainPoolDataConfig False
731+
, sioPoolStats = PoolStatsConfig False
732+
, sioJsonType = JsonTypeText
733+
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
734+
}
735+
736+
onlyGovInsertOptions :: SyncInsertOptions
737+
onlyGovInsertOptions =
738+
disableAllInsertOptions
739+
{ sioLedger = LedgerEnable
740+
, sioGovernance = GovernanceConfig True
741+
}
742+
743+
disableAllInsertOptions :: SyncInsertOptions
744+
disableAllInsertOptions =
745+
SyncInsertOptions
746+
{ sioTxCBOR = TxCBORConfig False
747+
, sioTxOut = TxOutDisable
748+
, sioLedger = LedgerDisable
749+
, sioShelley = ShelleyDisable
750+
, sioRewards = RewardsConfig False
751+
, sioMultiAsset = MultiAssetDisable
752+
, sioMetadata = MetadataDisable
753+
, sioPlutus = PlutusDisable
754+
, sioOffchainPoolData = OffchainPoolDataConfig False
755+
, sioPoolStats = PoolStatsConfig False
756+
, sioGovernance = GovernanceConfig False
757+
, sioJsonType = JsonTypeText
758+
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
759+
}
760+
649761
boolToEnableDisable :: IsString s => Bool -> s
650762
boolToEnableDisable True = "enable"
651763
boolToEnableDisable False = "disable"

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

Lines changed: 31 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import Cardano.DbSync.Api
77
import Cardano.DbSync.Config.Types
88
import qualified Cardano.DbSync.Gen as Gen
99
import Cardano.Prelude
10+
import Control.Monad (MonadFail (..))
1011
import Hedgehog
1112

1213
tests :: IO Bool
@@ -22,46 +23,50 @@ prop_extractInsertOptions :: Property
2223
prop_extractInsertOptions = property $ do
2324
cfg <- forAll Gen.syncPreConfig
2425

25-
let insertOpts = pcInsertConfig cfg
26-
coverInsertCfg insertOpts
26+
let insertCfg = pcInsertConfig cfg
27+
coverInsertCfg insertCfg
2728

28-
case insertOpts of
29-
FullInsertOptions ->
29+
case insertCfg of
30+
SyncInsertConfig (Just "full") _ ->
3031
extractInsertOptions cfg === fullInsertOptions
31-
OnlyUTxOInsertOptions ->
32+
SyncInsertConfig (Just "only_utxo") _ ->
3233
extractInsertOptions cfg === onlyUTxOInsertOptions
33-
OnlyGovInsertOptions ->
34+
SyncInsertConfig (Just "only_gov") _ ->
3435
extractInsertOptions cfg === onlyGovInsertOptions
35-
DisableAllInsertOptions ->
36+
SyncInsertConfig (Just "disable_all") _ ->
3637
extractInsertOptions cfg === disableAllInsertOptions
37-
SyncInsertConfig cfg' ->
38-
extractInsertOptions cfg === cfg'
38+
SyncInsertConfig Nothing opts ->
39+
extractInsertOptions cfg === opts
40+
_other -> fail "Unexpected SyncInsertConfig" -- This case should not happen if all presets are covered
3941

4042
prop_extractInsertOptionsRewards :: Property
4143
prop_extractInsertOptionsRewards = property $ do
4244
cfg <- forAll Gen.syncPreConfig
4345

44-
let insertOpts = pcInsertConfig cfg
45-
coverInsertCfg insertOpts
46+
let insertCfg = pcInsertConfig cfg
47+
coverInsertCfg insertCfg
4648

4749
let areRewardsEnabled' = areRewardsEnabled $ sioRewards (extractInsertOptions cfg)
4850

49-
case insertOpts of
50-
OnlyGovInsertOptions ->
51+
case insertCfg of
52+
SyncInsertConfig (Just "only_gov") _ ->
5153
assert $ not areRewardsEnabled'
52-
DisableAllInsertOptions ->
54+
SyncInsertConfig (Just "disable_all") _ ->
5355
assert $ not areRewardsEnabled'
54-
_ -> assert areRewardsEnabled'
56+
_other -> assert areRewardsEnabled'
5557

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
58+
coverInsertCfg :: SyncInsertConfig -> PropertyT IO ()
59+
coverInsertCfg cfg = do
60+
cover 5 "full" $ isPreset "full" cfg
61+
cover 5 "only utxo" $ isPreset "only_utxo" cfg
62+
cover 5 "only gov" $ isPreset "only_gov" cfg
63+
cover 5 "disable all" $ isPreset "disable_all" cfg
64+
cover 5 "custom config" $ isCustomConfig cfg
6365
where
64-
isSyncInsertConfig =
65-
case insertOpts of
66-
SyncInsertConfig _ -> True
67-
_ -> False
66+
isPreset :: Text -> SyncInsertConfig -> Bool
67+
isPreset preset (SyncInsertConfig (Just p) _) = p == preset
68+
isPreset _ _ = False
69+
70+
isCustomConfig :: SyncInsertConfig -> Bool
71+
isCustomConfig (SyncInsertConfig Nothing _) = True
72+
isCustomConfig _ = False

0 commit comments

Comments
 (0)