区块链学习笔记
区块头结构
定义如下
type Header struct {ParentHash common.Hash `json:"parentHash" gencodec:"required"`UncleHash common.Hash `json:"sha3Uncles" gencodec:"required"`Coinbase common.Address `json:"miner"`Root common.Hash `json:"stateRoot" gencodec:"required"`TxHash common.Hash `json:"transactionsRoot" gencodec:"required"`ReceiptHash common.Hash `json:"receiptsRoot" gencodec:"required"`Bloom Bloom `json:"logsBloom" gencodec:"required"`Difficulty *big.Int `json:"difficulty" gencodec:"required"`Number *big.Int `json:"number" gencodec:"required"`GasLimit uint64 `json:"gasLimit" gencodec:"required"`GasUsed uint64 `json:"gasUsed" gencodec:"required"`Time uint64 `json:"timestamp" gencodec:"required"`Extra []byte `json:"extraData" gencodec:"required"`MixDigest common.Hash `json:"mixHash"`Nonce BlockNonce `json:"nonce"`// BaseFee was added by EIP-1559 and is ignored in legacy headers.BaseFee *big.Int `json:"baseFeePerGas" rlp:"optional"`// WithdrawalsHash was added by EIP-4895 and is ignored in legacy headers.WithdrawalsHash *common.Hash `json:"withdrawalsRoot" rlp:"optional"`// BlobGasUsed was added by EIP-4844 and is ignored in legacy headers.BlobGasUsed *uint64 `json:"blobGasUsed" rlp:"optional"`// ExcessBlobGas was added by EIP-4844 and is ignored in legacy headers.ExcessBlobGas *uint64 `json:"excessBlobGas" rlp:"optional"`// ParentBeaconRoot was added by EIP-4788 and is ignored in legacy headers.ParentBeaconRoot *common.Hash `json:"parentBeaconBlockRoot" rlp:"optional"`// RequestsHash was added by EIP-7685 and is ignored in legacy headers.RequestsHash *common.Hash `json:"requestsHash" rlp:"optional"`
}
字段 | 含义 |
---|---|
ParentHash | 父区块哈希,指向前一个区块(父区块)的区块头哈希。这是构建区块链“链”结构的基础,确保了区块的顺序性和不可篡改性。 |
UncleHash | 叔块哈希,包含该区块引用的叔块(Ommer)列表的 Merkle 根哈希。在 PoW 时代用于激励矿工打包孤块,在 PoS 中此字段通常为空,但字段仍保留 |
Coinbase | 受益人地址,在 PoW 时代是矿工地址,用于接收区块奖励。在 PoS(权益证明) 时代,这是验证者(Validator)的提款地址(withdrawal address),用于接收出块奖励和交易费 |
Root | 状态根,这是极其关键的字段。它是执行完该区块中所有交易后,以太坊全局状态(账户余额、合约代码、存储等)的 Merkle 根哈希。任何状态的微小变化都会导致此哈希值完全不同,用于快速验证状态一致性 |
TxHash | 交易树根,该区块中包含的所有交易列表的 Merkle 根哈希。允许在不下载完整区块的情况下,高效地验证某笔交易是否包含在该区块中(Merkle Proof) |
ReceiptHash | 收据树根,该区块中所有交易执行收据(Receipts)的 Merkle 根哈希。收据包含交易执行结果(成功/失败)、消耗的 Gas、产生的日志(Logs)等。同样用于高效验证 |
Bloom | 布隆过滤器,基于 ReceiptHash 中所有交易日志(Logs)生成的一个布隆过滤器(Bloom Filter)。轻客户端可以使用它来快速判断某个区块是否可能包含与特定主题(Topic)或地址相关的日志,从而决定是否需要下载完整收据 |
Difficulty | 难度,在 PoW 时代用于共识算法。在 PoS 时代,此字段已被弃用,其值恒为 0。共识由权益和时间决定,而非计算难度 |
Number | 区块高度,该区块在区块链中的序号(从创世块 0 开始)。用于标识区块位置和计算分叉 |
GasLimit | Gas 限制,该区块所能消耗的最大 Gas 总量。由矿工/验证者设定,控制区块大小,防止无限循环等攻击 |
GasUsed | Gas 使用量,该区块中所有交易实际消耗的总 Gas 量。必须小于或等于 GasLimit |
Time | 时间戳,该区块被创建(或提议)时的Unix 时间戳(秒)。在 PoS 中,时间戳由共识层(Beacon Chain)决定,必须严格递增 |
Extra | 额外数据,一个最多 32 字节的自由字段。矿工/验证者可以在此写入自定义数据,如客户端版本、矿池标识等 |
MixDigest | 混合摘要, 在 PoW 时代与 Ethash 算法相关。在 PoS 时代,此字段已被弃用,其值恒为 0。 |
Nonce | 随机数,在 PoW 时代用于寻找满足难度要求的解。在 PoS 时代,此字段已被弃用,其值恒为 0 |
块结构
定义为
type Block struct {header *Headeruncles []*Headertransactions Transactionswithdrawals Withdrawals// witness is not an encoded part of the block body.// It is held in Block in order for easy relaying to the places// that process it.witness *ExecutionWitness// cacheshash atomic.Pointer[common.Hash]size atomic.Uint64// These fields are used by package eth to track// inter-peer block relay.ReceivedAt time.TimeReceivedFrom interface{}
}