GO-HPB源码解读--LevelDB操作

HPB使用LevelDB存储数据。go-hpb/blockchain/database_util.go文件封装了LevelDB的操作。因为LevelDB是<k, v>数据库,所以所有数据的存储都需要指定k。以下代码列出了所有需要保存的数据的k或者k的前缀。

···
voteResultKey = []byte(“vote-result-key”)

headHeaderKey = []byte("LastHeader")
headBlockKey  = []byte("LastBlock")
headFastKey   = []byte("LastFast")

// 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
randomPrefix        = []byte("random") // randomPrefix + num (uint64 big endian) + hash -> header

preimagePrefix = "secure-key-"              // preimagePrefix + hash -> preimage
configPrefix   = []byte("hpb-config-") // config prefix for the db
BloomBitsIndexPrefix = []byte("iB") // BloomBitsIndexPrefix is the data table of a chain indexer to track its progress
oldReceiptsPrefix = []byte("receipts-")
oldTxMetaSuffix   = []byte{0x01}

这里对主要数据的k前缀整理下,其中hash指的是区块的hash,等于header中的hash,代码中称canonical hash。num是指区块的高度或者位置,表示第几个区块
key|Value
-|-
h+num+hash|header
h+num+hash+t|td
h+num+n |hash
H+hash | num
b+num+hash |block body
r+num+hash |block receipts
l+hash | transaction/receipt lookup metadata
B+bit+section+hash |bloom bits

database_util.go文件大部分内容是write和get方法,以前对前缀的拼接

以下内容转自网络,作个备注

StateObject:
是一个账号(地址)的状态信息
对于普通账号,这个对象保存了balance, nonce等信息
对于智能合约账号,还额外保留了智能合约的状态,这个状态就是智能合约里的定义的各种变量的值。以太坊虚拟机的变量是以<k, v>存储的,所以这个状态就是大量<k, v>对象。