Skip to content

Commit

Permalink
feat: TX proposal procedures attribute
Browse files Browse the repository at this point in the history
This adds support for decoding and retrieving governance proposal
procedures from a TX

Fixes #344
  • Loading branch information
agaffney committed May 15, 2024
1 parent a82fa14 commit 8d794a8
Showing 1 changed file with 155 additions and 4 deletions.
159 changes: 155 additions & 4 deletions ledger/conway.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -176,6 +176,157 @@ 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 action
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()
}

// parameter_change_action = (0, gov_action_id / null, protocol_param_update, policy_hash / null)
type ParameterChangeGovAction struct {
cbor.StructAsArray
Type uint
ActionId *GovActionId
ParamUpdate BabbageProtocolParameterUpdate // TODO: use Conway params update type
PolicyHash []byte
}

func (a ParameterChangeGovAction) isGovAction() {}

// hard_fork_initiation_action = (1, gov_action_id / null, protocol_version)
type HardForkInitiationGovAction struct {
cbor.StructAsArray
Type uint
ActionId *GovActionId
ProtocolVersion struct {
cbor.StructAsArray
Major uint
Minor uint
}
}

func (a HardForkInitiationGovAction) isGovAction() {}

// treasury_withdrawals_action = (2, { reward_account => coin }, policy_hash / null)
type TreasuryWithdrawalGovAction struct {
cbor.StructAsArray
Type uint
Withdrawals map[*Address]uint64
PolicyHash []byte
}

func (a TreasuryWithdrawalGovAction) isGovAction() {}

// no_confidence = (3, gov_action_id / null)
type NoConfidenceGovAction struct {
cbor.StructAsArray
Type uint
ActionId *GovActionId
}

func (a NoConfidenceGovAction) isGovAction() {}

// update_committee = (4, gov_action_id / null, set<committee_cold_credential>, { committee_cold_credential => epoch }, unit_interval)
type UpdateCommitteeGovAction struct {
cbor.StructAsArray
Type uint
ActionId *GovActionId
Credentials []StakeCredential
CredEpochs map[*StakeCredential]uint
Unknown cbor.Rat
}

func (a UpdateCommitteeGovAction) isGovAction() {}

/*
new_constitution = (5, gov_action_id / null, constitution)
constitution =
[ anchor
, scripthash / null
]
*/
type NewConstitutionGovAction struct {
cbor.StructAsArray
Type uint
ActionId *GovActionId
Constitution struct {
cbor.StructAsArray
Anchor GovAnchor
ScriptHash []byte
}
}

func (a NewConstitutionGovAction) isGovAction() {}

// info_action = 6
type InfoGovAction struct {
cbor.StructAsArray
Type uint
}

func (a InfoGovAction) isGovAction() {}

type ConwayTransaction struct {
cbor.StructAsArray
cbor.DecodeStoreCbor
Expand Down

0 comments on commit 8d794a8

Please sign in to comment.