Skip to content

Commit 085bedf

Browse files
committed
Improve representation of Committee
Fixes #1633 Fixes #1637
1 parent 360e5c5 commit 085bedf

File tree

8 files changed

+100
-35
lines changed

8 files changed

+100
-35
lines changed

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

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -282,12 +282,13 @@ insertCommitteeRegistration ::
282282
ReaderT SqlBackend m ()
283283
insertCommitteeRegistration txId idx khCold cred = do
284284
khHotId <- insertCommitteeHash cred
285+
khColdId <- insertCommitteeHash khCold
285286
void
286287
. DB.insertCommitteeRegistration
287288
$ DB.CommitteeRegistration
288289
{ DB.committeeRegistrationTxId = txId
289290
, DB.committeeRegistrationCertIndex = idx
290-
, DB.committeeRegistrationColdKey = Generic.unCredentialHash khCold
291+
, DB.committeeRegistrationColdKeyId = khColdId
291292
, DB.committeeRegistrationHotKeyId = khHotId
292293
}
293294

@@ -300,12 +301,13 @@ insertCommitteeDeRegistration ::
300301
ReaderT SqlBackend m ()
301302
insertCommitteeDeRegistration txId idx khCold mAnchor = do
302303
votingAnchorId <- whenMaybe mAnchor $ insertVotingAnchor txId
304+
khColdId <- insertCommitteeHash khCold
303305
void
304306
. DB.insertCommitteeDeRegistration
305307
$ DB.CommitteeDeRegistration
306308
{ DB.committeeDeRegistrationTxId = txId
307309
, DB.committeeDeRegistrationCertIndex = idx
308-
, DB.committeeDeRegistrationColdKey = Generic.unCredentialHash khCold
310+
, DB.committeeDeRegistrationColdKeyId = khColdId
309311
, DB.committeeDeRegistrationVotingAnchorId = votingAnchorId
310312
}
311313

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

Lines changed: 34 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import Cardano.DbSync.Era.Shelley.Generic.ParamProposal
3535
import Cardano.DbSync.Era.Universal.Insert.Other (toDouble)
3636
import Cardano.DbSync.Era.Util (liftLookupFail)
3737
import Cardano.DbSync.Error
38+
import Cardano.DbSync.Ledger.Types
3839
import Cardano.DbSync.Util
3940
import Cardano.DbSync.Util.Bech32 (serialiseDrepToBech32)
4041
import Cardano.Ledger.BaseTypes
@@ -63,14 +64,16 @@ import Lens.Micro ((^.))
6364
import Ouroboros.Consensus.Cardano.Block (StandardConway, StandardCrypto)
6465

6566
insertGovActionProposal ::
67+
forall m.
6668
(MonadIO m, MonadBaseControl IO m) =>
6769
Cache ->
6870
DB.BlockId ->
6971
DB.TxId ->
7072
Maybe EpochNo ->
73+
Maybe (StrictMaybe (Committee StandardConway)) ->
7174
(Word64, ProposalProcedure StandardConway) ->
7275
ExceptT SyncNodeError (ReaderT SqlBackend m) ()
73-
insertGovActionProposal cache blkId txId govExpiresAt (index, pp) = do
76+
insertGovActionProposal cache blkId txId govExpiresAt mmCommittee (index, pp) = do
7477
addrId <-
7578
lift $ queryOrInsertRewardAccount cache CacheNew $ pProcReturnAddr pp
7679
votingAnchorId <- lift $ insertAnchor txId $ pProcAnchor pp
@@ -125,17 +128,39 @@ insertGovActionProposal cache blkId txId govExpiresAt (index, pp) = do
125128
, DB.treasuryWithdrawalAmount = Generic.coinToDbLovelace coin
126129
}
127130

128-
insertNewCommittee gaId removed added q = do
129-
void . DB.insertNewCommittee $
130-
DB.NewCommittee
131-
{ DB.newCommitteeGovActionProposalId = gaId
132-
, DB.newCommitteeQuorumNumerator = fromIntegral $ numerator r
133-
, DB.newCommitteeQuorumDenominator = fromIntegral $ denominator r
134-
, DB.newCommitteeDeletedMembers = textShow removed
135-
, DB.newCommitteeAddedMembers = textShow added
131+
insertNewCommittee ::
132+
DB.GovActionProposalId ->
133+
Set (Ledger.Credential 'ColdCommitteeRole StandardCrypto) ->
134+
Map (Ledger.Credential 'ColdCommitteeRole StandardCrypto) EpochNo ->
135+
UnitInterval ->
136+
ReaderT SqlBackend m ()
137+
insertNewCommittee gapId removed added q = do
138+
insertNewCommitteeInfo gapId q
139+
insertMembers gapId removed added q
140+
141+
insertNewCommitteeInfo gapId q =
142+
void . DB.insertNewCommitteeInfo $
143+
DB.NewCommitteeInfo
144+
{ DB.newCommitteeInfoGovActionProposalId = gapId
145+
, DB.newCommitteeInfoQuorumNumerator = fromIntegral $ numerator r
146+
, DB.newCommitteeInfoQuorumDenominator = fromIntegral $ denominator r
136147
}
137148
where
138149
r = unboundRational q -- TODO work directly with Ratio Word64. This is not currently supported in ledger
150+
insertMembers gapId removed added q = do
151+
whenJust mmCommittee $ \mCommittee -> do
152+
-- Nothing means we're not in Conway so it can't happen.
153+
let committee = updatedCommittee removed added q mCommittee
154+
mapM_ (insertNewMember gapId) (Map.toList $ committeeMembers committee)
155+
156+
insertNewMember gapId (cred, e) = do
157+
chId <- insertCommitteeHash cred
158+
void . DB.insertNewCommitteeMember $
159+
DB.NewCommitteeMember
160+
{ DB.newCommitteeMemberGovActionProposalId = gapId
161+
, DB.newCommitteeMemberCommitteeHashId = chId
162+
, DB.newCommitteeMemberExpirationEpoch = unEpochNo e
163+
}
139164

140165
--------------------------------------------------------------------------------------
141166
-- PROPOSAL

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ import Cardano.DbSync.Era.Universal.Insert.Other (
4141
import Cardano.DbSync.Era.Universal.Insert.Pool (IsPoolMember)
4242
import Cardano.DbSync.Era.Util (liftLookupFail, safeDecodeToJson)
4343
import Cardano.DbSync.Error
44-
import Cardano.DbSync.Ledger.Types (ApplyResult (..), getGovExpiresAt, lookupDepositsMap)
44+
import Cardano.DbSync.Ledger.Types (ApplyResult (..), getCommittee, getGovExpiresAt, lookupDepositsMap)
4545
import Cardano.DbSync.Util
4646
import Cardano.DbSync.Util.Cbor (serialiseTxMetadataToCbor)
4747
import Cardano.Ledger.BaseTypes
@@ -175,7 +175,7 @@ insertTx syncEnv isMember blkId epochNo slotNo applyResult blockIndex tx grouped
175175
Generic.txExtraKeyWitnesses tx
176176

177177
when (ioGov iopts) $ do
178-
mapM_ (insertGovActionProposal cache blkId txId (getGovExpiresAt applyResult epochNo)) $ zip [0 ..] (Generic.txProposalProcedure tx)
178+
mapM_ (insertGovActionProposal cache blkId txId (getGovExpiresAt applyResult epochNo) (getCommittee applyResult)) $ zip [0 ..] (Generic.txProposalProcedure tx)
179179
mapM_ (insertVotingProcedures tracer cache txId) (Generic.txVotingProcedure tx)
180180

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

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -244,6 +244,7 @@ applyBlock env blk = do
244244
, apSlotDetails = details
245245
, apStakeSlice = getStakeSlice env newState False
246246
, apEvents = ledgerEvents
247+
, apEnactState = getEnacted newLedgerState
247248
, apDepositsMap = DepositsMap deposits
248249
}
249250
else defaultApplyResult details

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

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,9 @@ import Cardano.DbSync.Types (
2424
import Cardano.Ledger.Alonzo.Scripts (Prices)
2525
import qualified Cardano.Ledger.BaseTypes as Ledger
2626
import Cardano.Ledger.Coin (Coin)
27+
import Cardano.Ledger.Conway.Governance
28+
import Cardano.Ledger.Credential (Credential (..))
29+
import Cardano.Ledger.Keys (KeyRole (..))
2730
import Cardano.Prelude hiding (atomically)
2831
import Cardano.Slotting.Slot (
2932
EpochNo (..),
@@ -37,7 +40,9 @@ import Control.Concurrent.STM.TBQueue (TBQueue)
3740
import qualified Data.Map.Strict as Map
3841
import qualified Data.Set as Set
3942
import qualified Data.Strict.Maybe as Strict
43+
import Lens.Micro
4044
import Ouroboros.Consensus.BlockchainTime.WallClock.Types (SystemStart (..))
45+
import Ouroboros.Consensus.Cardano.Block (StandardConway, StandardCrypto)
4146
import Ouroboros.Consensus.Ledger.Abstract (getTipSlot)
4247
import Ouroboros.Consensus.Ledger.Extended (ExtLedgerState (..))
4348
import qualified Ouroboros.Consensus.Node.ProtocolInfo as Consensus
@@ -131,6 +136,7 @@ data ApplyResult = ApplyResult
131136
, apSlotDetails :: !SlotDetails
132137
, apStakeSlice :: !Generic.StakeSliceRes
133138
, apEvents :: ![LedgerEvent]
139+
, apEnactState :: !(Maybe (EnactState StandardConway))
134140
, apDepositsMap :: !DepositsMap
135141
}
136142

@@ -145,6 +151,7 @@ defaultApplyResult slotDetails =
145151
, apSlotDetails = slotDetails
146152
, apStakeSlice = Generic.NoSlices
147153
, apEvents = []
154+
, apEnactState = Nothing
148155
, apDepositsMap = emptyDepositsMap
149156
}
150157

@@ -153,6 +160,30 @@ getGovExpiresAt applyResult e = case apGovExpiresAfter applyResult of
153160
Strict.Just ei -> Just $ Ledger.addEpochInterval e ei
154161
Strict.Nothing -> Nothing
155162

163+
getCommittee :: ApplyResult -> Maybe (Ledger.StrictMaybe (Committee StandardConway))
164+
getCommittee ar = case apEnactState ar of
165+
Nothing -> Nothing
166+
Just es -> Just $ es ^. ensCommitteeL
167+
168+
-- TODO reuse this function rom ledger after it's exported.
169+
updatedCommittee ::
170+
Set.Set (Credential 'ColdCommitteeRole StandardCrypto) ->
171+
Map.Map (Credential 'ColdCommitteeRole StandardCrypto) EpochNo ->
172+
Ledger.UnitInterval ->
173+
Ledger.StrictMaybe (Committee StandardConway) ->
174+
Committee StandardConway
175+
updatedCommittee membersToRemove membersToAdd newQuorum committee =
176+
case committee of
177+
Ledger.SNothing -> Committee membersToAdd newQuorum
178+
Ledger.SJust (Committee currentMembers _) ->
179+
let newCommitteeMembers =
180+
Map.union
181+
membersToAdd
182+
(currentMembers `Map.withoutKeys` membersToRemove)
183+
in Committee
184+
newCommitteeMembers
185+
newQuorum
186+
156187
newtype LedgerDB = LedgerDB
157188
{ ledgerDbCheckpoints :: AnchoredSeq (WithOrigin SlotNo) CardanoLedgerState CardanoLedgerState
158189
}

cardano-db/src/Cardano/Db/Delete.hs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,8 @@ deleteTablesAfterTxId mtxId mtxInId mtxOutId mmaTxOutId = do
147147
mgaId <- queryMinRefId GovActionProposalTxId txId
148148
whenJust mgaId $ \gaId -> do
149149
queryFirstAndDeleteAfter TreasuryWithdrawalGovActionProposalId gaId
150-
queryFirstAndDeleteAfter NewCommitteeGovActionProposalId gaId
150+
queryFirstAndDeleteAfter NewCommitteeInfoGovActionProposalId gaId
151+
queryFirstAndDeleteAfter NewCommitteeMemberGovActionProposalId gaId
151152
queryFirstAndDeleteAfter ConstitutionGovActionProposalId gaId
152153
deleteWhere [GovActionProposalId >=. gaId]
153154
mvaId <- queryMinRefId VotingAnchorTxId txId

cardano-db/src/Cardano/Db/Insert.hs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,8 @@ module Cardano.Db.Insert (
7171
insertConstitution,
7272
insertGovActionProposal,
7373
insertTreasuryWithdrawal,
74-
insertNewCommittee,
74+
insertNewCommitteeInfo,
75+
insertNewCommitteeMember,
7576
insertVotingProcedure,
7677
insertDrepHash,
7778
insertCommitteeHash,
@@ -396,8 +397,11 @@ insertGovActionProposal = insertUnchecked "GovActionProposal"
396397
insertTreasuryWithdrawal :: (MonadBaseControl IO m, MonadIO m) => TreasuryWithdrawal -> ReaderT SqlBackend m TreasuryWithdrawalId
397398
insertTreasuryWithdrawal = insertUnchecked "TreasuryWithdrawal"
398399

399-
insertNewCommittee :: (MonadBaseControl IO m, MonadIO m) => NewCommittee -> ReaderT SqlBackend m NewCommitteeId
400-
insertNewCommittee = insertUnchecked "NewCommittee"
400+
insertNewCommitteeInfo :: (MonadBaseControl IO m, MonadIO m) => NewCommitteeInfo -> ReaderT SqlBackend m NewCommitteeInfoId
401+
insertNewCommitteeInfo = insertUnchecked "NewCommitteeInfo"
402+
403+
insertNewCommitteeMember :: (MonadBaseControl IO m, MonadIO m) => NewCommitteeMember -> ReaderT SqlBackend m NewCommitteeMemberId
404+
insertNewCommitteeMember = insertUnchecked "NewCommitteeMember"
401405

402406
insertVotingProcedure :: (MonadBaseControl IO m, MonadIO m) => VotingProcedure -> ReaderT SqlBackend m VotingProcedureId
403407
insertVotingProcedure = insertUnchecked "VotingProcedure"

cardano-db/src/Cardano/Db/Schema.hs

Lines changed: 19 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -571,13 +571,13 @@ share
571571
CommitteeRegistration
572572
txId TxId noreference
573573
certIndex Word16
574-
coldKey ByteString sqltype=hash28type
574+
coldKeyId CommitteeHashId noreference
575575
hotKeyId CommitteeHashId noreference
576576

577577
CommitteeDeRegistration
578578
txId TxId noreference
579579
certIndex Word16
580-
coldKey ByteString sqltype=hash28type
580+
coldKeyId CommitteeHashId noreference
581581
votingAnchorId VotingAnchorId Maybe noreference
582582

583583
DrepRegistration
@@ -611,15 +611,18 @@ share
611611

612612
TreasuryWithdrawal
613613
govActionProposalId GovActionProposalId noreference
614-
stakeAddressId StakeAddressId noreference
615-
amount DbLovelace sqltype=lovelace
614+
stakeAddressId StakeAddressId noreference
615+
amount DbLovelace sqltype=lovelace
616+
617+
NewCommitteeInfo
618+
govActionProposalId GovActionProposalId noreference
619+
quorumNumerator Word64
620+
quorumDenominator Word64
616621

617-
NewCommittee
622+
NewCommitteeMember
618623
govActionProposalId GovActionProposalId noreference
619-
quorumNumerator Word64
620-
quorumDenominator Word64
621-
deletedMembers Text
622-
addedMembers Text
624+
committeeHashId CommitteeHashId noreference
625+
expirationEpoch Word64 sqltype=word31type
623626

624627
Constitution
625628
govActionProposalId GovActionProposalId noreference
@@ -1210,14 +1213,14 @@ schemaDocs =
12101213
"A table for every committee hot key registration. New in 13.2-Conway."
12111214
CommitteeRegistrationTxId # "The Tx table index of the tx that includes this certificate."
12121215
CommitteeRegistrationCertIndex # "The index of this registration within the certificates of this transaction."
1213-
CommitteeRegistrationColdKey # "The registered cold hey hash. TODO: should this reference DrepHashId or some separate hash table?"
1214-
CommitteeRegistrationHotKeyId # "The registered hot hey hash id"
1216+
CommitteeRegistrationColdKeyId # "The reference to the registered cold key hash id"
1217+
CommitteeRegistrationHotKeyId # "The reference to the registered hot key hash id"
12151218

12161219
CommitteeDeRegistration --^ do
12171220
"A table for every committee key de-registration. New in 13.2-Conway."
12181221
CommitteeDeRegistrationTxId # "The Tx table index of the tx that includes this certificate."
12191222
CommitteeDeRegistrationCertIndex # "The index of this deregistration within the certificates of this transaction."
1220-
CommitteeDeRegistrationColdKey # "The deregistered cold key hash"
1223+
CommitteeDeRegistrationColdKeyId # "The reference to the the deregistered cold key hash id"
12211224
CommitteeDeRegistrationVotingAnchorId # "The Voting anchor reference id"
12221225

12231226
DrepRegistration --^ do
@@ -1263,13 +1266,11 @@ schemaDocs =
12631266
TreasuryWithdrawalStakeAddressId # "The address that benefits from this withdrawal."
12641267
TreasuryWithdrawalAmount # "The amount for this withdrawl."
12651268

1266-
NewCommittee --^ do
1269+
NewCommitteeInfo --^ do
12671270
"A table for new committee proposed on a GovActionProposal. New in 13.2-Conway."
1268-
NewCommitteeGovActionProposalId # "The GovActionProposal table index for this new committee."
1269-
NewCommitteeQuorumNumerator # "The proposed quorum nominator."
1270-
NewCommitteeQuorumDenominator # "The proposed quorum denominator."
1271-
NewCommitteeDeletedMembers # "The removed members of the committee. This is now given in a text as a description, but may change. TODO: Conway."
1272-
NewCommitteeAddedMembers # "The new members of the committee. This is now given in a text as a description, but may change. TODO: Conway."
1271+
NewCommitteeInfoGovActionProposalId # "The GovActionProposal table index for this new committee."
1272+
NewCommitteeInfoQuorumNumerator # "The proposed quorum nominator."
1273+
NewCommitteeInfoQuorumDenominator # "The proposed quorum denominator."
12731274

12741275
Constitution --^ do
12751276
"A table for constitutiona attached to a GovActionProposal. New in 13.2-Conway."

0 commit comments

Comments
 (0)