github.com/amazechain/amc@v0.1.3/common/transaction/legacy_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/holiman/uint256"
    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 *uint256.Int   // wei per gas
    29  	Gas      uint64         // gas limit
    30  	To       *types.Address `rlp:"nil"` // nil means contract creation
    31  	From     *types.Address `rlp:"nil"` // nil means contract creation
    32  	Value    *uint256.Int   // wei amount
    33  	Data     []byte         // contract invocation input data
    34  	V, R, S  *uint256.Int   // signature values
    35  	Sign     []byte
    36  }
    37  
    38  // NewTransaction creates an unsigned legacy transaction.
    39  // Deprecated: use NewTx instead.
    40  func NewTransaction(nonce uint64, from types.Address, to *types.Address, amount *uint256.Int, gasLimit uint64, gasPrice *uint256.Int, data []byte) *Transaction {
    41  	return NewTx(&LegacyTx{
    42  		Nonce:    nonce,
    43  		To:       to,
    44  		From:     &from,
    45  		Value:    amount,
    46  		Gas:      gasLimit,
    47  		GasPrice: gasPrice,
    48  		Data:     data,
    49  	})
    50  }
    51  
    52  // NewContractCreation creates an unsigned legacy transaction.
    53  // Deprecated: use NewTx instead.
    54  func NewContractCreation(nonce uint64, amount *uint256.Int, gasLimit uint64, gasPrice *uint256.Int, data []byte) *Transaction {
    55  	return NewTx(&LegacyTx{
    56  		Nonce:    nonce,
    57  		Value:    amount,
    58  		Gas:      gasLimit,
    59  		GasPrice: gasPrice,
    60  		Data:     data,
    61  	})
    62  }
    63  
    64  // copy creates a deep copy of the transaction data and initializes all fields.
    65  func (tx *LegacyTx) copy() TxData {
    66  	cpy := &LegacyTx{
    67  		Nonce: tx.Nonce,
    68  		To:    copyAddressPtr(tx.To),
    69  		From:  copyAddressPtr(tx.From),
    70  		Data:  types.CopyBytes(tx.Data),
    71  		Gas:   tx.Gas,
    72  		// These are initialized below.
    73  		Value:    new(uint256.Int),
    74  		GasPrice: new(uint256.Int),
    75  		V:        new(uint256.Int),
    76  		R:        new(uint256.Int),
    77  		S:        new(uint256.Int),
    78  	}
    79  	if tx.Value != nil {
    80  		cpy.Value.Set(tx.Value)
    81  	}
    82  	if tx.GasPrice != nil {
    83  		cpy.GasPrice.Set(tx.GasPrice)
    84  	}
    85  	if tx.V != nil {
    86  		cpy.V.Set(tx.V)
    87  	}
    88  	if tx.R != nil {
    89  		cpy.R.Set(tx.R)
    90  	}
    91  	if tx.S != nil {
    92  		cpy.S.Set(tx.S)
    93  	}
    94  	if tx.sign() != nil {
    95  		copy(cpy.Sign, tx.Sign)
    96  	}
    97  
    98  	return cpy
    99  }
   100  
   101  // accessors for innerTx.
   102  func (tx *LegacyTx) txType() byte { return LegacyTxType }
   103  func (tx *LegacyTx) chainID() *uint256.Int {
   104  	return DeriveChainId(tx.V)
   105  }
   106  func (tx *LegacyTx) accessList() AccessList  { return nil }
   107  func (tx *LegacyTx) data() []byte            { return tx.Data }
   108  func (tx *LegacyTx) gas() uint64             { return tx.Gas }
   109  func (tx *LegacyTx) gasPrice() *uint256.Int  { return tx.GasPrice }
   110  func (tx *LegacyTx) gasTipCap() *uint256.Int { return tx.GasPrice }
   111  func (tx *LegacyTx) gasFeeCap() *uint256.Int { return tx.GasPrice }
   112  func (tx *LegacyTx) value() *uint256.Int     { return tx.Value }
   113  func (tx *LegacyTx) nonce() uint64           { return tx.Nonce }
   114  func (tx *LegacyTx) to() *types.Address      { return tx.To }
   115  func (tx *LegacyTx) from() *types.Address    { return tx.From }
   116  func (tx *LegacyTx) sign() []byte            { return tx.Sign }
   117  
   118  func (tx *LegacyTx) rawSignatureValues() (v, r, s *uint256.Int) {
   119  	return tx.V, tx.R, tx.S
   120  }
   121  
   122  func (tx *LegacyTx) setSignatureValues(chainID, v, r, s *uint256.Int) {
   123  	tx.V, tx.R, tx.S = v, r, s
   124  }
   125  
   126  func (tx *LegacyTx) hash() types.Hash {
   127  	hash := hash.RlpHash([]interface{}{
   128  		tx.Nonce,
   129  		tx.GasPrice,
   130  		tx.Gas,
   131  		tx.To,
   132  		tx.Value,
   133  		tx.Data,
   134  		tx.V, tx.R, tx.S,
   135  	})
   136  	return hash
   137  }