|
| 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 alonzo |
| 16 | + |
| 17 | +import ( |
| 18 | + "github.com/blinklabs-io/gouroboros/cbor" |
| 19 | + "github.com/blinklabs-io/gouroboros/ledger/common" |
| 20 | + "github.com/blinklabs-io/gouroboros/ledger/mary" |
| 21 | +) |
| 22 | + |
| 23 | +type AlonzoProtocolParameters struct { |
| 24 | + mary.MaryProtocolParameters |
| 25 | + MinPoolCost uint64 |
| 26 | + AdaPerUtxoByte uint64 |
| 27 | + CostModels map[uint][]uint64 |
| 28 | + ExecutionCosts common.ExUnitPrice |
| 29 | + MaxTxExUnits common.ExUnit |
| 30 | + MaxBlockExUnits common.ExUnit |
| 31 | + MaxValueSize uint |
| 32 | + CollateralPercentage uint |
| 33 | + MaxCollateralInputs uint |
| 34 | +} |
| 35 | + |
| 36 | +func (p *AlonzoProtocolParameters) Update(paramUpdate *AlonzoProtocolParameterUpdate) { |
| 37 | + p.MaryProtocolParameters.Update( |
| 38 | + ¶mUpdate.MaryProtocolParameterUpdate, |
| 39 | + ) |
| 40 | + if paramUpdate.MinPoolCost != nil { |
| 41 | + p.MinPoolCost = *paramUpdate.MinPoolCost |
| 42 | + } |
| 43 | + if paramUpdate.AdaPerUtxoByte != nil { |
| 44 | + p.AdaPerUtxoByte = *paramUpdate.AdaPerUtxoByte |
| 45 | + } |
| 46 | + if paramUpdate.CostModels != nil { |
| 47 | + p.CostModels = paramUpdate.CostModels |
| 48 | + } |
| 49 | + if paramUpdate.ExecutionCosts != nil { |
| 50 | + p.ExecutionCosts = *paramUpdate.ExecutionCosts |
| 51 | + } |
| 52 | + if paramUpdate.MaxTxExUnits != nil { |
| 53 | + p.MaxTxExUnits = *paramUpdate.MaxTxExUnits |
| 54 | + } |
| 55 | + if paramUpdate.MaxBlockExUnits != nil { |
| 56 | + p.MaxBlockExUnits = *paramUpdate.MaxBlockExUnits |
| 57 | + } |
| 58 | + if paramUpdate.MaxValueSize != nil { |
| 59 | + p.MaxValueSize = *paramUpdate.MaxValueSize |
| 60 | + } |
| 61 | + if paramUpdate.CollateralPercentage != nil { |
| 62 | + p.CollateralPercentage = *paramUpdate.CollateralPercentage |
| 63 | + } |
| 64 | + if paramUpdate.MaxCollateralInputs != nil { |
| 65 | + p.MaxCollateralInputs = *paramUpdate.MaxCollateralInputs |
| 66 | + } |
| 67 | +} |
| 68 | + |
| 69 | +func (p *AlonzoProtocolParameters) UpdateFromGenesis(genesis *AlonzoGenesis) { |
| 70 | + // XXX: do we need to convert this? |
| 71 | + p.AdaPerUtxoByte = genesis.LovelacePerUtxoWord |
| 72 | + p.MaxValueSize = genesis.MaxValueSize |
| 73 | + p.CollateralPercentage = genesis.CollateralPercentage |
| 74 | + p.MaxCollateralInputs = genesis.MaxCollateralInputs |
| 75 | + p.MaxTxExUnits = common.ExUnit{ |
| 76 | + Mem: genesis.MaxTxExUnits.Mem, |
| 77 | + Steps: genesis.MaxTxExUnits.Steps, |
| 78 | + } |
| 79 | + p.MaxBlockExUnits = common.ExUnit{ |
| 80 | + Mem: genesis.MaxBlockExUnits.Mem, |
| 81 | + Steps: genesis.MaxBlockExUnits.Steps, |
| 82 | + } |
| 83 | + if genesis.ExecutionPrices.Mem != nil && genesis.ExecutionPrices.Steps != nil { |
| 84 | + p.ExecutionCosts = common.ExUnitPrice{ |
| 85 | + MemPrice: &cbor.Rat{Rat: genesis.ExecutionPrices.Mem.Rat}, |
| 86 | + StepPrice: &cbor.Rat{Rat: genesis.ExecutionPrices.Steps.Rat}, |
| 87 | + } |
| 88 | + } |
| 89 | + // TODO: cost models |
| 90 | + // We have 150+ string values to map to array indexes |
| 91 | + // CostModels map[string]map[string]int |
| 92 | +} |
| 93 | + |
| 94 | +type AlonzoProtocolParameterUpdate struct { |
| 95 | + mary.MaryProtocolParameterUpdate |
| 96 | + MinPoolCost *uint64 `cbor:"16,keyasint"` |
| 97 | + AdaPerUtxoByte *uint64 `cbor:"17,keyasint"` |
| 98 | + CostModels map[uint][]uint64 `cbor:"18,keyasint"` |
| 99 | + ExecutionCosts *common.ExUnitPrice `cbor:"19,keyasint"` |
| 100 | + MaxTxExUnits *common.ExUnit `cbor:"20,keyasint"` |
| 101 | + MaxBlockExUnits *common.ExUnit `cbor:"21,keyasint"` |
| 102 | + MaxValueSize *uint `cbor:"22,keyasint"` |
| 103 | + CollateralPercentage *uint `cbor:"23,keyasint"` |
| 104 | + MaxCollateralInputs *uint `cbor:"24,keyasint"` |
| 105 | +} |
| 106 | + |
| 107 | +func (u *AlonzoProtocolParameterUpdate) UnmarshalCBOR(data []byte) error { |
| 108 | + return u.UnmarshalCbor(data, u) |
| 109 | +} |
0 commit comments