5.3 以太坊源码详解3
一、转账的概念和交易的基本流程
用户输入转入的地址和金额
系统用转账地址的私钥对交易进行签名(确保这笔交易是由发起交易的所有人)
对交易进行验证
存入交易缓存池
广播交易
二、交易的数据
type Transaction struct {
data txdata // 交易数据
// caches
hash atomic.Value // 交易哈希
size atomic.Value // 交易大小
from atomic.Value // 交易来源
}
type txdata struct {
AccountNonce uint64 //发送者发起的交易的总数量
Price, GasLimit *big.Int // gas价格与上线
Recipient *common.Address `rlp:"nil"` // nil means contract creation // 接收者的地址,如果该地址为空,代表其是一个合约的创建者
Amount *big.Int // 此次交易所转的以太币的数量
Payload []byte // 其他数据
V byte // signature // 交易签名数据
R, S *big.Int // signature 交易签名数据
}
三、哈希
type (
Hash [hashLength]byte // 哈希值
Address [addressLength]byte // 地址
)
func BytesToHash(b []byte) Hash {
var h Hash
h.SetBytes(b)
return h
}
四、区块数据结构
type Block struct {
header *Header // 区块头
uncles []*Header // 叔区块
transactions Transactions // 交易
receipts Receipts // 接收者
// caches
hash atomic.Value // 哈希
size atomic.Value // 区块大小
// Td is used by package core to store the total difficulty
// of the chain up to and including the block.
Td *big.Int // 总的难度值
// ReceivedAt is used by package eth to track block propagation time.
ReceivedAt time.Time // 接收时间
}
五、区块头数据结构
type Header struct {
ParentHash common.Hash // Hash to the previous block 上一个区块哈希
UncleHash common.Hash // Uncles of this block 叔区块的哈希
Coinbase common.Address // The coin base address 矿工接收奖励的地址
Root common.Hash // Block Trie state “State DB”的“state tired”的RLP的根节点的哈希值
TxHash common.Hash // Tx sha “state db”中“state tired”的RLP根节点哈希值
ReceiptHash common.Hash // Receipt sha “receipt tire”的RLP的根节点的哈希值
Bloom Bloom // Bloom 布隆过滤器
Difficulty *big.Int // Difficulty for the current block 区块难度
Number *big.Int // The block number 区块号
GasLimit *big.Int // Gas limit 理论上的gas上限
GasUsed *big.Int // Gas used 区块内所有交易执行所产生的gas的总和
Time uint64 // Creation time 区块创建时间
Extra []byte // Extra data
MixDigest common.Hash // for quick difficulty verification
Nonce BlockNonce // 随机数
}
六、创建新区块
// NewBlock creates a new block. The input data is copied,
// changes to header and to the field values will not affect the
// block.
//
// The values of TxHash, UncleHash, ReceiptHash and Bloom in header
// are ignored and set to values derived from the given txs, uncles
// and receipts.
func NewBlock(header *Header, txs []*Transaction, uncles []*Header, receipts []*Receipt) *Block {
b := &Block{header: copyHeader(header), Td: new(big.Int)}
// TODO: panic if len(txs) != len(receipts)
if len(txs) == 0 {
b.header.TxHash = emptyRootHash
} else {
b.header.TxHash = DeriveSha(Transactions(txs))
b.transactions = make(Transactions, len(txs))
copy(b.transactions, txs)
}
if len(receipts) == 0 {
b.header.ReceiptHash = emptyRootHash
} else {
b.header.ReceiptHash = DeriveSha(Receipts(receipts))
b.header.Bloom = CreateBloom(receipts)
b.receipts = make([]*Receipt, len(receipts))
copy(b.receipts, receipts)
}
if len(uncles) == 0 {
b.header.UncleHash = emptyUncleHash
} else {
b.header.UncleHash = CalcUncleHash(uncles)
b.uncles = make([]*Header, len(uncles))
for i := range uncles {
b.uncles[i] = copyHeader(uncles[i])
}
}
return b
}
-
学院Go语言视频主页
https://edu.csdn.net/lecturer/1928 -
扫码获取海量视频及源码 QQ群:721929980