2018-05-07 06:35:06 -05:00
|
|
|
// 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 (
|
|
|
|
"encoding/binary"
|
|
|
|
|
|
|
|
"github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/ethereum/go-ethereum/metrics"
|
|
|
|
)
|
|
|
|
|
|
|
|
// The fields below define the low level database schema prefixing.
|
|
|
|
var (
|
|
|
|
// databaseVerisionKey tracks the current database version.
|
|
|
|
databaseVerisionKey = []byte("DatabaseVersion")
|
|
|
|
|
2019-05-04 05:17:47 -05:00
|
|
|
// headHeaderKey tracks the latest known header's hash.
|
2018-05-07 06:35:06 -05:00
|
|
|
headHeaderKey = []byte("LastHeader")
|
|
|
|
|
2019-05-04 05:17:47 -05:00
|
|
|
// headBlockKey tracks the latest known full block's hash.
|
2018-05-07 06:35:06 -05:00
|
|
|
headBlockKey = []byte("LastBlock")
|
|
|
|
|
2018-11-09 04:51:07 -06:00
|
|
|
// headFastBlockKey tracks the latest known incomplete block's hash during fast sync.
|
2018-05-07 06:35:06 -05:00
|
|
|
headFastBlockKey = []byte("LastFast")
|
|
|
|
|
|
|
|
// fastTrieProgressKey tracks the number of trie entries imported during fast sync.
|
|
|
|
fastTrieProgressKey = []byte("TrieSync")
|
|
|
|
|
2019-12-02 05:27:20 -06:00
|
|
|
// snapshotRootKey tracks the hash of the last snapshot.
|
2019-11-22 05:23:49 -06:00
|
|
|
snapshotRootKey = []byte("SnapshotRoot")
|
2019-08-06 05:40:28 -05:00
|
|
|
|
2019-12-02 05:27:20 -06:00
|
|
|
// snapshotJournalKey tracks the in-memory diff layers across restarts.
|
|
|
|
snapshotJournalKey = []byte("SnapshotJournal")
|
|
|
|
|
2018-05-07 06:35:06 -05:00
|
|
|
// 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
|
|
|
|
|
2019-11-26 01:48:29 -06:00
|
|
|
txLookupPrefix = []byte("l") // txLookupPrefix + hash -> transaction/receipt lookup metadata
|
|
|
|
bloomBitsPrefix = []byte("B") // bloomBitsPrefix + bit (uint16 big endian) + section (uint64 big endian) + hash -> bloom bits
|
|
|
|
SnapshotAccountPrefix = []byte("a") // SnapshotAccountPrefix + account hash -> account trie value
|
2020-03-06 06:05:44 -06:00
|
|
|
SnapshotStoragePrefix = []byte("o") // SnapshotStoragePrefix + account hash + storage hash -> storage trie value
|
2018-05-07 06:35:06 -05:00
|
|
|
|
|
|
|
preimagePrefix = []byte("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
|
|
|
|
|
|
|
|
preimageCounter = metrics.NewRegisteredCounter("db/preimage/total", nil)
|
|
|
|
preimageHitCounter = metrics.NewRegisteredCounter("db/preimage/hits", nil)
|
|
|
|
)
|
|
|
|
|
all: integrate the freezer with fast sync
* all: freezer style syncing
core, eth, les, light: clean up freezer relative APIs
core, eth, les, trie, ethdb, light: clean a bit
core, eth, les, light: add unit tests
core, light: rewrite setHead function
core, eth: fix downloader unit tests
core: add receipt chain insertion test
core: use constant instead of hardcoding table name
core: fix rollback
core: fix setHead
core/rawdb: remove canonical block first and then iterate side chain
core/rawdb, ethdb: add hasAncient interface
eth/downloader: calculate ancient limit via cht first
core, eth, ethdb: lots of fixes
* eth/downloader: print ancient disable log only for fast sync
2019-04-25 09:59:48 -05:00
|
|
|
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"
|
|
|
|
)
|
|
|
|
|
2019-05-03 05:55:36 -05:00
|
|
|
// freezerNoSnappy configures whether compression is disabled for the ancient-tables.
|
|
|
|
// Hashes and difficulties don't compress well.
|
|
|
|
var freezerNoSnappy = map[string]bool{
|
|
|
|
freezerHeaderTable: false,
|
|
|
|
freezerHashTable: true,
|
|
|
|
freezerBodiesTable: false,
|
|
|
|
freezerReceiptTable: false,
|
|
|
|
freezerDifficultyTable: true,
|
|
|
|
}
|
|
|
|
|
2019-02-21 07:14:35 -06:00
|
|
|
// LegacyTxLookupEntry is the legacy TxLookupEntry definition with some unnecessary
|
|
|
|
// fields.
|
|
|
|
type LegacyTxLookupEntry struct {
|
2018-05-07 06:35:06 -05:00
|
|
|
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
|
|
|
|
}
|
2018-06-11 08:06:26 -05:00
|
|
|
|
2018-09-24 07:57:49 -05:00
|
|
|
// headerKeyPrefix = headerPrefix + num (uint64 big endian)
|
|
|
|
func headerKeyPrefix(number uint64) []byte {
|
|
|
|
return append(headerPrefix, encodeBlockNumber(number)...)
|
|
|
|
}
|
|
|
|
|
2018-06-11 08:06:26 -05:00
|
|
|
// 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()...)
|
|
|
|
}
|
|
|
|
|
2019-11-26 01:48:29 -06:00
|
|
|
// accountSnapshotKey = SnapshotAccountPrefix + hash
|
2019-08-06 05:40:28 -05:00
|
|
|
func accountSnapshotKey(hash common.Hash) []byte {
|
2019-11-26 01:48:29 -06:00
|
|
|
return append(SnapshotAccountPrefix, hash.Bytes()...)
|
2019-08-06 05:40:28 -05:00
|
|
|
}
|
|
|
|
|
2019-11-26 01:48:29 -06:00
|
|
|
// storageSnapshotKey = SnapshotStoragePrefix + account hash + storage hash
|
2019-08-06 05:40:28 -05:00
|
|
|
func storageSnapshotKey(accountHash, storageHash common.Hash) []byte {
|
2019-11-26 01:48:29 -06:00
|
|
|
return append(append(SnapshotStoragePrefix, accountHash.Bytes()...), storageHash.Bytes()...)
|
2019-08-06 05:40:28 -05:00
|
|
|
}
|
|
|
|
|
2019-11-26 01:48:29 -06:00
|
|
|
// storageSnapshotsKey = SnapshotStoragePrefix + account hash + storage hash
|
2019-08-06 05:40:28 -05:00
|
|
|
func storageSnapshotsKey(accountHash common.Hash) []byte {
|
2019-11-26 01:48:29 -06:00
|
|
|
return append(SnapshotStoragePrefix, accountHash.Bytes()...)
|
2019-08-06 05:40:28 -05:00
|
|
|
}
|
|
|
|
|
2018-06-11 08:06:26 -05:00
|
|
|
// 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()...)
|
|
|
|
}
|
|
|
|
|
|
|
|
// configKey = configPrefix + hash
|
|
|
|
func configKey(hash common.Hash) []byte {
|
|
|
|
return append(configPrefix, hash.Bytes()...)
|
|
|
|
}
|