From 296a6b119b879a83d1f191bbd45caa9c661dd5ce Mon Sep 17 00:00:00 2001 From: Aurora Gaffney Date: Wed, 15 May 2024 14:51:16 -0500 Subject: [PATCH] feat: TX proposal procedures attribute This adds support for decoding and retrieving governance proposal procedures from a TX Fixes #344 --- ledger/conway.go | 152 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 148 insertions(+), 4 deletions(-) diff --git a/ledger/conway.go b/ledger/conway.go index b6a71e41..5a1c1086 100644 --- a/ledger/conway.go +++ b/ledger/conway.go @@ -121,10 +121,10 @@ func (h *ConwayBlockHeader) Era() Era { type ConwayTransactionBody struct { BabbageTransactionBody - TxVotingProcedures VotingProcedures `cbor:"19,keyasint,omitempty"` - ProposalProcedures *cbor.Value `cbor:"20,keyasint,omitempty"` - CurrentTreasuryValue int64 `cbor:"21,keyasint,omitempty"` - Donation uint64 `cbor:"22,keyasint,omitempty"` + TxVotingProcedures VotingProcedures `cbor:"19,keyasint,omitempty"` + TxProposalProcedures []ProposalProcedure `cbor:"20,keyasint,omitempty"` + CurrentTreasuryValue int64 `cbor:"21,keyasint,omitempty"` + Donation uint64 `cbor:"22,keyasint,omitempty"` } func (b *ConwayTransactionBody) UnmarshalCBOR(cborData []byte) error { @@ -176,6 +176,150 @@ type GovActionId struct { GovActionIdx uint32 } +type ProposalProcedure struct { + cbor.StructAsArray + Deposit uint64 + RewardAccount Address + GovAction GovActionWrapper + Anchor GovAnchor +} + +const ( + GovActionTypeParameterChange = 0 + GovActionTypeHardForkInitiation = 1 + GovActionTypeTreasuryWithdrawal = 2 + GovActionTypeNoConfidence = 3 + GovActionTypeUpdateCommittee = 4 + GovActionTypeNewConstitution = 5 + GovActionTypeInfo = 6 +) + +type GovActionWrapper struct { + Type uint + Action GovAction +} + +func (g *GovActionWrapper) UnmarshalCBOR(data []byte) error { + // Determine action type + actionType, err := cbor.DecodeIdFromList(data) + if err != nil { + return err + } + var tmpAction GovAction + switch actionType { + case GovActionTypeParameterChange: + tmpAction = &ParameterChangeGovAction{} + case GovActionTypeHardForkInitiation: + tmpAction = &HardForkInitiationGovAction{} + case GovActionTypeTreasuryWithdrawal: + tmpAction = &TreasuryWithdrawalGovAction{} + case GovActionTypeNoConfidence: + tmpAction = &NoConfidenceGovAction{} + case GovActionTypeUpdateCommittee: + tmpAction = &UpdateCommitteeGovAction{} + case GovActionTypeNewConstitution: + tmpAction = &NewConstitutionGovAction{} + case GovActionTypeInfo: + tmpAction = &InfoGovAction{} + default: + return fmt.Errorf("unknown governance action type: %d", actionType) + } + // Decode cert + if _, err := cbor.Decode(data, tmpAction); err != nil { + return err + } + g.Type = uint(actionType) + g.Action = tmpAction + return nil +} + +func (g *GovActionWrapper) MarshalCBOR() ([]byte, error) { + return cbor.Encode(g.Action) +} + +type GovAction interface { + isGovAction() + Cbor() []byte + Utxorpc() *utxorpc.Certificate +} + +/* +proposal_procedures = nonempty_oset + +gov_action = + [ parameter_change_action + // hard_fork_initiation_action + // treasury_withdrawals_action + // no_confidence + // update_committee + // new_constitution + // info_action + ] + +policy_hash = scripthash + +parameter_change_action = (0, gov_action_id / null, protocol_param_update, policy_hash / null) + +hard_fork_initiation_action = (1, gov_action_id / null, protocol_version) + +treasury_withdrawals_action = (2, { reward_account => coin }, policy_hash / null) + +no_confidence = (3, gov_action_id / null) + +update_committee = (4, gov_action_id / null, set, { committee_cold_credential => epoch }, unit_interval) + +new_constitution = (5, gov_action_id / null, constitution) + +constitution = + [ anchor + , scripthash / null + ] + +info_action = 6 +*/ + +type ParameterChangeGovAction struct { + // TODO +} + +func (a ParameterChangeGovAction) isGovAction() {} + +type HardForkInitiationGovAction struct { + // TODO +} + +func (a HardForkInitiationGovAction) isGovAction() {} + +type TreasuryWithdrawalGovAction struct { + // TODO +} + +func (a TreasuryWithdrawalGovAction) isGovAction() {} + +type NoConfidenceGovAction struct { + // TODO +} + +func (a NoConfidenceGovAction) isGovAction() {} + +type UpdateCommitteeGovAction struct { + // TODO +} + +func (a UpdateCommitteeGovAction) isGovAction() {} + +type NewConstitutionGovAction struct { + // TODO +} + +func (a NewConstitutionGovAction) isGovAction() {} + +type InfoGovAction struct { + // TODO +} + +func (a InfoGovAction) isGovAction() {} + type ConwayTransaction struct { cbor.StructAsArray cbor.DecodeStoreCbor