Skip to content

Commit faa045f

Browse files
authored
chore: move certs, credentials, and gov types to sub-package (#690)
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.
1 parent 3cb9204 commit faa045f

File tree

5 files changed

+215
-179
lines changed

5 files changed

+215
-179
lines changed

ledger/certs.go renamed to ledger/common/certs.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package ledger
15+
package common
1616

1717
import (
1818
"fmt"

ledger/credentials.go renamed to ledger/common/credentials.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
package ledger
15+
package common
1616

1717
import (
1818
"fmt"

ledger/common/gov.go

Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
// Copyright 2024 Blink Labs Software
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package common
16+
17+
import (
18+
"fmt"
19+
20+
"github.com/blinklabs-io/gouroboros/cbor"
21+
)
22+
23+
// VotingProcedures is a convenience type to avoid needing to duplicate the full type definition everywhere
24+
type VotingProcedures map[*Voter]map[*GovActionId]VotingProcedure
25+
26+
const (
27+
VoterTypeConstitutionalCommitteeHotKeyHash uint8 = 0
28+
VoterTypeConstitutionalCommitteeHotScriptHash uint8 = 1
29+
VoterTypeDRepKeyHash uint8 = 2
30+
VoterTypeDRepScriptHash uint8 = 3
31+
VoterTypeStakingPoolKeyHash uint8 = 4
32+
)
33+
34+
type Voter struct {
35+
cbor.StructAsArray
36+
Type uint8
37+
Hash [28]byte
38+
}
39+
40+
const (
41+
GovVoteNo uint8 = 0
42+
GovVoteYes uint8 = 1
43+
GovVoteAbstain uint8 = 2
44+
)
45+
46+
type VotingProcedure struct {
47+
cbor.StructAsArray
48+
Vote uint8
49+
Anchor *GovAnchor
50+
}
51+
52+
type GovAnchor struct {
53+
cbor.StructAsArray
54+
Url string
55+
DataHash [32]byte
56+
}
57+
58+
type GovActionId struct {
59+
cbor.StructAsArray
60+
TransactionId [32]byte
61+
GovActionIdx uint32
62+
}
63+
64+
type ProposalProcedure struct {
65+
cbor.StructAsArray
66+
Deposit uint64
67+
RewardAccount Address
68+
GovAction GovActionWrapper
69+
Anchor GovAnchor
70+
}
71+
72+
const (
73+
GovActionTypeParameterChange = 0
74+
GovActionTypeHardForkInitiation = 1
75+
GovActionTypeTreasuryWithdrawal = 2
76+
GovActionTypeNoConfidence = 3
77+
GovActionTypeUpdateCommittee = 4
78+
GovActionTypeNewConstitution = 5
79+
GovActionTypeInfo = 6
80+
)
81+
82+
type GovActionWrapper struct {
83+
Type uint
84+
Action GovAction
85+
}
86+
87+
func (g *GovActionWrapper) UnmarshalCBOR(data []byte) error {
88+
// Determine action type
89+
actionType, err := cbor.DecodeIdFromList(data)
90+
if err != nil {
91+
return err
92+
}
93+
var tmpAction GovAction
94+
switch actionType {
95+
case GovActionTypeParameterChange:
96+
tmpAction = &ParameterChangeGovAction{}
97+
case GovActionTypeHardForkInitiation:
98+
tmpAction = &HardForkInitiationGovAction{}
99+
case GovActionTypeTreasuryWithdrawal:
100+
tmpAction = &TreasuryWithdrawalGovAction{}
101+
case GovActionTypeNoConfidence:
102+
tmpAction = &NoConfidenceGovAction{}
103+
case GovActionTypeUpdateCommittee:
104+
tmpAction = &UpdateCommitteeGovAction{}
105+
case GovActionTypeNewConstitution:
106+
tmpAction = &NewConstitutionGovAction{}
107+
case GovActionTypeInfo:
108+
tmpAction = &InfoGovAction{}
109+
default:
110+
return fmt.Errorf("unknown governance action type: %d", actionType)
111+
}
112+
// Decode action
113+
if _, err := cbor.Decode(data, tmpAction); err != nil {
114+
return err
115+
}
116+
g.Type = uint(actionType)
117+
g.Action = tmpAction
118+
return nil
119+
}
120+
121+
func (g *GovActionWrapper) MarshalCBOR() ([]byte, error) {
122+
return cbor.Encode(g.Action)
123+
}
124+
125+
type GovAction interface {
126+
isGovAction()
127+
}
128+
129+
type ParameterChangeGovAction struct {
130+
cbor.StructAsArray
131+
Type uint
132+
ActionId *GovActionId
133+
ParamUpdate cbor.RawMessage // NOTE: we use raw to defer processing to account for per-era types
134+
PolicyHash []byte
135+
}
136+
137+
func (a ParameterChangeGovAction) isGovAction() {}
138+
139+
type HardForkInitiationGovAction struct {
140+
cbor.StructAsArray
141+
Type uint
142+
ActionId *GovActionId
143+
ProtocolVersion struct {
144+
cbor.StructAsArray
145+
Major uint
146+
Minor uint
147+
}
148+
}
149+
150+
func (a HardForkInitiationGovAction) isGovAction() {}
151+
152+
type TreasuryWithdrawalGovAction struct {
153+
cbor.StructAsArray
154+
Type uint
155+
Withdrawals map[*Address]uint64
156+
PolicyHash []byte
157+
}
158+
159+
func (a TreasuryWithdrawalGovAction) isGovAction() {}
160+
161+
type NoConfidenceGovAction struct {
162+
cbor.StructAsArray
163+
Type uint
164+
ActionId *GovActionId
165+
}
166+
167+
func (a NoConfidenceGovAction) isGovAction() {}
168+
169+
type UpdateCommitteeGovAction struct {
170+
cbor.StructAsArray
171+
Type uint
172+
ActionId *GovActionId
173+
Credentials []StakeCredential
174+
CredEpochs map[*StakeCredential]uint
175+
Unknown cbor.Rat
176+
}
177+
178+
func (a UpdateCommitteeGovAction) isGovAction() {}
179+
180+
type NewConstitutionGovAction struct {
181+
cbor.StructAsArray
182+
Type uint
183+
ActionId *GovActionId
184+
Constitution struct {
185+
cbor.StructAsArray
186+
Anchor GovAnchor
187+
ScriptHash []byte
188+
}
189+
}
190+
191+
func (a NewConstitutionGovAction) isGovAction() {}
192+
193+
type InfoGovAction struct {
194+
cbor.StructAsArray
195+
Type uint
196+
}
197+
198+
func (a InfoGovAction) isGovAction() {}

ledger/compat.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,29 @@ func NewAddress(addr string) (Address, error) {
4141
return common.NewAddress(addr)
4242
}
4343

44+
// Governance types
45+
type VotingProcedure = common.VotingProcedure
46+
type VotingProcedures = common.VotingProcedures
47+
type ProposalProcedure = common.ProposalProcedure
48+
49+
// Certificates
50+
type Certificate = common.Certificate
51+
type CertificateWrapper = common.CertificateWrapper
52+
type PoolRetirementCertificate = common.PoolRetirementCertificate
53+
type PoolRegistrationCertificate = common.PoolRegistrationCertificate
54+
4455
// Other types
4556
type IssuerVkey = common.IssuerVkey
57+
58+
// Pools
59+
type PoolRelay = common.PoolRelay
4660
type PoolId = common.PoolId
4761

4862
func NewPoolIdFromBech32(poolId string) (PoolId, error) {
4963
return common.NewPoolIdFromBech32(poolId)
5064
}
5165

66+
// Assets
5267
type AssetFingerprint = common.AssetFingerprint
5368

5469
func NewAssetFingerprint(policyId []byte, assetName []byte) AssetFingerprint {

0 commit comments

Comments
 (0)