mirror of
https://github.com/ethereum/go-ethereum.git
synced 2026-06-19 21:31:37 +00:00
275 lines
10 KiB
Go
275 lines
10 KiB
Go
// Copyright 2018 The go-ethereum Authors
|
|
// This file is part of the go-ethereum library.
|
|
//
|
|
// The go-ethereum library is free software: you can redistribute it and/or modify
|
|
// it under the terms of the GNU Lesser General Public License as published by
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
// (at your option) any later version.
|
|
//
|
|
// The go-ethereum library is distributed in the hope that it will be useful,
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
// GNU Lesser General Public License for more details.
|
|
//
|
|
// You should have received a copy of the GNU Lesser General Public License
|
|
// along with the go-ethereum library. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
// Package rawdb contains a collection of low level database accessors.
|
|
package rawdb
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/binary"
|
|
|
|
"github.com/XinFinOrg/XDPoSChain/common"
|
|
"github.com/XinFinOrg/XDPoSChain/crypto"
|
|
"github.com/XinFinOrg/XDPoSChain/metrics"
|
|
)
|
|
|
|
// The fields below define the low level database schema prefixing.
|
|
var (
|
|
// databaseVersionKey tracks the current database version.
|
|
databaseVersionKey = []byte("DatabaseVersion")
|
|
|
|
// headHeaderKey tracks the latest known header's hash.
|
|
headHeaderKey = []byte("LastHeader")
|
|
|
|
// headBlockKey tracks the latest known full block's hash.
|
|
headBlockKey = []byte("LastBlock")
|
|
|
|
// headFastBlockKey tracks the latest known incomplete block's hash during fast sync.
|
|
headFastBlockKey = []byte("LastFast")
|
|
|
|
// fastTrieProgressKey tracks the number of trie entries imported during fast sync.
|
|
fastTrieProgressKey = []byte("TrieSync")
|
|
|
|
// badBlockKey tracks the list of bad blocks seen by local
|
|
badBlockKey = []byte("InvalidBlock")
|
|
|
|
// uncleanShutdownKey tracks the list of local crashes
|
|
uncleanShutdownKey = []byte("unclean-shutdown") // config prefix for the db
|
|
|
|
// Data item prefixes (use single byte to avoid mixing data types, avoid `i`, used for indexes).
|
|
headerPrefix = []byte("h") // headerPrefix + num (uint64 big endian) + hash -> header
|
|
headerTDSuffix = []byte("t") // headerPrefix + num (uint64 big endian) + hash + headerTDSuffix -> td
|
|
headerHashSuffix = []byte("n") // headerPrefix + num (uint64 big endian) + headerHashSuffix -> hash
|
|
headerNumberPrefix = []byte("H") // headerNumberPrefix + hash -> num (uint64 big endian)
|
|
|
|
blockBodyPrefix = []byte("b") // blockBodyPrefix + num (uint64 big endian) + hash -> block body
|
|
blockReceiptsPrefix = []byte("r") // blockReceiptsPrefix + num (uint64 big endian) + hash -> block receipts
|
|
|
|
txLookupPrefix = []byte("l") // txLookupPrefix + hash -> transaction/receipt lookup metadata
|
|
bloomBitsPrefix = []byte("B") // bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash -> bloom bits
|
|
codePrefix = []byte("c") // codePrefix + code hash -> account code
|
|
|
|
// used by old db, now only used for conversion
|
|
oldReceiptsPrefix = []byte("receipts-")
|
|
|
|
// Path-based storage scheme of merkle patricia trie.
|
|
trieNodeAccountPrefix = []byte("A") // trieNodeAccountPrefix + hexPath -> trie node
|
|
trieNodeStoragePrefix = []byte("O") // trieNodeStoragePrefix + accountHash + hexPath -> trie node
|
|
|
|
PreimagePrefix = []byte("secure-key-") // PreimagePrefix + hash -> preimage
|
|
configPrefix = []byte("ethereum-config-") // config prefix for the db
|
|
genesisPrefix = []byte("ethereum-genesis-") // genesis state 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
|
|
oldTxMetaSuffix = []byte{0x01}
|
|
|
|
// XDPoS snapshot prefix
|
|
xdposV1Prefix = []byte("XDPoS-")
|
|
xdposV2Prefix = []byte("XDPoS-V2-")
|
|
|
|
randomizeKey = []byte("randomizeKey")
|
|
sectionHeadKeyPrefix = []byte("shead")
|
|
validSectionsKey = []byte("count")
|
|
|
|
preimageCounter = metrics.NewRegisteredCounter("db/preimage/total", nil)
|
|
preimageHitCounter = metrics.NewRegisteredCounter("db/preimage/hits", nil)
|
|
)
|
|
|
|
const (
|
|
// freezerHeaderTable indicates the name of the freezer header table.
|
|
freezerHeaderTable = "headers"
|
|
|
|
// freezerHashTable indicates the name of the freezer canonical hash table.
|
|
freezerHashTable = "hashes"
|
|
|
|
// freezerBodiesTable indicates the name of the freezer block body table.
|
|
freezerBodiesTable = "bodies"
|
|
|
|
// freezerReceiptTable indicates the name of the freezer receipts table.
|
|
freezerReceiptTable = "receipts"
|
|
|
|
// freezerDifficultyTable indicates the name of the freezer total difficulty table.
|
|
freezerDifficultyTable = "diffs"
|
|
)
|
|
|
|
// LegacyTxLookupEntry is the legacy TxLookupEntry definition with some unnecessary
|
|
// fields.
|
|
type LegacyTxLookupEntry struct {
|
|
BlockHash common.Hash
|
|
BlockIndex uint64
|
|
Index uint64
|
|
}
|
|
|
|
// encodeBlockNumber encodes a block number as big endian uint64
|
|
func encodeBlockNumber(number uint64) []byte {
|
|
enc := make([]byte, 8)
|
|
binary.BigEndian.PutUint64(enc, number)
|
|
return enc
|
|
}
|
|
|
|
// headerKeyPrefix = headerPrefix + num (uint64 big endian)
|
|
func headerKeyPrefix(number uint64) []byte {
|
|
return append(headerPrefix, encodeBlockNumber(number)...)
|
|
}
|
|
|
|
// headerKey = headerPrefix + num (uint64 big endian) + hash
|
|
func headerKey(number uint64, hash common.Hash) []byte {
|
|
return append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
|
|
}
|
|
|
|
// headerTDKey = headerPrefix + num (uint64 big endian) + hash + headerTDSuffix
|
|
func headerTDKey(number uint64, hash common.Hash) []byte {
|
|
return append(headerKey(number, hash), headerTDSuffix...)
|
|
}
|
|
|
|
// headerHashKey = headerPrefix + num (uint64 big endian) + headerHashSuffix
|
|
func headerHashKey(number uint64) []byte {
|
|
return append(append(headerPrefix, encodeBlockNumber(number)...), headerHashSuffix...)
|
|
}
|
|
|
|
// headerNumberKey = headerNumberPrefix + hash
|
|
func headerNumberKey(hash common.Hash) []byte {
|
|
return append(headerNumberPrefix, hash.Bytes()...)
|
|
}
|
|
|
|
// blockBodyKey = blockBodyPrefix + num (uint64 big endian) + hash
|
|
func blockBodyKey(number uint64, hash common.Hash) []byte {
|
|
return append(append(blockBodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
|
|
}
|
|
|
|
// blockReceiptsKey = blockReceiptsPrefix + num (uint64 big endian) + hash
|
|
func blockReceiptsKey(number uint64, hash common.Hash) []byte {
|
|
return append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...)
|
|
}
|
|
|
|
// txLookupKey = txLookupPrefix + hash
|
|
func txLookupKey(hash common.Hash) []byte {
|
|
return append(txLookupPrefix, hash.Bytes()...)
|
|
}
|
|
|
|
// bloomBitsKey = bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash
|
|
func bloomBitsKey(bit uint, section uint64, hash common.Hash) []byte {
|
|
key := append(append(bloomBitsPrefix, make([]byte, 10)...), hash.Bytes()...)
|
|
|
|
binary.BigEndian.PutUint16(key[1:], uint16(bit))
|
|
binary.BigEndian.PutUint64(key[3:], section)
|
|
|
|
return key
|
|
}
|
|
|
|
// preimageKey = PreimagePrefix + hash
|
|
func preimageKey(hash common.Hash) []byte {
|
|
return append(PreimagePrefix, hash.Bytes()...)
|
|
}
|
|
|
|
// codeKey = codePrefix + hash
|
|
func codeKey(hash common.Hash) []byte {
|
|
return append(codePrefix, hash.Bytes()...)
|
|
}
|
|
|
|
// IsCodeKey reports whether the given byte slice is the key of contract code,
|
|
// if so return the raw code hash as well.
|
|
func IsCodeKey(key []byte) (bool, []byte) {
|
|
if bytes.HasPrefix(key, codePrefix) && len(key) == common.HashLength+len(codePrefix) {
|
|
return true, key[len(codePrefix):]
|
|
}
|
|
return false, nil
|
|
}
|
|
|
|
// configKey = configPrefix + hash
|
|
func configKey(hash common.Hash) []byte {
|
|
return append(configPrefix, hash.Bytes()...)
|
|
}
|
|
|
|
// genesisStateSpecKey = genesisPrefix + hash
|
|
func genesisStateSpecKey(hash common.Hash) []byte {
|
|
return append(genesisPrefix, hash.Bytes()...)
|
|
}
|
|
|
|
// accountTrieNodeKey = trieNodeAccountPrefix + nodePath.
|
|
func accountTrieNodeKey(path []byte) []byte {
|
|
return append(trieNodeAccountPrefix, path...)
|
|
}
|
|
|
|
// storageTrieNodeKey = trieNodeStoragePrefix + accountHash + nodePath.
|
|
func storageTrieNodeKey(accountHash common.Hash, path []byte) []byte {
|
|
return append(append(trieNodeStoragePrefix, accountHash.Bytes()...), path...)
|
|
}
|
|
|
|
// IsLegacyTrieNode reports whether a provided database entry is a legacy trie
|
|
// node. The characteristics of legacy trie node are:
|
|
// - the key length is 32 bytes
|
|
// - the key is the hash of val
|
|
func IsLegacyTrieNode(key []byte, val []byte) bool {
|
|
if len(key) != common.HashLength {
|
|
return false
|
|
}
|
|
return bytes.Equal(key, crypto.Keccak256(val))
|
|
}
|
|
|
|
// IsAccountTrieNode reports whether a provided database entry is an account
|
|
// trie node in path-based state scheme.
|
|
func IsAccountTrieNode(key []byte) (bool, []byte) {
|
|
if !bytes.HasPrefix(key, trieNodeAccountPrefix) {
|
|
return false, nil
|
|
}
|
|
// The remaining key should only consist a hex node path
|
|
// whose length is in the range 0 to 64 (64 is excluded
|
|
// since leaves are always wrapped with shortNode).
|
|
if len(key) >= len(trieNodeAccountPrefix)+common.HashLength*2 {
|
|
return false, nil
|
|
}
|
|
return true, key[len(trieNodeAccountPrefix):]
|
|
}
|
|
|
|
// IsStorageTrieNode reports whether a provided database entry is a storage
|
|
// trie node in path-based state scheme.
|
|
func IsStorageTrieNode(key []byte) (bool, common.Hash, []byte) {
|
|
if !bytes.HasPrefix(key, trieNodeStoragePrefix) {
|
|
return false, common.Hash{}, nil
|
|
}
|
|
// The remaining key consists of 2 parts:
|
|
// - 32 bytes account hash
|
|
// - hex node path whose length is in the range 0 to 64
|
|
if len(key) < len(trieNodeStoragePrefix)+common.HashLength {
|
|
return false, common.Hash{}, nil
|
|
}
|
|
if len(key) >= len(trieNodeStoragePrefix)+common.HashLength+common.HashLength*2 {
|
|
return false, common.Hash{}, nil
|
|
}
|
|
accountHash := common.BytesToHash(key[len(trieNodeStoragePrefix) : len(trieNodeStoragePrefix)+common.HashLength])
|
|
return true, accountHash, key[len(trieNodeStoragePrefix)+common.HashLength:]
|
|
}
|
|
|
|
// xdposV1Key = xdposV1Prefix + hash
|
|
func xdposV1Key(hash common.Hash) []byte {
|
|
return append(xdposV1Prefix, hash.Bytes()...)
|
|
}
|
|
|
|
// xdposV2Key = xdposV2Prefix + hash
|
|
func xdposV2Key(hash common.Hash) []byte {
|
|
return append(xdposV2Prefix, hash.Bytes()...)
|
|
}
|
|
|
|
// sectionHeadKey = sectionHeadKeyPrefix + section (uint64 big endian)
|
|
func sectionHeadKey(section uint64) []byte {
|
|
var data [8]byte
|
|
binary.BigEndian.PutUint64(data[:], section)
|
|
return append(sectionHeadKeyPrefix, data[:]...)
|
|
}
|