block底层存储方式

Header和Block的主要成员变量,最终还是要存储在底层数据库中。Ethereum 选用的是LevelDB, 属于非关系型数据库,存储单元是[k,v]键值对。

//core/database_util.go
var (
    headHeaderKey = []byte("LastHeader")
    headBlockKey  = []byte("LastBlock")
    headFastKey   = []byte("LastFast")
    trieSyncKey   = []byte("TrieSync")

    // Data item prefixes (use single byte to avoid mixing data types, avoid `i`).
    headerPrefix        = []byte("h") // headerPrefix + num (uint64 big endian) + hash -> header
    tdSuffix            = []byte("t") // headerPrefix + num (uint64 big endian) + hash + tdSuffix -> td
    numSuffix           = []byte("n") // headerPrefix + num (uint64 big endian) + numSuffix -> hash
    blockHashPrefix     = []byte("H") // blockHashPrefix + hash -> num (uint64 big endian)
    bodyPrefix          = []byte("b") // bodyPrefix + num (uint64 big endian) + hash -> block body
    blockReceiptsPrefix = []byte("r") // blockReceiptsPrefix + num (uint64 big endian) + hash -> block receipts
    lookupPrefix        = []byte("l") // lookupPrefix + hash -> transaction/receipt lookup metadata
    bloomBitsPrefix     = []byte("B") // bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash -> bloom bits

    preimagePrefix = "secure-key-"              // preimagePrefix + hash -> preimage
    configPrefix   = []byte("ethereum-config-") // config prefix for the db

    // Chain index prefixes (use `i` + single byte to avoid mixing data types).
    BloomBitsIndexPrefix = []byte("iB") // BloomBitsIndexPrefix is the data table of a chain indexer to track its progress

    // used by old db, now only used for conversion
    oldReceiptsPrefix = []byte("receipts-")
    oldTxMetaSuffix   = []byte{0x01}

    ErrChainConfigNotFound = errors.New("ChainConfig not found") // general config not found error

    preimageCounter    = metrics.NewRegisteredCounter("db/preimage/total", nil)
    preimageHitCounter = metrics.NewRegisteredCounter("db/preimage/hits", nil)
)

| key | value |
| ‘h’ + num + hash | header’s RLP raw data |
| ‘h’ + num + hash + ‘t’ | td |
| ‘h’ + num + ‘n’ | hash |
| ‘H’ + hash | num |
| ‘b’ + num + hash | body’s RLP raw data |
| ‘r’ + num + hash | receipts RLP |
| ‘l’ + hash | tx/receipt lookup metadata |

这里的hash就是该Block(或Header)对象的RLP哈希值,在代码中也被称为canonical hash;num是Number的uint64类型,大端(big endian)整型数。可以发现,num 和 hash是key中出现最多的成分;同时num和hash还分别作为value被单独存储,而每当此时则另一方必组成key。这些信息都在强烈的暗示,num(Number)和hash是Block最为重要的两个属性:num用来确定Block在整个区块链中所处的位置,hash用来辨识惟一的Block/Header对象

通过以上的设计,Block结构体的所有重要成员,都被存储进了底层数据库。当所有Block对象的信息都已经写进数据库后,我们就可以使用BlockChain结构体来处理整个块链。

2

    原文作者:古则
    原文地址: https://www.jianshu.com/p/a446acd45945
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞