## 1、一切從數據開始。
我們對比下etherum和pe:
下面是ethereum的block header:
~~~
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"`
/*
TODO (MariusVanDerWijden) Add this field once needed
// Random was added during the merge and contains the BeaconState randomness
Random common.Hash `json:"random" rlp:"optional"`
*/
}
~~~
下面是ep的block header:
~~~
type Header struct {
ParentHash Hash `json:"parentHash"`
Sha3Uncles Hash `json:"sha3Uncles"`
Miner Address `json:"miner"`
StateRoot Hash `json:"stateRoot"`
TxRoot Hash `json:"transactionsRoot"`
ReceiptsRoot Hash `json:"receiptsRoot"`
LogsBloom Bloom `json:"logsBloom"`
Difficulty uint64 `json:"difficulty"`
Number uint64 `json:"number"`
GasLimit uint64 `json:"gasLimit"`
GasUsed uint64 `json:"gasUsed"`
Timestamp uint64 `json:"timestamp"`
ExtraData []byte `json:"extraData"`
MixHash Hash `json:"mixHash"`
Nonce Nonce `json:"nonce"`
Hash Hash `json:"hash"`
}
~~~
ep 目前不支持以太坊最新的EIP-1599協議,其它部分和ethereum玩去一致(雖然變量名稱有些有區別,但數據類型完全一致,甚至映射的json名稱也完全一致)。
我們再看看block的結構比較:
下面是ep的block結構:
~~~
type Block struct {
Header *Header
Transactions []*Transaction
Uncles []*Header
// Cache
size atomic.Value // *uint64
}
~~~
下面是ethereum的block結構:
~~~
type Block struct {
header *Header
uncles []*Header
transactions Transactions
// caches
hash atomic.Value
size atomic.Value
// These fields are used by package eth to track
// inter-peer block relay.
ReceivedAt time.Time
ReceivedFrom interface{}
}
~~~
其中Transactions類型結構定義如下:
~~~
type Transactions []*Transaction
~~~
可以看出,ep的block結構相比,少了hash(cache字段,可以計算出來)、ReceviedAt、ReceivedFrom字段(后兩個字段用于eth進行跟蹤)。ep少了這幾個字段,對于跨鏈數據交換影響不大(跨鏈交換當然需要對源鏈的block數據進行數據抽取,然后按照目標鏈的結構進行重構)。
block的真正content是body,我們看看兩者的結構:
ethereum的body結構:
~~~
// Body is a simple (mutable, non-safe) data container for storing and moving
// a block's data contents (transactions and uncles) together.
type Body struct {
Transactions []*Transaction
Uncles []*Header
}
~~~
下面是ep的body結構:
~~~
type Body struct {
Transactions []*Transaction
Uncles []*Header
}
~~~
兩者的body結構完全相同。
以下是對block結構字段的闡述:
1. **parentHash:**The block header hash of the previous block. Each block contains the hash of the previous block, all the way back to the genesis block on the chain. This is the structural design that maintains the data from being tampered with (any tampering to the previous block will affect the hash value of all subsequent blocks).
2. **ommersHash:**The uncle block header and the hash value of part of the block body.
3. **beneficiary:**The Ethereum account that gets paid for mining this block.
4. **stateRoot:**The hash of the root node of the world state tree (after all transactions have been executed).
5. **transactionsRoot:**The hash value of the root node of the transaction tree. This tree contains all transactions in the block body.
6. **receiptsRoot:**Every time a transaction is executed, Ethereum generates a transaction receipt corresponding to the result. Here is the root node hash of this transaction receipt tree.
7. **logsBloom:**It is used to judge whether the transaction of a certain block generates a certain log. This avoids storing log information in chunks (saves a lot of space).
8. **difficulty:**The difficulty value of this block. This is a measure of the mining difficulty of the current block
9. **number:**The total number of preorder blocks. This indicates the height of the blockchain (i.e. how many blocks are on the blockchain). The number of the genesis block is 0 .
10. **gasLimit:**Every transaction requires gas. The gas limit indicates the total amount of gas that can be used by all transactions recorded in this block. This is a means of limiting the number of transactions in a block.
11. **gasUsed:**The total amount of gas actually consumed by each exchange in the block.
12. **timestamp:**The Unix timestamp when the block was created.
13. **extraData:**A variable-length byte array that can enter anything. When miners create blocks, anything can be added to this area.
14. **mixHash:**The hash value used to verify that a block was actually recorded on the chain.
15. **nonce:**Like**mixHash**, the value used to verify that the block was actually recorded on-chain.
簡要翻譯如下:
* Prev Hash - 父區塊的Keccak哈希值
* Nonce - 用于工作證明的計算
* Timestamp - 時間戳
* Uncles Hash - 叔塊的Keccak哈希值
* Benficiary - 受益人地址,礦工地址
* Logs Bloom - 事件地址和事件topic的布隆濾波器
* Difficult - 前一個區塊的難度
* Extra Data - 與該區塊相關的32字節數據
* Block Num - 祖先塊數
* Gas Limit -當前每個區塊的gas使用限制值
* Gas Used - 該區塊中用于交易的gas消耗值
* Mix Hash - 與nonce一起用來證明工作證明計算的256位值
* State Root - 狀態樹的根哈希值
* Transaction Root - 交易樹的根哈希值
* Receipt Root - 收據樹的根哈希值
## 2、數據存儲結構
我們先看看bitcoin的鏈結構:

圖描述很清楚,不做贅述。
我們再看看以太坊的鏈結構:

- 重要更新說明
- linechain發布
- linechain新版設計
- 引言一
- 引言二
- 引言三
- vs-code設置及開發環境設置
- BoltDB數據庫應用
- 關于Go語言、VS-code的一些Tips
- 區塊鏈的架構
- 網絡通信與區塊鏈
- 單元測試
- 比特幣腳本語言
- 關于區塊鏈的一些概念
- 區塊鏈組件
- 區塊鏈第一版:基本原型
- 區塊鏈第二版:增加工作量證明
- 區塊鏈第三版:持久化
- 區塊鏈第四版:交易
- 區塊鏈第五版:實現錢包
- 區塊鏈第六版:實現UTXO集
- 區塊鏈第七版:網絡
- 階段小結
- 區塊鏈第八版:P2P
- P2P網絡架構
- 區塊鏈網絡層
- P2P區塊鏈最簡體驗
- libp2p建立P2P網絡的關鍵概念
- 區塊鏈結構層設計與實現
- 用戶交互層設計與實現
- 網絡層設計與實現
- 建立節點發現機制
- 向區塊鏈網絡請求區塊信息
- 向區塊鏈網絡發布消息
- 運行區塊鏈
- LineChain
- 系統運行流程
- Multihash
- 區塊鏈網絡的節點發現機制深入探討
- DHT
- Bootstrap
- 連接到所有引導節點
- Advertise
- 搜索其它peers
- 連接到搜到的其它peers
- 區塊鏈網絡的消息訂發布-訂閱機制深入探討
- LineChain:適用于智能合約編程的腳本語言支持
- LineChain:解決分叉問題
- LineChain:多重簽名
- libp2p升級到v0.22版本
- 以太坊基礎
- 重溫以太坊的樹結構
- 世界狀態樹
- (智能合約)賬戶存儲樹
- 交易樹
- 交易收據樹
- 小結
- 以太坊的存儲結構
- 以太坊狀態數據庫
- MPT
- 以太坊POW共識算法
- 智能合約存儲
- Polygon Edge
- block結構
- transaction數據結構
- 數據結構小結
- 關于本區塊鏈的一些說明
- UML工具-PlantUML
- libp2p介紹
- JSON-RPC
- docker制作:啟動多個應用系統
- Dockerfile
- docker-entrypoint.sh
- supervisord.conf
- docker run
- nginx.conf
- docker基礎操作整理
- jupyter計算交互環境
- git技巧一
- git技巧二
- 使用github項目的最佳實踐
- windows下package管理工具