From ac00e1efa73b917ae37fb3db7cba88b8fb78747f Mon Sep 17 00:00:00 2001 From: Aurora Gaffney Date: Sat, 22 Feb 2025 16:24:00 -0500 Subject: [PATCH] refactor: merge duplicate ExUnits types Signed-off-by: Aurora Gaffney --- ledger/alonzo/alonzo.go | 6 +++--- ledger/alonzo/pparams.go | 24 ++++++++++++------------ ledger/alonzo/pparams_test.go | 24 ++++++++++++------------ ledger/babbage/pparams.go | 12 ++++++------ ledger/babbage/pparams_test.go | 24 ++++++++++++------------ ledger/common/common.go | 4 ++-- ledger/common/pparams.go | 6 ------ ledger/common/tx.go | 2 +- ledger/conway/conway.go | 6 +++--- ledger/conway/pparams.go | 8 ++++---- ledger/conway/pparams_test.go | 24 ++++++++++++------------ 11 files changed, 67 insertions(+), 73 deletions(-) diff --git a/ledger/alonzo/alonzo.go b/ledger/alonzo/alonzo.go index b89bc487..450aa2a4 100644 --- a/ledger/alonzo/alonzo.go +++ b/ledger/alonzo/alonzo.go @@ -307,7 +307,7 @@ type AlonzoRedeemer struct { Tag common.RedeemerTag Index uint32 Data cbor.LazyValue - ExUnits common.RedeemerExUnits + ExUnits common.ExUnits } type AlonzoRedeemers []AlonzoRedeemer @@ -325,13 +325,13 @@ func (r AlonzoRedeemers) Indexes(tag common.RedeemerTag) []uint { func (r AlonzoRedeemers) Value( index uint, tag common.RedeemerTag, -) (cbor.LazyValue, common.RedeemerExUnits) { +) (cbor.LazyValue, common.ExUnits) { for _, redeemer := range r { if redeemer.Tag == tag && uint(redeemer.Index) == index { return redeemer.Data, redeemer.ExUnits } } - return cbor.LazyValue{}, common.RedeemerExUnits{} + return cbor.LazyValue{}, common.ExUnits{} } type AlonzoTransactionWitnessSet struct { diff --git a/ledger/alonzo/pparams.go b/ledger/alonzo/pparams.go index fbd6ddcf..f93e8d33 100644 --- a/ledger/alonzo/pparams.go +++ b/ledger/alonzo/pparams.go @@ -30,8 +30,8 @@ type AlonzoProtocolParameters struct { AdaPerUtxoByte uint64 CostModels map[uint][]int64 ExecutionCosts common.ExUnitPrice - MaxTxExUnits common.ExUnit - MaxBlockExUnits common.ExUnit + MaxTxExUnits common.ExUnits + MaxBlockExUnits common.ExUnits MaxValueSize uint CollateralPercentage uint MaxCollateralInputs uint @@ -80,13 +80,13 @@ func (p *AlonzoProtocolParameters) UpdateFromGenesis(genesis *AlonzoGenesis) { p.MaxValueSize = genesis.MaxValueSize p.CollateralPercentage = genesis.CollateralPercentage p.MaxCollateralInputs = genesis.MaxCollateralInputs - p.MaxTxExUnits = common.ExUnit{ - Mem: genesis.MaxTxExUnits.Mem, - Steps: genesis.MaxTxExUnits.Steps, + p.MaxTxExUnits = common.ExUnits{ + Memory: uint64(genesis.MaxTxExUnits.Mem), + Steps: uint64(genesis.MaxTxExUnits.Steps), } - p.MaxBlockExUnits = common.ExUnit{ - Mem: genesis.MaxBlockExUnits.Mem, - Steps: genesis.MaxBlockExUnits.Steps, + p.MaxBlockExUnits = common.ExUnits{ + Memory: uint64(genesis.MaxBlockExUnits.Mem), + Steps: uint64(genesis.MaxBlockExUnits.Steps), } if genesis.ExecutionPrices.Mem != nil && genesis.ExecutionPrices.Steps != nil { @@ -106,8 +106,8 @@ type AlonzoProtocolParameterUpdate struct { AdaPerUtxoByte *uint64 `cbor:"17,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"` + MaxTxExUnits *common.ExUnits `cbor:"20,keyasint"` + MaxBlockExUnits *common.ExUnits `cbor:"21,keyasint"` MaxValueSize *uint `cbor:"22,keyasint"` CollateralPercentage *uint `cbor:"23,keyasint"` MaxCollateralInputs *uint `cbor:"24,keyasint"` @@ -190,11 +190,11 @@ func (p *AlonzoProtocolParameters) Utxorpc() *cardano.PParams { }, }, MaxExecutionUnitsPerTransaction: &cardano.ExUnits{ - Memory: uint64(p.MaxTxExUnits.Mem), + Memory: uint64(p.MaxTxExUnits.Memory), Steps: uint64(p.MaxTxExUnits.Steps), }, MaxExecutionUnitsPerBlock: &cardano.ExUnits{ - Memory: uint64(p.MaxBlockExUnits.Mem), + Memory: uint64(p.MaxBlockExUnits.Memory), Steps: uint64(p.MaxBlockExUnits.Steps), }, } diff --git a/ledger/alonzo/pparams_test.go b/ledger/alonzo/pparams_test.go index 9b446c05..2a4117ca 100644 --- a/ledger/alonzo/pparams_test.go +++ b/ledger/alonzo/pparams_test.go @@ -89,9 +89,9 @@ func TestAlonzoProtocolParamsUpdate(t *testing.T) { }, }, }, - MaxTxExUnits: common.ExUnit{ - Mem: 1, - Steps: 1, + MaxTxExUnits: common.ExUnits{ + Memory: 1, + Steps: 1, }, }, updateCbor: "a2021a0001200014821a00aba9501b00000002540be400", @@ -103,9 +103,9 @@ func TestAlonzoProtocolParamsUpdate(t *testing.T) { }, }, }, - MaxTxExUnits: common.ExUnit{ - Mem: 11250000, - Steps: 10000000000, + MaxTxExUnits: common.ExUnits{ + Memory: 11250000, + Steps: 10000000000, }, }, }, @@ -214,13 +214,13 @@ func TestAlonzoUtxorpc(t *testing.T) { MemPrice: &cbor.Rat{Rat: big.NewRat(1, 2)}, StepPrice: &cbor.Rat{Rat: big.NewRat(2, 3)}, }, - MaxTxExUnits: common.ExUnit{ - Mem: 1000000, - Steps: 200000, + MaxTxExUnits: common.ExUnits{ + Memory: 1000000, + Steps: 200000, }, - MaxBlockExUnits: common.ExUnit{ - Mem: 5000000, - Steps: 1000000, + MaxBlockExUnits: common.ExUnits{ + Memory: 5000000, + Steps: 1000000, }, CostModels: map[uint][]int64{ 1: {100, 200, 300}, diff --git a/ledger/babbage/pparams.go b/ledger/babbage/pparams.go index ee117043..fe73766d 100644 --- a/ledger/babbage/pparams.go +++ b/ledger/babbage/pparams.go @@ -45,8 +45,8 @@ type BabbageProtocolParameters struct { AdaPerUtxoByte uint64 CostModels map[uint][]int64 ExecutionCosts common.ExUnitPrice - MaxTxExUnits common.ExUnit - MaxBlockExUnits common.ExUnit + MaxTxExUnits common.ExUnits + MaxBlockExUnits common.ExUnits MaxValueSize uint CollateralPercentage uint MaxCollateralInputs uint @@ -143,8 +143,8 @@ type BabbageProtocolParameterUpdate struct { AdaPerUtxoByte *uint64 `cbor:"17,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"` + MaxTxExUnits *common.ExUnits `cbor:"20,keyasint"` + MaxBlockExUnits *common.ExUnits `cbor:"21,keyasint"` MaxValueSize *uint `cbor:"22,keyasint"` CollateralPercentage *uint `cbor:"23,keyasint"` MaxCollateralInputs *uint `cbor:"24,keyasint"` @@ -229,11 +229,11 @@ func (p *BabbageProtocolParameters) Utxorpc() *cardano.PParams { }, }, MaxExecutionUnitsPerTransaction: &cardano.ExUnits{ - Memory: uint64(p.MaxTxExUnits.Mem), + Memory: uint64(p.MaxTxExUnits.Memory), Steps: uint64(p.MaxTxExUnits.Steps), }, MaxExecutionUnitsPerBlock: &cardano.ExUnits{ - Memory: uint64(p.MaxBlockExUnits.Mem), + Memory: uint64(p.MaxBlockExUnits.Memory), Steps: uint64(p.MaxBlockExUnits.Steps), }, } diff --git a/ledger/babbage/pparams_test.go b/ledger/babbage/pparams_test.go index 25262784..6f4d5f20 100644 --- a/ledger/babbage/pparams_test.go +++ b/ledger/babbage/pparams_test.go @@ -44,17 +44,17 @@ func TestBabbageProtocolParamsUpdate(t *testing.T) { { startParams: babbage.BabbageProtocolParameters{ MaxBlockBodySize: 1, - MaxTxExUnits: common.ExUnit{ - Mem: 1, - Steps: 1, + MaxTxExUnits: common.ExUnits{ + Memory: 1, + Steps: 1, }, }, updateCbor: "a2021a0001200014821a00aba9501b00000002540be400", expectedParams: babbage.BabbageProtocolParameters{ MaxBlockBodySize: 73728, - MaxTxExUnits: common.ExUnit{ - Mem: 11250000, - Steps: 10000000000, + MaxTxExUnits: common.ExUnits{ + Memory: 11250000, + Steps: 10000000000, }, }, }, @@ -463,13 +463,13 @@ func TestBabbageUtxorpc(t *testing.T) { MemPrice: &cbor.Rat{Rat: big.NewRat(1, 2)}, StepPrice: &cbor.Rat{Rat: big.NewRat(2, 3)}, }, - MaxTxExUnits: common.ExUnit{ - Mem: 1000000, - Steps: 200000, + MaxTxExUnits: common.ExUnits{ + Memory: 1000000, + Steps: 200000, }, - MaxBlockExUnits: common.ExUnit{ - Mem: 5000000, - Steps: 1000000, + MaxBlockExUnits: common.ExUnits{ + Memory: 5000000, + Steps: 1000000, }, CostModels: map[uint][]int64{ 1: {100, 200, 300}, diff --git a/ledger/common/common.go b/ledger/common/common.go index 51b0590d..8620b51f 100644 --- a/ledger/common/common.go +++ b/ledger/common/common.go @@ -318,8 +318,8 @@ func (i IssuerVkey) PoolId() string { return encoded } -// RedeemerExUnits represents the steps and memory usage for script execution -type RedeemerExUnits struct { +// ExUnits represents the steps and memory usage for script execution +type ExUnits struct { cbor.StructAsArray Memory uint64 Steps uint64 diff --git a/ledger/common/pparams.go b/ledger/common/pparams.go index 0596e215..eaee567c 100644 --- a/ledger/common/pparams.go +++ b/ledger/common/pparams.go @@ -36,12 +36,6 @@ type ProtocolParameters interface { Utxorpc() *cardano.PParams } -type ExUnit struct { - cbor.StructAsArray - Mem uint - Steps uint -} - type ExUnitPrice struct { cbor.StructAsArray MemPrice *cbor.Rat diff --git a/ledger/common/tx.go b/ledger/common/tx.go index bcc7fae6..4671cc2f 100644 --- a/ledger/common/tx.go +++ b/ledger/common/tx.go @@ -87,7 +87,7 @@ type TransactionWitnessSet interface { type TransactionWitnessRedeemers interface { Indexes(RedeemerTag) []uint - Value(uint, RedeemerTag) (cbor.LazyValue, RedeemerExUnits) + Value(uint, RedeemerTag) (cbor.LazyValue, ExUnits) } type Utxo struct { diff --git a/ledger/conway/conway.go b/ledger/conway/conway.go index dc80ae8f..d45be898 100644 --- a/ledger/conway/conway.go +++ b/ledger/conway/conway.go @@ -157,7 +157,7 @@ type ConwayRedeemerKey struct { type ConwayRedeemerValue struct { cbor.StructAsArray Data cbor.LazyValue - ExUnits common.RedeemerExUnits + ExUnits common.ExUnits } type ConwayRedeemers struct { @@ -203,7 +203,7 @@ func (r ConwayRedeemers) Indexes(tag common.RedeemerTag) []uint { func (r ConwayRedeemers) Value( index uint, tag common.RedeemerTag, -) (cbor.LazyValue, common.RedeemerExUnits) { +) (cbor.LazyValue, common.ExUnits) { redeemer, ok := r.Redeemers[ConwayRedeemerKey{ Tag: tag, Index: uint32(index), // #nosec G115 @@ -211,7 +211,7 @@ func (r ConwayRedeemers) Value( if ok { return redeemer.Data, redeemer.ExUnits } - return cbor.LazyValue{}, common.RedeemerExUnits{} + return cbor.LazyValue{}, common.ExUnits{} } type ConwayTransactionWitnessSet struct { diff --git a/ledger/conway/pparams.go b/ledger/conway/pparams.go index e82fa0d8..746b57c2 100644 --- a/ledger/conway/pparams.go +++ b/ledger/conway/pparams.go @@ -43,8 +43,8 @@ type ConwayProtocolParameters struct { AdaPerUtxoByte uint64 CostModels map[uint][]int64 ExecutionCosts common.ExUnitPrice - MaxTxExUnits common.ExUnit - MaxBlockExUnits common.ExUnit + MaxTxExUnits common.ExUnits + MaxBlockExUnits common.ExUnits MaxValueSize uint CollateralPercentage uint MaxCollateralInputs uint @@ -132,11 +132,11 @@ func (p *ConwayProtocolParameters) Utxorpc() *cardano.PParams { }, }, MaxExecutionUnitsPerTransaction: &cardano.ExUnits{ - Memory: uint64(p.MaxTxExUnits.Mem), + Memory: uint64(p.MaxTxExUnits.Memory), Steps: uint64(p.MaxTxExUnits.Steps), }, MaxExecutionUnitsPerBlock: &cardano.ExUnits{ - Memory: uint64(p.MaxBlockExUnits.Mem), + Memory: uint64(p.MaxBlockExUnits.Memory), Steps: uint64(p.MaxBlockExUnits.Steps), }, } diff --git a/ledger/conway/pparams_test.go b/ledger/conway/pparams_test.go index 127e50a7..72fc2f7e 100644 --- a/ledger/conway/pparams_test.go +++ b/ledger/conway/pparams_test.go @@ -49,17 +49,17 @@ func TestConwayProtocolParamsUpdate(t *testing.T) { { startParams: conway.ConwayProtocolParameters{ MaxBlockBodySize: 1, - MaxTxExUnits: common.ExUnit{ - Mem: 1, - Steps: 1, + MaxTxExUnits: common.ExUnits{ + Memory: 1, + Steps: 1, }, }, updateCbor: "a2021a0001200014821a00aba9501b00000002540be400", expectedParams: conway.ConwayProtocolParameters{ MaxBlockBodySize: 73728, - MaxTxExUnits: common.ExUnit{ - Mem: 11250000, - Steps: 10000000000, + MaxTxExUnits: common.ExUnits{ + Memory: 11250000, + Steps: 10000000000, }, }, }, @@ -506,13 +506,13 @@ func TestUtxorpc(t *testing.T) { MemPrice: &cbor.Rat{Rat: big.NewRat(1, 2)}, StepPrice: &cbor.Rat{Rat: big.NewRat(2, 3)}, }, - MaxTxExUnits: common.ExUnit{ - Mem: 1000000, - Steps: 200000, + MaxTxExUnits: common.ExUnits{ + Memory: 1000000, + Steps: 200000, }, - MaxBlockExUnits: common.ExUnit{ - Mem: 5000000, - Steps: 1000000, + MaxBlockExUnits: common.ExUnits{ + Memory: 5000000, + Steps: 1000000, }, CostModels: map[uint][]int64{ 1: {100, 200, 300},