transaction数据结构

我们对比ethereum和ep的transaction的数据结构:

下面是ethereum的transaction的数据结构:

  1. type Transaction struct {
  2. inner TxData // Consensus contents of a transaction
  3. time time.Time // Time first seen locally (spam avoidance)
  4. // caches
  5. hash atomic.Value
  6. size atomic.Value
  7. from atomic.Value
  8. }

其中txData数据结构有三种实现:DynamicFeeTx, LegacyTx and AccessListTx.

  1. type DynamicFeeTx struct {
  2. ChainID *big.Int
  3. Nonce uint64
  4. GasTipCap *big.Int // a.k.a. maxPriorityFeePerGas
  5. GasFeeCap *big.Int // a.k.a. maxFeePerGas
  6. Gas uint64
  7. To *common.Address `rlp:"nil"` // nil means contract creation
  8. Value *big.Int
  9. Data []byte
  10. AccessList AccessList
  11. // Signature values
  12. //`r and s are outputs of an ECDSA signature, and`v`is the recovery id
  13. V *big.Int `json:"v" gencodec:"required"`
  14. R *big.Int `json:"r" gencodec:"required"`
  15. S *big.Int `json:"s" gencodec:"required"`
  16. }
  1. // LegacyTx is the transaction data of regular Ethereum transactions.
  2. type LegacyTx struct {
  3. Nonce uint64 // nonce of sender account
  4. GasPrice *big.Int // wei per gas
  5. Gas uint64 // gas limit
  6. To *common.Address `rlp:"nil"` // nil means contract creation
  7. Value *big.Int // wei amount
  8. Data []byte // contract invocation input data
  9. V, R, S *big.Int // signature values
  10. }
  1. // AccessListTx is the data of EIP-2930 access list transactions.
  2. type AccessListTx struct {
  3. ChainID *big.Int // destination chain ID
  4. Nonce uint64 // nonce of sender account
  5. GasPrice *big.Int // wei per gas
  6. Gas uint64 // gas limit
  7. To *common.Address `rlp:"nil"` // nil means contract creation
  8. Value *big.Int // wei amount
  9. Data []byte // contract invocation input data
  10. AccessList AccessList // EIP-2930 access list
  11. V, R, S *big.Int // signature values
  12. }

其中LegacyTx是历史上的ethereum的结构,他们还是有些区别,其它两种都与协议有关。另外,最开始的ethereum的transaction结构是没有chainID的。

我们看看ep的transaction数据结构:

  1. type Transaction struct {
  2. Nonce uint64
  3. GasPrice *big.Int
  4. Gas uint64
  5. To *Address
  6. Value *big.Int
  7. Input []byte
  8. V *big.Int
  9. R *big.Int
  10. S *big.Int
  11. Hash Hash
  12. From Address
  13. // Cache
  14. size atomic.Value
  15. }

这个结构与上面的LegacyTx相比,多了From字段和Cache字段,对比其它的结构,对跨链数据交换影响不大(以太坊必须向后兼容LegacyTx)。

下面是对transaction的关键字段的说明:

  • nonce: The number of transaction serial numbers sent by this account (which can be roughly understood as “this is the first transaction of this account”).
  • gasPrice: The fee (measured in Wei) paid per unit of gas to perform this transaction and perform calculations.
  • gasLimit: The maximum amount of gas that can be used when executing this transaction.
  • to: If this transaction is used to send ether, here is the EOA address that receives ether; if this transaction is used to send a message to the contract (for example, calling a method in a smart contract), here is the address of the contract; if this The transaction is used to create the contract, here the value is empty.
  • value: If this transaction is used to send and receive ether, here is the amount of tokens measured in Wei in the receiving account; if this transaction is used to send a message call to the contract, here is the amount of Wei paid to the smart contract that receives this message ; if this transaction is used to create a contract, here is the amount of ether in Wei stored in the account when the contract was initialized.
  • v, r, s: Values used in the cryptographic signature of the transaction, which can be used to determine the sender of the transaction.
  • data(only used for value transfer and sending message calls to smart contracts) input data that accompanies the message call (for example, if you want to execute a setter method in a smart contract, the data field should include the identifier of the setter method, and the parameter value you want to set).

    All transactions in the block are also stored in the Merkle tree. And the root node hash value of this tree is saved by the block header!