Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat:add Mary protocol parameters support #589

Merged
merged 1 commit into from
Apr 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
35 changes: 34 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,39 @@ type ShelleyProtocolParameterUpdate struct {
MinUtxoValue uint `cbor:"15,keyasint"`
}

const (
NonceType0 = 0
NonceType1 = 1
)

type Nonce struct {
cbor.StructAsArray
Type uint
Value [32]byte
}

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 uses default value
case 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, 0x42, 0x01, 0x02},
},
{
name: "UnsupportedNonceType",
data: []byte{0x82, 0x02},
expectedErr: "unsupported nonce type 2",
},
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This doesn't actually fail. Your unmarshal function is checking the first value in the list (0 in this case), which is a supported value. Instead I'd use something like 8102 ([2]), which will cause an error.

}

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