交易的数据结构

交易的数据结构定义在core.types.transaction.go中,结构如下:

  1. type Transaction struct {
  2. data txdata
  3. // caches
  4. hash atomic.Value
  5. size atomic.Value
  6. from atomic.Value
  7. }

交易的结构体中只有一个data字段,是txdata类型的。其他的hash,size,from都是缓存。 txdata结构体定义如下:

  1. type txdata struct {
  2. AccountNonce uint64 `json:"nonce" gencodec:"required"`
  3. Price *big.Int `json:"gasPrice" gencodec:"required"`
  4. GasLimit uint64 `json:"gas" gencodec:"required"`
  5. Recipient *common.Address `json:"to" rlp:"nil"` // nil means contract creation
  6. Amount *big.Int `json:"value" gencodec:"required"`
  7. Payload []byte `json:"input" gencodec:"required"`
  8. // Signature values
  9. V *big.Int `json:"v" gencodec:"required"`
  10. R *big.Int `json:"r" gencodec:"required"`
  11. S *big.Int `json:"s" gencodec:"required"`
  12. // This is only used when marshaling to JSON.
  13. Hash *common.Hash `json:"hash" rlp:"-"`
  14. }

AccountNonce是交易发送者已经发送交易的次数 Price是此交易的gas费用 GasLimit是本次交易允许消耗gas的最大数量 Recipient是交易的接收者 Amount是交易的以太坊数量 Payload是交易携带的数据 V,R,S是交易的签名数据 这里没有交易的发起者,因为发起者可以通过签名的数据获得。

交易的hash

交易的hash会首先从Transaction的缓存中读取hash,如果缓存中没有,则通过rlpHash来计算hash,并将hash放入到缓存中。 交易的hash是通过Hash()方法获得的。

  1. // Hash hashes the RLP encoding of tx.
  2. // It uniquely identifies the transaction.
  3. func (tx *Transaction) Hash() common.Hash {
  4. if hash := tx.hash.Load(); hash != nil {
  5. return hash.(common.Hash)
  6. }
  7. v := rlpHash(tx)
  8. tx.hash.Store(v)
  9. return v
  10. }

这里交易的hash实际上是对Transaction结构体重的data字段进行hash得到的结果。

交易类型

目前交易有两种类型 第一种是以太坊转账,这里在创建交易时需要在sendTransaction写入to字段,即写转到的地址。 第二种是合约交易,以太坊代码中定义在发送合约交易时,sendTransaction中的to字段置空,这样就能够知道是合约交易。 在执行交易时,在命令行中调用eth.sendTransaction即可执行交易。 sendTransaction具体的实现在account下的eth account analysis.md文件中。