Skip to content

Commit eb7b196

Browse files
authored
feat: Allegra protocol param updates (#747)
* update protocol parameter set from protocol param update spec * move protocol params Nonce type to common Fixes #742
1 parent 4e361c5 commit eb7b196

File tree

7 files changed

+210
-84
lines changed

7 files changed

+210
-84
lines changed

ledger/allegra/allegra.go

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -308,14 +308,6 @@ func (t *AllegraTransaction) Cbor() []byte {
308308
return cborData
309309
}
310310

311-
type AllegraProtocolParameters struct {
312-
shelley.ShelleyProtocolParameters
313-
}
314-
315-
type AllegraProtocolParameterUpdate struct {
316-
shelley.ShelleyProtocolParameterUpdate
317-
}
318-
319311
func NewAllegraBlockFromCbor(data []byte) (*AllegraBlock, error) {
320312
var allegraBlock AllegraBlock
321313
if _, err := cbor.Decode(data, &allegraBlock); err != nil {

ledger/allegra/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 allegra
16+
17+
import "github.com/blinklabs-io/gouroboros/ledger/shelley"
18+
19+
type AllegraProtocolParameters struct {
20+
shelley.ShelleyProtocolParameters
21+
}
22+
23+
func (p *AllegraProtocolParameters) Update(paramUpdate *AllegraProtocolParameterUpdate) {
24+
p.ShelleyProtocolParameters.Update(
25+
&paramUpdate.ShelleyProtocolParameterUpdate,
26+
)
27+
}
28+
29+
type AllegraProtocolParameterUpdate struct {
30+
shelley.ShelleyProtocolParameterUpdate
31+
}

ledger/allegra/pparams_test.go

Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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 allegra_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/shelley"
26+
)
27+
28+
func TestAllegraProtocolParamsUpdate(t *testing.T) {
29+
testDefs := []struct {
30+
startParams allegra.AllegraProtocolParameters
31+
updateCbor string
32+
expectedParams allegra.AllegraProtocolParameters
33+
}{
34+
{
35+
startParams: allegra.AllegraProtocolParameters{
36+
ShelleyProtocolParameters: shelley.ShelleyProtocolParameters{
37+
Decentralization: &cbor.Rat{Rat: new(big.Rat).SetInt64(1)},
38+
},
39+
},
40+
updateCbor: "a10cd81e82090a",
41+
expectedParams: allegra.AllegraProtocolParameters{
42+
ShelleyProtocolParameters: shelley.ShelleyProtocolParameters{
43+
Decentralization: &cbor.Rat{Rat: big.NewRat(9, 10)},
44+
},
45+
},
46+
},
47+
{
48+
startParams: allegra.AllegraProtocolParameters{
49+
ShelleyProtocolParameters: shelley.ShelleyProtocolParameters{
50+
ProtocolMajor: 3,
51+
},
52+
},
53+
updateCbor: "a10e820400",
54+
expectedParams: allegra.AllegraProtocolParameters{
55+
ShelleyProtocolParameters: shelley.ShelleyProtocolParameters{
56+
ProtocolMajor: 4,
57+
},
58+
},
59+
},
60+
}
61+
for _, testDef := range testDefs {
62+
cborBytes, err := hex.DecodeString(testDef.updateCbor)
63+
if err != nil {
64+
t.Fatalf("unexpected error: %s", err)
65+
}
66+
var tmpUpdate allegra.AllegraProtocolParameterUpdate
67+
if _, err := cbor.Decode(cborBytes, &tmpUpdate); err != nil {
68+
t.Fatalf("unexpected error: %s", err)
69+
}
70+
tmpParams := testDef.startParams
71+
tmpParams.Update(&tmpUpdate)
72+
if !reflect.DeepEqual(tmpParams, testDef.expectedParams) {
73+
t.Fatalf("did not get expected params:\n got: %#v\n wanted: %#v", tmpParams, testDef.expectedParams)
74+
}
75+
}
76+
}

ledger/common/pparams.go

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,50 @@
1414

1515
package common
1616

17+
import (
18+
"fmt"
19+
20+
"github.com/blinklabs-io/gouroboros/cbor"
21+
)
22+
1723
type ProtocolParameterUpdate interface {
1824
IsProtocolParameterUpdate()
1925
Cbor() []byte
2026
}
27+
28+
const (
29+
NonceType0 = 0
30+
NonceType1 = 1
31+
)
32+
33+
var NeutralNonce = Nonce{
34+
Type: NonceType0,
35+
}
36+
37+
type Nonce struct {
38+
cbor.StructAsArray
39+
Type uint
40+
Value [32]byte
41+
}
42+
43+
func (n *Nonce) UnmarshalCBOR(data []byte) error {
44+
nonceType, err := cbor.DecodeIdFromList(data)
45+
if err != nil {
46+
return err
47+
}
48+
49+
n.Type = uint(nonceType)
50+
51+
switch nonceType {
52+
case NonceType0:
53+
// Value uses default value
54+
case NonceType1:
55+
if err := cbor.DecodeGeneric(data, n); err != nil {
56+
fmt.Printf("Nonce decode error: %+v\n", data)
57+
return err
58+
}
59+
default:
60+
return fmt.Errorf("unsupported nonce type %d", nonceType)
61+
}
62+
return nil
63+
}

ledger/common/pparams_test.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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 common_test
16+
17+
import (
18+
"testing"
19+
20+
"github.com/blinklabs-io/gouroboros/ledger/common"
21+
)
22+
23+
func TestNonceUnmarshalCBOR(t *testing.T) {
24+
testCases := []struct {
25+
name string
26+
data []byte
27+
expectedErr string
28+
}{
29+
{
30+
name: "NonceType0",
31+
data: []byte{0x81, 0x00},
32+
},
33+
{
34+
name: "NonceType1",
35+
data: []byte{0x82, 0x01, 0x42, 0x01, 0x02},
36+
},
37+
{
38+
name: "UnsupportedNonceType",
39+
data: []byte{0x82, 0x02},
40+
expectedErr: "unsupported nonce type 2",
41+
},
42+
}
43+
44+
for _, tc := range testCases {
45+
t.Run(tc.name, func(t *testing.T) {
46+
n := &common.Nonce{}
47+
err := n.UnmarshalCBOR(tc.data)
48+
if err != nil {
49+
if tc.expectedErr == "" || err.Error() != tc.expectedErr {
50+
t.Errorf("unexpected error: %v", err)
51+
}
52+
} else if tc.expectedErr != "" {
53+
t.Errorf("expected error: %v, got nil", tc.expectedErr)
54+
}
55+
})
56+
}
57+
}

ledger/shelley/pparams.go

Lines changed: 3 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -15,10 +15,10 @@
1515
package shelley
1616

1717
import (
18-
"fmt"
1918
"math/big"
2019

2120
"github.com/blinklabs-io/gouroboros/cbor"
21+
"github.com/blinklabs-io/gouroboros/ledger/common"
2222
)
2323

2424
type ShelleyProtocolParameters struct {
@@ -36,7 +36,7 @@ type ShelleyProtocolParameters struct {
3636
Rho *cbor.Rat
3737
Tau *cbor.Rat
3838
Decentralization *cbor.Rat
39-
Nonce *Nonce
39+
Nonce *common.Nonce
4040
ProtocolMajor uint
4141
ProtocolMinor uint
4242
MinUtxoValue uint
@@ -145,7 +145,7 @@ type ShelleyProtocolParameterUpdate struct {
145145
Rho *cbor.Rat `cbor:"10,keyasint"`
146146
Tau *cbor.Rat `cbor:"11,keyasint"`
147147
Decentralization *cbor.Rat `cbor:"12,keyasint"`
148-
Nonce *Nonce `cbor:"13,keyasint"`
148+
Nonce *common.Nonce `cbor:"13,keyasint"`
149149
ProtocolVersion *ShelleyProtocolParametersProtocolVersion `cbor:"14,keyasint"`
150150
MinUtxoValue *uint `cbor:"15,keyasint"`
151151
}
@@ -155,40 +155,3 @@ func (ShelleyProtocolParameterUpdate) IsProtocolParameterUpdate() {}
155155
func (u *ShelleyProtocolParameterUpdate) UnmarshalCBOR(data []byte) error {
156156
return u.UnmarshalCbor(data, u)
157157
}
158-
159-
const (
160-
NonceType0 = 0
161-
NonceType1 = 1
162-
)
163-
164-
var NeutralNonce = Nonce{
165-
Type: NonceType0,
166-
}
167-
168-
type Nonce struct {
169-
cbor.StructAsArray
170-
Type uint
171-
Value [32]byte
172-
}
173-
174-
func (n *Nonce) UnmarshalCBOR(data []byte) error {
175-
nonceType, err := cbor.DecodeIdFromList(data)
176-
if err != nil {
177-
return err
178-
}
179-
180-
n.Type = uint(nonceType)
181-
182-
switch nonceType {
183-
case NonceType0:
184-
// Value uses default value
185-
case NonceType1:
186-
if err := cbor.DecodeGeneric(data, n); err != nil {
187-
fmt.Printf("Nonce decode error: %+v\n", data)
188-
return err
189-
}
190-
default:
191-
return fmt.Errorf("unsupported nonce type %d", nonceType)
192-
}
193-
return nil
194-
}

ledger/shelley/pparams_test.go

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -25,42 +25,6 @@ import (
2525
"github.com/blinklabs-io/gouroboros/ledger/shelley"
2626
)
2727

28-
func TestNonceUnmarshalCBOR(t *testing.T) {
29-
testCases := []struct {
30-
name string
31-
data []byte
32-
expectedErr string
33-
}{
34-
{
35-
name: "NonceType0",
36-
data: []byte{0x81, 0x00},
37-
},
38-
{
39-
name: "NonceType1",
40-
data: []byte{0x82, 0x01, 0x42, 0x01, 0x02},
41-
},
42-
{
43-
name: "UnsupportedNonceType",
44-
data: []byte{0x82, 0x02},
45-
expectedErr: "unsupported nonce type 2",
46-
},
47-
}
48-
49-
for _, tc := range testCases {
50-
t.Run(tc.name, func(t *testing.T) {
51-
n := &shelley.Nonce{}
52-
err := n.UnmarshalCBOR(tc.data)
53-
if err != nil {
54-
if tc.expectedErr == "" || err.Error() != tc.expectedErr {
55-
t.Errorf("unexpected error: %v", err)
56-
}
57-
} else if tc.expectedErr != "" {
58-
t.Errorf("expected error: %v, got nil", tc.expectedErr)
59-
}
60-
})
61-
}
62-
}
63-
6428
func TestShelleyProtocolParamsUpdate(t *testing.T) {
6529
testDefs := []struct {
6630
startParams shelley.ShelleyProtocolParameters

0 commit comments

Comments
 (0)