github.com/amazechain/amc@v0.1.3/common/transaction/dynamic_fee_tx.go (about)

     1  // Copyright 2022 The AmazeChain Authors
     2  // This file is part of the AmazeChain library.
     3  //
     4  // The AmazeChain 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 AmazeChain 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 AmazeChain library. If not, see <http://www.gnu.org/licenses/>.
    16  
    17  package transaction
    18  
    19  import (
    20  	"github.com/amazechain/amc/common/hash"
    21  	"github.com/amazechain/amc/common/types"
    22  	"github.com/amazechain/amc/internal/avm/common"
    23  	"github.com/holiman/uint256"
    24  )
    25  
    26  type DynamicFeeTx struct {
    27  	ChainID    *uint256.Int
    28  	Nonce      uint64
    29  	GasTipCap  *uint256.Int // a.k.a. maxPriorityFeePerGas
    30  	GasFeeCap  *uint256.Int // a.k.a. maxFeePerGas
    31  	Gas        uint64
    32  	To         *types.Address `rlp:"nil"` // nil means contract creation
    33  	From       *types.Address `rlp:"nil"` // nil means contract creation
    34  	Value      *uint256.Int
    35  	Data       []byte
    36  	AccessList AccessList
    37  	Sign       []byte       // Signature values
    38  	V, R, S    *uint256.Int // signature values
    39  }
    40  
    41  // copy creates a deep copy of the transaction data and initializes all fields.
    42  func (tx *DynamicFeeTx) copy() TxData {
    43  	cpy := &DynamicFeeTx{
    44  		Nonce: tx.Nonce,
    45  		To:    copyAddressPtr(tx.To),
    46  		From:  copyAddressPtr(tx.From),
    47  		Data:  common.CopyBytes(tx.Data),
    48  		Gas:   tx.Gas,
    49  		// These are copied below.
    50  		AccessList: make(AccessList, len(tx.AccessList)),
    51  		Value:      new(uint256.Int),
    52  		ChainID:    new(uint256.Int),
    53  		GasTipCap:  new(uint256.Int),
    54  		GasFeeCap:  new(uint256.Int),
    55  		V:          new(uint256.Int),
    56  		R:          new(uint256.Int),
    57  		S:          new(uint256.Int),
    58  	}
    59  	copy(cpy.AccessList, tx.AccessList)
    60  	if tx.Value != nil {
    61  		cpy.Value.Set(tx.Value)
    62  	}
    63  	if tx.ChainID != nil {
    64  		cpy.ChainID.Set(tx.ChainID)
    65  	}
    66  	if tx.GasTipCap != nil {
    67  		cpy.GasTipCap.Set(tx.GasTipCap)
    68  	}
    69  	if tx.GasFeeCap != nil {
    70  		cpy.GasFeeCap.Set(tx.GasFeeCap)
    71  	}
    72  	if tx.V != nil {
    73  		cpy.V.Set(tx.V)
    74  	}
    75  	if tx.R != nil {
    76  		cpy.R.Set(tx.R)
    77  	}
    78  	if tx.S != nil {
    79  		cpy.S.Set(tx.S)
    80  	}
    81  	if tx.Sign != nil {
    82  		copy(cpy.Sign, tx.Sign)
    83  	}
    84  	return cpy
    85  }
    86  
    87  // accessors for innerTx.
    88  func (tx *DynamicFeeTx) txType() byte            { return DynamicFeeTxType }
    89  func (tx *DynamicFeeTx) chainID() *uint256.Int   { return tx.ChainID }
    90  func (tx *DynamicFeeTx) accessList() AccessList  { return tx.AccessList }
    91  func (tx *DynamicFeeTx) data() []byte            { return tx.Data }
    92  func (tx *DynamicFeeTx) gas() uint64             { return tx.Gas }
    93  func (tx *DynamicFeeTx) gasFeeCap() *uint256.Int { return tx.GasFeeCap }
    94  func (tx *DynamicFeeTx) gasTipCap() *uint256.Int { return tx.GasTipCap }
    95  func (tx *DynamicFeeTx) gasPrice() *uint256.Int  { return tx.GasFeeCap }
    96  func (tx *DynamicFeeTx) value() *uint256.Int     { return tx.Value }
    97  func (tx *DynamicFeeTx) nonce() uint64           { return tx.Nonce }
    98  func (tx *DynamicFeeTx) to() *types.Address      { return tx.To }
    99  func (tx *DynamicFeeTx) from() *types.Address    { return tx.From }
   100  func (tx *DynamicFeeTx) sign() []byte            { return tx.Sign }
   101  
   102  // Hash computes the hash (but not for signatures!)
   103  func (tx *DynamicFeeTx) hash() types.Hash {
   104  	hash := hash.PrefixedRlpHash(DynamicFeeTxType, []interface{}{
   105  		tx.ChainID,
   106  		tx.Nonce,
   107  		tx.GasTipCap,
   108  		tx.GasFeeCap,
   109  		tx.Gas,
   110  		tx.To,
   111  		tx.Value,
   112  		tx.Data,
   113  		tx.AccessList,
   114  		tx.V, tx.R, tx.S,
   115  	})
   116  	return hash
   117  }
   118  
   119  func (tx *DynamicFeeTx) rawSignatureValues() (v, r, s *uint256.Int) {
   120  	return tx.V, tx.R, tx.S
   121  }
   122  
   123  func (tx *DynamicFeeTx) setSignatureValues(chainID, v, r, s *uint256.Int) {
   124  	tx.ChainID, tx.V, tx.R, tx.S = chainID, v, r, s
   125  }