Skip to content

Commit d5e5b84

Browse files
committed
Compute the correct committee on proposal
1 parent dace671 commit d5e5b84

File tree

4 files changed

+34
-44
lines changed

4 files changed

+34
-44
lines changed

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

Lines changed: 14 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ import Cardano.DbSync.Era.Shelley.Generic.ParamProposal
3939
import Cardano.DbSync.Era.Universal.Insert.Other (toDouble)
4040
import Cardano.DbSync.Era.Util (liftLookupFail)
4141
import Cardano.DbSync.Error
42-
import Cardano.DbSync.Ledger.Types
42+
import Cardano.DbSync.Ledger.State
4343
import Cardano.DbSync.Util
4444
import Cardano.DbSync.Util.Bech32 (serialiseDrepToBech32)
4545
import Cardano.Ledger.BaseTypes
@@ -74,10 +74,10 @@ insertGovActionProposal ::
7474
DB.BlockId ->
7575
DB.TxId ->
7676
Maybe EpochNo ->
77-
Maybe (StrictMaybe (Committee StandardConway)) ->
78-
(Word64, ProposalProcedure StandardConway) ->
77+
Maybe (ConwayGovState StandardConway) ->
78+
(Word64, (GovActionId StandardCrypto, ProposalProcedure StandardConway)) ->
7979
ExceptT SyncNodeError (ReaderT SqlBackend m) ()
80-
insertGovActionProposal trce cache blkId txId govExpiresAt mmCommittee (index, pp) = do
80+
insertGovActionProposal trce cache blkId txId govExpiresAt mcgs (index, (govId, pp)) = do
8181
addrId <-
8282
lift $ queryOrInsertRewardAccount trce cache UpdateCache $ pProcReturnAddr pp
8383
votingAnchorId <- lift $ insertVotingAnchor blkId DB.GovActionAnchor $ pProcAnchor pp
@@ -110,7 +110,7 @@ insertGovActionProposal trce cache blkId txId govExpiresAt mmCommittee (index, p
110110
}
111111
case pProcGovAction pp of
112112
TreasuryWithdrawals mp _ -> lift $ mapM_ (insertTreasuryWithdrawal govActionProposalId) (Map.toList mp)
113-
UpdateCommittee _ removed added q -> lift $ insertNewCommittee (Just govActionProposalId) removed added q
113+
UpdateCommittee {} -> lift $ insertNewCommittee govActionProposalId
114114
NewConstitution _ constitution -> lift $ void $ insertConstitution blkId (Just govActionProposalId) constitution
115115
_ -> pure ()
116116
where
@@ -133,26 +133,22 @@ insertGovActionProposal trce cache blkId txId govExpiresAt mmCommittee (index, p
133133
}
134134

135135
insertNewCommittee ::
136-
Maybe DB.GovActionProposalId ->
137-
Set (Ledger.Credential 'ColdCommitteeRole StandardCrypto) ->
138-
Map (Ledger.Credential 'ColdCommitteeRole StandardCrypto) EpochNo ->
139-
UnitInterval ->
136+
DB.GovActionProposalId ->
140137
ReaderT SqlBackend m ()
141-
insertNewCommittee gapId removed added q = do
142-
void $ insertCommittee' gapId (updatedCommittee removed added q <$> mmCommittee) q
138+
insertNewCommittee govActionProposalId = do
139+
whenJust mcgs $ \cgs ->
140+
case findProposedCommittee govId cgs of
141+
Right (Just committee) -> void $ insertCommittee (Just govActionProposalId) committee
142+
other ->
143+
liftIO $ logWarning trce $ textShow other <> ": Failed to find committee for " <> textShow pp
143144

144145
insertCommittee :: (MonadIO m, MonadBaseControl IO m) => Maybe DB.GovActionProposalId -> Committee StandardConway -> ReaderT SqlBackend m DB.CommitteeId
145146
insertCommittee mgapId committee = do
146-
insertCommittee' mgapId (Just committee) (committeeThreshold committee)
147-
148-
insertCommittee' :: (MonadIO m, MonadBaseControl IO m) => Maybe DB.GovActionProposalId -> Maybe (Committee StandardConway) -> UnitInterval -> ReaderT SqlBackend m DB.CommitteeId
149-
insertCommittee' mgapId mcommittee q = do
150147
committeeId <- insertCommitteeDB
151-
whenJust mcommittee $ \committee ->
152-
mapM_ (insertNewMember committeeId) (Map.toList $ committeeMembers committee)
148+
mapM_ (insertNewMember committeeId) (Map.toList $ committeeMembers committee)
153149
pure committeeId
154150
where
155-
r = unboundRational q -- TODO work directly with Ratio Word64. This is not currently supported in ledger
151+
r = unboundRational $ committeeThreshold committee -- TODO work directly with Ratio Word64. This is not currently supported in ledger
156152
insertNewMember committeeId (cred, e) = do
157153
chId <- insertCommitteeHash cred
158154
void . DB.insertCommitteeMember $

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ insertTx syncEnv isMember blkId epochNo slotNo applyResult blockIndex tx grouped
184184
Generic.txExtraKeyWitnesses tx
185185

186186
when (ioGov iopts) $ do
187-
mapM_ (insertGovActionProposal tracer cache blkId txId (getGovExpiresAt applyResult epochNo) (apCommittee applyResult)) $ zip [0 ..] (snd <$> Generic.txProposalProcedure tx)
187+
mapM_ (insertGovActionProposal tracer cache blkId txId (getGovExpiresAt applyResult epochNo) (apGovActionState applyResult)) $ zip [0 ..] (Generic.txProposalProcedure tx)
188188
mapM_ (insertVotingProcedures tracer cache blkId txId) (Generic.txVotingProcedure tx)
189189

190190
let !txIns = map (prepareTxIn txId redeemers) resolvedInputs

cardano-db-sync/src/Cardano/DbSync/Ledger/State.hs

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -248,7 +248,7 @@ applyBlock env blk = do
248248
, apSlotDetails = details
249249
, apStakeSlice = getStakeSlice env newState False
250250
, apEvents = ledgerEvents
251-
, apCommittee = getCommittee newLedgerState
251+
, apGovActionState = getGovState newLedgerState
252252
, apDepositsMap = DepositsMap deposits
253253
}
254254
else defaultApplyResult details
@@ -277,7 +277,7 @@ applyBlock env blk = do
277277
, Generic.neAdaPots = maybeToStrict mPots
278278
, Generic.neEpochUpdate = Generic.epochUpdate newState
279279
, Generic.neDRepState = maybeToStrict $ getDrepState newState
280-
, Generic.neEnacted = maybeToStrict $ getEnacted newState
280+
, Generic.neEnacted = maybeToStrict $ getGovState newState
281281
}
282282

283283
applyToEpochBlockNo :: Bool -> Bool -> EpochBlockNo -> EpochBlockNo
@@ -294,18 +294,12 @@ applyBlock env blk = do
294294
finaliseDrepDistr ledger =
295295
ledger & newEpochStateT %~ forceDRepPulsingState @StandardConway
296296

297-
getEnacted :: ExtLedgerState CardanoBlock -> Maybe (ConwayGovState StandardConway)
298-
getEnacted ls = case ledgerState ls of
297+
getGovState :: ExtLedgerState CardanoBlock -> Maybe (ConwayGovState StandardConway)
298+
getGovState ls = case ledgerState ls of
299299
LedgerStateConway cls ->
300300
Just $ Consensus.shelleyLedgerState cls ^. Shelley.newEpochStateGovStateL
301301
_ -> Nothing
302302

303-
getCommittee :: ExtLedgerState CardanoBlock -> Maybe (StrictMaybe (Committee StandardConway))
304-
getCommittee ls = case ledgerState ls of
305-
LedgerStateConway cls ->
306-
Just $ Consensus.shelleyLedgerState cls ^. (Shelley.newEpochStateGovStateL . cgsCommitteeL)
307-
_ -> Nothing
308-
309303
getStakeSlice :: HasLedgerEnv -> CardanoLedgerState -> Bool -> Generic.StakeSliceRes
310304
getStakeSlice env cls isMigration =
311305
case clsEpochBlockNo cls of
@@ -860,32 +854,33 @@ findAdaPots = go
860854
go (_ : rest) = go rest
861855

862856
-- | Given an committee action id and the current GovState, return the proposed committee.
863-
-- If the id is not included in the proposals, return Nothing.
864-
findProposedCommittee :: GovActionId StandardCrypto -> ConwayGovState StandardConway -> Maybe (Committee StandardConway)
857+
-- If it's not a Committee action or is not included in the proposals, return Nothing.
858+
findProposedCommittee :: GovActionId StandardCrypto -> ConwayGovState StandardConway -> Either Text (Maybe (Committee StandardConway))
865859
findProposedCommittee gaId cgs = do
866860
(rootCommittee, updateList) <- findRoot gaId
867861
computeCommittee rootCommittee updateList
868862
where
869863
ps = cgsProposals cgs
870864
findRoot = findRootRecursively []
871865

872-
findRootRecursively :: [GovAction StandardConway] -> GovActionId StandardCrypto -> Maybe (StrictMaybe (Committee StandardConway), [GovAction StandardConway])
866+
findRootRecursively :: [GovAction StandardConway] -> GovActionId StandardCrypto -> Either Text (StrictMaybe (Committee StandardConway), [GovAction StandardConway])
873867
findRootRecursively acc gid = do
874-
gas <- proposalsLookupId gid ps
868+
gas <- fromNothing ("Didn't find proposal " <> textShow gid) $ proposalsLookupId gid ps
875869
let ga = pProcGovAction (gasProposalProcedure gas)
876870
case ga of
877-
NoConfidence _ -> Just (Ledger.SNothing, acc)
878-
UpdateCommittee Ledger.SNothing _ _ _ -> Just (cgsCommittee cgs, ga : acc)
871+
NoConfidence _ -> Right (Ledger.SNothing, acc)
872+
UpdateCommittee Ledger.SNothing _ _ _ -> Right (cgsCommittee cgs, ga : acc)
879873
UpdateCommittee gpid _ _ _
880874
| gpid == ps ^. pRootsL . grCommitteeL . prRootL ->
881-
Just (cgsCommittee cgs, ga : acc)
875+
Right (cgsCommittee cgs, ga : acc)
882876
UpdateCommittee (Ledger.SJust gpid) _ _ _ -> findRootRecursively (ga : acc) (unGovPurposeId gpid)
883-
_ -> Nothing
877+
_ -> Left "Found invalid gov action referenced by committee"
884878

885-
computeCommittee :: StrictMaybe (Committee StandardConway) -> [GovAction StandardConway] -> Maybe (Committee StandardConway)
879+
computeCommittee :: StrictMaybe (Committee StandardConway) -> [GovAction StandardConway] -> Either Text (Maybe (Committee StandardConway))
886880
computeCommittee sCommittee actions =
887-
Ledger.strictMaybeToMaybe $ foldl applyCommitteeUpdate sCommittee actions
881+
Ledger.strictMaybeToMaybe <$> foldM applyCommitteeUpdate sCommittee actions
888882

889883
applyCommitteeUpdate scommittee = \case
890-
UpdateCommittee _ toRemove toAdd q -> Ledger.SJust $ updatedCommittee toRemove toAdd q scommittee
891-
_ -> Ledger.SNothing -- Should never happen since the accumulator only gets UpdateCommittee
884+
UpdateCommittee _ toRemove toAdd q -> Right $ Ledger.SJust $ updatedCommittee toRemove toAdd q scommittee
885+
_ -> Left "Unexpected gov action." -- Should never happen since the accumulator only includes UpdateCommittee
886+
fromNothing err = maybe (Left err) Right

cardano-db-sync/src/Cardano/DbSync/Ledger/Types.hs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@ import Cardano.DbSync.Types (
2323
SlotDetails,
2424
)
2525
import Cardano.Ledger.Alonzo.Scripts (Prices)
26-
import Cardano.Ledger.BaseTypes (StrictMaybe)
2726
import qualified Cardano.Ledger.BaseTypes as Ledger
2827
import Cardano.Ledger.Coin (Coin)
2928
import Cardano.Ledger.Conway.Governance
@@ -143,7 +142,7 @@ data ApplyResult = ApplyResult
143142
, apSlotDetails :: !SlotDetails
144143
, apStakeSlice :: !Generic.StakeSliceRes
145144
, apEvents :: ![LedgerEvent]
146-
, apCommittee :: !(Maybe (StrictMaybe (Committee StandardConway)))
145+
, apGovActionState :: !(Maybe (ConwayGovState StandardConway))
147146
, apDepositsMap :: !DepositsMap
148147
}
149148

@@ -159,7 +158,7 @@ defaultApplyResult slotDetails =
159158
, apSlotDetails = slotDetails
160159
, apStakeSlice = Generic.NoSlices
161160
, apEvents = []
162-
, apCommittee = Nothing
161+
, apGovActionState = Nothing
163162
, apDepositsMap = emptyDepositsMap
164163
}
165164

0 commit comments

Comments
 (0)