15
15
package common
16
16
17
17
import (
18
- "fmt"
19
18
"math/big"
20
19
21
20
"github.com/blinklabs-io/gouroboros/cbor"
@@ -129,21 +128,18 @@ func (id *GovActionId) ToPlutusData() data.PlutusData {
129
128
)
130
129
}
131
130
132
- type ProposalProcedure struct {
133
- cbor.StructAsArray
134
- Deposit uint64
135
- RewardAccount Address
136
- GovAction GovActionWrapper
137
- Anchor GovAnchor
131
+ type ProposalProcedure interface {
132
+ isProposalProcedure ()
133
+ ToPlutusData () data.PlutusData
134
+ Deposit () uint64
135
+ RewardAccount () Address
136
+ GovAction () GovAction
137
+ Anchor () GovAnchor
138
138
}
139
139
140
- func (p * ProposalProcedure ) ToPlutusData () data.PlutusData {
141
- return data .NewConstr (0 ,
142
- data .NewInteger (new (big.Int ).SetUint64 (p .Deposit )),
143
- p .RewardAccount .ToPlutusData (),
144
- p .GovAction .ToPlutusData (),
145
- )
146
- }
140
+ type ProposalProcedureBase struct {}
141
+
142
+ func (ProposalProcedureBase ) isProposalProcedure () {}
147
143
148
144
const (
149
145
GovActionTypeParameterChange = 0
@@ -155,76 +151,14 @@ const (
155
151
GovActionTypeInfo = 6
156
152
)
157
153
158
- type GovActionWrapper struct {
159
- Type uint
160
- Action GovAction
161
- }
162
-
163
- func (g * GovActionWrapper ) ToPlutusData () data.PlutusData {
164
- return g .Action .ToPlutusData ()
165
- }
166
-
167
- func (g * GovActionWrapper ) UnmarshalCBOR (data []byte ) error {
168
- // Determine action type
169
- actionType , err := cbor .DecodeIdFromList (data )
170
- if err != nil {
171
- return err
172
- }
173
- var tmpAction GovAction
174
- switch actionType {
175
- case GovActionTypeParameterChange :
176
- tmpAction = & ParameterChangeGovAction {}
177
- case GovActionTypeHardForkInitiation :
178
- tmpAction = & HardForkInitiationGovAction {}
179
- case GovActionTypeTreasuryWithdrawal :
180
- tmpAction = & TreasuryWithdrawalGovAction {}
181
- case GovActionTypeNoConfidence :
182
- tmpAction = & NoConfidenceGovAction {}
183
- case GovActionTypeUpdateCommittee :
184
- tmpAction = & UpdateCommitteeGovAction {}
185
- case GovActionTypeNewConstitution :
186
- tmpAction = & NewConstitutionGovAction {}
187
- case GovActionTypeInfo :
188
- tmpAction = & InfoGovAction {}
189
- default :
190
- return fmt .Errorf ("unknown governance action type: %d" , actionType )
191
- }
192
- // Decode action
193
- if _ , err := cbor .Decode (data , tmpAction ); err != nil {
194
- return err
195
- }
196
- // action type is known within uint range
197
- g .Type = uint (actionType ) // #nosec G115
198
- g .Action = tmpAction
199
- return nil
200
- }
201
-
202
- func (g * GovActionWrapper ) MarshalCBOR () ([]byte , error ) {
203
- return cbor .Encode (g .Action )
204
- }
205
-
206
154
type GovAction interface {
207
155
isGovAction ()
208
156
ToPlutusData () data.PlutusData
209
157
}
210
158
211
- type ParameterChangeGovAction struct {
212
- cbor.StructAsArray
213
- Type uint
214
- ActionId * GovActionId
215
- ParamUpdate cbor.RawMessage // NOTE: we use raw to defer processing to account for per-era types
216
- PolicyHash []byte
217
- }
218
-
219
- func (a * ParameterChangeGovAction ) ToPlutusData () data.PlutusData {
220
- return data .NewConstr (0 ,
221
- a .ActionId .ToPlutusData (),
222
- data .NewByteString (a .ParamUpdate ),
223
- data .NewByteString (a .PolicyHash ),
224
- )
225
- }
159
+ type GovActionBase struct {}
226
160
227
- func (a ParameterChangeGovAction ) isGovAction () {}
161
+ func (GovActionBase ) isGovAction () {}
228
162
229
163
type HardForkInitiationGovAction struct {
230
164
cbor.StructAsArray
@@ -238,8 +172,12 @@ type HardForkInitiationGovAction struct {
238
172
}
239
173
240
174
func (a * HardForkInitiationGovAction ) ToPlutusData () data.PlutusData {
175
+ actionId := data .NewConstr (1 )
176
+ if a .ActionId != nil {
177
+ actionId = data .NewConstr (0 , a .ActionId .ToPlutusData ())
178
+ }
241
179
return data .NewConstr (1 ,
242
- a . ActionId . ToPlutusData () ,
180
+ actionId ,
243
181
data .NewConstr (
244
182
0 ,
245
183
data .NewInteger (
@@ -265,13 +203,20 @@ func (a *TreasuryWithdrawalGovAction) ToPlutusData() data.PlutusData {
265
203
pairs := make ([][2 ]data.PlutusData , 0 , len (a .Withdrawals ))
266
204
for addr , amount := range a .Withdrawals {
267
205
pairs = append (pairs , [2 ]data.PlutusData {
268
- data . NewConstr ( 0 , addr .ToPlutusData () ),
206
+ addr .ToPlutusData (),
269
207
data .NewInteger (new (big.Int ).SetUint64 (amount )),
270
208
})
271
209
}
210
+ policyHash := data .NewConstr (1 )
211
+ if len (a .PolicyHash ) > 0 {
212
+ policyHash = data .NewConstr (
213
+ 0 ,
214
+ data .NewByteString (a .PolicyHash ),
215
+ )
216
+ }
272
217
return data .NewConstr (2 ,
273
218
data .NewMap (pairs ),
274
- data . NewByteString ( a . PolicyHash ) ,
219
+ policyHash ,
275
220
)
276
221
}
277
222
@@ -284,8 +229,12 @@ type NoConfidenceGovAction struct {
284
229
}
285
230
286
231
func (a * NoConfidenceGovAction ) ToPlutusData () data.PlutusData {
232
+ actionId := data .NewConstr (1 )
233
+ if a .ActionId != nil {
234
+ actionId = data .NewConstr (0 , a .ActionId .ToPlutusData ())
235
+ }
287
236
return data .NewConstr (3 ,
288
- a . ActionId . ToPlutusData () ,
237
+ actionId ,
289
238
)
290
239
}
291
240
@@ -301,6 +250,10 @@ type UpdateCommitteeGovAction struct {
301
250
}
302
251
303
252
func (a * UpdateCommitteeGovAction ) ToPlutusData () data.PlutusData {
253
+ actionId := data .NewConstr (1 )
254
+ if a .ActionId != nil {
255
+ actionId = data .NewConstr (0 , a .ActionId .ToPlutusData ())
256
+ }
304
257
removedItems := make ([]data.PlutusData , 0 , len (a .Credentials ))
305
258
for _ , cred := range a .Credentials {
306
259
removedItems = append (removedItems , cred .ToPlutusData ())
@@ -325,11 +278,14 @@ func (a *UpdateCommitteeGovAction) ToPlutusData() data.PlutusData {
325
278
}
326
279
327
280
return data .NewConstr (4 ,
328
- a . ActionId . ToPlutusData () ,
281
+ actionId ,
329
282
data .NewList (removedItems ... ),
330
283
data .NewMap (addedPairs ),
331
- data .NewInteger (num ),
332
- data .NewInteger (den ),
284
+ data .NewConstr (
285
+ 0 ,
286
+ data .NewInteger (num ),
287
+ data .NewInteger (den ),
288
+ ),
333
289
)
334
290
}
335
291
@@ -347,11 +303,21 @@ type NewConstitutionGovAction struct {
347
303
}
348
304
349
305
func (a * NewConstitutionGovAction ) ToPlutusData () data.PlutusData {
306
+ actionId := data .NewConstr (1 )
307
+ if a .ActionId != nil {
308
+ actionId = data .NewConstr (0 , a .ActionId .ToPlutusData ())
309
+ }
310
+ scriptHash := data .NewConstr (1 )
311
+ if len (a .Constitution .ScriptHash ) > 0 {
312
+ scriptHash = data .NewConstr (
313
+ 0 ,
314
+ data .NewByteString (a .Constitution .ScriptHash ),
315
+ )
316
+ }
350
317
return data .NewConstr (5 ,
351
- a . ActionId . ToPlutusData () ,
318
+ actionId ,
352
319
data .NewConstr (0 ,
353
- a .Constitution .Anchor .ToPlutusData (),
354
- data .NewByteString (a .Constitution .ScriptHash ),
320
+ scriptHash ,
355
321
),
356
322
)
357
323
}
0 commit comments