31
31
import qualified Cardano.Crypto as Crypto
32
32
import Cardano.Db (DbWord64 (.. ))
33
33
import qualified Cardano.Db as DB
34
- import Cardano.DbSync.Api.Types (SyncEnv (.. ))
34
+ import Cardano.DbSync.Api.Types (InsertOptions ( .. ), SyncEnv ( .. ), SyncOptions (.. ))
35
35
import Cardano.DbSync.Cache (queryOrInsertRewardAccount , queryPoolKeyOrInsert )
36
- import Cardano.DbSync.Cache.Types (CacheAction (.. ), CacheStatus (.. ))
36
+ import Cardano.DbSync.Cache.Types (CacheStatus (.. ), CacheUpdateAction (.. ))
37
+ import Cardano.DbSync.Config.Types (ShelleyInsertConfig (.. ))
37
38
import qualified Cardano.DbSync.Era.Shelley.Generic as Generic
38
39
import Cardano.DbSync.Era.Shelley.Generic.ParamProposal
39
40
import Cardano.DbSync.Era.Universal.Insert.Other (toDouble )
@@ -77,42 +78,44 @@ insertGovActionProposal ::
77
78
Maybe (StrictMaybe (Committee StandardConway )) ->
78
79
(Word64 , ProposalProcedure StandardConway ) ->
79
80
ExceptT SyncNodeError (ReaderT SqlBackend m ) ()
80
- insertGovActionProposal trce cache blkId txId govExpiresAt mmCommittee (index, pp) = do
81
- addrId <-
82
- lift $ queryOrInsertRewardAccount trce cache UpdateCache $ pProcReturnAddr pp
83
- votingAnchorId <- lift $ insertVotingAnchor blkId DB. GovActionAnchor $ pProcAnchor pp
84
- mParamProposalId <- lift $
85
- case pProcGovAction pp of
86
- ParameterChange _ pparams _ ->
87
- Just <$> insertParamProposal blkId txId (convertConwayParamProposal pparams)
88
- _ -> pure Nothing
89
- prevGovActionDBId <- case mprevGovAction of
90
- Nothing -> pure Nothing
91
- Just prevGovActionId -> Just <$> resolveGovActionProposal prevGovActionId
92
- govActionProposalId <-
93
- lift $
94
- DB. insertGovActionProposal $
95
- DB. GovActionProposal
96
- { DB. govActionProposalTxId = txId
97
- , DB. govActionProposalIndex = index
98
- , DB. govActionProposalPrevGovActionProposal = prevGovActionDBId
99
- , DB. govActionProposalDeposit = Generic. coinToDbLovelace $ pProcDeposit pp
100
- , DB. govActionProposalReturnAddress = addrId
101
- , DB. govActionProposalExpiration = (\ epochNum -> unEpochNo epochNum + 1 ) <$> govExpiresAt
102
- , DB. govActionProposalVotingAnchorId = Just votingAnchorId
103
- , DB. govActionProposalType = Generic. toGovAction $ pProcGovAction pp
104
- , DB. govActionProposalDescription = Text. decodeUtf8 $ LBS. toStrict $ Aeson. encode (pProcGovAction pp)
105
- , DB. govActionProposalParamProposal = mParamProposalId
106
- , DB. govActionProposalRatifiedEpoch = Nothing
107
- , DB. govActionProposalEnactedEpoch = Nothing
108
- , DB. govActionProposalDroppedEpoch = Nothing
109
- , DB. govActionProposalExpiredEpoch = Nothing
110
- }
111
- case pProcGovAction pp of
112
- TreasuryWithdrawals mp _ -> lift $ mapM_ (insertTreasuryWithdrawal govActionProposalId) (Map. toList mp)
113
- UpdateCommittee _ removed added q -> lift $ insertNewCommittee (Just govActionProposalId) removed added q
114
- NewConstitution _ constitution -> lift $ void $ insertConstitution blkId (Just govActionProposalId) constitution
115
- _ -> pure ()
81
+ insertGovActionProposal syncEnv blkId txId govExpiresAt mmCommittee (index, pp) = do
82
+ mAddrId <- lift $ queryOrInsertRewardAccount syncEnv cache UpdateCache $ pProcReturnAddr pp
83
+ case mAddrId of
84
+ Nothing -> pure ()
85
+ Just addrId -> do
86
+ votingAnchorId <- lift $ insertVotingAnchor txId DB. GovActionAnchor $ pProcAnchor pp
87
+ mParamProposalId <- lift $
88
+ case pProcGovAction pp of
89
+ ParameterChange _ pparams _ ->
90
+ Just <$> insertParamProposal blkId txId (convertConwayParamProposal pparams)
91
+ _ -> pure Nothing
92
+ prevGovActionDBId <- case mprevGovAction of
93
+ Nothing -> pure Nothing
94
+ Just prevGovActionId -> resolveGovActionProposal syncEnv prevGovActionId
95
+ govActionProposalId <-
96
+ lift $
97
+ DB. insertGovActionProposal $
98
+ DB. GovActionProposal
99
+ { DB. govActionProposalTxId = txId
100
+ , DB. govActionProposalIndex = index
101
+ , DB. govActionProposalPrevGovActionProposal = prevGovActionDBId
102
+ , DB. govActionProposalDeposit = Generic. coinToDbLovelace $ pProcDeposit pp
103
+ , DB. govActionProposalReturnAddress = addrId
104
+ , DB. govActionProposalExpiration = unEpochNo <$> govExpiresAt
105
+ , DB. govActionProposalVotingAnchorId = Just votingAnchorId
106
+ , DB. govActionProposalType = Generic. toGovAction $ pProcGovAction pp
107
+ , DB. govActionProposalDescription = Text. decodeUtf8 $ LBS. toStrict $ Aeson. encode (pProcGovAction pp)
108
+ , DB. govActionProposalParamProposal = mParamProposalId
109
+ , DB. govActionProposalRatifiedEpoch = Nothing
110
+ , DB. govActionProposalEnactedEpoch = Nothing
111
+ , DB. govActionProposalDroppedEpoch = Nothing
112
+ , DB. govActionProposalExpiredEpoch = Nothing
113
+ }
114
+ case pProcGovAction pp of
115
+ TreasuryWithdrawals mp _ -> lift $ mapM_ (insertTreasuryWithdrawal govActionProposalId) (Map. toList mp)
116
+ UpdateCommittee _ removed added q -> lift $ insertNewCommittee govActionProposalId removed added q
117
+ NewConstitution _ constitution -> lift $ insertConstitution txId govActionProposalId constitution
118
+ _ -> pure ()
116
119
where
117
120
cache = envCache syncEnv
118
121
mprevGovAction :: Maybe (GovActionId StandardCrypto ) = case pProcGovAction pp of
@@ -183,17 +186,26 @@ insertCommittee' mgapId mcommittee q = do
183
186
--------------------------------------------------------------------------------------
184
187
resolveGovActionProposal ::
185
188
MonadIO m =>
189
+ SyncEnv ->
186
190
GovActionId StandardCrypto ->
187
- ExceptT SyncNodeError (ReaderT SqlBackend m ) DB. GovActionProposalId
188
- resolveGovActionProposal gaId = do
191
+ ExceptT SyncNodeError (ReaderT SqlBackend m ) ( Maybe DB. GovActionProposalId)
192
+ resolveGovActionProposal syncEnv gaId = do
189
193
gaTxId <-
190
194
liftLookupFail " resolveGovActionProposal.queryTxId" $
191
195
DB. queryTxId $
192
196
Generic. unTxHash $
193
197
gaidTxId gaId
194
198
let (GovActionIx index) = gaidGovActionIx gaId
195
- liftLookupFail " resolveGovActionProposal.queryGovActionProposalId" $
196
- DB. queryGovActionProposalId gaTxId (fromIntegral index) -- TODO: Use Word32?
199
+ case ioShelley insertOpts of
200
+ -- if we have whitelist for stake addresses then don't input the proposal
201
+ ShelleyStakeAddrs _ -> pure Nothing
202
+ _ -> do
203
+ result <-
204
+ liftLookupFail " resolveGovActionProposal.queryGovActionProposalId" $
205
+ DB. queryGovActionProposalId gaTxId (fromIntegral index) -- TODO: Use Word32?
206
+ pure $ Just result
207
+ where
208
+ insertOpts = soptInsertOptions $ envOptions syncEnv
197
209
198
210
insertParamProposal ::
199
211
(MonadBaseControl IO m , MonadIO m ) =>
@@ -293,32 +305,35 @@ insertVotingProcedure ::
293
305
(Word16 , (GovActionId StandardCrypto , VotingProcedure StandardConway )) ->
294
306
ExceptT SyncNodeError (ReaderT SqlBackend m ) ()
295
307
insertVotingProcedure syncEnv txId voter (index, (gaId, vp)) = do
296
- govActionId <- resolveGovActionProposal gaId
297
- votingAnchorId <- whenMaybe (strictMaybeToMaybe $ vProcAnchor vp) $ lift . insertVotingAnchor blkId DB. OtherAnchor
298
- (mCommitteeVoterId, mDRepVoter, mStakePoolVoter) <- case voter of
299
- CommitteeVoter cred -> do
300
- khId <- lift $ insertCommitteeHash cred
301
- pure (Just khId, Nothing , Nothing )
302
- DRepVoter cred -> do
303
- drep <- lift $ insertCredDrepHash cred
304
- pure (Nothing , Just drep, Nothing )
305
- StakePoolVoter poolkh -> do
306
- poolHashId <- lift $ queryPoolKeyOrInsert " insertVotingProcedure" syncEnv (envCache syncEnv) UpdateCache False poolkh
307
- pure (Nothing , Nothing , Just poolHashId)
308
- void
309
- . lift
310
- . DB. insertVotingProcedure
311
- $ DB. VotingProcedure
312
- { DB. votingProcedureTxId = txId
313
- , DB. votingProcedureIndex = index
314
- , DB. votingProcedureGovActionProposalId = govActionId
315
- , DB. votingProcedureCommitteeVoter = mCommitteeVoterId
316
- , DB. votingProcedureDrepVoter = mDRepVoter
317
- , DB. votingProcedurePoolVoter = mStakePoolVoter
318
- , DB. votingProcedureVoterRole = Generic. toVoterRole voter
319
- , DB. votingProcedureVote = Generic. toVote $ vProcVote vp
320
- , DB. votingProcedureVotingAnchorId = votingAnchorId
321
- }
308
+ maybeGovActionId <- resolveGovActionProposal syncEnv gaId
309
+ case maybeGovActionId of
310
+ Nothing -> pure ()
311
+ Just govActionId -> do
312
+ votingAnchorId <- whenMaybe (strictMaybeToMaybe $ vProcAnchor vp) $ lift . insertVotingAnchor txId DB. OtherAnchor
313
+ (mCommitteeVoterId, mDRepVoter, mStakePoolVoter) <- case voter of
314
+ CommitteeVoter cred -> do
315
+ khId <- lift $ insertCommitteeHash cred
316
+ pure (Just khId, Nothing , Nothing )
317
+ DRepVoter cred -> do
318
+ drep <- lift $ insertCredDrepHash cred
319
+ pure (Nothing , Just drep, Nothing )
320
+ StakePoolVoter poolkh -> do
321
+ poolHashId <- lift $ queryPoolKeyOrInsert " insertVotingProcedure" syncEnv (envCache syncEnv) UpdateCache False poolkh
322
+ pure (Nothing , Nothing , Just poolHashId)
323
+ void
324
+ . lift
325
+ . DB. insertVotingProcedure
326
+ $ DB. VotingProcedure
327
+ { DB. votingProcedureTxId = txId
328
+ , DB. votingProcedureIndex = index
329
+ , DB. votingProcedureGovActionProposalId = govActionId
330
+ , DB. votingProcedureCommitteeVoter = mCommitteeVoterId
331
+ , DB. votingProcedureDrepVoter = mDRepVoter
332
+ , DB. votingProcedurePoolVoter = mStakePoolVoter
333
+ , DB. votingProcedureVoterRole = Generic. toVoterRole voter
334
+ , DB. votingProcedureVote = Generic. toVote $ vProcVote vp
335
+ , DB. votingProcedureVotingAnchorId = votingAnchorId
336
+ }
322
337
323
338
insertVotingAnchor :: (MonadIO m , MonadBaseControl IO m ) => DB. BlockId -> DB. AnchorType -> Anchor StandardCrypto -> ReaderT SqlBackend m DB. VotingAnchorId
324
339
insertVotingAnchor blockId anchorType anchor =
@@ -428,23 +443,27 @@ updateDropped epochNo ratifiedActions = do
428
443
insertUpdateEnacted ::
429
444
forall m .
430
445
(MonadBaseControl IO m , MonadIO m ) =>
431
- Trace IO Text ->
446
+ SyncEnv ->
432
447
DB. BlockId ->
433
448
EpochNo ->
434
449
ConwayGovState StandardConway ->
435
450
ExceptT SyncNodeError (ReaderT SqlBackend m ) ()
436
- insertUpdateEnacted trce blkId epochNo enactedState = do
451
+ insertUpdateEnacted syncEnv blkId epochNo enactedState = do
437
452
whenJust (strictMaybeToMaybe (grPParamUpdate govIds)) $ \ prevId -> do
438
- gaId <- resolveGovActionProposal $ unGovPurposeId prevId
439
- void $ lift $ DB. updateGovActionEnacted gaId (unEpochNo epochNo)
453
+ mGaId <- resolveGovActionProposal syncEnv $ getPrevId prevId
454
+ case maybeGaId of
455
+ Nothing -> pure ()
456
+ Just gaId -> lift $ DB. updateGovActionEnacted gaId (unEpochNo epochNo)
440
457
441
458
whenJust (strictMaybeToMaybe (grHardFork govIds)) $ \ prevId -> do
442
- gaId <- resolveGovActionProposal $ unGovPurposeId prevId
443
- void $ lift $ DB. updateGovActionEnacted gaId (unEpochNo epochNo)
459
+ mGaId <- resolveGovActionProposal $ unGovPurposeId prevId
460
+ case mGaId of
461
+ Nothing -> pure ()
462
+ Just gaId -> void $ lift $ DB. updateGovActionEnacted gaId (unEpochNo epochNo)
444
463
445
- (mcommitteeId, mnoConfidenceGaId) <- handleCommittee
464
+ (mcommitteeId, mnoConfidenceGaId) <- handleCommittee syncEnv blkId
446
465
447
- constitutionId <- handleConstitution
466
+ constitutionId <- handleConstitution syncEnv blkId
448
467
449
468
void $
450
469
lift $
@@ -458,67 +477,73 @@ insertUpdateEnacted trce blkId epochNo enactedState = do
458
477
where
459
478
govIds = govStatePrevGovActionIds enactedState
460
479
461
- handleCommittee = do
462
- mCommitteeGaId <- case strictMaybeToMaybe (grCommittee govIds) of
480
+ handleCommittee syncEnv govIds blkId = do
481
+ mCommitteeGaId <- case strictMaybeToMaybe (grCommittee govIds) of
482
+ Nothing -> pure Nothing
483
+ Just prevId -> do
484
+ mGaId <- resolveGovActionProposal syncEnv $ unGovPurposeId prevId
485
+ case mGaId of
463
486
Nothing -> pure Nothing
464
- Just prevId -> do
465
- gaId <- resolveGovActionProposal $ unGovPurposeId prevId
487
+ Just gaId -> do
466
488
_nCommittee <- lift $ DB. updateGovActionEnacted gaId (unEpochNo epochNo)
467
489
pure $ Just gaId
468
490
469
- case (mCommitteeGaId, strictMaybeToMaybe (cgsCommittee enactedState)) of
470
- (Nothing , Nothing ) -> pure (Nothing , Nothing )
471
- (Nothing , Just committee) -> do
472
- -- No enacted proposal means we're after conway genesis territory
473
- committeeIds <- lift $ DB. queryProposalCommittee Nothing
474
- case committeeIds of
475
- [] -> do
476
- committeeId <- lift $ insertCommittee Nothing committee
477
- pure (Just committeeId, Nothing )
478
- (committeeId : _rest) ->
479
- pure (Just committeeId, Nothing )
480
- (Just committeeGaId, Nothing ) ->
481
- -- No committee with enacted action means it's a no confidence action.
482
- pure (Nothing , Just committeeGaId)
483
- (Just committeeGaId, Just committee) -> do
484
- committeeIds <- lift $ DB. queryProposalCommittee (Just committeeGaId)
485
- case committeeIds of
486
- [] -> do
487
- -- This should never happen. Having a committee and an enacted action, means
488
- -- the committee came from a proposal which should be returned from the query.
489
- liftIO $
490
- logWarning trce $
491
- mconcat
492
- [ " The impossible happened! Couldn't find the committee "
493
- , textShow committee
494
- , " which was enacted by a proposal "
495
- , textShow committeeGaId
496
- ]
497
- pure (Nothing , Nothing )
498
- (committeeId : _rest) ->
499
- pure (Just committeeId, Nothing )
500
-
501
- handleConstitution = do
502
- mConstitutionGaId <- case strictMaybeToMaybe (grConstitution govIds) of
491
+ case (mCommitteeGaId, strictMaybeToMaybe (cgsCommittee enactedState)) of
492
+ (Nothing , Nothing ) -> pure (Nothing , Nothing )
493
+ (Nothing , Just committee) -> do
494
+ -- No enacted proposal means we're after conway genesis territory
495
+ committeeIds <- lift $ DB. queryProposalCommittee Nothing
496
+ case committeeIds of
497
+ [] -> do
498
+ committeeId <- lift $ insertCommittee Nothing committee
499
+ pure (Just committeeId, Nothing )
500
+ (committeeId : _rest) ->
501
+ pure (Just committeeId, Nothing )
502
+ (Just committeeGaId, Nothing ) ->
503
+ -- No committee with enacted action means it's a no confidence action.
504
+ pure (Nothing , Just committeeGaId)
505
+ (Just committeeGaId, Just committee) -> do
506
+ committeeIds <- lift $ DB. queryProposalCommittee (Just committeeGaId)
507
+ case committeeIds of
508
+ [] -> do
509
+ -- This should never happen. Having a committee and an enacted action, means
510
+ -- the committee came from a proposal which should be returned from the query.
511
+ liftIO $
512
+ logWarning trce $
513
+ mconcat
514
+ [ " The impossible happened! Couldn't find the committee "
515
+ , textShow committee
516
+ , " which was enacted by a proposal "
517
+ , textShow committeeGaId
518
+ ]
519
+ pure (Nothing , Nothing )
520
+ (committeeId : _rest) ->
521
+ pure (Just committeeId, Nothing )
522
+
523
+ handleConstitution syncEnv govIds = do
524
+ mConstitutionGaId <- case strictMaybeToMaybe (grConstitution govIds) of
525
+ Nothing -> pure Nothing
526
+ Just prevId -> do
527
+ mGaId <- resolveGovActionProposal syncEnv $ unGovPurposeId prevId
528
+ case mGaId of
503
529
Nothing -> pure Nothing
504
- Just prevId -> do
505
- gaId <- resolveGovActionProposal $ unGovPurposeId prevId
530
+ Just gaId -> do
506
531
_nConstitution <- lift $ DB. updateGovActionEnacted gaId (unEpochNo epochNo)
507
532
pure $ Just gaId
508
533
509
- constitutionIds <- lift $ DB. queryProposalConstitution mConstitutionGaId
510
- case constitutionIds of
511
- -- The first case can only happen once on the first Conway epoch.
512
- -- On next epochs there will be at least one constitution, so the query will return something.
513
- [] -> lift $ insertConstitution blkId Nothing (cgsConstitution enactedState)
514
- constitutionId : rest -> do
515
- unless (null rest) $
516
- liftIO $
517
- logWarning trce $
518
- mconcat
519
- [ " Found multiple constitutions for proposal "
520
- , textShow mConstitutionGaId
521
- , " : "
522
- , textShow constitutionIds
523
- ]
524
- pure constitutionId
534
+ constitutionIds <- lift $ DB. queryProposalConstitution mConstitutionGaId
535
+ case constitutionIds of
536
+ -- The first case can only happen once on the first Conway epoch.
537
+ -- On next epochs there will be at least one constitution, so the query will return something.
538
+ [] -> lift $ insertConstitution blkId Nothing (cgsConstitution enactedState)
539
+ constitutionId : rest -> do
540
+ unless (null rest) $
541
+ liftIO $
542
+ logWarning trce $
543
+ mconcat
544
+ [ " Found multiple constitutions for proposal "
545
+ , textShow mConstitutionGaId
546
+ , " : "
547
+ , textShow constitutionIds
548
+ ]
549
+ pure constitutionId
0 commit comments