From 7e4b9f1cbc27c9fbf5438c65479c0cde7f47ec2d Mon Sep 17 00:00:00 2001 From: Aurora Gaffney Date: Sun, 6 Oct 2024 18:52:17 -0500 Subject: [PATCH] feat: Mary protocol parameter updates * update protocol parameter set from protocol param update spec Fixes #743 --- ledger/mary/mary.go | 8 ---- ledger/mary/pparams.go | 31 ++++++++++++++ ledger/mary/pparams_test.go | 85 +++++++++++++++++++++++++++++++++++++ 3 files changed, 116 insertions(+), 8 deletions(-) create mode 100644 ledger/mary/pparams.go create mode 100644 ledger/mary/pparams_test.go diff --git a/ledger/mary/mary.go b/ledger/mary/mary.go index 2708a0c7..7d2ced51 100644 --- a/ledger/mary/mary.go +++ b/ledger/mary/mary.go @@ -399,14 +399,6 @@ func (v *MaryTransactionOutputValue) MarshalCBOR() ([]byte, error) { } } -type MaryProtocolParameters struct { - allegra.AllegraProtocolParameters -} - -type MaryProtocolParameterUpdate struct { - allegra.AllegraProtocolParameterUpdate -} - func NewMaryBlockFromCbor(data []byte) (*MaryBlock, error) { var maryBlock MaryBlock if _, err := cbor.Decode(data, &maryBlock); err != nil { diff --git a/ledger/mary/pparams.go b/ledger/mary/pparams.go new file mode 100644 index 00000000..595019f9 --- /dev/null +++ b/ledger/mary/pparams.go @@ -0,0 +1,31 @@ +// Copyright 2024 Blink Labs Software +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package mary + +import "github.com/blinklabs-io/gouroboros/ledger/allegra" + +type MaryProtocolParameters struct { + allegra.AllegraProtocolParameters +} + +func (p *MaryProtocolParameters) Update(paramUpdate *MaryProtocolParameterUpdate) { + p.AllegraProtocolParameters.Update( + ¶mUpdate.AllegraProtocolParameterUpdate, + ) +} + +type MaryProtocolParameterUpdate struct { + allegra.AllegraProtocolParameterUpdate +} diff --git a/ledger/mary/pparams_test.go b/ledger/mary/pparams_test.go new file mode 100644 index 00000000..2dacbbaf --- /dev/null +++ b/ledger/mary/pparams_test.go @@ -0,0 +1,85 @@ +// Copyright 2024 Blink Labs Software +// +// Licensed under the Apache License, Version 2.0 (the "License"); +// you may not use this file except in compliance with the License. +// You may obtain a copy of the License at +// +// http://www.apache.org/licenses/LICENSE-2.0 +// +// Unless required by applicable law or agreed to in writing, software +// distributed under the License is distributed on an "AS IS" BASIS, +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +// See the License for the specific language governing permissions and +// limitations under the License. + +package mary_test + +import ( + "encoding/hex" + "math/big" + "reflect" + "testing" + + "github.com/blinklabs-io/gouroboros/cbor" + "github.com/blinklabs-io/gouroboros/ledger/allegra" + "github.com/blinklabs-io/gouroboros/ledger/mary" + "github.com/blinklabs-io/gouroboros/ledger/shelley" +) + +func TestMaryProtocolParamsUpdate(t *testing.T) { + testDefs := []struct { + startParams mary.MaryProtocolParameters + updateCbor string + expectedParams mary.MaryProtocolParameters + }{ + { + startParams: mary.MaryProtocolParameters{ + AllegraProtocolParameters: allegra.AllegraProtocolParameters{ + ShelleyProtocolParameters: shelley.ShelleyProtocolParameters{ + Decentralization: &cbor.Rat{Rat: new(big.Rat).SetInt64(1)}, + }, + }, + }, + updateCbor: "a10cd81e82090a", + expectedParams: mary.MaryProtocolParameters{ + AllegraProtocolParameters: allegra.AllegraProtocolParameters{ + ShelleyProtocolParameters: shelley.ShelleyProtocolParameters{ + Decentralization: &cbor.Rat{Rat: big.NewRat(9, 10)}, + }, + }, + }, + }, + { + startParams: mary.MaryProtocolParameters{ + AllegraProtocolParameters: allegra.AllegraProtocolParameters{ + ShelleyProtocolParameters: shelley.ShelleyProtocolParameters{ + ProtocolMajor: 4, + }, + }, + }, + updateCbor: "a10e820500", + expectedParams: mary.MaryProtocolParameters{ + AllegraProtocolParameters: allegra.AllegraProtocolParameters{ + ShelleyProtocolParameters: shelley.ShelleyProtocolParameters{ + ProtocolMajor: 5, + }, + }, + }, + }, + } + for _, testDef := range testDefs { + cborBytes, err := hex.DecodeString(testDef.updateCbor) + if err != nil { + t.Fatalf("unexpected error: %s", err) + } + var tmpUpdate mary.MaryProtocolParameterUpdate + if _, err := cbor.Decode(cborBytes, &tmpUpdate); err != nil { + t.Fatalf("unexpected error: %s", err) + } + tmpParams := testDef.startParams + tmpParams.Update(&tmpUpdate) + if !reflect.DeepEqual(tmpParams, testDef.expectedParams) { + t.Fatalf("did not get expected params:\n got: %#v\n wanted: %#v", tmpParams, testDef.expectedParams) + } + } +}