Skip to content

Commit 10fe7a8

Browse files
committed
fix resolveGovActionProposal for stake addrs whitelist
1 parent 4e127ef commit 10fe7a8

File tree

3 files changed

+162
-137
lines changed

3 files changed

+162
-137
lines changed

cardano-db-sync/src/Cardano/DbSync/Era/Universal/Block.hs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ insertBlockUniversal syncEnv shouldLog withinTwoMins withinHalfHour blk details
142142
]
143143

144144
whenStrictJust (apNewEpoch applyResult) $ \newEpoch -> do
145-
insertOnNewEpoch tracer iopts blkId (Generic.blkSlotNo blk) epochNo newEpoch
145+
insertOnNewEpoch syncEnv blkId (Generic.blkSlotNo blk) epochNo newEpoch
146146

147147
insertStakeSlice syncEnv $ apStakeSlice applyResult
148148

cardano-db-sync/src/Cardano/DbSync/Era/Universal/Epoch.hs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,14 +62,12 @@ import Database.Persist.Sql (SqlBackend)
6262
--------------------------------------------------------------------------------------------
6363
insertOnNewEpoch ::
6464
(MonadBaseControl IO m, MonadIO m) =>
65-
Trace IO Text ->
66-
InsertOptions ->
6765
DB.BlockId ->
6866
SlotNo ->
6967
EpochNo ->
7068
Generic.NewEpoch ->
7169
ExceptT SyncNodeError (ReaderT SqlBackend m) ()
72-
insertOnNewEpoch tracer iopts blkId slotNo epochNo newEpoch = do
70+
insertOnNewEpoch syncEnv blkId slotNo epochNo newEpoch = do
7371
whenStrictJust (Generic.euProtoParams epochUpdate) $ \params ->
7472
lift $ insertEpochParam tracer blkId epochNo params (Generic.euNonce epochUpdate)
7573
whenStrictJust (Generic.neAdaPots newEpoch) $ \pots ->
@@ -84,7 +82,9 @@ insertOnNewEpoch tracer iopts blkId slotNo epochNo newEpoch = do
8482
insertUpdateEnacted tracer blkId epochNo enactedSt
8583
where
8684
epochUpdate :: Generic.EpochUpdate
87-
epochUpdate = Generic.neEpochUpdate newEpoch
85+
epochUpdate = Generic.neEpochUpdate newEpoc
86+
tracer = getTrace syncEnv
87+
iopts = getInsertOptions syncEnv
8888

8989
insertEpochParam ::
9090
(MonadBaseControl IO m, MonadIO m) =>

cardano-db-sync/src/Cardano/DbSync/Era/Universal/Insert/GovAction.hs

Lines changed: 157 additions & 132 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,10 @@ where
3131
import qualified Cardano.Crypto as Crypto
3232
import Cardano.Db (DbWord64 (..))
3333
import qualified Cardano.Db as DB
34-
import Cardano.DbSync.Api.Types (SyncEnv (..))
34+
import Cardano.DbSync.Api.Types (InsertOptions (..), SyncEnv (..), SyncOptions (..))
3535
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 (..))
3738
import qualified Cardano.DbSync.Era.Shelley.Generic as Generic
3839
import Cardano.DbSync.Era.Shelley.Generic.ParamProposal
3940
import Cardano.DbSync.Era.Universal.Insert.Other (toDouble)
@@ -77,42 +78,44 @@ insertGovActionProposal ::
7778
Maybe (StrictMaybe (Committee StandardConway)) ->
7879
(Word64, ProposalProcedure StandardConway) ->
7980
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 ()
116119
where
117120
cache = envCache syncEnv
118121
mprevGovAction :: Maybe (GovActionId StandardCrypto) = case pProcGovAction pp of
@@ -183,17 +186,26 @@ insertCommittee' mgapId mcommittee q = do
183186
--------------------------------------------------------------------------------------
184187
resolveGovActionProposal ::
185188
MonadIO m =>
189+
SyncEnv ->
186190
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
189193
gaTxId <-
190194
liftLookupFail "resolveGovActionProposal.queryTxId" $
191195
DB.queryTxId $
192196
Generic.unTxHash $
193197
gaidTxId gaId
194198
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
197209

198210
insertParamProposal ::
199211
(MonadBaseControl IO m, MonadIO m) =>
@@ -293,32 +305,35 @@ insertVotingProcedure ::
293305
(Word16, (GovActionId StandardCrypto, VotingProcedure StandardConway)) ->
294306
ExceptT SyncNodeError (ReaderT SqlBackend m) ()
295307
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+
}
322337

323338
insertVotingAnchor :: (MonadIO m, MonadBaseControl IO m) => DB.BlockId -> DB.AnchorType -> Anchor StandardCrypto -> ReaderT SqlBackend m DB.VotingAnchorId
324339
insertVotingAnchor blockId anchorType anchor =
@@ -428,23 +443,27 @@ updateDropped epochNo ratifiedActions = do
428443
insertUpdateEnacted ::
429444
forall m.
430445
(MonadBaseControl IO m, MonadIO m) =>
431-
Trace IO Text ->
446+
SyncEnv ->
432447
DB.BlockId ->
433448
EpochNo ->
434449
ConwayGovState StandardConway ->
435450
ExceptT SyncNodeError (ReaderT SqlBackend m) ()
436-
insertUpdateEnacted trce blkId epochNo enactedState = do
451+
insertUpdateEnacted syncEnv blkId epochNo enactedState = do
437452
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)
440457

441458
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)
444463

445-
(mcommitteeId, mnoConfidenceGaId) <- handleCommittee
464+
(mcommitteeId, mnoConfidenceGaId) <- handleCommittee syncEnv blkId
446465

447-
constitutionId <- handleConstitution
466+
constitutionId <- handleConstitution syncEnv blkId
448467

449468
void $
450469
lift $
@@ -458,67 +477,73 @@ insertUpdateEnacted trce blkId epochNo enactedState = do
458477
where
459478
govIds = govStatePrevGovActionIds enactedState
460479

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
463486
Nothing -> pure Nothing
464-
Just prevId -> do
465-
gaId <- resolveGovActionProposal $ unGovPurposeId prevId
487+
Just gaId -> do
466488
_nCommittee <- lift $ DB.updateGovActionEnacted gaId (unEpochNo epochNo)
467489
pure $ Just gaId
468490

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
503529
Nothing -> pure Nothing
504-
Just prevId -> do
505-
gaId <- resolveGovActionProposal $ unGovPurposeId prevId
530+
Just gaId -> do
506531
_nConstitution <- lift $ DB.updateGovActionEnacted gaId (unEpochNo epochNo)
507532
pure $ Just gaId
508533

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

Comments
 (0)