github.com/amazechain/amc@v0.1.3/internal/avm/types/legacy_tx.go (about)

     1  // Copyright 2023 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 types
    18  
    19  import (
    20  	"math/big"
    21  
    22  	"github.com/amazechain/amc/internal/avm/common"
    23  )
    24  
    25  // LegacyTx is the transaction data of regular Ethereum transactions.
    26  type LegacyTx struct {
    27  	Nonce    uint64          // nonce of sender account
    28  	GasPrice *big.Int        // wei per gas
    29  	Gas      uint64          // gas limit
    30  	To       *common.Address `rlp:"nil"` // nil means contract creation
    31  	Value    *big.Int        // wei amount
    32  	Data     []byte          // contract invocation input data
    33  	V, R, S  *big.Int        // signature values
    34  }
    35  
    36  // NewTransaction creates an unsigned legacy transaction.
    37  // Deprecated: use NewTx instead.
    38  func NewTransaction(nonce uint64, to common.Address, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte) *Transaction {
    39  	return NewTx(&LegacyTx{
    40  		Nonce:    nonce,
    41  		To:       &to,
    42  		Value:    amount,
    43  		Gas:      gasLimit,
    44  		GasPrice: gasPrice,
    45  		Data:     data,
    46  	})
    47  }
    48  
    49  // NewContractCreation creates an unsigned legacy transaction.
    50  // Deprecated: use NewTx instead.
    51  func NewContractCreation(nonce uint64, amount *big.Int, gasLimit uint64, gasPrice *big.Int, data []byte) *Transaction {
    52  	return NewTx(&LegacyTx{
    53  		Nonce:    nonce,
    54  		Value:    amount,
    55  		Gas:      gasLimit,
    56  		GasPrice: gasPrice,
    57  		Data:     data,
    58  	})
    59  }
    60  
    61  // copy creates a deep copy of the transaction data and initializes all fields.
    62  func (tx *LegacyTx) copy() TxData {
    63  	cpy := &LegacyTx{
    64  		Nonce: tx.Nonce,
    65  		To:    copyAddressPtr(tx.To),
    66  		Data:  common.CopyBytes(tx.Data),
    67  		Gas:   tx.Gas,
    68  		// These are initialized below.
    69  		Value:    new(big.Int),
    70  		GasPrice: new(big.Int),
    71  		V:        new(big.Int),
    72  		R:        new(big.Int),
    73  		S:        new(big.Int),
    74  	}
    75  	if tx.Value != nil {
    76  		cpy.Value.Set(tx.Value)
    77  	}
    78  	if tx.GasPrice != nil {
    79  		cpy.GasPrice.Set(tx.GasPrice)
    80  	}
    81  	if tx.V != nil {
    82  		cpy.V.Set(tx.V)
    83  	}
    84  	if tx.R != nil {
    85  		cpy.R.Set(tx.R)
    86  	}
    87  	if tx.S != nil {
    88  		cpy.S.Set(tx.S)
    89  	}
    90  	return cpy
    91  }
    92  
    93  // accessors for innerTx.
    94  func (tx *LegacyTx) txType() byte           { return LegacyTxType }
    95  func (tx *LegacyTx) chainID() *big.Int      { return deriveChainId(tx.V) }
    96  func (tx *LegacyTx) accessList() AccessList { return nil }
    97  func (tx *LegacyTx) data() []byte           { return tx.Data }
    98  func (tx *LegacyTx) gas() uint64            { return tx.Gas }
    99  func (tx *LegacyTx) gasPrice() *big.Int     { return tx.GasPrice }
   100  func (tx *LegacyTx) gasTipCap() *big.Int    { return tx.GasPrice }
   101  func (tx *LegacyTx) gasFeeCap() *big.Int    { return tx.GasPrice }
   102  func (tx *LegacyTx) value() *big.Int        { return tx.Value }
   103  func (tx *LegacyTx) nonce() uint64          { return tx.Nonce }
   104  func (tx *LegacyTx) to() *common.Address    { return tx.To }
   105  
   106  func (tx *LegacyTx) rawSignatureValues() (v, r, s *big.Int) {
   107  	return tx.V, tx.R, tx.S
   108  }
   109  
   110  func (tx *LegacyTx) setSignatureValues(chainID, v, r, s *big.Int) {
   111  	tx.V, tx.R, tx.S = v, r, s
   112  }