区块存储

区块的存储是由leveldb完成的,leveldb的数据是以键值对存储的。在这里保存区块信息时,key一般是与hash相关的,value所保存的数据结构是经过RLP编码的。
在代码中,core/database_util.go中封装了区块存储和读取相关的代码。
在存储区块信息时,会将区块头和区块体分开进行存储。因此在区块的结构体中,能够看到Header和Body两个结构体。
区块头(Header)的存储格式为:

  1. headerPrefix + num (uint64 big endian) + hash -> rlpEncode(header)

key是由区块头的前缀,区块号和区块hash构成。value是区块头的RLP编码。
区块体(Body)的存储格式为:

  1. bodyPrefix + num (uint64 big endian) + hash -> rlpEncode(block body)

key是由区块体前缀,区块号和区块hash构成。value是区块体的RLP编码。
在database_util.go中,key的前缀可以区分leveldb中存储的是什么类型的数据。

  1. var (
  2. headHeaderKey = []byte("LastHeader")
  3. headBlockKey = []byte("LastBlock")
  4. headFastKey = []byte("LastFast")
  5. // Data item prefixes (use single byte to avoid mixing data types, avoid `i`).
  6. headerPrefix = []byte("h") // headerPrefix + num (uint64 big endian) + hash -> header
  7. tdSuffix = []byte("t") // headerPrefix + num (uint64 big endian) + hash + tdSuffix -> td
  8. numSuffix = []byte("n") // headerPrefix + num (uint64 big endian) + numSuffix -> hash
  9. blockHashPrefix = []byte("H") // blockHashPrefix + hash -> num (uint64 big endian)
  10. bodyPrefix = []byte("b") // bodyPrefix + num (uint64 big endian) + hash -> block body
  11. blockReceiptsPrefix = []byte("r") // blockReceiptsPrefix + num (uint64 big endian) + hash -> block receipts
  12. lookupPrefix = []byte("l") // lookupPrefix + hash -> transaction/receipt lookup metadata
  13. bloomBitsPrefix = []byte("B") // bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash -> bloom bits
  14. preimagePrefix = "secure-key-" // preimagePrefix + hash -> preimage
  15. configPrefix = []byte("ethereum-config-") // config prefix for the db
  16. // Chain index prefixes (use `i` + single byte to avoid mixing data types).
  17. BloomBitsIndexPrefix = []byte("iB") // BloomBitsIndexPrefix is the data table of a chain indexer to track its progress
  18. // used by old db, now only used for conversion
  19. oldReceiptsPrefix = []byte("receipts-")
  20. oldTxMetaSuffix = []byte{0x01}
  21. ErrChainConfigNotFound = errors.New("ChainConfig not found") // general config not found error
  22. preimageCounter = metrics.NewCounter("db/preimage/total")
  23. preimageHitCounter = metrics.NewCounter("db/preimage/hits")
  24. )

database_util.go最开始就定义了所有的前缀。这里的注释详细说明了每一个前缀存储了什么数据类型。
database_util.go中的其他方法则是对leveldb的操作。其中get方法是读取数据库中的内容,write则是向leveldb中写入数据。
要讲一个区块的信息写入数据库,则需要调用其中的WriteBlock方法。

  1. // WriteBlock serializes a block into the database, header and body separately.
  2. func WriteBlock(db ethdb.Putter, block *types.Block) error {
  3. // Store the body first to retain database consistency
  4. if err := WriteBody(db, block.Hash(), block.NumberU64(), block.Body()); err != nil {
  5. return err
  6. }
  7. // Store the header too, signaling full block ownership
  8. if err := WriteHeader(db, block.Header()); err != nil {
  9. return err
  10. }
  11. return nil
  12. }

这里我们看到,将一个区块信息写入数据库其实是分别将区块头和区块体写入数据库。
首先来看区块头的存储。区块头的存储是由WriteHeader方法完成的。

  1. // WriteHeader serializes a block header into the database.
  2. func WriteHeader(db ethdb.Putter, header *types.Header) error {
  3. data, err := rlp.EncodeToBytes(header)
  4. if err != nil {
  5. return err
  6. }
  7. hash := header.Hash().Bytes()
  8. num := header.Number.Uint64()
  9. encNum := encodeBlockNumber(num)
  10. key := append(blockHashPrefix, hash...)
  11. if err := db.Put(key, encNum); err != nil {
  12. log.Crit("Failed to store hash to number mapping", "err", err)
  13. }
  14. key = append(append(headerPrefix, encNum...), hash...)
  15. if err := db.Put(key, data); err != nil {
  16. log.Crit("Failed to store header", "err", err)
  17. }
  18. return nil
  19. }

这里首先对区块头进行了RLP编码,然后将区块号转换成为byte格式,开始组装key。
这里首先向数据库中存储了一条区块hash->区块号的键值对,然后才将区块头的信息写入数据库。
接下来是区块体的存储。区块体存储是由WriteBody方法实现。

  1. // WriteBody serializes the body of a block into the database.
  2. func WriteBody(db ethdb.Putter, hash common.Hash, number uint64, body *types.Body) error {
  3. data, err := rlp.EncodeToBytes(body)
  4. if err != nil {
  5. return err
  6. }
  7. return WriteBodyRLP(db, hash, number, data)
  8. }
  9. // WriteBodyRLP writes a serialized body of a block into the database.
  10. func WriteBodyRLP(db ethdb.Putter, hash common.Hash, number uint64, rlp rlp.RawValue) error {
  11. key := append(append(bodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
  12. if err := db.Put(key, rlp); err != nil {
  13. log.Crit("Failed to store block body", "err", err)
  14. }
  15. return nil
  16. }

WriteBody首先将区块体的信息进行RLP编码,然后调用WriteBodyRLP方法将区块体的信息写入数据库。key的组装方法如之前所述。

交易存储

交易主要在数据库中仅存储交易的Meta信息。

  1. txHash + txMetaSuffix -> rlpEncode(txMeta)

交易的Meta信息结构体如下:

  1. // TxLookupEntry is a positional metadata to help looking up the data content of
  2. // a transaction or receipt given only its hash.
  3. type TxLookupEntry struct {
  4. BlockHash common.Hash
  5. BlockIndex uint64
  6. Index uint64
  7. }

这里,meta信息会存储块的hash,块号和块上第几笔交易这些信息。
交易Meta存储是以交易hash加交易的Meta前缀为key,Meta的RLP编码为value。
交易写入数据库是通过WriteTxLookupEntries方法实现的。

  1. // WriteTxLookupEntries stores a positional metadata for every transaction from
  2. // a block, enabling hash based transaction and receipt lookups.
  3. func WriteTxLookupEntries(db ethdb.Putter, block *types.Block) error {
  4. // Iterate over each transaction and encode its metadata
  5. for i, tx := range block.Transactions() {
  6. entry := TxLookupEntry{
  7. BlockHash: block.Hash(),
  8. BlockIndex: block.NumberU64(),
  9. Index: uint64(i),
  10. }
  11. data, err := rlp.EncodeToBytes(entry)
  12. if err != nil {
  13. return err
  14. }
  15. if err := db.Put(append(lookupPrefix, tx.Hash().Bytes()...), data); err != nil {
  16. return err
  17. }
  18. }
  19. return nil
  20. }

这里,在将交易meta入库时,会遍历块上的所有交易,并构造交易的meta信息,进行RLP编码。然后以交易hash为key,meta为value进行存储。
这样就将一笔交易写入数据库中。
从数据库中读取交易信息时通过GetTransaction方法获得的。

  1. // GetTransaction retrieves a specific transaction from the database, along with
  2. // its added positional metadata.
  3. func GetTransaction(db DatabaseReader, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) {
  4. // Retrieve the lookup metadata and resolve the transaction from the body
  5. blockHash, blockNumber, txIndex := GetTxLookupEntry(db, hash)
  6. if blockHash != (common.Hash{}) {
  7. body := GetBody(db, blockHash, blockNumber)
  8. if body == nil || len(body.Transactions) <= int(txIndex) {
  9. log.Error("Transaction referenced missing", "number", blockNumber, "hash", blockHash, "index", txIndex)
  10. return nil, common.Hash{}, 0, 0
  11. }
  12. return body.Transactions[txIndex], blockHash, blockNumber, txIndex
  13. }
  14. // Old transaction representation, load the transaction and it's metadata separately
  15. data, _ := db.Get(hash.Bytes())
  16. if len(data) == 0 {
  17. return nil, common.Hash{}, 0, 0
  18. }
  19. var tx types.Transaction
  20. if err := rlp.DecodeBytes(data, &tx); err != nil {
  21. return nil, common.Hash{}, 0, 0
  22. }
  23. // Retrieve the blockchain positional metadata
  24. data, _ = db.Get(append(hash.Bytes(), oldTxMetaSuffix...))
  25. if len(data) == 0 {
  26. return nil, common.Hash{}, 0, 0
  27. }
  28. var entry TxLookupEntry
  29. if err := rlp.DecodeBytes(data, &entry); err != nil {
  30. return nil, common.Hash{}, 0, 0
  31. }
  32. return &tx, entry.BlockHash, entry.BlockIndex, entry.Index
  33. }

这个方法会首先通过交易hash从数据库中获取交易的meta信息,包括交易所在块的hash,块号和第几笔交易。
接下来使用块号和块hash获取从数据库中读取块的信息。
然后根据第几笔交易从块上获取交易的具体信息。
这里以太坊将交易的存储换成了新的存储方式,即交易的具体信息存储在块上,交易hash只对应交易的meta信息,并不包含交易的具体信息。
而以前的交易存储则是需要存储交易的具体信息和meta信息。
因此GetTransaction方法会支持原有的数据存储方式。