Skip to content

Commit

Permalink
feat:add Mary protocol parameters support
Browse files Browse the repository at this point in the history
Signed-off-by: Ales Verbic <[email protected]>
  • Loading branch information
verbotenj committed Apr 21, 2024
1 parent 0b2aa02 commit f6dc9e2
Show file tree
Hide file tree
Showing 4 changed files with 103 additions and 3 deletions.
23 changes: 21 additions & 2 deletions ledger/mary.go
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ type MaryBlock struct {
cbor.DecodeStoreCbor
Header *MaryBlockHeader
TransactionBodies []MaryTransactionBody
TransactionWitnessSets []ShelleyTransactionWitnessSet
TransactionWitnessSets []MaryTransactionWitnessSet
TransactionMetadataSet map[uint]*cbor.Value
}

Expand Down Expand Up @@ -115,6 +115,11 @@ func (h *MaryBlockHeader) Era() Era {

type MaryTransactionBody struct {
AllegraTransactionBody
Update struct {
cbor.StructAsArray
ProtocolParamUpdates map[Blake2b224]MaryProtocolParameterUpdate
Epoch uint64
} `cbor:"6,keyasint,omitempty"`
TxOutputs []MaryTransactionOutput `cbor:"1,keyasint,omitempty"`
Mint MultiAsset[MultiAssetTypeMint] `cbor:"9,keyasint,omitempty"`
}
Expand All @@ -136,10 +141,16 @@ type MaryTransaction struct {
cbor.StructAsArray
cbor.DecodeStoreCbor
Body MaryTransactionBody
WitnessSet ShelleyTransactionWitnessSet
WitnessSet MaryTransactionWitnessSet
TxMetadata *cbor.Value
}

type MaryTransactionWitnessSet struct {
ShelleyTransactionWitnessSet
Script []interface{} `cbor:"4,keyasint,omitempty"`
PlutusScripts []interface{} `cbor:"5,keyasint,omitempty"`
}

func (t MaryTransaction) Hash() string {
return t.Body.Hash()
}
Expand Down Expand Up @@ -275,6 +286,14 @@ func (v *MaryTransactionOutputValue) MarshalCBOR() ([]byte, error) {
}
}

type MaryProtocolParameters struct {
AllegraProtocolParameters
}

type MaryProtocolParameterUpdate struct {
AllegraProtocolParameterUpdate
}

func NewMaryBlockFromCbor(data []byte) (*MaryBlock, error) {
var maryBlock MaryBlock
if _, err := cbor.Decode(data, &maryBlock); err != nil {
Expand Down
36 changes: 35 additions & 1 deletion ledger/shelley.go
Original file line number Diff line number Diff line change
Expand Up @@ -432,7 +432,7 @@ type ShelleyProtocolParameterUpdate struct {
Rho *cbor.Rat `cbor:"10,keyasint"`
Tau *cbor.Rat `cbor:"11,keyasint"`
Decentralization *cbor.Rat `cbor:"12,keyasint"`
Nonce *cbor.Rat `cbor:"13,keyasint"`
Nonce *Nonce `cbor:"13,keyasint"`
ProtocolVersion struct {
cbor.StructAsArray
Major uint
Expand All @@ -441,6 +441,40 @@ type ShelleyProtocolParameterUpdate struct {
MinUtxoValue uint `cbor:"15,keyasint"`
}

const (
NonceType0 = 0
NonceType1 = 1
)

type Nonce struct {
cbor.StructAsArray
Type uint
Value any
}

func (n *Nonce) UnmarshalCBOR(data []byte) error {
nonceType, err := cbor.DecodeIdFromList(data)
if err != nil {
return err
}

n.Type = uint(nonceType)

switch nonceType {
case NonceType0:
// Value is nil by default
case NonceType1:
n.Type = NonceType1
if err := cbor.DecodeGeneric(data, n); err != nil {
fmt.Printf("Nonce decode error: %+v\n", data)
return err
}
default:
return fmt.Errorf("unsupported nonce type %d", nonceType)
}
return nil
}

func NewShelleyBlockFromCbor(data []byte) (*ShelleyBlock, error) {
var shelleyBlock ShelleyBlock
if _, err := cbor.Decode(data, &shelleyBlock); err != nil {
Expand Down
41 changes: 41 additions & 0 deletions ledger/shelley_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package ledger

import (
"testing"
)

func TestNonceUnmarshalCBOR(t *testing.T) {
testCases := []struct {
name string
data []byte
expectedErr string
}{
{
name: "NonceType0",
data: []byte{0x81, 0x00},
},
{
name: "NonceType1",
data: []byte{0x82, 0x01, 0x01},
},
{
name: "UnsupportedNonceType",
data: []byte{0x82, 0x02},
expectedErr: "unsupported nonce type 2",
},
}

for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
n := &Nonce{}
err := n.UnmarshalCBOR(tc.data)
if err != nil {
if tc.expectedErr == "" || err.Error() != tc.expectedErr {
t.Errorf("unexpected error: %v", err)
}
} else if tc.expectedErr != "" {
t.Errorf("expected error: %v, got nil", tc.expectedErr)
}
})
}
}
6 changes: 6 additions & 0 deletions protocol/localstatequery/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,12 @@ func (c *Client) GetCurrentProtocolParams() (CurrentProtocolParamsResult, error)
return nil, err
}
return result[0], nil
case ledger.EraIdMary:
result := []ledger.MaryProtocolParameters{}
if err := c.runQuery(query, &result); err != nil {
return nil, err
}
return result[0], nil
case ledger.EraIdAllegra:
result := []ledger.AllegraProtocolParameters{}
if err := c.runQuery(query, &result); err != nil {
Expand Down

0 comments on commit f6dc9e2

Please sign in to comment.