Skip to content

Commit a13f579

Browse files
authored
feat: Mary protocol parameter updates (#748)
* update protocol parameter set from protocol param update spec Fixes #743
1 parent eb7b196 commit a13f579

File tree

3 files changed

+116
-8
lines changed

3 files changed

+116
-8
lines changed

ledger/mary/mary.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -399,14 +399,6 @@ func (v *MaryTransactionOutputValue) MarshalCBOR() ([]byte, error) {
399399
}
400400
}
401401

402-
type MaryProtocolParameters struct {
403-
allegra.AllegraProtocolParameters
404-
}
405-
406-
type MaryProtocolParameterUpdate struct {
407-
allegra.AllegraProtocolParameterUpdate
408-
}
409-
410402
func NewMaryBlockFromCbor(data []byte) (*MaryBlock, error) {
411403
var maryBlock MaryBlock
412404
if _, err := cbor.Decode(data, &maryBlock); err != nil {

ledger/mary/pparams.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
// Copyright 2024 Blink Labs Software
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package mary
16+
17+
import "github.com/blinklabs-io/gouroboros/ledger/allegra"
18+
19+
type MaryProtocolParameters struct {
20+
allegra.AllegraProtocolParameters
21+
}
22+
23+
func (p *MaryProtocolParameters) Update(paramUpdate *MaryProtocolParameterUpdate) {
24+
p.AllegraProtocolParameters.Update(
25+
&paramUpdate.AllegraProtocolParameterUpdate,
26+
)
27+
}
28+
29+
type MaryProtocolParameterUpdate struct {
30+
allegra.AllegraProtocolParameterUpdate
31+
}

ledger/mary/pparams_test.go

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
// Copyright 2024 Blink Labs Software
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
15+
package mary_test
16+
17+
import (
18+
"encoding/hex"
19+
"math/big"
20+
"reflect"
21+
"testing"
22+
23+
"github.com/blinklabs-io/gouroboros/cbor"
24+
"github.com/blinklabs-io/gouroboros/ledger/allegra"
25+
"github.com/blinklabs-io/gouroboros/ledger/mary"
26+
"github.com/blinklabs-io/gouroboros/ledger/shelley"
27+
)
28+
29+
func TestMaryProtocolParamsUpdate(t *testing.T) {
30+
testDefs := []struct {
31+
startParams mary.MaryProtocolParameters
32+
updateCbor string
33+
expectedParams mary.MaryProtocolParameters
34+
}{
35+
{
36+
startParams: mary.MaryProtocolParameters{
37+
AllegraProtocolParameters: allegra.AllegraProtocolParameters{
38+
ShelleyProtocolParameters: shelley.ShelleyProtocolParameters{
39+
Decentralization: &cbor.Rat{Rat: new(big.Rat).SetInt64(1)},
40+
},
41+
},
42+
},
43+
updateCbor: "a10cd81e82090a",
44+
expectedParams: mary.MaryProtocolParameters{
45+
AllegraProtocolParameters: allegra.AllegraProtocolParameters{
46+
ShelleyProtocolParameters: shelley.ShelleyProtocolParameters{
47+
Decentralization: &cbor.Rat{Rat: big.NewRat(9, 10)},
48+
},
49+
},
50+
},
51+
},
52+
{
53+
startParams: mary.MaryProtocolParameters{
54+
AllegraProtocolParameters: allegra.AllegraProtocolParameters{
55+
ShelleyProtocolParameters: shelley.ShelleyProtocolParameters{
56+
ProtocolMajor: 4,
57+
},
58+
},
59+
},
60+
updateCbor: "a10e820500",
61+
expectedParams: mary.MaryProtocolParameters{
62+
AllegraProtocolParameters: allegra.AllegraProtocolParameters{
63+
ShelleyProtocolParameters: shelley.ShelleyProtocolParameters{
64+
ProtocolMajor: 5,
65+
},
66+
},
67+
},
68+
},
69+
}
70+
for _, testDef := range testDefs {
71+
cborBytes, err := hex.DecodeString(testDef.updateCbor)
72+
if err != nil {
73+
t.Fatalf("unexpected error: %s", err)
74+
}
75+
var tmpUpdate mary.MaryProtocolParameterUpdate
76+
if _, err := cbor.Decode(cborBytes, &tmpUpdate); err != nil {
77+
t.Fatalf("unexpected error: %s", err)
78+
}
79+
tmpParams := testDef.startParams
80+
tmpParams.Update(&tmpUpdate)
81+
if !reflect.DeepEqual(tmpParams, testDef.expectedParams) {
82+
t.Fatalf("did not get expected params:\n got: %#v\n wanted: %#v", tmpParams, testDef.expectedParams)
83+
}
84+
}
85+
}

0 commit comments

Comments
 (0)