🔥码云GVP开源项目 12k star Uniapp+ElementUI 功能强大 支持多语言、二开方便! 广告
## 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的链结构: ![](https://img.kancloud.cn/91/9d/919d05cc142e506cb4b234d50d70b68f_1048x582.png) 图描述很清楚,不做赘述。 我们再看看以太坊的链结构: ![](https://img.kancloud.cn/53/3e/533e9c911dc25c0a45cfffe15010cc8d_1048x728.png)