From 9371673d61f04c8d160e0facaa142cee9319b66a Mon Sep 17 00:00:00 2001 From: Aurora Gaffney Date: Sat, 24 Aug 2024 16:46:14 -0500 Subject: [PATCH] chore: move certs, credentials, and gov types to sub-package This also changes the way that we decode parameter change governance actions. The param updates are captured as raw CBOR to allow for decoding with per-era logic. --- ledger/{ => common}/certs.go | 2 +- ledger/{ => common}/credentials.go | 2 +- ledger/common/gov.go | 198 +++++++++++++++++++++++++++++ ledger/compat.go | 15 +++ ledger/conway.go | 177 -------------------------- 5 files changed, 215 insertions(+), 179 deletions(-) rename ledger/{ => common}/certs.go (99%) rename ledger/{ => common}/credentials.go (99%) create mode 100644 ledger/common/gov.go diff --git a/ledger/certs.go b/ledger/common/certs.go similarity index 99% rename from ledger/certs.go rename to ledger/common/certs.go index bc0c7d31..1a6f9107 100644 --- a/ledger/certs.go +++ b/ledger/common/certs.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package ledger +package common import ( "fmt" diff --git a/ledger/credentials.go b/ledger/common/credentials.go similarity index 99% rename from ledger/credentials.go rename to ledger/common/credentials.go index 3dc172ad..2d94f6c4 100644 --- a/ledger/credentials.go +++ b/ledger/common/credentials.go @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -package ledger +package common import ( "fmt" diff --git a/ledger/common/gov.go b/ledger/common/gov.go new file mode 100644 index 00000000..fb75eeda --- /dev/null +++ b/ledger/common/gov.go @@ -0,0 +1,198 @@ +// Copyright 2024 Blink Labs Software +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package common + +import ( + "fmt" + + "github.com/blinklabs-io/gouroboros/cbor" +) + +// VotingProcedures is a convenience type to avoid needing to duplicate the full type definition everywhere +type VotingProcedures map[*Voter]map[*GovActionId]VotingProcedure + +const ( + VoterTypeConstitutionalCommitteeHotKeyHash uint8 = 0 + VoterTypeConstitutionalCommitteeHotScriptHash uint8 = 1 + VoterTypeDRepKeyHash uint8 = 2 + VoterTypeDRepScriptHash uint8 = 3 + VoterTypeStakingPoolKeyHash uint8 = 4 +) + +type Voter struct { + cbor.StructAsArray + Type uint8 + Hash [28]byte +} + +const ( + GovVoteNo uint8 = 0 + GovVoteYes uint8 = 1 + GovVoteAbstain uint8 = 2 +) + +type VotingProcedure struct { + cbor.StructAsArray + Vote uint8 + Anchor *GovAnchor +} + +type GovAnchor struct { + cbor.StructAsArray + Url string + DataHash [32]byte +} + +type GovActionId struct { + cbor.StructAsArray + TransactionId [32]byte + 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() +} + +type ParameterChangeGovAction struct { + cbor.StructAsArray + Type uint + ActionId *GovActionId + ParamUpdate cbor.RawMessage // NOTE: we use raw to defer processing to account for per-era types + PolicyHash []byte +} + +func (a ParameterChangeGovAction) isGovAction() {} + +type HardForkInitiationGovAction struct { + cbor.StructAsArray + Type uint + ActionId *GovActionId + ProtocolVersion struct { + cbor.StructAsArray + Major uint + Minor uint + } +} + +func (a HardForkInitiationGovAction) isGovAction() {} + +type TreasuryWithdrawalGovAction struct { + cbor.StructAsArray + Type uint + Withdrawals map[*Address]uint64 + PolicyHash []byte +} + +func (a TreasuryWithdrawalGovAction) isGovAction() {} + +type NoConfidenceGovAction struct { + cbor.StructAsArray + Type uint + ActionId *GovActionId +} + +func (a NoConfidenceGovAction) isGovAction() {} + +type UpdateCommitteeGovAction struct { + cbor.StructAsArray + Type uint + ActionId *GovActionId + Credentials []StakeCredential + CredEpochs map[*StakeCredential]uint + Unknown cbor.Rat +} + +func (a UpdateCommitteeGovAction) isGovAction() {} + +type NewConstitutionGovAction struct { + cbor.StructAsArray + Type uint + ActionId *GovActionId + Constitution struct { + cbor.StructAsArray + Anchor GovAnchor + ScriptHash []byte + } +} + +func (a NewConstitutionGovAction) isGovAction() {} + +type InfoGovAction struct { + cbor.StructAsArray + Type uint +} + +func (a InfoGovAction) isGovAction() {} diff --git a/ledger/compat.go b/ledger/compat.go index bfdc5963..dd509c59 100644 --- a/ledger/compat.go +++ b/ledger/compat.go @@ -41,14 +41,29 @@ func NewAddress(addr string) (Address, error) { return common.NewAddress(addr) } +// Governance types +type VotingProcedure = common.VotingProcedure +type VotingProcedures = common.VotingProcedures +type ProposalProcedure = common.ProposalProcedure + +// Certificates +type Certificate = common.Certificate +type CertificateWrapper = common.CertificateWrapper +type PoolRetirementCertificate = common.PoolRetirementCertificate +type PoolRegistrationCertificate = common.PoolRegistrationCertificate + // Other types type IssuerVkey = common.IssuerVkey + +// Pools +type PoolRelay = common.PoolRelay type PoolId = common.PoolId func NewPoolIdFromBech32(poolId string) (PoolId, error) { return common.NewPoolIdFromBech32(poolId) } +// Assets type AssetFingerprint = common.AssetFingerprint func NewAssetFingerprint(policyId []byte, assetName []byte) AssetFingerprint { diff --git a/ledger/conway.go b/ledger/conway.go index 6c0f8496..396eb1d7 100644 --- a/ledger/conway.go +++ b/ledger/conway.go @@ -257,183 +257,6 @@ type DRepVotingThresholds struct { TreasureWithdrawal cbor.Rat } -// VotingProcedures is a convenience type to avoid needing to duplicate the full type definition everywhere -type VotingProcedures map[*Voter]map[*GovActionId]VotingProcedure - -const ( - VoterTypeConstitutionalCommitteeHotKeyHash uint8 = 0 - VoterTypeConstitutionalCommitteeHotScriptHash uint8 = 1 - VoterTypeDRepKeyHash uint8 = 2 - VoterTypeDRepScriptHash uint8 = 3 - VoterTypeStakingPoolKeyHash uint8 = 4 -) - -type Voter struct { - cbor.StructAsArray - Type uint8 - Hash [28]byte -} - -const ( - GovVoteNo uint8 = 0 - GovVoteYes uint8 = 1 - GovVoteAbstain uint8 = 2 -) - -type VotingProcedure struct { - cbor.StructAsArray - Vote uint8 - Anchor *GovAnchor -} - -type GovAnchor struct { - cbor.StructAsArray - Url string - DataHash [32]byte -} - -type GovActionId struct { - cbor.StructAsArray - TransactionId [32]byte - 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() -} - -type ParameterChangeGovAction struct { - cbor.StructAsArray - Type uint - ActionId *GovActionId - ParamUpdate ConwayProtocolParameterUpdate - PolicyHash []byte -} - -func (a ParameterChangeGovAction) isGovAction() {} - -type HardForkInitiationGovAction struct { - cbor.StructAsArray - Type uint - ActionId *GovActionId - ProtocolVersion struct { - cbor.StructAsArray - Major uint - Minor uint - } -} - -func (a HardForkInitiationGovAction) isGovAction() {} - -type TreasuryWithdrawalGovAction struct { - cbor.StructAsArray - Type uint - Withdrawals map[*Address]uint64 - PolicyHash []byte -} - -func (a TreasuryWithdrawalGovAction) isGovAction() {} - -type NoConfidenceGovAction struct { - cbor.StructAsArray - Type uint - ActionId *GovActionId -} - -func (a NoConfidenceGovAction) isGovAction() {} - -type UpdateCommitteeGovAction struct { - cbor.StructAsArray - Type uint - ActionId *GovActionId - Credentials []StakeCredential - CredEpochs map[*StakeCredential]uint - Unknown cbor.Rat -} - -func (a UpdateCommitteeGovAction) isGovAction() {} - -type NewConstitutionGovAction struct { - cbor.StructAsArray - Type uint - ActionId *GovActionId - Constitution struct { - cbor.StructAsArray - Anchor GovAnchor - ScriptHash []byte - } -} - -func (a NewConstitutionGovAction) isGovAction() {} - -type InfoGovAction struct { - cbor.StructAsArray - Type uint -} - -func (a InfoGovAction) isGovAction() {} - type ConwayTransaction struct { cbor.StructAsArray cbor.DecodeStoreCbor