Skip to content

Commit

Permalink
feat: add Utxorpc support for cardano.PParams in other eras (#769)
Browse files Browse the repository at this point in the history
Signed-off-by: Ales Verbic <verbotenj@blinklabs.io>
  • Loading branch information
verbotenj authored Oct 28, 2024
1 parent 98ac789 commit b450b5c
Show file tree
Hide file tree
Showing 10 changed files with 552 additions and 7 deletions.
3 changes: 1 addition & 2 deletions ledger/allegra/pparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,5 @@ func (u *AllegraProtocolParameterUpdate) UnmarshalCBOR(data []byte) error {
}

func (p *AllegraProtocolParameters) Utxorpc() *cardano.PParams {
// TODO: Implement the conversion logic to cardano.PParams
return &cardano.PParams{}
return p.ShelleyProtocolParameters.Utxorpc()
}
57 changes: 57 additions & 0 deletions ledger/allegra/pparams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
"github.com/blinklabs-io/gouroboros/cbor"
"github.com/blinklabs-io/gouroboros/ledger/allegra"
"github.com/blinklabs-io/gouroboros/ledger/shelley"
"github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
)

func TestAllegraProtocolParamsUpdate(t *testing.T) {
Expand Down Expand Up @@ -78,3 +79,59 @@ func TestAllegraProtocolParamsUpdate(t *testing.T) {
}
}
}

func TestAllegraUtxorpc(t *testing.T) {
inputParams := allegra.AllegraProtocolParameters{
ShelleyProtocolParameters: shelley.ShelleyProtocolParameters{
MinFeeA: 500,
MinFeeB: 2,
MaxBlockBodySize: 65536,
MaxTxSize: 16384,
MaxBlockHeaderSize: 1024,
KeyDeposit: 2000,
PoolDeposit: 500000,
MaxEpoch: 2160,
NOpt: 100,
A0: &cbor.Rat{Rat: big.NewRat(1, 2)},
Rho: &cbor.Rat{Rat: big.NewRat(3, 4)},
Tau: &cbor.Rat{Rat: big.NewRat(5, 6)},
ProtocolMajor: 8,
ProtocolMinor: 0,
MinUtxoValue: 1000000,
},
}

expectedUtxorpc := &cardano.PParams{
MinFeeCoefficient: 500,
MinFeeConstant: 2,
MaxBlockBodySize: 65536,
MaxTxSize: 16384,
MaxBlockHeaderSize: 1024,
StakeKeyDeposit: 2000,
PoolDeposit: 500000,
PoolRetirementEpochBound: 2160,
DesiredNumberOfPools: 100,
PoolInfluence: &cardano.RationalNumber{
Numerator: int32(1),
Denominator: uint32(2),
},
MonetaryExpansion: &cardano.RationalNumber{
Numerator: int32(3),
Denominator: uint32(4),
},
TreasuryExpansion: &cardano.RationalNumber{
Numerator: int32(5),
Denominator: uint32(6),
},
ProtocolVersion: &cardano.ProtocolVersion{
Major: 8,
Minor: 0,
},
}

result := inputParams.Utxorpc()

if !reflect.DeepEqual(result, expectedUtxorpc) {
t.Fatalf("Utxorpc() test failed for Allegra:\nExpected: %#v\nGot: %#v", expectedUtxorpc, result)
}
}
53 changes: 51 additions & 2 deletions ledger/alonzo/pparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,55 @@ func (u *AlonzoProtocolParameterUpdate) UnmarshalCBOR(data []byte) error {
}

func (p *AlonzoProtocolParameters) Utxorpc() *cardano.PParams {
// TODO: Implement the conversion logic to cardano.PParams
return &cardano.PParams{}
return &cardano.PParams{
CoinsPerUtxoByte: p.AdaPerUtxoByte,
MaxTxSize: uint64(p.MaxTxSize),
MinFeeCoefficient: uint64(p.MinFeeA),
MinFeeConstant: uint64(p.MinFeeB),
MaxBlockBodySize: uint64(p.MaxBlockBodySize),
MaxBlockHeaderSize: uint64(p.MaxBlockHeaderSize),
StakeKeyDeposit: uint64(p.KeyDeposit),
PoolDeposit: uint64(p.PoolDeposit),
PoolRetirementEpochBound: uint64(p.MaxEpoch),
DesiredNumberOfPools: uint64(p.NOpt),
PoolInfluence: &cardano.RationalNumber{
Numerator: int32(p.A0.Num().Int64()),
Denominator: uint32(p.A0.Denom().Int64()),
},
MonetaryExpansion: &cardano.RationalNumber{
Numerator: int32(p.Rho.Num().Int64()),
Denominator: uint32(p.Rho.Denom().Int64()),
},
TreasuryExpansion: &cardano.RationalNumber{
Numerator: int32(p.Tau.Num().Int64()),
Denominator: uint32(p.Tau.Denom().Int64()),
},
MinPoolCost: p.MinPoolCost,
ProtocolVersion: &cardano.ProtocolVersion{
Major: uint32(p.ProtocolMajor),
Minor: uint32(p.ProtocolMinor),
},
MaxValueSize: uint64(p.MaxValueSize),
CollateralPercentage: uint64(p.CollateralPercentage),
MaxCollateralInputs: uint64(p.MaxCollateralInputs),
CostModels: common.ConvertToUtxorpcCardanoCostModels(p.CostModels),
Prices: &cardano.ExPrices{
Memory: &cardano.RationalNumber{
Numerator: int32(p.ExecutionCosts.MemPrice.Num().Int64()),
Denominator: uint32(p.ExecutionCosts.MemPrice.Denom().Int64()),
},
Steps: &cardano.RationalNumber{
Numerator: int32(p.ExecutionCosts.StepPrice.Num().Int64()),
Denominator: uint32(p.ExecutionCosts.StepPrice.Denom().Int64()),
},
},
MaxExecutionUnitsPerTransaction: &cardano.ExUnits{
Memory: uint64(p.MaxTxExUnits.Mem),
Steps: uint64(p.MaxTxExUnits.Steps),
},
MaxExecutionUnitsPerBlock: &cardano.ExUnits{
Memory: uint64(p.MaxBlockExUnits.Mem),
Steps: uint64(p.MaxBlockExUnits.Steps),
},
}
}
116 changes: 116 additions & 0 deletions ledger/alonzo/pparams_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/blinklabs-io/gouroboros/ledger/common"
"github.com/blinklabs-io/gouroboros/ledger/mary"
"github.com/blinklabs-io/gouroboros/ledger/shelley"
"github.com/utxorpc/go-codegen/utxorpc/v1alpha/cardano"
)

func TestAlonzoProtocolParamsUpdate(t *testing.T) {
Expand Down Expand Up @@ -181,3 +182,118 @@ func TestAlonzoProtocolParamsUpdateFromGenesis(t *testing.T) {
}
}
}

func TestAlonzoUtxorpc(t *testing.T) {
inputParams := alonzo.AlonzoProtocolParameters{
MaryProtocolParameters: mary.MaryProtocolParameters{
AllegraProtocolParameters: allegra.AllegraProtocolParameters{
ShelleyProtocolParameters: shelley.ShelleyProtocolParameters{
MaxTxSize: 16384,
MinFeeA: 500,
MinFeeB: 2,
MaxBlockBodySize: 65536,
MaxBlockHeaderSize: 1024,
KeyDeposit: 2000,
PoolDeposit: 500000,
MaxEpoch: 2160,
NOpt: 100,
A0: &cbor.Rat{Rat: big.NewRat(1, 2)},
Rho: &cbor.Rat{Rat: big.NewRat(3, 4)},
Tau: &cbor.Rat{Rat: big.NewRat(5, 6)},
ProtocolMajor: 8,
ProtocolMinor: 0,
},
},
},
AdaPerUtxoByte: 44,
MinPoolCost: 340000000,
MaxValueSize: 1024,
CollateralPercentage: 150,
MaxCollateralInputs: 5,
ExecutionCosts: common.ExUnitPrice{
MemPrice: &cbor.Rat{Rat: big.NewRat(1, 2)},
StepPrice: &cbor.Rat{Rat: big.NewRat(2, 3)},
},
MaxTxExUnits: common.ExUnit{
Mem: 1000000,
Steps: 200000,
},
MaxBlockExUnits: common.ExUnit{
Mem: 5000000,
Steps: 1000000,
},
CostModels: map[uint][]int64{
1: {100, 200, 300},
2: {400, 500, 600},
3: {700, 800, 900},
},
}

expectedUtxorpc := &cardano.PParams{
CoinsPerUtxoByte: 44,
MaxTxSize: 16384,
MinFeeCoefficient: 500,
MinFeeConstant: 2,
MaxBlockBodySize: 65536,
MaxBlockHeaderSize: 1024,
StakeKeyDeposit: 2000,
PoolDeposit: 500000,
PoolRetirementEpochBound: 2160,
DesiredNumberOfPools: 100,
PoolInfluence: &cardano.RationalNumber{
Numerator: int32(1),
Denominator: uint32(2),
},
MonetaryExpansion: &cardano.RationalNumber{
Numerator: int32(3),
Denominator: uint32(4),
},
TreasuryExpansion: &cardano.RationalNumber{
Numerator: int32(5),
Denominator: uint32(6),
},
MinPoolCost: 340000000,
ProtocolVersion: &cardano.ProtocolVersion{
Major: 8,
Minor: 0,
},
MaxValueSize: 1024,
CollateralPercentage: 150,
MaxCollateralInputs: 5,
CostModels: &cardano.CostModels{
PlutusV1: &cardano.CostModel{
Values: []int64{100, 200, 300},
},
PlutusV2: &cardano.CostModel{
Values: []int64{400, 500, 600},
},
PlutusV3: &cardano.CostModel{
Values: []int64{700, 800, 900},
},
},
Prices: &cardano.ExPrices{
Memory: &cardano.RationalNumber{
Numerator: int32(1),
Denominator: uint32(2),
},
Steps: &cardano.RationalNumber{
Numerator: int32(2),
Denominator: uint32(3),
},
},
MaxExecutionUnitsPerTransaction: &cardano.ExUnits{
Memory: 1000000,
Steps: 200000,
},
MaxExecutionUnitsPerBlock: &cardano.ExUnits{
Memory: 5000000,
Steps: 1000000,
},
}

result := inputParams.Utxorpc()

if !reflect.DeepEqual(result, expectedUtxorpc) {
t.Fatalf("Utxorpc() test failed for Alonzo:\nExpected: %#v\nGot: %#v", expectedUtxorpc, result)
}
}
55 changes: 53 additions & 2 deletions ledger/babbage/pparams.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,57 @@ func (u *BabbageProtocolParameterUpdate) UnmarshalCBOR(data []byte) error {
}

func (p *BabbageProtocolParameters) Utxorpc() *cardano.PParams {
// TODO: Implement the conversion logic to cardano.PParams
return &cardano.PParams{}
return &cardano.PParams{
CoinsPerUtxoByte: p.AdaPerUtxoByte,
MaxTxSize: uint64(p.MaxTxSize),
MinFeeCoefficient: uint64(p.MinFeeA),
MinFeeConstant: uint64(p.MinFeeB),
MaxBlockBodySize: uint64(p.MaxBlockBodySize),
MaxBlockHeaderSize: uint64(p.MaxBlockHeaderSize),
StakeKeyDeposit: uint64(p.KeyDeposit),
PoolDeposit: uint64(p.PoolDeposit),
PoolRetirementEpochBound: uint64(p.MaxEpoch),
DesiredNumberOfPools: uint64(p.NOpt),
PoolInfluence: &cardano.RationalNumber{
Numerator: int32(p.A0.Num().Int64()),
Denominator: uint32(p.A0.Denom().Int64()),
},
MonetaryExpansion: &cardano.RationalNumber{
Numerator: int32(p.Rho.Num().Int64()),
Denominator: uint32(p.Rho.Denom().Int64()),
},
TreasuryExpansion: &cardano.RationalNumber{
Numerator: int32(p.Tau.Num().Int64()),
Denominator: uint32(p.Tau.Denom().Int64()),
},
MinPoolCost: p.MinPoolCost,
ProtocolVersion: &cardano.ProtocolVersion{
Major: uint32(p.ProtocolMajor),
Minor: uint32(p.ProtocolMinor),
},
MaxValueSize: uint64(p.MaxValueSize),
CollateralPercentage: uint64(p.CollateralPercentage),
MaxCollateralInputs: uint64(p.MaxCollateralInputs),
CostModels: common.ConvertToUtxorpcCardanoCostModels(
p.CostModels,
),
Prices: &cardano.ExPrices{
Memory: &cardano.RationalNumber{
Numerator: int32(p.ExecutionCosts.MemPrice.Num().Int64()),
Denominator: uint32(p.ExecutionCosts.MemPrice.Denom().Int64()),
},
Steps: &cardano.RationalNumber{
Numerator: int32(p.ExecutionCosts.StepPrice.Num().Int64()),
Denominator: uint32(p.ExecutionCosts.StepPrice.Denom().Int64()),
},
},
MaxExecutionUnitsPerTransaction: &cardano.ExUnits{
Memory: uint64(p.MaxTxExUnits.Mem),
Steps: uint64(p.MaxTxExUnits.Steps),
},
MaxExecutionUnitsPerBlock: &cardano.ExUnits{
Memory: uint64(p.MaxBlockExUnits.Mem),
Steps: uint64(p.MaxBlockExUnits.Steps),
},
}
}
Loading

0 comments on commit b450b5c

Please sign in to comment.