以太坊交易结构解析和负载Payload
1. 交易中的几个参数介绍:
- To: 交易接收方地址(20Bytes)
- Value: 交易发送的ether数量(wei)
- Startgas:交易者愿意花费的最大Gas数量
- Gasprice: 交易者愿意支付的Gas价格
- startgas*gasprice = 最终ether数值
- nonce: 交易发起者填写的序列号,防止重放攻击
- Data:可变长度的二进制数据载荷
- V,R,S: ECDSA数字签名信息
Transaction代码
type Transaction struct {
//交易数据
data txdata
hash atomic.Value
size atomic.Value
//钱包根据 from来找到
//account := accounts.Account{Address: args.From}
from atomic.Value
}
type txdata struct {
//发送者发起的交易总数
AccountNonce uint64 `json:"nonce" gencodec:"required"`
//交易的Gas价格
Price *big.Int `json:"gasPrice" gencodec:"required"`
//交易允许消耗的最大Gas
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
Hash *common.Hash `json:"hash" rlp:"-"`
}
2. 创建交易
代码 internal/ethapi/api.go
// SendTxArgs represents the arguments to sumbit a new transaction into the transaction pool.
type SendTxArgs struct {
From common.Address `json:"from"`
To *common.Address `json:"to"`
Gas *hexutil.Uint64 `json:"gas"`
GasPrice *hexutil.Big `json:"gasPrice"`
Value *hexutil.Big `json:"value"`
Nonce *hexutil.Uint64 `json:"nonce"`
// We accept "data" and "input" for backwards-compatibility reasons. "input" is the
// newer name and should be preferred by clients.
Data *hexutil.Bytes `json:"data"`
Input *hexutil.Bytes `json:"input"`
}
3. 交易的 value 和 data
- 交易的主要“有效负载”包含在两个字段中:value 和 data。交易可以同时有 value 和 data,仅有 value,仅有 data,或者既没有 value 也没有 data。所有四种组合都有效。
- 仅有 value 的交易就是一笔以太的付款
- 仅有 data 的交易一般是合约调用
- 进行合约调用的同时,我们除了传输 data, 还可以发送以太,从而交易中同时包含 data 和 value
- 没有 value 也没有 data 的交易,只是在浪费 gas,但它是有效的
注意:以太坊单笔交易运行携带最大数据量为44KB
浙公网安备 33010602011771号