1. 程式人生 > >區塊鏈入門教程以太坊原始碼分析交易資料分析eth

區塊鏈入門教程以太坊原始碼分析交易資料分析eth

交易的資料結構

交易的資料結構定義在core.types.transaction.go中,結構如下:

type Transaction struct {
data txdata
// caches
hash atomic.Value
size atomic.Value
from atomic.Value
}

交易的結構體中只有一個data欄位,是txdata型別的。其他的hash,size,from都是快取。
txdata結構體定義如下:

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

AccountNonce是交易傳送者已經發送交易的次數
Price是此交易的gas費用
GasLimit是本次交易允許消耗gas的最大數量
Recipient是交易的接收者
Amount是交易的以太坊數量
Payload是交易攜帶的資料
V,R,S是交易的簽名資料
這裡沒有交易的發起者,因為發起者可以通過簽名的資料獲得。

交易的hash

交易的hash會首先從Transaction的快取中讀取hash,如果快取中沒有,則通過rlpHash來計算hash,並將hash放入到快取中。
交易的hash是通過Hash()方法獲得的。

// Hash hashes the RLP encoding of tx.
// It uniquely identifies the transaction.
func (tx *Transaction) Hash() common.Hash {
if hash := tx.hash.Load(); hash != nil {
return hash.(common.Hash)
}
v := rlpHash(tx)
tx.hash.Store(v)
return v
}

這裡交易的hash實際上是對Transaction結構體重的data欄位進行hash得到的結果。
##交易型別
目前交易有兩種型別
第一種是以太坊轉賬,這裡在建立交易時需要在sendTransaction寫入to欄位,即寫轉到的地址。
第二種是合約交易,以太坊程式碼中定義在傳送合約交易時,sendTransaction中的to欄位置空,這樣就能夠知道是合約交易。
在執行交易時,在命令列中呼叫eth.sendTransaction即可執行交易。
sendTransaction具體的實現在account下的eth account analysis.md檔案中。