正在學習區塊鏈,如果我哪裏有錯誤希望大家指出,如果有任何想法也歡迎留言。這些筆記本身是在typora上寫的,如果有顯示不正確的敬請諒解。筆記本身也是給我自己寫的,所以如果有侵權的請通知我,我立即刪除。
2. 以太坊數據結構
2.1 本地數據結構
下圖是本地存儲的以太坊數據結構
// 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{}
}
肖老師說對於本節課只有前三項是有效的
header:指向區塊鏈塊頭的指針
uncles:指向叔叔節點的指針數組
transaction:交易的列表(以太坊中交易不是放在塊頭的嗎)
2.2 區塊頭結構
// 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 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"`
}
ParentHash:是父區塊的哈希值,就是前一個區塊塊頭的哈希值。
UncleHash:叔叔區塊的哈希值
Coinbase:礦工的原始地址,和比特幣一樣
Root:狀態樹根哈希
TxHash:交易樹根哈希
ReceiptHash:收據樹根哈希
Bloom:布隆過濾器
Difficulty:挖礦難度
GasLimit:是這個區塊中更夠消耗的區塊上限,類似於比特幣中的1M限制。爲了防止某些區塊太大,對整個網絡產生影響。這個值需要調整的時候調整範圍是上一個區塊的正負1024/1。比特幣中分叉很多都是對1M空間理解不一致導致的,現在你覺得大你就下調,你覺得小就上調。雖然1024/1看起來很小,但是以太幣出塊時間很短,所以還是很快就能達到一個大多數礦工都能認可的平均大小。
GasUsed:整個區塊中所有交易的汽油費之和。
Time:大致時間戳
Extra:
MixDigest:挖礦相關
Nonce:和挖礦相關
2.3 實際上傳的以太坊數據
// "external" block encoding. used for eth protocol, etc.
type extblock struct {
Header *Header
Txs []*Transaction
Uncles []*Header
}
真正上傳的時候只傳前三項,但是如果僅傳指針,數據呢?我還不清楚。
2.4 RLP(Recursive Length Prefix)序列化
我最早接觸序列化是在左神的二叉樹序列化中,那個的目的就是把一種複雜的數據變成一種方便傳輸的結構,例如Json和xml都是,不過他們都是文本形式的,RLP是面向字節流的。
肖老師在講的時候提到了一種比較常用的方式,叫protocal buffer,也有叫protobuf的,具體可以百度。至於RLP,是一種非常簡單的結構,包括哈希、整數等一切類型都會轉換成字符數組。具體的可以百度,很多很多,或者看github上的以太坊wiki。
《RLP》