Skip to content

Commit

Permalink
feat: Babbage protocol parameter updates (#752)
Browse files Browse the repository at this point in the history
* update protocol parameter set from protocol param update spec
* move protocol parameters protocol version type to common

Fixes #745
  • Loading branch information
agaffney authored Oct 10, 2024
1 parent 2d38767 commit 568c539
Show file tree
Hide file tree
Showing 6 changed files with 243 additions and 72 deletions.
2 changes: 1 addition & 1 deletion ledger/alonzo/pparams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ func TestAlonzoProtocolParamsUpdate(t *testing.T) {
}
}

func TestShelleyProtocolParamsUpdateFromGenesis(t *testing.T) {
func TestAlonzoProtocolParamsUpdateFromGenesis(t *testing.T) {
testDefs := []struct {
startParams alonzo.AlonzoProtocolParameters
genesisJson string
Expand Down
64 changes: 0 additions & 64 deletions ledger/babbage/babbage.go
Original file line number Diff line number Diff line change
Expand Up @@ -656,70 +656,6 @@ func (t *BabbageTransaction) Utxorpc() *utxorpc.Tx {
return t.Body.Utxorpc()
}

// BabbageProtocolParameters represents the current Babbage protocol parameters as seen in local-state-query
type BabbageProtocolParameters struct {
cbor.StructAsArray
MinFeeA uint
MinFeeB uint
MaxBlockBodySize uint
MaxTxSize uint
MaxBlockHeaderSize uint
KeyDeposit uint
PoolDeposit uint
MaxEpoch uint
NOpt uint
A0 *cbor.Rat
Rho *cbor.Rat
Tau *cbor.Rat
ProtocolMajor uint
ProtocolMinor uint
MinPoolCost uint
AdaPerUtxoByte uint
CostModels map[uint][]int
ExecutionUnitPrices []*cbor.Rat // [priceMemory priceSteps]
MaxTxExecutionUnits []uint
MaxBlockExecutionUnits []uint
MaxValueSize uint
CollateralPercentage uint
MaxCollateralInputs uint
}

type BabbageProtocolParameterUpdate struct {
cbor.DecodeStoreCbor
MinFeeA uint `cbor:"0,keyasint"`
MinFeeB uint `cbor:"1,keyasint"`
MaxBlockBodySize uint `cbor:"2,keyasint"`
MaxTxSize uint `cbor:"3,keyasint"`
MaxBlockHeaderSize uint `cbor:"4,keyasint"`
KeyDeposit uint `cbor:"5,keyasint"`
PoolDeposit uint `cbor:"6,keyasint"`
MaxEpoch uint `cbor:"7,keyasint"`
NOpt uint `cbor:"8,keyasint"`
A0 *cbor.Rat `cbor:"9,keyasint"`
Rho *cbor.Rat `cbor:"10,keyasint"`
Tau *cbor.Rat `cbor:"11,keyasint"`
ProtocolVersion struct {
cbor.StructAsArray
Major uint
Minor uint
} `cbor:"14,keyasint"`
MinPoolCost uint `cbor:"16,keyasint"`
AdaPerUtxoByte uint `cbor:"17,keyasint"`
CostModels map[uint][]int `cbor:"18,keyasint"`
ExecutionUnitPrices []*cbor.Rat `cbor:"19,keyasint"`
MaxTxExecutionUnits []uint `cbor:"20,keyasint"`
MaxBlockExecutionUnits []uint `cbor:"21,keyasint"`
MaxValueSize uint `cbor:"22,keyasint"`
CollateralPercentage uint `cbor:"23,keyasint"`
MaxCollateralInputs uint `cbor:"24,keyasint"`
}

func (BabbageProtocolParameterUpdate) IsProtocolParameterUpdate() {}

func (u *BabbageProtocolParameterUpdate) UnmarshalCBOR(data []byte) error {
return u.UnmarshalCbor(data, u)
}

func NewBabbageBlockFromCbor(data []byte) (*BabbageBlock, error) {
var babbageBlock BabbageBlock
if _, err := cbor.Decode(data, &babbageBlock); err != nil {
Expand Down
150 changes: 150 additions & 0 deletions ledger/babbage/pparams.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,150 @@
// 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 babbage

import (
"github.com/blinklabs-io/gouroboros/cbor"
"github.com/blinklabs-io/gouroboros/ledger/common"
)

// BabbageProtocolParameters represents the current Babbage protocol parameters as seen in local-state-query
type BabbageProtocolParameters struct {
cbor.StructAsArray
MinFeeA uint
MinFeeB uint
MaxBlockBodySize uint
MaxTxSize uint
MaxBlockHeaderSize uint
KeyDeposit uint
PoolDeposit uint
MaxEpoch uint
NOpt uint
A0 *cbor.Rat
Rho *cbor.Rat
Tau *cbor.Rat
ProtocolMajor uint
ProtocolMinor uint
MinPoolCost uint64
AdaPerUtxoByte uint64
CostModels map[uint][]uint64
ExecutionCosts common.ExUnitPrice
MaxTxExUnits common.ExUnit
MaxBlockExUnits common.ExUnit
MaxValueSize uint
CollateralPercentage uint
MaxCollateralInputs uint
}

func (p *BabbageProtocolParameters) Update(paramUpdate *BabbageProtocolParameterUpdate) {
if paramUpdate.MinFeeA != nil {
p.MinFeeA = *paramUpdate.MinFeeA
}
if paramUpdate.MinFeeB != nil {
p.MinFeeB = *paramUpdate.MinFeeB
}
if paramUpdate.MaxBlockBodySize != nil {
p.MaxBlockBodySize = *paramUpdate.MaxBlockBodySize
}
if paramUpdate.MaxTxSize != nil {
p.MaxTxSize = *paramUpdate.MaxTxSize
}
if paramUpdate.MaxBlockHeaderSize != nil {
p.MaxBlockHeaderSize = *paramUpdate.MaxBlockHeaderSize
}
if paramUpdate.KeyDeposit != nil {
p.KeyDeposit = *paramUpdate.KeyDeposit
}
if paramUpdate.PoolDeposit != nil {
p.PoolDeposit = *paramUpdate.PoolDeposit
}
if paramUpdate.MaxEpoch != nil {
p.MaxEpoch = *paramUpdate.MaxEpoch
}
if paramUpdate.NOpt != nil {
p.NOpt = *paramUpdate.NOpt
}
if paramUpdate.A0 != nil {
p.A0 = paramUpdate.A0
}
if paramUpdate.Rho != nil {
p.Rho = paramUpdate.Rho
}
if paramUpdate.Tau != nil {
p.Tau = paramUpdate.Tau
}
if paramUpdate.ProtocolVersion != nil {
p.ProtocolMajor = paramUpdate.ProtocolVersion.Major
p.ProtocolMinor = paramUpdate.ProtocolVersion.Minor
}
if paramUpdate.MinPoolCost != nil {
p.MinPoolCost = *paramUpdate.MinPoolCost
}
if paramUpdate.AdaPerUtxoByte != nil {
p.AdaPerUtxoByte = *paramUpdate.AdaPerUtxoByte
}
if paramUpdate.CostModels != nil {
p.CostModels = paramUpdate.CostModels
}
if paramUpdate.ExecutionCosts != nil {
p.ExecutionCosts = *paramUpdate.ExecutionCosts
}
if paramUpdate.MaxTxExUnits != nil {
p.MaxTxExUnits = *paramUpdate.MaxTxExUnits
}
if paramUpdate.MaxBlockExUnits != nil {
p.MaxBlockExUnits = *paramUpdate.MaxBlockExUnits
}
if paramUpdate.MaxValueSize != nil {
p.MaxValueSize = *paramUpdate.MaxValueSize
}
if paramUpdate.CollateralPercentage != nil {
p.CollateralPercentage = *paramUpdate.CollateralPercentage
}
if paramUpdate.MaxCollateralInputs != nil {
p.MaxCollateralInputs = *paramUpdate.MaxCollateralInputs
}
}

type BabbageProtocolParameterUpdate struct {
cbor.DecodeStoreCbor
MinFeeA *uint `cbor:"0,keyasint"`
MinFeeB *uint `cbor:"1,keyasint"`
MaxBlockBodySize *uint `cbor:"2,keyasint"`
MaxTxSize *uint `cbor:"3,keyasint"`
MaxBlockHeaderSize *uint `cbor:"4,keyasint"`
KeyDeposit *uint `cbor:"5,keyasint"`
PoolDeposit *uint `cbor:"6,keyasint"`
MaxEpoch *uint `cbor:"7,keyasint"`
NOpt *uint `cbor:"8,keyasint"`
A0 *cbor.Rat `cbor:"9,keyasint"`
Rho *cbor.Rat `cbor:"10,keyasint"`
Tau *cbor.Rat `cbor:"11,keyasint"`
ProtocolVersion *common.ProtocolParametersProtocolVersion `cbor:"14,keyasint"`
MinPoolCost *uint64 `cbor:"16,keyasint"`
AdaPerUtxoByte *uint64 `cbor:"17,keyasint"`
CostModels map[uint][]uint64 `cbor:"18,keyasint"`
ExecutionCosts *common.ExUnitPrice `cbor:"19,keyasint"`
MaxTxExUnits *common.ExUnit `cbor:"20,keyasint"`
MaxBlockExUnits *common.ExUnit `cbor:"21,keyasint"`
MaxValueSize *uint `cbor:"22,keyasint"`
CollateralPercentage *uint `cbor:"23,keyasint"`
MaxCollateralInputs *uint `cbor:"24,keyasint"`
}

func (BabbageProtocolParameterUpdate) IsProtocolParameterUpdate() {}

func (u *BabbageProtocolParameterUpdate) UnmarshalCBOR(data []byte) error {
return u.UnmarshalCbor(data, u)
}
85 changes: 85 additions & 0 deletions ledger/babbage/pparams_test.go
Original file line number Diff line number Diff line change
@@ -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 babbage_test

import (
"encoding/hex"
"reflect"
"testing"

"github.com/blinklabs-io/gouroboros/cbor"
"github.com/blinklabs-io/gouroboros/ledger/babbage"
"github.com/blinklabs-io/gouroboros/ledger/common"
)

func TestBabbageProtocolParamsUpdate(t *testing.T) {
testDefs := []struct {
startParams babbage.BabbageProtocolParameters
updateCbor string
expectedParams babbage.BabbageProtocolParameters
}{
{
startParams: babbage.BabbageProtocolParameters{
ProtocolMajor: 7,
},
updateCbor: "a10e820800",
expectedParams: babbage.BabbageProtocolParameters{
ProtocolMajor: 8,
},
},
{
startParams: babbage.BabbageProtocolParameters{
MaxBlockBodySize: 1,
MaxTxExUnits: common.ExUnit{
Mem: 1,
Steps: 1,
},
},
updateCbor: "a2021a0001200014821a00aba9501b00000002540be400",
expectedParams: babbage.BabbageProtocolParameters{
MaxBlockBodySize: 73728,
MaxTxExUnits: common.ExUnit{
Mem: 11250000,
Steps: 10000000000,
},
},
},
{
startParams: babbage.BabbageProtocolParameters{},
updateCbor: "a112a20098a61a0003236119032c01011903e819023b00011903e8195e7104011903e818201a0001ca761928eb041959d818641959d818641959d818641959d818641959d818641959d81864186418641959d81864194c5118201a0002acfa182019b551041a000363151901ff00011a00015c3518201a000797751936f404021a0002ff941a0006ea7818dc0001011903e8196ff604021a0003bd081a00034ec5183e011a00102e0f19312a011a00032e801901a5011a0002da781903e819cf06011a00013a34182019a8f118201903e818201a00013aac0119e143041903e80a1a00030219189c011a00030219189c011a0003207c1901d9011a000330001901ff0119ccf3182019fd40182019ffd5182019581e18201940b318201a00012adf18201a0002ff941a0006ea7818dc0001011a00010f92192da7000119eabb18201a0002ff941a0006ea7818dc0001011a0002ff941a0006ea7818dc0001011a000c504e197712041a001d6af61a0001425b041a00040c660004001a00014fab18201a0003236119032c010119a0de18201a00033d7618201979f41820197fb8182019a95d1820197df718201995aa18201a009063b91903fd0a0198af1a0003236119032c01011903e819023b00011903e8195e7104011903e818201a0001ca761928eb041959d818641959d818641959d818641959d818641959d818641959d81864186418641959d81864194c5118201a0002acfa182019b551041a000363151901ff00011a00015c3518201a000797751936f404021a0002ff941a0006ea7818dc0001011903e8196ff604021a0003bd081a00034ec5183e011a00102e0f19312a011a00032e801901a5011a0002da781903e819cf06011a00013a34182019a8f118201903e818201a00013aac0119e143041903e80a1a00030219189c011a00030219189c011a0003207c1901d9011a000330001901ff0119ccf3182019fd40182019ffd5182019581e18201940b318201a00012adf18201a0002ff941a0006ea7818dc0001011a00010f92192da7000119eabb18201a0002ff941a0006ea7818dc0001011a0002ff941a0006ea7818dc0001011a0011b22c1a0005fdde00021a000c504e197712041a001d6af61a0001425b041a00040c660004001a00014fab18201a0003236119032c010119a0de18201a00033d7618201979f41820197fb8182019a95d1820197df718201995aa18201b00000004a817c8001b00000004a817c8001a009063b91903fd0a1b00000004a817c800001b00000004a817c800",
expectedParams: babbage.BabbageProtocolParameters{
CostModels: map[uint][]uint64{
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},
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},
},
},
},
}
for _, testDef := range testDefs {
cborBytes, err := hex.DecodeString(testDef.updateCbor)
if err != nil {
t.Fatalf("unexpected error: %s", err)
}
var tmpUpdate babbage.BabbageProtocolParameterUpdate
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)
}
}
}
6 changes: 6 additions & 0 deletions ledger/common/pparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,12 @@ type ProtocolParameterUpdate interface {
Cbor() []byte
}

type ProtocolParametersProtocolVersion struct {
cbor.StructAsArray
Major uint
Minor uint
}

const (
NonceType0 = 0
NonceType1 = 1
Expand Down
8 changes: 1 addition & 7 deletions ledger/shelley/pparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,6 @@ func (p *ShelleyProtocolParameters) UpdateFromGenesis(genesis *ShelleyGenesis) {
//p.Nonce *cbor.Rat
}

type ShelleyProtocolParametersProtocolVersion struct {
cbor.StructAsArray
Major uint
Minor uint
}

type ShelleyProtocolParameterUpdate struct {
cbor.DecodeStoreCbor
MinFeeA *uint `cbor:"0,keyasint"`
Expand All @@ -146,7 +140,7 @@ type ShelleyProtocolParameterUpdate struct {
Tau *cbor.Rat `cbor:"11,keyasint"`
Decentralization *cbor.Rat `cbor:"12,keyasint"`
Nonce *common.Nonce `cbor:"13,keyasint"`
ProtocolVersion *ShelleyProtocolParametersProtocolVersion `cbor:"14,keyasint"`
ProtocolVersion *common.ProtocolParametersProtocolVersion `cbor:"14,keyasint"`
MinUtxoValue *uint `cbor:"15,keyasint"`
}

Expand Down

0 comments on commit 568c539

Please sign in to comment.