Skip to content

Commit 568c539

Browse files
authored
feat: Babbage protocol parameter updates (#752)
* update protocol parameter set from protocol param update spec * move protocol parameters protocol version type to common Fixes #745
1 parent 2d38767 commit 568c539

File tree

6 files changed

+243
-72
lines changed

6 files changed

+243
-72
lines changed

ledger/alonzo/pparams_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ func TestAlonzoProtocolParamsUpdate(t *testing.T) {
124124
}
125125
}
126126

127-
func TestShelleyProtocolParamsUpdateFromGenesis(t *testing.T) {
127+
func TestAlonzoProtocolParamsUpdateFromGenesis(t *testing.T) {
128128
testDefs := []struct {
129129
startParams alonzo.AlonzoProtocolParameters
130130
genesisJson string

ledger/babbage/babbage.go

Lines changed: 0 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -656,70 +656,6 @@ func (t *BabbageTransaction) Utxorpc() *utxorpc.Tx {
656656
return t.Body.Utxorpc()
657657
}
658658

659-
// BabbageProtocolParameters represents the current Babbage protocol parameters as seen in local-state-query
660-
type BabbageProtocolParameters struct {
661-
cbor.StructAsArray
662-
MinFeeA uint
663-
MinFeeB uint
664-
MaxBlockBodySize uint
665-
MaxTxSize uint
666-
MaxBlockHeaderSize uint
667-
KeyDeposit uint
668-
PoolDeposit uint
669-
MaxEpoch uint
670-
NOpt uint
671-
A0 *cbor.Rat
672-
Rho *cbor.Rat
673-
Tau *cbor.Rat
674-
ProtocolMajor uint
675-
ProtocolMinor uint
676-
MinPoolCost uint
677-
AdaPerUtxoByte uint
678-
CostModels map[uint][]int
679-
ExecutionUnitPrices []*cbor.Rat // [priceMemory priceSteps]
680-
MaxTxExecutionUnits []uint
681-
MaxBlockExecutionUnits []uint
682-
MaxValueSize uint
683-
CollateralPercentage uint
684-
MaxCollateralInputs uint
685-
}
686-
687-
type BabbageProtocolParameterUpdate struct {
688-
cbor.DecodeStoreCbor
689-
MinFeeA uint `cbor:"0,keyasint"`
690-
MinFeeB uint `cbor:"1,keyasint"`
691-
MaxBlockBodySize uint `cbor:"2,keyasint"`
692-
MaxTxSize uint `cbor:"3,keyasint"`
693-
MaxBlockHeaderSize uint `cbor:"4,keyasint"`
694-
KeyDeposit uint `cbor:"5,keyasint"`
695-
PoolDeposit uint `cbor:"6,keyasint"`
696-
MaxEpoch uint `cbor:"7,keyasint"`
697-
NOpt uint `cbor:"8,keyasint"`
698-
A0 *cbor.Rat `cbor:"9,keyasint"`
699-
Rho *cbor.Rat `cbor:"10,keyasint"`
700-
Tau *cbor.Rat `cbor:"11,keyasint"`
701-
ProtocolVersion struct {
702-
cbor.StructAsArray
703-
Major uint
704-
Minor uint
705-
} `cbor:"14,keyasint"`
706-
MinPoolCost uint `cbor:"16,keyasint"`
707-
AdaPerUtxoByte uint `cbor:"17,keyasint"`
708-
CostModels map[uint][]int `cbor:"18,keyasint"`
709-
ExecutionUnitPrices []*cbor.Rat `cbor:"19,keyasint"`
710-
MaxTxExecutionUnits []uint `cbor:"20,keyasint"`
711-
MaxBlockExecutionUnits []uint `cbor:"21,keyasint"`
712-
MaxValueSize uint `cbor:"22,keyasint"`
713-
CollateralPercentage uint `cbor:"23,keyasint"`
714-
MaxCollateralInputs uint `cbor:"24,keyasint"`
715-
}
716-
717-
func (BabbageProtocolParameterUpdate) IsProtocolParameterUpdate() {}
718-
719-
func (u *BabbageProtocolParameterUpdate) UnmarshalCBOR(data []byte) error {
720-
return u.UnmarshalCbor(data, u)
721-
}
722-
723659
func NewBabbageBlockFromCbor(data []byte) (*BabbageBlock, error) {
724660
var babbageBlock BabbageBlock
725661
if _, err := cbor.Decode(data, &babbageBlock); err != nil {

ledger/babbage/pparams.go

Lines changed: 150 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,150 @@
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 babbage
16+
17+
import (
18+
"github.com/blinklabs-io/gouroboros/cbor"
19+
"github.com/blinklabs-io/gouroboros/ledger/common"
20+
)
21+
22+
// BabbageProtocolParameters represents the current Babbage protocol parameters as seen in local-state-query
23+
type BabbageProtocolParameters struct {
24+
cbor.StructAsArray
25+
MinFeeA uint
26+
MinFeeB uint
27+
MaxBlockBodySize uint
28+
MaxTxSize uint
29+
MaxBlockHeaderSize uint
30+
KeyDeposit uint
31+
PoolDeposit uint
32+
MaxEpoch uint
33+
NOpt uint
34+
A0 *cbor.Rat
35+
Rho *cbor.Rat
36+
Tau *cbor.Rat
37+
ProtocolMajor uint
38+
ProtocolMinor uint
39+
MinPoolCost uint64
40+
AdaPerUtxoByte uint64
41+
CostModels map[uint][]uint64
42+
ExecutionCosts common.ExUnitPrice
43+
MaxTxExUnits common.ExUnit
44+
MaxBlockExUnits common.ExUnit
45+
MaxValueSize uint
46+
CollateralPercentage uint
47+
MaxCollateralInputs uint
48+
}
49+
50+
func (p *BabbageProtocolParameters) Update(paramUpdate *BabbageProtocolParameterUpdate) {
51+
if paramUpdate.MinFeeA != nil {
52+
p.MinFeeA = *paramUpdate.MinFeeA
53+
}
54+
if paramUpdate.MinFeeB != nil {
55+
p.MinFeeB = *paramUpdate.MinFeeB
56+
}
57+
if paramUpdate.MaxBlockBodySize != nil {
58+
p.MaxBlockBodySize = *paramUpdate.MaxBlockBodySize
59+
}
60+
if paramUpdate.MaxTxSize != nil {
61+
p.MaxTxSize = *paramUpdate.MaxTxSize
62+
}
63+
if paramUpdate.MaxBlockHeaderSize != nil {
64+
p.MaxBlockHeaderSize = *paramUpdate.MaxBlockHeaderSize
65+
}
66+
if paramUpdate.KeyDeposit != nil {
67+
p.KeyDeposit = *paramUpdate.KeyDeposit
68+
}
69+
if paramUpdate.PoolDeposit != nil {
70+
p.PoolDeposit = *paramUpdate.PoolDeposit
71+
}
72+
if paramUpdate.MaxEpoch != nil {
73+
p.MaxEpoch = *paramUpdate.MaxEpoch
74+
}
75+
if paramUpdate.NOpt != nil {
76+
p.NOpt = *paramUpdate.NOpt
77+
}
78+
if paramUpdate.A0 != nil {
79+
p.A0 = paramUpdate.A0
80+
}
81+
if paramUpdate.Rho != nil {
82+
p.Rho = paramUpdate.Rho
83+
}
84+
if paramUpdate.Tau != nil {
85+
p.Tau = paramUpdate.Tau
86+
}
87+
if paramUpdate.ProtocolVersion != nil {
88+
p.ProtocolMajor = paramUpdate.ProtocolVersion.Major
89+
p.ProtocolMinor = paramUpdate.ProtocolVersion.Minor
90+
}
91+
if paramUpdate.MinPoolCost != nil {
92+
p.MinPoolCost = *paramUpdate.MinPoolCost
93+
}
94+
if paramUpdate.AdaPerUtxoByte != nil {
95+
p.AdaPerUtxoByte = *paramUpdate.AdaPerUtxoByte
96+
}
97+
if paramUpdate.CostModels != nil {
98+
p.CostModels = paramUpdate.CostModels
99+
}
100+
if paramUpdate.ExecutionCosts != nil {
101+
p.ExecutionCosts = *paramUpdate.ExecutionCosts
102+
}
103+
if paramUpdate.MaxTxExUnits != nil {
104+
p.MaxTxExUnits = *paramUpdate.MaxTxExUnits
105+
}
106+
if paramUpdate.MaxBlockExUnits != nil {
107+
p.MaxBlockExUnits = *paramUpdate.MaxBlockExUnits
108+
}
109+
if paramUpdate.MaxValueSize != nil {
110+
p.MaxValueSize = *paramUpdate.MaxValueSize
111+
}
112+
if paramUpdate.CollateralPercentage != nil {
113+
p.CollateralPercentage = *paramUpdate.CollateralPercentage
114+
}
115+
if paramUpdate.MaxCollateralInputs != nil {
116+
p.MaxCollateralInputs = *paramUpdate.MaxCollateralInputs
117+
}
118+
}
119+
120+
type BabbageProtocolParameterUpdate struct {
121+
cbor.DecodeStoreCbor
122+
MinFeeA *uint `cbor:"0,keyasint"`
123+
MinFeeB *uint `cbor:"1,keyasint"`
124+
MaxBlockBodySize *uint `cbor:"2,keyasint"`
125+
MaxTxSize *uint `cbor:"3,keyasint"`
126+
MaxBlockHeaderSize *uint `cbor:"4,keyasint"`
127+
KeyDeposit *uint `cbor:"5,keyasint"`
128+
PoolDeposit *uint `cbor:"6,keyasint"`
129+
MaxEpoch *uint `cbor:"7,keyasint"`
130+
NOpt *uint `cbor:"8,keyasint"`
131+
A0 *cbor.Rat `cbor:"9,keyasint"`
132+
Rho *cbor.Rat `cbor:"10,keyasint"`
133+
Tau *cbor.Rat `cbor:"11,keyasint"`
134+
ProtocolVersion *common.ProtocolParametersProtocolVersion `cbor:"14,keyasint"`
135+
MinPoolCost *uint64 `cbor:"16,keyasint"`
136+
AdaPerUtxoByte *uint64 `cbor:"17,keyasint"`
137+
CostModels map[uint][]uint64 `cbor:"18,keyasint"`
138+
ExecutionCosts *common.ExUnitPrice `cbor:"19,keyasint"`
139+
MaxTxExUnits *common.ExUnit `cbor:"20,keyasint"`
140+
MaxBlockExUnits *common.ExUnit `cbor:"21,keyasint"`
141+
MaxValueSize *uint `cbor:"22,keyasint"`
142+
CollateralPercentage *uint `cbor:"23,keyasint"`
143+
MaxCollateralInputs *uint `cbor:"24,keyasint"`
144+
}
145+
146+
func (BabbageProtocolParameterUpdate) IsProtocolParameterUpdate() {}
147+
148+
func (u *BabbageProtocolParameterUpdate) UnmarshalCBOR(data []byte) error {
149+
return u.UnmarshalCbor(data, u)
150+
}

ledger/babbage/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 babbage_test
16+
17+
import (
18+
"encoding/hex"
19+
"reflect"
20+
"testing"
21+
22+
"github.com/blinklabs-io/gouroboros/cbor"
23+
"github.com/blinklabs-io/gouroboros/ledger/babbage"
24+
"github.com/blinklabs-io/gouroboros/ledger/common"
25+
)
26+
27+
func TestBabbageProtocolParamsUpdate(t *testing.T) {
28+
testDefs := []struct {
29+
startParams babbage.BabbageProtocolParameters
30+
updateCbor string
31+
expectedParams babbage.BabbageProtocolParameters
32+
}{
33+
{
34+
startParams: babbage.BabbageProtocolParameters{
35+
ProtocolMajor: 7,
36+
},
37+
updateCbor: "a10e820800",
38+
expectedParams: babbage.BabbageProtocolParameters{
39+
ProtocolMajor: 8,
40+
},
41+
},
42+
{
43+
startParams: babbage.BabbageProtocolParameters{
44+
MaxBlockBodySize: 1,
45+
MaxTxExUnits: common.ExUnit{
46+
Mem: 1,
47+
Steps: 1,
48+
},
49+
},
50+
updateCbor: "a2021a0001200014821a00aba9501b00000002540be400",
51+
expectedParams: babbage.BabbageProtocolParameters{
52+
MaxBlockBodySize: 73728,
53+
MaxTxExUnits: common.ExUnit{
54+
Mem: 11250000,
55+
Steps: 10000000000,
56+
},
57+
},
58+
},
59+
{
60+
startParams: babbage.BabbageProtocolParameters{},
61+
updateCbor: "a112a20098a61a0003236119032c01011903e819023b00011903e8195e7104011903e818201a0001ca761928eb041959d818641959d818641959d818641959d818641959d818641959d81864186418641959d81864194c5118201a0002acfa182019b551041a000363151901ff00011a00015c3518201a000797751936f404021a0002ff941a0006ea7818dc0001011903e8196ff604021a0003bd081a00034ec5183e011a00102e0f19312a011a00032e801901a5011a0002da781903e819cf06011a00013a34182019a8f118201903e818201a00013aac0119e143041903e80a1a00030219189c011a00030219189c011a0003207c1901d9011a000330001901ff0119ccf3182019fd40182019ffd5182019581e18201940b318201a00012adf18201a0002ff941a0006ea7818dc0001011a00010f92192da7000119eabb18201a0002ff941a0006ea7818dc0001011a0002ff941a0006ea7818dc0001011a000c504e197712041a001d6af61a0001425b041a00040c660004001a00014fab18201a0003236119032c010119a0de18201a00033d7618201979f41820197fb8182019a95d1820197df718201995aa18201a009063b91903fd0a0198af1a0003236119032c01011903e819023b00011903e8195e7104011903e818201a0001ca761928eb041959d818641959d818641959d818641959d818641959d818641959d81864186418641959d81864194c5118201a0002acfa182019b551041a000363151901ff00011a00015c3518201a000797751936f404021a0002ff941a0006ea7818dc0001011903e8196ff604021a0003bd081a00034ec5183e011a00102e0f19312a011a00032e801901a5011a0002da781903e819cf06011a00013a34182019a8f118201903e818201a00013aac0119e143041903e80a1a00030219189c011a00030219189c011a0003207c1901d9011a000330001901ff0119ccf3182019fd40182019ffd5182019581e18201940b318201a00012adf18201a0002ff941a0006ea7818dc0001011a00010f92192da7000119eabb18201a0002ff941a0006ea7818dc0001011a0002ff941a0006ea7818dc0001011a0011b22c1a0005fdde00021a000c504e197712041a001d6af61a0001425b041a00040c660004001a00014fab18201a0003236119032c010119a0de18201a00033d7618201979f41820197fb8182019a95d1820197df718201995aa18201b00000004a817c8001b00000004a817c8001a009063b91903fd0a1b00000004a817c800001b00000004a817c800",
62+
expectedParams: babbage.BabbageProtocolParameters{
63+
CostModels: map[uint][]uint64{
64+
0: []uint64{0x32361, 0x32c, 0x1, 0x1, 0x3e8, 0x23b, 0x0, 0x1, 0x3e8, 0x5e71, 0x4, 0x1, 0x3e8, 0x20, 0x1ca76, 0x28eb, 0x4, 0x59d8, 0x64, 0x59d8, 0x64, 0x59d8, 0x64, 0x59d8, 0x64, 0x59d8, 0x64, 0x59d8, 0x64, 0x64, 0x64, 0x59d8, 0x64, 0x4c51, 0x20, 0x2acfa, 0x20, 0xb551, 0x4, 0x36315, 0x1ff, 0x0, 0x1, 0x15c35, 0x20, 0x79775, 0x36f4, 0x4, 0x2, 0x2ff94, 0x6ea78, 0xdc, 0x0, 0x1, 0x1, 0x3e8, 0x6ff6, 0x4, 0x2, 0x3bd08, 0x34ec5, 0x3e, 0x1, 0x102e0f, 0x312a, 0x1, 0x32e80, 0x1a5, 0x1, 0x2da78, 0x3e8, 0xcf06, 0x1, 0x13a34, 0x20, 0xa8f1, 0x20, 0x3e8, 0x20, 0x13aac, 0x1, 0xe143, 0x4, 0x3e8, 0xa, 0x30219, 0x9c, 0x1, 0x30219, 0x9c, 0x1, 0x3207c, 0x1d9, 0x1, 0x33000, 0x1ff, 0x1, 0xccf3, 0x20, 0xfd40, 0x20, 0xffd5, 0x20, 0x581e, 0x20, 0x40b3, 0x20, 0x12adf, 0x20, 0x2ff94, 0x6ea78, 0xdc, 0x0, 0x1, 0x1, 0x10f92, 0x2da7, 0x0, 0x1, 0xeabb, 0x20, 0x2ff94, 0x6ea78, 0xdc, 0x0, 0x1, 0x1, 0x2ff94, 0x6ea78, 0xdc, 0x0, 0x1, 0x1, 0xc504e, 0x7712, 0x4, 0x1d6af6, 0x1425b, 0x4, 0x40c66, 0x0, 0x4, 0x0, 0x14fab, 0x20, 0x32361, 0x32c, 0x1, 0x1, 0xa0de, 0x20, 0x33d76, 0x20, 0x79f4, 0x20, 0x7fb8, 0x20, 0xa95d, 0x20, 0x7df7, 0x20, 0x95aa, 0x20, 0x9063b9, 0x3fd, 0xa},
65+
1: []uint64{0x32361, 0x32c, 0x1, 0x1, 0x3e8, 0x23b, 0x0, 0x1, 0x3e8, 0x5e71, 0x4, 0x1, 0x3e8, 0x20, 0x1ca76, 0x28eb, 0x4, 0x59d8, 0x64, 0x59d8, 0x64, 0x59d8, 0x64, 0x59d8, 0x64, 0x59d8, 0x64, 0x59d8, 0x64, 0x64, 0x64, 0x59d8, 0x64, 0x4c51, 0x20, 0x2acfa, 0x20, 0xb551, 0x4, 0x36315, 0x1ff, 0x0, 0x1, 0x15c35, 0x20, 0x79775, 0x36f4, 0x4, 0x2, 0x2ff94, 0x6ea78, 0xdc, 0x0, 0x1, 0x1, 0x3e8, 0x6ff6, 0x4, 0x2, 0x3bd08, 0x34ec5, 0x3e, 0x1, 0x102e0f, 0x312a, 0x1, 0x32e80, 0x1a5, 0x1, 0x2da78, 0x3e8, 0xcf06, 0x1, 0x13a34, 0x20, 0xa8f1, 0x20, 0x3e8, 0x20, 0x13aac, 0x1, 0xe143, 0x4, 0x3e8, 0xa, 0x30219, 0x9c, 0x1, 0x30219, 0x9c, 0x1, 0x3207c, 0x1d9, 0x1, 0x33000, 0x1ff, 0x1, 0xccf3, 0x20, 0xfd40, 0x20, 0xffd5, 0x20, 0x581e, 0x20, 0x40b3, 0x20, 0x12adf, 0x20, 0x2ff94, 0x6ea78, 0xdc, 0x0, 0x1, 0x1, 0x10f92, 0x2da7, 0x0, 0x1, 0xeabb, 0x20, 0x2ff94, 0x6ea78, 0xdc, 0x0, 0x1, 0x1, 0x2ff94, 0x6ea78, 0xdc, 0x0, 0x1, 0x1, 0x11b22c, 0x5fdde, 0x0, 0x2, 0xc504e, 0x7712, 0x4, 0x1d6af6, 0x1425b, 0x4, 0x40c66, 0x0, 0x4, 0x0, 0x14fab, 0x20, 0x32361, 0x32c, 0x1, 0x1, 0xa0de, 0x20, 0x33d76, 0x20, 0x79f4, 0x20, 0x7fb8, 0x20, 0xa95d, 0x20, 0x7df7, 0x20, 0x95aa, 0x20, 0x4a817c800, 0x4a817c800, 0x9063b9, 0x3fd, 0xa, 0x4a817c800, 0x0, 0x4a817c800},
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 babbage.BabbageProtocolParameterUpdate
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+
}

ledger/common/pparams.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ type ProtocolParameterUpdate interface {
2525
Cbor() []byte
2626
}
2727

28+
type ProtocolParametersProtocolVersion struct {
29+
cbor.StructAsArray
30+
Major uint
31+
Minor uint
32+
}
33+
2834
const (
2935
NonceType0 = 0
3036
NonceType1 = 1

ledger/shelley/pparams.go

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -124,12 +124,6 @@ func (p *ShelleyProtocolParameters) UpdateFromGenesis(genesis *ShelleyGenesis) {
124124
//p.Nonce *cbor.Rat
125125
}
126126

127-
type ShelleyProtocolParametersProtocolVersion struct {
128-
cbor.StructAsArray
129-
Major uint
130-
Minor uint
131-
}
132-
133127
type ShelleyProtocolParameterUpdate struct {
134128
cbor.DecodeStoreCbor
135129
MinFeeA *uint `cbor:"0,keyasint"`
@@ -146,7 +140,7 @@ type ShelleyProtocolParameterUpdate struct {
146140
Tau *cbor.Rat `cbor:"11,keyasint"`
147141
Decentralization *cbor.Rat `cbor:"12,keyasint"`
148142
Nonce *common.Nonce `cbor:"13,keyasint"`
149-
ProtocolVersion *ShelleyProtocolParametersProtocolVersion `cbor:"14,keyasint"`
143+
ProtocolVersion *common.ProtocolParametersProtocolVersion `cbor:"14,keyasint"`
150144
MinUtxoValue *uint `cbor:"15,keyasint"`
151145
}
152146

0 commit comments

Comments
 (0)