相关命令分析
- eth.blockNumber
查看当前块高。 - eth.getBlock()
根据高度查看块内容。 - eth.syncing
获取同步状态,可以看到全网最新块高,提示用户同步进度。
区块分析
创世区块:
{
difficulty: 17179869184,
extraData: "0x11bbe8db4e347b4e8c937c1c8370e4b5ed33adb3db69cbdb7a38e1e50b1b82fa",
gasLimit: 5000,
gasUsed: 0,
hash: "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3",
logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
miner: "0x0000000000000000000000000000000000000000",
mixHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
nonce: "0x0000000000000042",
number: 0,
parentHash: "0x0000000000000000000000000000000000000000000000000000000000000000",
receiptsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
size: 540,
stateRoot: "0xd7f8974fb5ac78d9ac099b9ad5018bedc2ce0a72dad1827a1709da30580f0544",
timestamp: 0,
totalDifficulty: 17179869184,
transactions: [],
transactionsRoot: "0x56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421",
uncles: []
}
以太坊主网第410000的区块:
{
difficulty: 1390302857940436,
extraData: "0x7563776f6e67",
gasLimit: 6708771,
gasUsed: 105000,
hash: "0xa108580144142887e58cf074d4ea0be93b00c13ed1992d3897edb078fabe7118",
logsBloom: "0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000",
miner: "0x9435d50503aee35c8757ae4933f7a0ab56597805",
mixHash: "0xfe8975ce7e272e88243ce9ec91d1a826fa5fe930d9e18de6a72ef57b4f9aa954",
nonce: "0xf0d4aef0039682a8",
number: 4100000,
parentHash: "0xd060e9516fc2a26aab3728d7fad2f2301d9822e6b2100eff58a16abd185881a1",
receiptsRoot: "0xa36be6a4f6a8e9a63a6b90dfa174d5cc6e7662f0d05007f39ef8a14aab54da95",
sha3Uncles: "0x1dcc4de8dec75d7aab85b567b6ccd41ad312451b948a7413f0a142fd40d49347",
size: 1095,
stateRoot: "0xf49e0946c05d675cafd607613b009a3121a7e9ed6ed1f614a0c05e3bf6bebea1",
timestamp: 1501511212,
totalDifficulty: 595854527970155343606,
transactions: ["0x5728a5597f1dd7bb3e8843bfd6a8fe4156ae4605408d990c54dced5945e4a7af", "0x902aa2582c63328fcca071296a389b55a1c84983f1a3553ef05b1a14b16bd913", "0x452169cbce264f8d30074d2ddcb8caca2ba7e7b4effdca26301696c6a242ea5f", "0x9dee172d17a82354010ad18c9a7d32e49141b406d4802a6b0d5b25359809a1b3", "0xaa1b5f94f285a344d3e99f85529343daa3f72741752cad946a995e6544bd14ab"],
transactionsRoot: "0xe3eec697b2dcc74678edff5b022e65263c8428e47f11c25b9d4ba9d518632269",
uncles: []
}
可以看出从创世区块到现在以太坊的block结构都未做更改,表格中是以太坊黄皮书对块的数据结构的解释。黄皮书地址
key | value |
---|---|
difficulty | 表示当前区块的难度水平, 这个值根据前一个区块的难度水平和时间戳计算得到。 |
extraData | 附加数据,32字节以内的字节数组 |
gasLimit | 目前每个区块的燃料消耗上限 |
gasUsed | 当前区块的所有交易使用燃料之和 |
hash | block hash,可以理解为这个block在整个blockchain中的索引 |
logsBloom | 日记 Bloom 过滤器由可索引信息(日志地址和日志主题)组成,这个信息包含在每个日志入口, 来自交易列表中每个交易的接受者 |
miner | 打包这一个block的矿工地址 |
mixHash | 混合哈希, 与一个与随机数 (nonce)相关的 256 位哈希计算, 用于证明针对当前区块已经完成了足够的计算 |
nonce | 随机数, 一个 64 位哈希, 和计算混合哈希相关, 用于证明针对当前区块已经完成了足够的计算 |
number | 区块编号, 等于当前区块的直系前辈区块数量(创始区块的区块编号为0) |
parentHash | 父哈希(创世区块没有父哈希,所以parentHash为0x0000000000000000000000000000000000000000000000000000000000000000) |
receiptsRoot | 接受者字典树根节点哈希: 接受者字典树根节点的 Keccak 256 位哈希, 在接受者字典树含有区块中的所有交易信息中的接受者 |
sha3Uncles | ???? |
size | 猜测为block包体的大小 |
stateRoot | 状态字典树根节点哈希: 状态字典树根节点的 Keccak 256 位哈希, 交易打包到当前区块且区块定稿后可以生成这个值 |
timestamp | 时间戳, 当前区块初始化时的Unix时间戳 |
totalDifficulty | ??? |
transactions | block所包含的交易 |
transactionsRoot | 交易字典树根节点哈希: 交易字典树根节点的 Keccak 256 位哈希, 在交易字典树含有区块中的所有交易列表 |
- difficulty:是当前块的难度,值根据前一个区块的难度水平和时间戳计算得到。推荐大家看一下知乎上的一篇文章以太坊是如何计算难度的。具体计算公式如下,有兴趣的朋友可以尝试计算一下。(以太坊规定任何区块的难度都不可以低于创世区块)
periodCount = block_num / 100000
diff = (parent_diff + (parent_diff / 2048 * max(1 - (block_timestamp - parent_timestamp) / 10, -99)) ) + 2^(periodCount - 2)
- transactionsRoot:为了防止数据篡改,eth沿用的btc的MerkleTree证明,相关技术内容后续会做单独阐述。
Golang数据结构源码
位置:core/types/block.go
// Header represents a block header in the Ethereum blockchain.
type Header struct {
ParentHash common.Hash `json:"parentHash" gencodec:"required"`
UncleHash common.Hash `json:"sha3Uncles" gencodec:"required"`
Coinbase common.Address `json:"miner" gencodec:"required"`
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 *big.Int `json:"gasLimit" gencodec:"required"`
GasUsed *big.Int `json:"gasUsed" gencodec:"required"`
Time *big.Int `json:"timestamp" gencodec:"required"`
Extra []byte `json:"extraData" gencodec:"required"`
MixDigest common.Hash `json:"mixHash" gencodec:"required"`
Nonce BlockNonce `json:"nonce" gencodec:"required"`
}
// Block represents an entire block in the Ethereum blockchain.
type Block struct {
header *Header
uncles []*Header
transactions Transactions
// 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
// These fields are used by package eth to track
// inter-peer block relay.
ReceivedAt time.Time
ReceivedFrom interface{}
}
网友评论