|
| 1 | +// Copyright 2024 The Celo Authors |
| 2 | +// This file is part of the celo library. |
| 3 | +// |
| 4 | +// The celo library is free software: you can redistribute it and/or modify |
| 5 | +// it under the terms of the GNU Lesser General Public License as published by |
| 6 | +// the Free Software Foundation, either version 3 of the License, or |
| 7 | +// (at your option) any later version. |
| 8 | +// |
| 9 | +// The celo library is distributed in the hope that it will be useful, |
| 10 | +// but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | +// GNU Lesser General Public License for more details. |
| 13 | +// |
| 14 | +// You should have received a copy of the GNU Lesser General Public License |
| 15 | +// along with the celo library. If not, see <http://www.gnu.org/licenses/>. |
| 16 | + |
| 17 | +package types |
| 18 | + |
| 19 | +import ( |
| 20 | + "bytes" |
| 21 | + "math/big" |
| 22 | + |
| 23 | + "github.com/ethereum/go-ethereum/common" |
| 24 | + "github.com/ethereum/go-ethereum/rlp" |
| 25 | +) |
| 26 | + |
| 27 | +const CeloDynamicFeeTxV2Type = 0x7b |
| 28 | + |
| 29 | +// CeloDynamicFeeTxV2 represents a CIP-64 transaction. |
| 30 | +type CeloDynamicFeeTxV2 struct { |
| 31 | + ChainID *big.Int |
| 32 | + Nonce uint64 |
| 33 | + GasTipCap *big.Int |
| 34 | + GasFeeCap *big.Int |
| 35 | + Gas uint64 |
| 36 | + To *common.Address `rlp:"nil"` // nil means contract creation |
| 37 | + Value *big.Int |
| 38 | + Data []byte |
| 39 | + AccessList AccessList |
| 40 | + |
| 41 | + FeeCurrency *common.Address `rlp:"nil"` // nil means native currency |
| 42 | + |
| 43 | + // Signature values |
| 44 | + V *big.Int `json:"v" gencodec:"required"` |
| 45 | + R *big.Int `json:"r" gencodec:"required"` |
| 46 | + S *big.Int `json:"s" gencodec:"required"` |
| 47 | +} |
| 48 | + |
| 49 | +// copy creates a deep copy of the transaction data and initializes all fields. |
| 50 | +func (tx *CeloDynamicFeeTxV2) copy() TxData { |
| 51 | + cpy := &CeloDynamicFeeTxV2{ |
| 52 | + Nonce: tx.Nonce, |
| 53 | + To: copyAddressPtr(tx.To), |
| 54 | + Data: common.CopyBytes(tx.Data), |
| 55 | + Gas: tx.Gas, |
| 56 | + FeeCurrency: copyAddressPtr(tx.FeeCurrency), |
| 57 | + // These are copied below. |
| 58 | + AccessList: make(AccessList, len(tx.AccessList)), |
| 59 | + Value: new(big.Int), |
| 60 | + ChainID: new(big.Int), |
| 61 | + GasTipCap: new(big.Int), |
| 62 | + GasFeeCap: new(big.Int), |
| 63 | + V: new(big.Int), |
| 64 | + R: new(big.Int), |
| 65 | + S: new(big.Int), |
| 66 | + } |
| 67 | + copy(cpy.AccessList, tx.AccessList) |
| 68 | + if tx.Value != nil { |
| 69 | + cpy.Value.Set(tx.Value) |
| 70 | + } |
| 71 | + if tx.ChainID != nil { |
| 72 | + cpy.ChainID.Set(tx.ChainID) |
| 73 | + } |
| 74 | + if tx.GasTipCap != nil { |
| 75 | + cpy.GasTipCap.Set(tx.GasTipCap) |
| 76 | + } |
| 77 | + if tx.GasFeeCap != nil { |
| 78 | + cpy.GasFeeCap.Set(tx.GasFeeCap) |
| 79 | + } |
| 80 | + if tx.V != nil { |
| 81 | + cpy.V.Set(tx.V) |
| 82 | + } |
| 83 | + if tx.R != nil { |
| 84 | + cpy.R.Set(tx.R) |
| 85 | + } |
| 86 | + if tx.S != nil { |
| 87 | + cpy.S.Set(tx.S) |
| 88 | + } |
| 89 | + return cpy |
| 90 | +} |
| 91 | + |
| 92 | +// accessors for innerTx. |
| 93 | +func (tx *CeloDynamicFeeTxV2) txType() byte { return CeloDynamicFeeTxV2Type } |
| 94 | +func (tx *CeloDynamicFeeTxV2) chainID() *big.Int { return tx.ChainID } |
| 95 | +func (tx *CeloDynamicFeeTxV2) accessList() AccessList { return tx.AccessList } |
| 96 | +func (tx *CeloDynamicFeeTxV2) data() []byte { return tx.Data } |
| 97 | +func (tx *CeloDynamicFeeTxV2) gas() uint64 { return tx.Gas } |
| 98 | +func (tx *CeloDynamicFeeTxV2) gasFeeCap() *big.Int { return tx.GasFeeCap } |
| 99 | +func (tx *CeloDynamicFeeTxV2) gasTipCap() *big.Int { return tx.GasTipCap } |
| 100 | +func (tx *CeloDynamicFeeTxV2) gasPrice() *big.Int { return tx.GasFeeCap } |
| 101 | +func (tx *CeloDynamicFeeTxV2) value() *big.Int { return tx.Value } |
| 102 | +func (tx *CeloDynamicFeeTxV2) nonce() uint64 { return tx.Nonce } |
| 103 | +func (tx *CeloDynamicFeeTxV2) to() *common.Address { return tx.To } |
| 104 | +func (tx *CeloDynamicFeeTxV2) isSystemTx() bool { return false } |
| 105 | + |
| 106 | +func (tx *CeloDynamicFeeTxV2) effectiveGasPrice(dst *big.Int, baseFee *big.Int) *big.Int { |
| 107 | + if baseFee == nil { |
| 108 | + return dst.Set(tx.GasFeeCap) |
| 109 | + } |
| 110 | + tip := dst.Sub(tx.GasFeeCap, baseFee) |
| 111 | + if tip.Cmp(tx.GasTipCap) > 0 { |
| 112 | + tip.Set(tx.GasTipCap) |
| 113 | + } |
| 114 | + return tip.Add(tip, baseFee) |
| 115 | +} |
| 116 | + |
| 117 | +func (tx *CeloDynamicFeeTxV2) rawSignatureValues() (v, r, s *big.Int) { |
| 118 | + return tx.V, tx.R, tx.S |
| 119 | +} |
| 120 | + |
| 121 | +func (tx *CeloDynamicFeeTxV2) setSignatureValues(chainID, v, r, s *big.Int) { |
| 122 | + tx.ChainID, tx.V, tx.R, tx.S = chainID, v, r, s |
| 123 | +} |
| 124 | + |
| 125 | +func (tx *CeloDynamicFeeTxV2) encode(b *bytes.Buffer) error { |
| 126 | + return rlp.Encode(b, tx) |
| 127 | +} |
| 128 | + |
| 129 | +func (tx *CeloDynamicFeeTxV2) decode(input []byte) error { |
| 130 | + return rlp.DecodeBytes(input, tx) |
| 131 | +} |
0 commit comments