diff --git a/ledger/alonzo/pparams.go b/ledger/alonzo/pparams.go index d28ae704..63525ad8 100644 --- a/ledger/alonzo/pparams.go +++ b/ledger/alonzo/pparams.go @@ -24,7 +24,7 @@ type AlonzoProtocolParameters struct { mary.MaryProtocolParameters MinPoolCost uint64 AdaPerUtxoByte uint64 - CostModels map[uint][]uint64 + CostModels map[uint][]int64 ExecutionCosts common.ExUnitPrice MaxTxExUnits common.ExUnit MaxBlockExUnits common.ExUnit @@ -95,7 +95,7 @@ type AlonzoProtocolParameterUpdate struct { mary.MaryProtocolParameterUpdate MinPoolCost *uint64 `cbor:"16,keyasint"` AdaPerUtxoByte *uint64 `cbor:"17,keyasint"` - CostModels map[uint][]uint64 `cbor:"18,keyasint"` + CostModels map[uint][]int64 `cbor:"18,keyasint"` ExecutionCosts *common.ExUnitPrice `cbor:"19,keyasint"` MaxTxExUnits *common.ExUnit `cbor:"20,keyasint"` MaxBlockExUnits *common.ExUnit `cbor:"21,keyasint"` diff --git a/ledger/babbage/pparams.go b/ledger/babbage/pparams.go index b8b63d21..706116d1 100644 --- a/ledger/babbage/pparams.go +++ b/ledger/babbage/pparams.go @@ -38,7 +38,7 @@ type BabbageProtocolParameters struct { ProtocolMinor uint MinPoolCost uint64 AdaPerUtxoByte uint64 - CostModels map[uint][]uint64 + CostModels map[uint][]int64 ExecutionCosts common.ExUnitPrice MaxTxExUnits common.ExUnit MaxBlockExUnits common.ExUnit @@ -134,7 +134,7 @@ type BabbageProtocolParameterUpdate struct { ProtocolVersion *common.ProtocolParametersProtocolVersion `cbor:"14,keyasint"` MinPoolCost *uint64 `cbor:"16,keyasint"` AdaPerUtxoByte *uint64 `cbor:"17,keyasint"` - CostModels map[uint][]uint64 `cbor:"18,keyasint"` + CostModels map[uint][]int64 `cbor:"18,keyasint"` ExecutionCosts *common.ExUnitPrice `cbor:"19,keyasint"` MaxTxExUnits *common.ExUnit `cbor:"20,keyasint"` MaxBlockExUnits *common.ExUnit `cbor:"21,keyasint"` diff --git a/ledger/babbage/pparams_test.go b/ledger/babbage/pparams_test.go index cceca889..750f873f 100644 --- a/ledger/babbage/pparams_test.go +++ b/ledger/babbage/pparams_test.go @@ -60,9 +60,9 @@ func TestBabbageProtocolParamsUpdate(t *testing.T) { 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}, + CostModels: map[uint][]int64{ + 0: []int64{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: []int64{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}, }, }, }, diff --git a/ledger/common/genesis.go b/ledger/common/genesis.go new file mode 100644 index 00000000..e0c44162 --- /dev/null +++ b/ledger/common/genesis.go @@ -0,0 +1,33 @@ +// 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 common + +import ( + "fmt" + "math/big" +) + +// GenesisRat is a wrapper to big.Rat that allows for unmarshaling from a bare float from JSON +type GenesisRat struct { + *big.Rat +} + +func (r *GenesisRat) UnmarshalJSON(data []byte) error { + r.Rat = new(big.Rat) + if _, ok := r.Rat.SetString(string(data)); !ok { + return fmt.Errorf("math/big: cannot unmarshal %q into a *big.Rat", data) + } + return nil +} diff --git a/ledger/conway/conway.go b/ledger/conway/conway.go index f50e7466..5c2e7b7c 100644 --- a/ledger/conway/conway.go +++ b/ledger/conway/conway.go @@ -227,85 +227,6 @@ func (b *ConwayTransactionBody) Donation() uint64 { return b.TxDonation } -type ConwayProtocolParameters 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 - Protocol struct { - cbor.StructAsArray - Major uint - Minor uint - } - MinPoolCost uint - AdaPerUtxoByte uint - CostModels map[uint][]int - ExecutionUnitPrices []*cbor.Rat // [priceMemory priceSteps] - MaxTxExecutionUnits []uint - MaxBlockExecutionUnits []uint - MaxValueSize uint - CollateralPercentage uint - MaxCollateralInputs uint - PoolVotingThresholds PoolVotingThresholds - DRepVotingThresholds DRepVotingThresholds - MinCommitteeSize uint - CommitteeTermLimit uint64 - GovActionValidityPeriod uint64 - GovActionDeposit uint64 - DRepDeposit uint64 - DRepInactivityPeriod uint64 - MinFeeRefScriptCostPerByte *cbor.Rat -} - -type ConwayProtocolParameterUpdate struct { - babbage.BabbageProtocolParameterUpdate - PoolVotingThresholds PoolVotingThresholds `cbor:"25,keyasint"` - DRepVotingThresholds DRepVotingThresholds `cbor:"26,keyasint"` - MinCommitteeSize uint `cbor:"27,keyasint"` - CommitteeTermLimit uint64 `cbor:"28,keyasint"` - GovActionValidityPeriod uint64 `cbor:"29,keyasint"` - GovActionDeposit uint64 `cbor:"30,keyasint"` - DRepDeposit uint64 `cbor:"31,keyasint"` - DRepInactivityPeriod uint64 `cbor:"32,keyasint"` - MinFeeRefScriptCostPerByte *cbor.Rat `cbor:"33,keyasint"` -} - -func (u *ConwayProtocolParameterUpdate) UnmarshalCBOR(data []byte) error { - return u.UnmarshalCbor(data, u) -} - -type PoolVotingThresholds struct { - cbor.StructAsArray - MotionNoConfidence cbor.Rat - CommitteeNormal cbor.Rat - CommitteeNoConfidence cbor.Rat - HardForkInitiation cbor.Rat - SecurityRelevantParameterVotingThreshold cbor.Rat -} - -type DRepVotingThresholds struct { - cbor.StructAsArray - MotionNoConfidence cbor.Rat - CommitteeNormal cbor.Rat - CommitteeNoConfidence cbor.Rat - UpdateConstitution cbor.Rat - HardForkInitiation cbor.Rat - PPNetworkGroup cbor.Rat - PPEconomicGroup cbor.Rat - PPTechnicalGroup cbor.Rat - PPGovGroup cbor.Rat - TreasureWithdrawal cbor.Rat -} - type ConwayTransaction struct { cbor.StructAsArray cbor.DecodeStoreCbor diff --git a/ledger/conway/genesis.go b/ledger/conway/genesis.go index 41929fcc..4abfe039 100644 --- a/ledger/conway/genesis.go +++ b/ledger/conway/genesis.go @@ -18,6 +18,8 @@ import ( "encoding/json" "io" "os" + + "github.com/blinklabs-io/gouroboros/ledger/common" ) type ConwayGenesis struct { @@ -29,31 +31,31 @@ type ConwayGenesis struct { GovActionDeposit uint64 DRepDeposit uint64 `json:"dRepDeposit"` DRepInactivityPeriod uint64 `json:"dRepActivity"` - MinFeeRefScriptCostPerByte float32 - PlutusV3CostModel []int `json:"plutusV3CostModel"` + MinFeeRefScriptCostPerByte *common.GenesisRat + PlutusV3CostModel []int64 `json:"plutusV3CostModel"` Constitution ConwayGenesisConstitution Committee ConwayGenesisCommittee } type ConwayGenesisPoolVotingThresholds struct { - CommitteeNormal float32 - CommitteeNoConfidence float32 - HardForkInitiation float32 - MotionNoConfidence float32 - PpSecurityGroup float32 + CommitteeNormal *common.GenesisRat + CommitteeNoConfidence *common.GenesisRat + HardForkInitiation *common.GenesisRat + MotionNoConfidence *common.GenesisRat + PpSecurityGroup *common.GenesisRat } type ConwayGenesisDRepVotingThresholds struct { - MotionNoConfidence float32 - CommitteeNormal float32 - CommitteeNoConfidence float32 - UpdateToConstitution float32 - HardForkInitiation float32 - PpNetworkGroup float32 - PpEconomicGroup float32 - PpTechnicalGroup float32 - PpGovGroup float32 - TreasuryWithdrawal float32 + MotionNoConfidence *common.GenesisRat + CommitteeNormal *common.GenesisRat + CommitteeNoConfidence *common.GenesisRat + UpdateToConstitution *common.GenesisRat + HardForkInitiation *common.GenesisRat + PpNetworkGroup *common.GenesisRat + PpEconomicGroup *common.GenesisRat + PpTechnicalGroup *common.GenesisRat + PpGovGroup *common.GenesisRat + TreasuryWithdrawal *common.GenesisRat } type ConwayGenesisConstitution struct { diff --git a/ledger/conway/genesis_test.go b/ledger/conway/genesis_test.go index df50d92c..9f1877e3 100644 --- a/ledger/conway/genesis_test.go +++ b/ledger/conway/genesis_test.go @@ -15,10 +15,12 @@ package conway_test import ( + "math/big" "reflect" "strings" "testing" + "github.com/blinklabs-io/gouroboros/ledger/common" "github.com/blinklabs-io/gouroboros/ledger/conway" ) @@ -330,23 +332,23 @@ const conwayGenesisConfig = ` var expectedGenesisObj = conway.ConwayGenesis{ PoolVotingThresholds: conway.ConwayGenesisPoolVotingThresholds{ - CommitteeNoConfidence: 0.51, - CommitteeNormal: 0.51, - HardForkInitiation: 0.51, - MotionNoConfidence: 0.51, - PpSecurityGroup: 0.51, + CommitteeNoConfidence: &common.GenesisRat{Rat: big.NewRat(51, 100)}, + CommitteeNormal: &common.GenesisRat{Rat: big.NewRat(51, 100)}, + HardForkInitiation: &common.GenesisRat{Rat: big.NewRat(51, 100)}, + MotionNoConfidence: &common.GenesisRat{Rat: big.NewRat(51, 100)}, + PpSecurityGroup: &common.GenesisRat{Rat: big.NewRat(51, 100)}, }, DRepVotingThresholds: conway.ConwayGenesisDRepVotingThresholds{ - CommitteeNoConfidence: 0.6, - CommitteeNormal: 0.67, - HardForkInitiation: 0.6, - MotionNoConfidence: 0.67, - PpEconomicGroup: 0.67, - PpGovGroup: 0.75, - PpNetworkGroup: 0.67, - PpTechnicalGroup: 0.67, - TreasuryWithdrawal: 0.67, - UpdateToConstitution: 0.75, + CommitteeNoConfidence: &common.GenesisRat{Rat: big.NewRat(60, 100)}, + CommitteeNormal: &common.GenesisRat{Rat: big.NewRat(67, 100)}, + HardForkInitiation: &common.GenesisRat{Rat: big.NewRat(60, 100)}, + MotionNoConfidence: &common.GenesisRat{Rat: big.NewRat(67, 100)}, + PpEconomicGroup: &common.GenesisRat{Rat: big.NewRat(67, 100)}, + PpGovGroup: &common.GenesisRat{Rat: big.NewRat(75, 100)}, + PpNetworkGroup: &common.GenesisRat{Rat: big.NewRat(67, 100)}, + PpTechnicalGroup: &common.GenesisRat{Rat: big.NewRat(67, 100)}, + TreasuryWithdrawal: &common.GenesisRat{Rat: big.NewRat(67, 100)}, + UpdateToConstitution: &common.GenesisRat{Rat: big.NewRat(75, 100)}, }, MinCommitteeSize: 7, CommitteeTermLimit: 146, @@ -354,8 +356,8 @@ var expectedGenesisObj = conway.ConwayGenesis{ GovActionDeposit: 100000000000, DRepDeposit: 500000000, DRepInactivityPeriod: 20, - MinFeeRefScriptCostPerByte: 15, - PlutusV3CostModel: []int{ + MinFeeRefScriptCostPerByte: &common.GenesisRat{Rat: new(big.Rat).SetUint64(15)}, + PlutusV3CostModel: []int64{ 100788, 420, 1, 1, 1000, 173, 0, 1, 1000, 59957, 4, 1, 11183, 32, 201305, 8356, 4, 16000, 100, 16000, 100, 16000, 100, 16000, 100, 16000, 100, 16000, 100, 100, 100, 16000, 100, 94375, 32, 132994, 32, 61462, 4, 72010, 178, 0, 1, 22151, 32, 91189, 769, 4, 2, 85848, 123203, 7305, -900, 1716, 549, 57, 85848, 0, 1, 1, 1000, 42921, 4, 2, 24548, 29498, 38, 1, 898148, 27279, 1, 51775, 558, 1, 39184, 1000, 60594, 1, 141895, 32, 83150, 32, 15299, 32, 76049, 1, 13169, 4, 22100, 10, 28999, 74, 1, 28999, 74, 1, 43285, 552, 1, 44749, 541, 1, 33852, 32, 68246, 32, 72362, 32, 7243, 32, 7391, 32, 11546, 32, 85848, 123203, 7305, -900, 1716, 549, 57, 85848, 0, 1, 90434, 519, 0, 1, 74433, 32, 85848, 123203, 7305, -900, 1716, 549, 57, 85848, 0, 1, 1, 85848, 123203, 7305, -900, 1716, 549, 57, 85848, 0, 1, 955506, 213312, 0, 2, 270652, 22588, 4, 1457325, 64566, 4, 20467, 1, 4, 0, 141992, 32, 100788, 420, 1, 1, 81663, 32, 59498, 32, 20142, 32, 24588, 32, 20744, 32, 25933, 32, 24623, 32, 43053543, 10, 53384111, 14333, 10, 43574283, 26308, 10, 16000, 100, 16000, 100, 962335, 18, 2780678, 6, 442008, 1, 52538055, 3756, 18, 267929, 18, 76433006, 8868, 18, 52948122, 18, 1995836, 36, 3227919, 12, 901022, 1, 166917843, 4307, 36, 284546, 36, 158221314, 26549, 36, 74698472, 36, 333849714, 1, 254006273, 72, 2174038, 72, 2261318, 64571, 4, 207616, 8310, 4, 1293828, 28716, 63, 0, 1, 1006041, 43623, 251, 0, 1, }, Constitution: conway.ConwayGenesisConstitution{ diff --git a/ledger/conway/pparams.go b/ledger/conway/pparams.go new file mode 100644 index 00000000..40cd3272 --- /dev/null +++ b/ledger/conway/pparams.go @@ -0,0 +1,252 @@ +// 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 conway + +import ( + "github.com/blinklabs-io/gouroboros/cbor" + "github.com/blinklabs-io/gouroboros/ledger/babbage" + "github.com/blinklabs-io/gouroboros/ledger/common" +) + +type ConwayProtocolParameters 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 + ProtocolVersion common.ProtocolParametersProtocolVersion + MinPoolCost uint64 + AdaPerUtxoByte uint64 + CostModels map[uint][]int64 + ExecutionCosts common.ExUnitPrice + MaxTxExUnits common.ExUnit + MaxBlockExUnits common.ExUnit + MaxValueSize uint + CollateralPercentage uint + MaxCollateralInputs uint + PoolVotingThresholds PoolVotingThresholds + DRepVotingThresholds DRepVotingThresholds + MinCommitteeSize uint + CommitteeTermLimit uint64 + GovActionValidityPeriod uint64 + GovActionDeposit uint64 + DRepDeposit uint64 + DRepInactivityPeriod uint64 + MinFeeRefScriptCostPerByte *cbor.Rat +} + +func (p *ConwayProtocolParameters) Update(paramUpdate *ConwayProtocolParameterUpdate) { + 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.ProtocolVersion = *paramUpdate.ProtocolVersion + } + 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 + } + if paramUpdate.PoolVotingThresholds != nil { + p.PoolVotingThresholds = *paramUpdate.PoolVotingThresholds + } + if paramUpdate.DRepVotingThresholds != nil { + p.DRepVotingThresholds = *paramUpdate.DRepVotingThresholds + } + if paramUpdate.MinCommitteeSize != nil { + p.MinCommitteeSize = *paramUpdate.MinCommitteeSize + } + if paramUpdate.CommitteeTermLimit != nil { + p.CommitteeTermLimit = *paramUpdate.CommitteeTermLimit + } + if paramUpdate.GovActionValidityPeriod != nil { + p.GovActionValidityPeriod = *paramUpdate.GovActionValidityPeriod + } + if paramUpdate.GovActionDeposit != nil { + p.GovActionDeposit = *paramUpdate.GovActionDeposit + } + if paramUpdate.DRepDeposit != nil { + p.DRepDeposit = *paramUpdate.DRepDeposit + } + if paramUpdate.DRepInactivityPeriod != nil { + p.DRepInactivityPeriod = *paramUpdate.DRepInactivityPeriod + } + if paramUpdate.MinFeeRefScriptCostPerByte != nil { + p.MinFeeRefScriptCostPerByte = paramUpdate.MinFeeRefScriptCostPerByte + } +} + +func (p *ConwayProtocolParameters) UpdateFromGenesis(genesis *ConwayGenesis) { + p.MinCommitteeSize = genesis.MinCommitteeSize + p.CommitteeTermLimit = genesis.CommitteeTermLimit + p.GovActionValidityPeriod = genesis.GovActionValidityPeriod + p.GovActionDeposit = genesis.GovActionDeposit + p.DRepDeposit = genesis.DRepDeposit + p.DRepInactivityPeriod = genesis.DRepInactivityPeriod + if genesis.MinFeeRefScriptCostPerByte != nil { + p.MinFeeRefScriptCostPerByte = &cbor.Rat{Rat: genesis.MinFeeRefScriptCostPerByte.Rat} + } + if len(genesis.PlutusV3CostModel) > 0 { + p.CostModels[2] = genesis.PlutusV3CostModel + } + if genesis.PoolVotingThresholds.MotionNoConfidence != nil { + p.PoolVotingThresholds.MotionNoConfidence = cbor.Rat{Rat: genesis.PoolVotingThresholds.MotionNoConfidence.Rat} + } + if genesis.PoolVotingThresholds.CommitteeNormal != nil { + p.PoolVotingThresholds.CommitteeNormal = cbor.Rat{Rat: genesis.PoolVotingThresholds.CommitteeNormal.Rat} + } + if genesis.PoolVotingThresholds.CommitteeNoConfidence != nil { + p.PoolVotingThresholds.CommitteeNoConfidence = cbor.Rat{Rat: genesis.PoolVotingThresholds.CommitteeNoConfidence.Rat} + } + if genesis.PoolVotingThresholds.HardForkInitiation != nil { + p.PoolVotingThresholds.HardForkInitiation = cbor.Rat{Rat: genesis.PoolVotingThresholds.HardForkInitiation.Rat} + } + if genesis.PoolVotingThresholds.PpSecurityGroup != nil { + p.PoolVotingThresholds.PpSecurityGroup = cbor.Rat{Rat: genesis.PoolVotingThresholds.PpSecurityGroup.Rat} + } + if genesis.DRepVotingThresholds.MotionNoConfidence != nil { + p.DRepVotingThresholds.MotionNoConfidence = cbor.Rat{Rat: genesis.DRepVotingThresholds.MotionNoConfidence.Rat} + } + if genesis.DRepVotingThresholds.CommitteeNormal != nil { + p.DRepVotingThresholds.CommitteeNormal = cbor.Rat{Rat: genesis.DRepVotingThresholds.CommitteeNormal.Rat} + } + if genesis.DRepVotingThresholds.CommitteeNoConfidence != nil { + p.DRepVotingThresholds.CommitteeNoConfidence = cbor.Rat{Rat: genesis.DRepVotingThresholds.CommitteeNoConfidence.Rat} + } + if genesis.DRepVotingThresholds.UpdateToConstitution != nil { + p.DRepVotingThresholds.UpdateToConstitution = cbor.Rat{Rat: genesis.DRepVotingThresholds.UpdateToConstitution.Rat} + } + if genesis.DRepVotingThresholds.HardForkInitiation != nil { + p.DRepVotingThresholds.HardForkInitiation = cbor.Rat{Rat: genesis.DRepVotingThresholds.HardForkInitiation.Rat} + } + if genesis.DRepVotingThresholds.PpNetworkGroup != nil { + p.DRepVotingThresholds.PpNetworkGroup = cbor.Rat{Rat: genesis.DRepVotingThresholds.PpNetworkGroup.Rat} + } + if genesis.DRepVotingThresholds.PpEconomicGroup != nil { + p.DRepVotingThresholds.PpEconomicGroup = cbor.Rat{Rat: genesis.DRepVotingThresholds.PpEconomicGroup.Rat} + } + if genesis.DRepVotingThresholds.PpTechnicalGroup != nil { + p.DRepVotingThresholds.PpTechnicalGroup = cbor.Rat{Rat: genesis.DRepVotingThresholds.PpTechnicalGroup.Rat} + } + if genesis.DRepVotingThresholds.PpGovGroup != nil { + p.DRepVotingThresholds.PpGovGroup = cbor.Rat{Rat: genesis.DRepVotingThresholds.PpGovGroup.Rat} + } + if genesis.DRepVotingThresholds.TreasuryWithdrawal != nil { + p.DRepVotingThresholds.TreasuryWithdrawal = cbor.Rat{Rat: genesis.DRepVotingThresholds.TreasuryWithdrawal.Rat} + } +} + +type ConwayProtocolParameterUpdate struct { + babbage.BabbageProtocolParameterUpdate + PoolVotingThresholds *PoolVotingThresholds `cbor:"25,keyasint"` + DRepVotingThresholds *DRepVotingThresholds `cbor:"26,keyasint"` + MinCommitteeSize *uint `cbor:"27,keyasint"` + CommitteeTermLimit *uint64 `cbor:"28,keyasint"` + GovActionValidityPeriod *uint64 `cbor:"29,keyasint"` + GovActionDeposit *uint64 `cbor:"30,keyasint"` + DRepDeposit *uint64 `cbor:"31,keyasint"` + DRepInactivityPeriod *uint64 `cbor:"32,keyasint"` + MinFeeRefScriptCostPerByte *cbor.Rat `cbor:"33,keyasint"` +} + +func (u *ConwayProtocolParameterUpdate) UnmarshalCBOR(data []byte) error { + return u.UnmarshalCbor(data, u) +} + +type PoolVotingThresholds struct { + cbor.StructAsArray + MotionNoConfidence cbor.Rat + CommitteeNormal cbor.Rat + CommitteeNoConfidence cbor.Rat + HardForkInitiation cbor.Rat + PpSecurityGroup cbor.Rat +} + +type DRepVotingThresholds struct { + cbor.StructAsArray + MotionNoConfidence cbor.Rat + CommitteeNormal cbor.Rat + CommitteeNoConfidence cbor.Rat + UpdateToConstitution cbor.Rat + HardForkInitiation cbor.Rat + PpNetworkGroup cbor.Rat + PpEconomicGroup cbor.Rat + PpTechnicalGroup cbor.Rat + PpGovGroup cbor.Rat + TreasuryWithdrawal cbor.Rat +} diff --git a/ledger/conway/pparams_test.go b/ledger/conway/pparams_test.go new file mode 100644 index 00000000..24d21db3 --- /dev/null +++ b/ledger/conway/pparams_test.go @@ -0,0 +1,120 @@ +// 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 conway_test + +import ( + "encoding/hex" + "reflect" + "strings" + "testing" + + "github.com/blinklabs-io/gouroboros/cbor" + "github.com/blinklabs-io/gouroboros/ledger/common" + "github.com/blinklabs-io/gouroboros/ledger/conway" +) + +func TestConwayProtocolParamsUpdate(t *testing.T) { + testDefs := []struct { + startParams conway.ConwayProtocolParameters + updateCbor string + expectedParams conway.ConwayProtocolParameters + }{ + { + startParams: conway.ConwayProtocolParameters{ + ProtocolVersion: common.ProtocolParametersProtocolVersion{ + Major: 8, + }, + }, + updateCbor: "a10e820900", + expectedParams: conway.ConwayProtocolParameters{ + ProtocolVersion: common.ProtocolParametersProtocolVersion{ + Major: 9, + }, + }, + }, + { + startParams: conway.ConwayProtocolParameters{ + MaxBlockBodySize: 1, + MaxTxExUnits: common.ExUnit{ + Mem: 1, + Steps: 1, + }, + }, + updateCbor: "a2021a0001200014821a00aba9501b00000002540be400", + expectedParams: conway.ConwayProtocolParameters{ + MaxBlockBodySize: 73728, + MaxTxExUnits: common.ExUnit{ + Mem: 11250000, + Steps: 10000000000, + }, + }, + }, + { + startParams: conway.ConwayProtocolParameters{}, + updateCbor: "a112a20098a61a0003236119032c01011903e819023b00011903e8195e7104011903e818201a0001ca761928eb041959d818641959d818641959d818641959d818641959d818641959d81864186418641959d81864194c5118201a0002acfa182019b551041a000363151901ff00011a00015c3518201a000797751936f404021a0002ff941a0006ea7818dc0001011903e8196ff604021a0003bd081a00034ec5183e011a00102e0f19312a011a00032e801901a5011a0002da781903e819cf06011a00013a34182019a8f118201903e818201a00013aac0119e143041903e80a1a00030219189c011a00030219189c011a0003207c1901d9011a000330001901ff0119ccf3182019fd40182019ffd5182019581e18201940b318201a00012adf18201a0002ff941a0006ea7818dc0001011a00010f92192da7000119eabb18201a0002ff941a0006ea7818dc0001011a0002ff941a0006ea7818dc0001011a000c504e197712041a001d6af61a0001425b041a00040c660004001a00014fab18201a0003236119032c010119a0de18201a00033d7618201979f41820197fb8182019a95d1820197df718201995aa18201a009063b91903fd0a0198af1a0003236119032c01011903e819023b00011903e8195e7104011903e818201a0001ca761928eb041959d818641959d818641959d818641959d818641959d818641959d81864186418641959d81864194c5118201a0002acfa182019b551041a000363151901ff00011a00015c3518201a000797751936f404021a0002ff941a0006ea7818dc0001011903e8196ff604021a0003bd081a00034ec5183e011a00102e0f19312a011a00032e801901a5011a0002da781903e819cf06011a00013a34182019a8f118201903e818201a00013aac0119e143041903e80a1a00030219189c011a00030219189c011a0003207c1901d9011a000330001901ff0119ccf3182019fd40182019ffd5182019581e18201940b318201a00012adf18201a0002ff941a0006ea7818dc0001011a00010f92192da7000119eabb18201a0002ff941a0006ea7818dc0001011a0002ff941a0006ea7818dc0001011a0011b22c1a0005fdde00021a000c504e197712041a001d6af61a0001425b041a00040c660004001a00014fab18201a0003236119032c010119a0de18201a00033d7618201979f41820197fb8182019a95d1820197df718201995aa18201b00000004a817c8001b00000004a817c8001a009063b91903fd0a1b00000004a817c800001b00000004a817c800", + expectedParams: conway.ConwayProtocolParameters{ + CostModels: map[uint][]int64{ + 0: []int64{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: []int64{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 conway.ConwayProtocolParameterUpdate + 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) + } + } +} + +func TestConwayProtocolParamsUpdateFromGenesis(t *testing.T) { + testDefs := []struct { + startParams conway.ConwayProtocolParameters + genesisJson string + expectedParams conway.ConwayProtocolParameters + }{ + { + startParams: conway.ConwayProtocolParameters{ + MaxBlockBodySize: 1, + }, + genesisJson: `{"govActionDeposit": 100000000000}`, + expectedParams: conway.ConwayProtocolParameters{ + MaxBlockBodySize: 1, + GovActionDeposit: 100000000000, + }, + }, + } + for _, testDef := range testDefs { + tmpGenesis, err := conway.NewConwayGenesisFromReader(strings.NewReader(testDef.genesisJson)) + if err != nil { + t.Fatalf("unexpected error: %s", err) + } + tmpParams := testDef.startParams + tmpParams.UpdateFromGenesis(&tmpGenesis) + if !reflect.DeepEqual(tmpParams, testDef.expectedParams) { + t.Fatalf("did not get expected params:\n got: %#v\n wanted: %#v", tmpParams, testDef.expectedParams) + } + } +} diff --git a/ledger/shelley/genesis.go b/ledger/shelley/genesis.go index 92c8fd54..e32b23c8 100644 --- a/ledger/shelley/genesis.go +++ b/ledger/shelley/genesis.go @@ -16,11 +16,11 @@ package shelley import ( "encoding/json" - "fmt" "io" - "math/big" "os" "time" + + "github.com/blinklabs-io/gouroboros/ledger/common" ) type ShelleyGenesis struct { @@ -51,10 +51,10 @@ type ShelleyGenesisProtocolParams struct { PoolDeposit uint MaxEpoch uint `json:"eMax"` NOpt uint - A0 *GenesisRat - Rho *GenesisRat - Tau *GenesisRat - Decentralization *GenesisRat `json:"decentralisationParam"` + A0 *common.GenesisRat + Rho *common.GenesisRat + Tau *common.GenesisRat + Decentralization *common.GenesisRat `json:"decentralisationParam"` ExtraEntropy map[string]string ProtocolVersion struct { Major uint @@ -82,16 +82,3 @@ func NewShelleyGenesisFromFile(path string) (ShelleyGenesis, error) { defer f.Close() return NewShelleyGenesisFromReader(f) } - -// GenesisRat is a wrapper to big.Rat that allows for unmarshaling from a bare float from JSON -type GenesisRat struct { - *big.Rat -} - -func (r *GenesisRat) UnmarshalJSON(data []byte) error { - r.Rat = new(big.Rat) - if _, ok := r.Rat.SetString(string(data)); !ok { - return fmt.Errorf("math/big: cannot unmarshal %q into a *big.Rat", data) - } - return nil -} diff --git a/ledger/shelley/genesis_test.go b/ledger/shelley/genesis_test.go index aac0c1c6..a55850ac 100644 --- a/ledger/shelley/genesis_test.go +++ b/ledger/shelley/genesis_test.go @@ -21,6 +21,7 @@ import ( "testing" "time" + "github.com/blinklabs-io/gouroboros/ledger/common" "github.com/blinklabs-io/gouroboros/ledger/shelley" ) @@ -126,10 +127,10 @@ var expectedGenesisObj = shelley.ShelleyGenesis{ PoolDeposit: 500000000, MaxEpoch: 18, NOpt: 150, - A0: &shelley.GenesisRat{Rat: big.NewRat(3, 10)}, - Rho: &shelley.GenesisRat{Rat: big.NewRat(3, 1000)}, - Tau: &shelley.GenesisRat{Rat: big.NewRat(2, 10)}, - Decentralization: &shelley.GenesisRat{Rat: new(big.Rat).SetInt64(1)}, + A0: &common.GenesisRat{Rat: big.NewRat(3, 10)}, + Rho: &common.GenesisRat{Rat: big.NewRat(3, 1000)}, + Tau: &common.GenesisRat{Rat: big.NewRat(2, 10)}, + Decentralization: &common.GenesisRat{Rat: new(big.Rat).SetInt64(1)}, ExtraEntropy: map[string]string{ "tag": "NeutralNonce", },