@@ -22,6 +22,7 @@ module Cardano.DbSync.Config.Types (
22
22
SyncNodeConfig (.. ),
23
23
SyncPreConfig (.. ),
24
24
SyncInsertConfig (.. ),
25
+ SyncInsertPreset (.. ),
25
26
SyncInsertOptions (.. ),
26
27
TxCBORConfig (.. ),
27
28
PoolStatsConfig (.. ),
@@ -55,6 +56,10 @@ module Cardano.DbSync.Config.Types (
55
56
isTxOutConsumed ,
56
57
isTxOutPrune ,
57
58
forceTxIn ,
59
+ fullInsertOptions ,
60
+ onlyUTxOInsertOptions ,
61
+ onlyGovInsertOptions ,
62
+ disableAllInsertOptions ,
58
63
) where
59
64
60
65
import qualified Cardano.BM.Configuration as Logging
@@ -68,7 +73,8 @@ import Cardano.Slotting.Slot (SlotNo (..))
68
73
import Control.Monad (fail )
69
74
import Data.Aeson (FromJSON (.. ), ToJSON (.. ), Value (.. ), (.!=) , (.:) , (.:?) , (.=) )
70
75
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 )
72
78
import Data.ByteString.Short (ShortByteString (), fromShort , toShort )
73
79
import Data.Default.Class (Default (.. ))
74
80
import Ouroboros.Consensus.Cardano.CanHardFork (TriggerHardFork (.. ))
@@ -150,12 +156,17 @@ data SyncPreConfig = SyncPreConfig
150
156
}
151
157
deriving (Show )
152
158
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
159
170
deriving (Eq , Show )
160
171
161
172
data SyncInsertOptions = SyncInsertOptions
@@ -386,23 +397,73 @@ instance FromJSON SyncProtocol where
386
397
String " Cardano" -> pure SyncProtocolCardano
387
398
x -> typeMismatch " Protocol" x
388
399
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
+
389
414
instance FromJSON SyncInsertConfig where
390
415
parseJSON = Aeson. withObject " SyncInsertConfig" $ \ obj -> do
391
416
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
399
442
400
443
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
406
467
407
468
instance FromJSON SyncInsertOptions where
408
469
parseJSON = Aeson. withObject " SyncInsertOptions" $ \ obj ->
@@ -433,11 +494,14 @@ instance ToJSON SyncInsertOptions where
433
494
, " plutus" .= sioPlutus
434
495
, " governance" .= sioGovernance
435
496
, " offchain_pool_data" .= sioOffchainPoolData
436
- , " pool_stat " .= sioPoolStats
497
+ , " pool_stats " .= sioPoolStats
437
498
, " json_type" .= sioJsonType
438
499
, " remove_jsonb_from_schema" .= sioRemoveJsonbFromSchema
439
500
]
440
501
502
+ instance ToJSON RewardsConfig where
503
+ toJSON (RewardsConfig enabled) = Aeson. Bool enabled
504
+
441
505
instance ToJSON TxCBORConfig where
442
506
toJSON = boolToEnableDisable . isTxCBOREnabled
443
507
@@ -626,7 +690,7 @@ instance FromJSON JsonTypeConfig where
626
690
other -> fail $ " unexpected json_type: " <> show other
627
691
628
692
instance Default SyncInsertConfig where
629
- def = SyncInsertConfig def
693
+ def = SyncInsertConfig Nothing def
630
694
631
695
instance Default SyncInsertOptions where
632
696
def =
@@ -646,6 +710,68 @@ instance Default SyncInsertOptions where
646
710
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
647
711
}
648
712
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
+
649
775
boolToEnableDisable :: IsString s => Bool -> s
650
776
boolToEnableDisable True = " enable"
651
777
boolToEnableDisable False = " disable"
0 commit comments