Skip to content

Commit 1c7eca1

Browse files
authored
feat(ledger): Add ToPlutusData() function to gov vote-related types (#1117)
Signed-off-by: Akhil Repala <[email protected]>
1 parent 058a7cb commit 1c7eca1

File tree

2 files changed

+232
-0
lines changed

2 files changed

+232
-0
lines changed

ledger/common/gov.go

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
"fmt"
1919

2020
"github.com/blinklabs-io/gouroboros/cbor"
21+
"github.com/blinklabs-io/plutigo/pkg/data"
2122
)
2223

2324
// VotingProcedures is a convenience type to avoid needing to duplicate the full type definition everywhere
@@ -37,18 +38,70 @@ type Voter struct {
3738
Hash [28]byte
3839
}
3940

41+
func (v Voter) ToPlutusData() data.PlutusData {
42+
switch v.Type {
43+
case VoterTypeConstitutionalCommitteeHotScriptHash:
44+
cred := &Credential{
45+
CredType: CredentialTypeScriptHash,
46+
Credential: NewBlake2b224(v.Hash[:]),
47+
}
48+
return data.NewConstr(0, cred.ToPlutusData())
49+
case VoterTypeConstitutionalCommitteeHotKeyHash:
50+
cred := &Credential{
51+
CredType: CredentialTypeAddrKeyHash,
52+
Credential: NewBlake2b224(v.Hash[:]),
53+
}
54+
return data.NewConstr(0, cred.ToPlutusData())
55+
case VoterTypeDRepScriptHash:
56+
cred := &Credential{
57+
CredType: CredentialTypeScriptHash,
58+
Credential: NewBlake2b224(v.Hash[:]),
59+
}
60+
return data.NewConstr(1, cred.ToPlutusData())
61+
case VoterTypeDRepKeyHash:
62+
cred := &Credential{
63+
CredType: CredentialTypeAddrKeyHash,
64+
Credential: NewBlake2b224(v.Hash[:]),
65+
}
66+
return data.NewConstr(1, cred.ToPlutusData())
67+
case VoterTypeStakingPoolKeyHash:
68+
return data.NewConstr(2, data.NewByteString(v.Hash[:]))
69+
default:
70+
return nil
71+
}
72+
}
73+
4074
const (
4175
GovVoteNo uint8 = 0
4276
GovVoteYes uint8 = 1
4377
GovVoteAbstain uint8 = 2
4478
)
4579

80+
type Vote uint8
81+
82+
func (v Vote) ToPlutusData() data.PlutusData {
83+
switch v {
84+
case Vote(GovVoteNo):
85+
return data.NewConstr(0)
86+
case Vote(GovVoteYes):
87+
return data.NewConstr(1)
88+
case Vote(GovVoteAbstain):
89+
return data.NewConstr(2)
90+
default:
91+
return nil
92+
}
93+
}
94+
4695
type VotingProcedure struct {
4796
cbor.StructAsArray
4897
Vote uint8
4998
Anchor *GovAnchor
5099
}
51100

101+
func (vp VotingProcedure) ToPlutusData() data.PlutusData {
102+
return Vote(vp.Vote).ToPlutusData()
103+
}
104+
52105
type GovAnchor struct {
53106
cbor.StructAsArray
54107
Url string

ledger/common/gov_test.go

Lines changed: 179 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,179 @@
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+
"reflect"
19+
"testing"
20+
21+
"github.com/blinklabs-io/plutigo/pkg/data"
22+
)
23+
24+
// Ttests the ToPlutusData method for Voter types
25+
func TestVoterToPlutusData(t *testing.T) {
26+
// Use the same hash value for all tests to avoid confusion
27+
testHash := [28]byte{0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}
28+
testHashBytes := []byte{0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11, 0x11}
29+
30+
testCases := []struct {
31+
name string
32+
voter Voter
33+
expectedData data.PlutusData
34+
}{
35+
{
36+
name: "ConstitutionalCommitteeHotScriptHash",
37+
voter: Voter{
38+
Type: VoterTypeConstitutionalCommitteeHotScriptHash,
39+
Hash: testHash,
40+
},
41+
expectedData: data.NewConstr(0, data.NewConstr(1, data.NewByteString(testHashBytes))),
42+
},
43+
{
44+
name: "ConstitutionalCommitteeHotKeyHash",
45+
voter: Voter{
46+
Type: VoterTypeConstitutionalCommitteeHotKeyHash,
47+
Hash: testHash,
48+
},
49+
expectedData: data.NewConstr(0, data.NewConstr(0, data.NewByteString(testHashBytes))),
50+
},
51+
{
52+
name: "DRepScriptHash",
53+
voter: Voter{
54+
Type: VoterTypeDRepScriptHash,
55+
Hash: testHash,
56+
},
57+
expectedData: data.NewConstr(1, data.NewConstr(1, data.NewByteString(testHashBytes))),
58+
},
59+
{
60+
name: "DRepKeyHash",
61+
voter: Voter{
62+
Type: VoterTypeDRepKeyHash,
63+
Hash: testHash,
64+
},
65+
expectedData: data.NewConstr(1, data.NewConstr(0, data.NewByteString(testHashBytes))),
66+
},
67+
{
68+
name: "StakingPoolKeyHash",
69+
voter: Voter{
70+
Type: VoterTypeStakingPoolKeyHash,
71+
Hash: testHash,
72+
},
73+
expectedData: data.NewConstr(2, data.NewByteString(testHashBytes)),
74+
},
75+
{
76+
name: "UnknownType",
77+
voter: Voter{
78+
Type: 255, // Unknown type
79+
Hash: [28]byte{},
80+
},
81+
expectedData: nil,
82+
},
83+
}
84+
85+
for _, tc := range testCases {
86+
t.Run(tc.name, func(t *testing.T) {
87+
result := tc.voter.ToPlutusData()
88+
if !reflect.DeepEqual(result, tc.expectedData) {
89+
t.Errorf("ToPlutusData() = %#v, want %#v", result, tc.expectedData)
90+
}
91+
})
92+
}
93+
}
94+
95+
// Tests the ToPlutusData method for Vote types
96+
func TestVoteToPlutusData(t *testing.T) {
97+
testCases := []struct {
98+
name string
99+
vote Vote
100+
expectedData data.PlutusData
101+
}{
102+
{
103+
name: "No",
104+
vote: Vote(GovVoteNo),
105+
expectedData: data.NewConstr(0),
106+
},
107+
{
108+
name: "Yes",
109+
vote: Vote(GovVoteYes),
110+
expectedData: data.NewConstr(1),
111+
},
112+
{
113+
name: "Abstain",
114+
vote: Vote(GovVoteAbstain),
115+
expectedData: data.NewConstr(2),
116+
},
117+
{
118+
name: "Unknown",
119+
vote: Vote(255), // Unknown vote
120+
expectedData: nil,
121+
},
122+
}
123+
124+
for _, tc := range testCases {
125+
t.Run(tc.name, func(t *testing.T) {
126+
result := tc.vote.ToPlutusData()
127+
if !reflect.DeepEqual(result, tc.expectedData) {
128+
t.Errorf("ToPlutusData() = %#v, want %#v", result, tc.expectedData)
129+
}
130+
})
131+
}
132+
}
133+
134+
// Tests the ToPlutusData method for VotingProcedure types
135+
func TestVotingProcedureToPlutusData(t *testing.T) {
136+
testCases := []struct {
137+
name string
138+
procedure VotingProcedure
139+
expectedData data.PlutusData
140+
}{
141+
{
142+
name: "NoVote",
143+
procedure: VotingProcedure{
144+
Vote: GovVoteNo,
145+
},
146+
expectedData: data.NewConstr(0),
147+
},
148+
{
149+
name: "YesVote",
150+
procedure: VotingProcedure{
151+
Vote: GovVoteYes,
152+
},
153+
expectedData: data.NewConstr(1),
154+
},
155+
{
156+
name: "AbstainVote",
157+
procedure: VotingProcedure{
158+
Vote: GovVoteAbstain,
159+
},
160+
expectedData: data.NewConstr(2),
161+
},
162+
{
163+
name: "UnknownVote",
164+
procedure: VotingProcedure{
165+
Vote: 255, // Unknown vote
166+
},
167+
expectedData: nil,
168+
},
169+
}
170+
171+
for _, tc := range testCases {
172+
t.Run(tc.name, func(t *testing.T) {
173+
result := tc.procedure.ToPlutusData()
174+
if !reflect.DeepEqual(result, tc.expectedData) {
175+
t.Errorf("ToPlutusData() = %#v, want %#v", result, tc.expectedData)
176+
}
177+
})
178+
}
179+
}

0 commit comments

Comments
 (0)