|
| 1 | +// Copyright 2026 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 conway |
| 16 | + |
| 17 | +import ( |
| 18 | + "errors" |
| 19 | + "fmt" |
| 20 | + "math" |
| 21 | + "math/big" |
| 22 | + |
| 23 | + "github.com/blinklabs-io/gouroboros/cbor" |
| 24 | + "github.com/blinklabs-io/gouroboros/ledger/common" |
| 25 | +) |
| 26 | + |
| 27 | +const ( |
| 28 | + // MaxRefScriptSizePerTx is the Conway transaction limit from the ledger |
| 29 | + // specification. Dijkstra moves this value into protocol parameters. |
| 30 | + MaxRefScriptSizePerTx uint64 = 200 * 1024 |
| 31 | + // MaxRefScriptSizePerBlock is the Conway block limit from the ledger |
| 32 | + // specification. Dijkstra moves this value into protocol parameters. |
| 33 | + MaxRefScriptSizePerBlock uint64 = 1024 * 1024 |
| 34 | + // RefScriptCostStride is the fixed Conway reference-script fee tier size. |
| 35 | + RefScriptCostStride uint64 = 25_600 |
| 36 | +) |
| 37 | + |
| 38 | +func UtxoValidateRefScriptSizePerTx( |
| 39 | + tx common.Transaction, |
| 40 | + slot uint64, |
| 41 | + ls common.LedgerState, |
| 42 | + pp common.ProtocolParameters, |
| 43 | +) error { |
| 44 | + if _, ok := pp.(*ConwayProtocolParameters); !ok { |
| 45 | + return errors.New("pparams are not expected type") |
| 46 | + } |
| 47 | + if !tx.IsValid() { |
| 48 | + return nil |
| 49 | + } |
| 50 | + totalSize, err := common.ConsumedReferenceScriptSize(tx, ls) |
| 51 | + if err != nil { |
| 52 | + return err |
| 53 | + } |
| 54 | + if totalSize > MaxRefScriptSizePerTx { |
| 55 | + return common.RefScriptSizePerTxTooLargeError{ |
| 56 | + TxSize: totalSize, |
| 57 | + MaxSize: MaxRefScriptSizePerTx, |
| 58 | + } |
| 59 | + } |
| 60 | + return nil |
| 61 | +} |
| 62 | + |
| 63 | +// ValidateRefScriptSizePerBlock checks the Conway block limit. The optional |
| 64 | +// state parameter keeps the original two-argument calling shape usable for |
| 65 | +// publishing-only blocks while allowing consumed scripts to be resolved. |
| 66 | +func ValidateRefScriptSizePerBlock( |
| 67 | + block *ConwayBlock, |
| 68 | + pp common.ProtocolParameters, |
| 69 | + utxoStates ...common.UtxoState, |
| 70 | +) error { |
| 71 | + conwayPparams, ok := pp.(*ConwayProtocolParameters) |
| 72 | + if !ok { |
| 73 | + return errors.New("pparams are not expected type") |
| 74 | + } |
| 75 | + if len(utxoStates) > 1 { |
| 76 | + return errors.New("expected at most one ledger state") |
| 77 | + } |
| 78 | + var utxoState common.UtxoState |
| 79 | + if len(utxoStates) == 1 { |
| 80 | + utxoState = utxoStates[0] |
| 81 | + } |
| 82 | + totalSize, err := common.ConsumedReferenceScriptSizePerBlock( |
| 83 | + block, |
| 84 | + utxoState, |
| 85 | + conwayPparams.ProtocolVersion.Major >= common.ProtocolVersionVanRossem, |
| 86 | + nil, |
| 87 | + ) |
| 88 | + if err != nil { |
| 89 | + return err |
| 90 | + } |
| 91 | + if totalSize > MaxRefScriptSizePerBlock { |
| 92 | + return common.RefScriptSizePerBlockTooLargeError{ |
| 93 | + BlockSize: totalSize, |
| 94 | + MaxSize: MaxRefScriptSizePerBlock, |
| 95 | + } |
| 96 | + } |
| 97 | + return nil |
| 98 | +} |
| 99 | + |
| 100 | +// CalculateRefScriptFee calculates the tiered reference-script fee and floors |
| 101 | +// the exact rational result once, after all tiers have been accumulated. |
| 102 | +func CalculateRefScriptFee( |
| 103 | + scriptSize uint64, |
| 104 | + baseCost *cbor.Rat, |
| 105 | + stride uint64, |
| 106 | + multiplier *cbor.Rat, |
| 107 | +) (uint64, error) { |
| 108 | + if scriptSize == 0 { |
| 109 | + return 0, nil |
| 110 | + } |
| 111 | + if baseCost == nil || baseCost.Rat == nil || baseCost.Sign() < 0 { |
| 112 | + return 0, errors.New("invalid reference-script base cost") |
| 113 | + } |
| 114 | + if stride == 0 { |
| 115 | + return 0, errors.New("reference-script cost stride must be greater than zero") |
| 116 | + } |
| 117 | + if multiplier == nil || multiplier.Rat == nil || multiplier.Sign() <= 0 { |
| 118 | + return 0, errors.New("invalid reference-script cost multiplier") |
| 119 | + } |
| 120 | + price := new(big.Rat).Set(baseCost.Rat) |
| 121 | + total := new(big.Rat) |
| 122 | + remaining := scriptSize |
| 123 | + for remaining > 0 { |
| 124 | + tierSize := min(remaining, stride) |
| 125 | + tierCost := new(big.Rat).Mul( |
| 126 | + price, |
| 127 | + new(big.Rat).SetInt(new(big.Int).SetUint64(tierSize)), |
| 128 | + ) |
| 129 | + total.Add(total, tierCost) |
| 130 | + remaining -= tierSize |
| 131 | + price.Mul(price, multiplier.Rat) |
| 132 | + } |
| 133 | + fee := new(big.Int).Quo(total.Num(), total.Denom()) |
| 134 | + if !fee.IsUint64() { |
| 135 | + return 0, fmt.Errorf("reference-script fee overflow: %s", fee) |
| 136 | + } |
| 137 | + return fee.Uint64(), nil |
| 138 | +} |
| 139 | + |
| 140 | +// MinFeeTxWithRefScriptSize adds the Conway tiered reference-script fee to |
| 141 | +// the size-based transaction fee. Callers that already resolved the UTxO set |
| 142 | +// can use this function without repeating the lookup. |
| 143 | +func MinFeeTxWithRefScriptSize( |
| 144 | + tx common.Transaction, |
| 145 | + pparams common.ProtocolParameters, |
| 146 | + scriptSize uint64, |
| 147 | +) (uint64, error) { |
| 148 | + conwayPparams, ok := pparams.(*ConwayProtocolParameters) |
| 149 | + if !ok { |
| 150 | + return 0, errors.New("pparams are not expected type") |
| 151 | + } |
| 152 | + baseFee, err := MinFeeTx(tx, pparams) |
| 153 | + if err != nil { |
| 154 | + return 0, err |
| 155 | + } |
| 156 | + refScriptFee, err := CalculateRefScriptFee( |
| 157 | + scriptSize, |
| 158 | + conwayPparams.MinFeeRefScriptCostPerByte, |
| 159 | + RefScriptCostStride, |
| 160 | + &cbor.Rat{Rat: big.NewRat(6, 5)}, |
| 161 | + ) |
| 162 | + if err != nil { |
| 163 | + return 0, err |
| 164 | + } |
| 165 | + if baseFee > math.MaxUint64-refScriptFee { |
| 166 | + return 0, errors.New("minimum transaction fee overflow") |
| 167 | + } |
| 168 | + return baseFee + refScriptFee, nil |
| 169 | +} |
| 170 | + |
| 171 | +// MinFeeTxWithUtxo calculates the Conway minimum fee using the same consumed |
| 172 | +// reference-script set as the transaction and block size limits. |
| 173 | +func MinFeeTxWithUtxo( |
| 174 | + tx common.Transaction, |
| 175 | + pparams common.ProtocolParameters, |
| 176 | + utxoState common.UtxoState, |
| 177 | +) (uint64, error) { |
| 178 | + scriptSize, err := common.ConsumedReferenceScriptSize(tx, utxoState) |
| 179 | + if err != nil { |
| 180 | + return 0, err |
| 181 | + } |
| 182 | + return MinFeeTxWithRefScriptSize(tx, pparams, scriptSize) |
| 183 | +} |
0 commit comments