Skip to content

Commit 34e77e7

Browse files
authored
feat:add Mary protocol parameters support (#589)
Signed-off-by: Ales Verbic <[email protected]>
1 parent b7ffc33 commit 34e77e7

File tree

4 files changed

+102
-3
lines changed

4 files changed

+102
-3
lines changed

ledger/mary.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ type MaryBlock struct {
3939
cbor.DecodeStoreCbor
4040
Header *MaryBlockHeader
4141
TransactionBodies []MaryTransactionBody
42-
TransactionWitnessSets []ShelleyTransactionWitnessSet
42+
TransactionWitnessSets []MaryTransactionWitnessSet
4343
TransactionMetadataSet map[uint]*cbor.Value
4444
}
4545

@@ -115,6 +115,11 @@ func (h *MaryBlockHeader) Era() Era {
115115

116116
type MaryTransactionBody struct {
117117
AllegraTransactionBody
118+
Update struct {
119+
cbor.StructAsArray
120+
ProtocolParamUpdates map[Blake2b224]MaryProtocolParameterUpdate
121+
Epoch uint64
122+
} `cbor:"6,keyasint,omitempty"`
118123
TxOutputs []MaryTransactionOutput `cbor:"1,keyasint,omitempty"`
119124
Mint MultiAsset[MultiAssetTypeMint] `cbor:"9,keyasint,omitempty"`
120125
}
@@ -136,10 +141,16 @@ type MaryTransaction struct {
136141
cbor.StructAsArray
137142
cbor.DecodeStoreCbor
138143
Body MaryTransactionBody
139-
WitnessSet ShelleyTransactionWitnessSet
144+
WitnessSet MaryTransactionWitnessSet
140145
TxMetadata *cbor.Value
141146
}
142147

148+
type MaryTransactionWitnessSet struct {
149+
ShelleyTransactionWitnessSet
150+
Script []interface{} `cbor:"4,keyasint,omitempty"`
151+
PlutusScripts []interface{} `cbor:"5,keyasint,omitempty"`
152+
}
153+
143154
func (t MaryTransaction) Hash() string {
144155
return t.Body.Hash()
145156
}
@@ -275,6 +286,14 @@ func (v *MaryTransactionOutputValue) MarshalCBOR() ([]byte, error) {
275286
}
276287
}
277288

289+
type MaryProtocolParameters struct {
290+
AllegraProtocolParameters
291+
}
292+
293+
type MaryProtocolParameterUpdate struct {
294+
AllegraProtocolParameterUpdate
295+
}
296+
278297
func NewMaryBlockFromCbor(data []byte) (*MaryBlock, error) {
279298
var maryBlock MaryBlock
280299
if _, err := cbor.Decode(data, &maryBlock); err != nil {

ledger/shelley.go

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -432,7 +432,7 @@ type ShelleyProtocolParameterUpdate struct {
432432
Rho *cbor.Rat `cbor:"10,keyasint"`
433433
Tau *cbor.Rat `cbor:"11,keyasint"`
434434
Decentralization *cbor.Rat `cbor:"12,keyasint"`
435-
Nonce *cbor.Rat `cbor:"13,keyasint"`
435+
Nonce *Nonce `cbor:"13,keyasint"`
436436
ProtocolVersion struct {
437437
cbor.StructAsArray
438438
Major uint
@@ -441,6 +441,39 @@ type ShelleyProtocolParameterUpdate struct {
441441
MinUtxoValue uint `cbor:"15,keyasint"`
442442
}
443443

444+
const (
445+
NonceType0 = 0
446+
NonceType1 = 1
447+
)
448+
449+
type Nonce struct {
450+
cbor.StructAsArray
451+
Type uint
452+
Value [32]byte
453+
}
454+
455+
func (n *Nonce) UnmarshalCBOR(data []byte) error {
456+
nonceType, err := cbor.DecodeIdFromList(data)
457+
if err != nil {
458+
return err
459+
}
460+
461+
n.Type = uint(nonceType)
462+
463+
switch nonceType {
464+
case NonceType0:
465+
// Value uses default value
466+
case NonceType1:
467+
if err := cbor.DecodeGeneric(data, n); err != nil {
468+
fmt.Printf("Nonce decode error: %+v\n", data)
469+
return err
470+
}
471+
default:
472+
return fmt.Errorf("unsupported nonce type %d", nonceType)
473+
}
474+
return nil
475+
}
476+
444477
func NewShelleyBlockFromCbor(data []byte) (*ShelleyBlock, error) {
445478
var shelleyBlock ShelleyBlock
446479
if _, err := cbor.Decode(data, &shelleyBlock); err != nil {

ledger/shelley_test.go

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
package ledger
2+
3+
import (
4+
"testing"
5+
)
6+
7+
func TestNonceUnmarshalCBOR(t *testing.T) {
8+
testCases := []struct {
9+
name string
10+
data []byte
11+
expectedErr string
12+
}{
13+
{
14+
name: "NonceType0",
15+
data: []byte{0x81, 0x00},
16+
},
17+
{
18+
name: "NonceType1",
19+
data: []byte{0x82, 0x01, 0x42, 0x01, 0x02},
20+
},
21+
{
22+
name: "UnsupportedNonceType",
23+
data: []byte{0x82, 0x02},
24+
expectedErr: "unsupported nonce type 2",
25+
},
26+
}
27+
28+
for _, tc := range testCases {
29+
t.Run(tc.name, func(t *testing.T) {
30+
n := &Nonce{}
31+
err := n.UnmarshalCBOR(tc.data)
32+
if err != nil {
33+
if tc.expectedErr == "" || err.Error() != tc.expectedErr {
34+
t.Errorf("unexpected error: %v", err)
35+
}
36+
} else if tc.expectedErr != "" {
37+
t.Errorf("expected error: %v, got nil", tc.expectedErr)
38+
}
39+
})
40+
}
41+
}

protocol/localstatequery/client.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -354,6 +354,12 @@ func (c *Client) GetCurrentProtocolParams() (CurrentProtocolParamsResult, error)
354354
return nil, err
355355
}
356356
return result[0], nil
357+
case ledger.EraIdMary:
358+
result := []ledger.MaryProtocolParameters{}
359+
if err := c.runQuery(query, &result); err != nil {
360+
return nil, err
361+
}
362+
return result[0], nil
357363
case ledger.EraIdAllegra:
358364
result := []ledger.AllegraProtocolParameters{}
359365
if err := c.runQuery(query, &result); err != nil {

0 commit comments

Comments
 (0)