@@ -55,6 +55,10 @@ module Cardano.DbSync.Config.Types (
55
55
isTxOutConsumed ,
56
56
isTxOutPrune ,
57
57
forceTxIn ,
58
+ fullInsertOptions ,
59
+ onlyUTxOInsertOptions ,
60
+ onlyGovInsertOptions ,
61
+ disableAllInsertOptions ,
58
62
) where
59
63
60
64
import qualified Cardano.BM.Configuration as Logging
@@ -68,10 +72,11 @@ import Cardano.Slotting.Slot (SlotNo (..))
68
72
import Control.Monad (fail )
69
73
import Data.Aeson (FromJSON (.. ), ToJSON (.. ), Value (.. ), (.!=) , (.:) , (.:?) , (.=) )
70
74
import qualified Data.Aeson as Aeson
71
- import Data.Aeson.Types (Parser , typeMismatch )
75
+ import Data.Aeson.Types (Parser , typeMismatch , Pair )
72
76
import Data.ByteString.Short (ShortByteString (), fromShort , toShort )
73
77
import Data.Default.Class (Default (.. ))
74
78
import Ouroboros.Consensus.Cardano.CanHardFork (TriggerHardFork (.. ))
79
+ import Data.Aeson.Key (fromText )
75
80
76
81
newtype LogFileDir = LogFileDir
77
82
{ unLogFileDir :: FilePath
@@ -150,14 +155,20 @@ data SyncPreConfig = SyncPreConfig
150
155
}
151
156
deriving (Show )
152
157
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
+ }
159
162
deriving (Eq , Show )
160
163
164
+ -- data SyncInsertConfig
165
+ -- = FullInsertOptions
166
+ -- | OnlyUTxOInsertOptions
167
+ -- | OnlyGovInsertOptions
168
+ -- | DisableAllInsertOptions
169
+ -- | SyncInsertConfig SyncInsertOptions
170
+ -- deriving (Eq, Show)
171
+
161
172
data SyncInsertOptions = SyncInsertOptions
162
173
{ sioTxCBOR :: TxCBORConfig
163
174
, sioTxOut :: TxOutConfig
@@ -389,20 +400,57 @@ instance FromJSON SyncProtocol where
389
400
instance FromJSON SyncInsertConfig where
390
401
parseJSON = Aeson. withObject " SyncInsertConfig" $ \ obj -> do
391
402
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
398
408
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
+
399
430
400
431
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
406
454
407
455
instance FromJSON SyncInsertOptions where
408
456
parseJSON = Aeson. withObject " SyncInsertOptions" $ \ obj ->
@@ -433,11 +481,14 @@ instance ToJSON SyncInsertOptions where
433
481
, " plutus" .= sioPlutus
434
482
, " governance" .= sioGovernance
435
483
, " offchain_pool_data" .= sioOffchainPoolData
436
- , " pool_stat " .= sioPoolStats
484
+ , " pool_stats " .= sioPoolStats
437
485
, " json_type" .= sioJsonType
438
486
, " remove_jsonb_from_schema" .= sioRemoveJsonbFromSchema
439
487
]
440
488
489
+ instance ToJSON RewardsConfig where
490
+ toJSON (RewardsConfig enabled) = Aeson. Bool enabled
491
+
441
492
instance ToJSON TxCBORConfig where
442
493
toJSON = boolToEnableDisable . isTxCBOREnabled
443
494
@@ -626,7 +677,7 @@ instance FromJSON JsonTypeConfig where
626
677
other -> fail $ " unexpected json_type: " <> show other
627
678
628
679
instance Default SyncInsertConfig where
629
- def = SyncInsertConfig def
680
+ def = SyncInsertConfig Nothing def
630
681
631
682
instance Default SyncInsertOptions where
632
683
def =
@@ -646,6 +697,67 @@ instance Default SyncInsertOptions where
646
697
, sioRemoveJsonbFromSchema = RemoveJsonbFromSchemaConfig False
647
698
}
648
699
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
+
649
761
boolToEnableDisable :: IsString s => Bool -> s
650
762
boolToEnableDisable True = " enable"
651
763
boolToEnableDisable False = " disable"
0 commit comments