相關(guān)命令分析
- eth.blockNumber
查看當(dāng)前塊高。 - eth.getBlock()
根據(jù)高度查看塊內(nèi)容。 - eth.syncing
獲取同步狀態(tài),可以看到全網(wǎng)最新塊高狂打,提示用戶同步進(jìn)度。
區(qū)塊分析
創(chuàng)世區(qū)塊:
{
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: []
}
以太坊主網(wǎng)第410000的區(qū)塊:
{
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: []
}
可以看出從創(chuàng)世區(qū)塊到現(xiàn)在以太坊的block結(jié)構(gòu)都未做更改混弥,表格中是以太坊黃皮書對(duì)塊的數(shù)據(jù)結(jié)構(gòu)的解釋趴乡。黃皮書地址
key | value |
---|---|
difficulty | 表示當(dāng)前區(qū)塊的難度水平, 這個(gè)值根據(jù)前一個(gè)區(qū)塊的難度水平和時(shí)間戳計(jì)算得到。 |
extraData | 附加數(shù)據(jù)蝗拿,32字節(jié)以內(nèi)的字節(jié)數(shù)組 |
gasLimit | 目前每個(gè)區(qū)塊的燃料消耗上限 |
gasUsed | 當(dāng)前區(qū)塊的所有交易使用燃料之和 |
hash | block hash晾捏,可以理解為這個(gè)block在整個(gè)blockchain中的索引 |
logsBloom | 日記 Bloom 過濾器由可索引信息(日志地址和日志主題)組成,這個(gè)信息包含在每個(gè)日志入口, 來(lái)自交易列表中每個(gè)交易的接受者 |
miner | 打包這一個(gè)block的礦工地址 |
mixHash | 混合哈希, 與一個(gè)與隨機(jī)數(shù) (nonce)相關(guān)的 256 位哈希計(jì)算, 用于證明針對(duì)當(dāng)前區(qū)塊已經(jīng)完成了足夠的計(jì)算 |
nonce | 隨機(jī)數(shù), 一個(gè) 64 位哈希, 和計(jì)算混合哈希相關(guān), 用于證明針對(duì)當(dāng)前區(qū)塊已經(jīng)完成了足夠的計(jì)算 |
number | 區(qū)塊編號(hào), 等于當(dāng)前區(qū)塊的直系前輩區(qū)塊數(shù)量(創(chuàng)始區(qū)塊的區(qū)塊編號(hào)為0) |
parentHash | 父哈希(創(chuàng)世區(qū)塊沒有父哈希哀托,所以parentHash為0x0000000000000000000000000000000000000000000000000000000000000000) |
receiptsRoot | 接受者字典樹根節(jié)點(diǎn)哈希: 接受者字典樹根節(jié)點(diǎn)的 Keccak 256 位哈希, 在接受者字典樹含有區(qū)塊中的所有交易信息中的接受者 |
sha3Uncles | ???? |
size | 猜測(cè)為block包體的大小 |
stateRoot | 狀態(tài)字典樹根節(jié)點(diǎn)哈希: 狀態(tài)字典樹根節(jié)點(diǎn)的 Keccak 256 位哈希, 交易打包到當(dāng)前區(qū)塊且區(qū)塊定稿后可以生成這個(gè)值 |
timestamp | 時(shí)間戳, 當(dāng)前區(qū)塊初始化時(shí)的Unix時(shí)間戳 |
totalDifficulty | ??? |
transactions | block所包含的交易 |
transactionsRoot | 交易字典樹根節(jié)點(diǎn)哈希: 交易字典樹根節(jié)點(diǎn)的 Keccak 256 位哈希, 在交易字典樹含有區(qū)塊中的所有交易列表 |
- difficulty:是當(dāng)前塊的難度惦辛,值根據(jù)前一個(gè)區(qū)塊的難度水平和時(shí)間戳計(jì)算得到。推薦大家看一下知乎上的一篇文章以太坊是如何計(jì)算難度的仓手。具體計(jì)算公式如下胖齐,有興趣的朋友可以嘗試計(jì)算一下。(以太坊規(guī)定任何區(qū)塊的難度都不可以低于創(chuàng)世區(qū)塊)
periodCount = block_num / 100000
diff = (parent_diff + (parent_diff / 2048 * max(1 - (block_timestamp - parent_timestamp) / 10, -99)) ) + 2^(periodCount - 2)
- transactionsRoot:為了防止數(shù)據(jù)篡改嗽冒,eth沿用的btc的MerkleTree證明呀伙,相關(guān)技術(shù)內(nèi)容后續(xù)會(huì)做單獨(dú)闡述。
Golang數(shù)據(jù)結(jié)構(gòu)源碼
位置: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{}
}