mirror of https://github.com/ethereum/go-ethereum
core/rawdb: separate raw database access to own package (#16666)
parent
5463ed9996
commit
6cf0ab38bd
@ -1,652 +0,0 @@ |
||||
// Copyright 2015 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 core |
||||
|
||||
import ( |
||||
"bytes" |
||||
"encoding/binary" |
||||
"encoding/json" |
||||
"errors" |
||||
"fmt" |
||||
"math/big" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/core/types" |
||||
"github.com/ethereum/go-ethereum/ethdb" |
||||
"github.com/ethereum/go-ethereum/log" |
||||
"github.com/ethereum/go-ethereum/metrics" |
||||
"github.com/ethereum/go-ethereum/params" |
||||
"github.com/ethereum/go-ethereum/rlp" |
||||
) |
||||
|
||||
// DatabaseReader wraps the Get method of a backing data store.
|
||||
type DatabaseReader interface { |
||||
Get(key []byte) (value []byte, err error) |
||||
} |
||||
|
||||
// DatabaseDeleter wraps the Delete method of a backing data store.
|
||||
type DatabaseDeleter interface { |
||||
Delete(key []byte) error |
||||
} |
||||
|
||||
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) |
||||
) |
||||
|
||||
// TxLookupEntry is a positional metadata to help looking up the data content of
|
||||
// a transaction or receipt given only its hash.
|
||||
type TxLookupEntry 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 |
||||
} |
||||
|
||||
// GetCanonicalHash retrieves a hash assigned to a canonical block number.
|
||||
func GetCanonicalHash(db DatabaseReader, number uint64) common.Hash { |
||||
data, _ := db.Get(append(append(headerPrefix, encodeBlockNumber(number)...), numSuffix...)) |
||||
if len(data) == 0 { |
||||
return common.Hash{} |
||||
} |
||||
return common.BytesToHash(data) |
||||
} |
||||
|
||||
// missingNumber is returned by GetBlockNumber if no header with the
|
||||
// given block hash has been stored in the database
|
||||
const missingNumber = uint64(0xffffffffffffffff) |
||||
|
||||
// GetBlockNumber returns the block number assigned to a block hash
|
||||
// if the corresponding header is present in the database
|
||||
func GetBlockNumber(db DatabaseReader, hash common.Hash) uint64 { |
||||
data, _ := db.Get(append(blockHashPrefix, hash.Bytes()...)) |
||||
if len(data) != 8 { |
||||
return missingNumber |
||||
} |
||||
return binary.BigEndian.Uint64(data) |
||||
} |
||||
|
||||
// GetHeadHeaderHash retrieves the hash of the current canonical head block's
|
||||
// header. The difference between this and GetHeadBlockHash is that whereas the
|
||||
// last block hash is only updated upon a full block import, the last header
|
||||
// hash is updated already at header import, allowing head tracking for the
|
||||
// light synchronization mechanism.
|
||||
func GetHeadHeaderHash(db DatabaseReader) common.Hash { |
||||
data, _ := db.Get(headHeaderKey) |
||||
if len(data) == 0 { |
||||
return common.Hash{} |
||||
} |
||||
return common.BytesToHash(data) |
||||
} |
||||
|
||||
// GetHeadBlockHash retrieves the hash of the current canonical head block.
|
||||
func GetHeadBlockHash(db DatabaseReader) common.Hash { |
||||
data, _ := db.Get(headBlockKey) |
||||
if len(data) == 0 { |
||||
return common.Hash{} |
||||
} |
||||
return common.BytesToHash(data) |
||||
} |
||||
|
||||
// GetHeadFastBlockHash retrieves the hash of the current canonical head block during
|
||||
// fast synchronization. The difference between this and GetHeadBlockHash is that
|
||||
// whereas the last block hash is only updated upon a full block import, the last
|
||||
// fast hash is updated when importing pre-processed blocks.
|
||||
func GetHeadFastBlockHash(db DatabaseReader) common.Hash { |
||||
data, _ := db.Get(headFastKey) |
||||
if len(data) == 0 { |
||||
return common.Hash{} |
||||
} |
||||
return common.BytesToHash(data) |
||||
} |
||||
|
||||
// GetTrieSyncProgress retrieves the number of tries nodes fast synced to allow
|
||||
// reportinc correct numbers across restarts.
|
||||
func GetTrieSyncProgress(db DatabaseReader) uint64 { |
||||
data, _ := db.Get(trieSyncKey) |
||||
if len(data) == 0 { |
||||
return 0 |
||||
} |
||||
return new(big.Int).SetBytes(data).Uint64() |
||||
} |
||||
|
||||
// GetHeaderRLP retrieves a block header in its raw RLP database encoding, or nil
|
||||
// if the header's not found.
|
||||
func GetHeaderRLP(db DatabaseReader, hash common.Hash, number uint64) rlp.RawValue { |
||||
data, _ := db.Get(headerKey(hash, number)) |
||||
return data |
||||
} |
||||
|
||||
// GetHeader retrieves the block header corresponding to the hash, nil if none
|
||||
// found.
|
||||
func GetHeader(db DatabaseReader, hash common.Hash, number uint64) *types.Header { |
||||
data := GetHeaderRLP(db, hash, number) |
||||
if len(data) == 0 { |
||||
return nil |
||||
} |
||||
header := new(types.Header) |
||||
if err := rlp.Decode(bytes.NewReader(data), header); err != nil { |
||||
log.Error("Invalid block header RLP", "hash", hash, "err", err) |
||||
return nil |
||||
} |
||||
return header |
||||
} |
||||
|
||||
// GetBodyRLP retrieves the block body (transactions and uncles) in RLP encoding.
|
||||
func GetBodyRLP(db DatabaseReader, hash common.Hash, number uint64) rlp.RawValue { |
||||
data, _ := db.Get(blockBodyKey(hash, number)) |
||||
return data |
||||
} |
||||
|
||||
func headerKey(hash common.Hash, number uint64) []byte { |
||||
return append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...) |
||||
} |
||||
|
||||
func blockBodyKey(hash common.Hash, number uint64) []byte { |
||||
return append(append(bodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...) |
||||
} |
||||
|
||||
// GetBody retrieves the block body (transactons, uncles) corresponding to the
|
||||
// hash, nil if none found.
|
||||
func GetBody(db DatabaseReader, hash common.Hash, number uint64) *types.Body { |
||||
data := GetBodyRLP(db, hash, number) |
||||
if len(data) == 0 { |
||||
return nil |
||||
} |
||||
body := new(types.Body) |
||||
if err := rlp.Decode(bytes.NewReader(data), body); err != nil { |
||||
log.Error("Invalid block body RLP", "hash", hash, "err", err) |
||||
return nil |
||||
} |
||||
return body |
||||
} |
||||
|
||||
// GetTd retrieves a block's total difficulty corresponding to the hash, nil if
|
||||
// none found.
|
||||
func GetTd(db DatabaseReader, hash common.Hash, number uint64) *big.Int { |
||||
data, _ := db.Get(append(append(append(headerPrefix, encodeBlockNumber(number)...), hash[:]...), tdSuffix...)) |
||||
if len(data) == 0 { |
||||
return nil |
||||
} |
||||
td := new(big.Int) |
||||
if err := rlp.Decode(bytes.NewReader(data), td); err != nil { |
||||
log.Error("Invalid block total difficulty RLP", "hash", hash, "err", err) |
||||
return nil |
||||
} |
||||
return td |
||||
} |
||||
|
||||
// GetBlock retrieves an entire block corresponding to the hash, assembling it
|
||||
// back from the stored header and body. If either the header or body could not
|
||||
// be retrieved nil is returned.
|
||||
//
|
||||
// Note, due to concurrent download of header and block body the header and thus
|
||||
// canonical hash can be stored in the database but the body data not (yet).
|
||||
func GetBlock(db DatabaseReader, hash common.Hash, number uint64) *types.Block { |
||||
// Retrieve the block header and body contents
|
||||
header := GetHeader(db, hash, number) |
||||
if header == nil { |
||||
return nil |
||||
} |
||||
body := GetBody(db, hash, number) |
||||
if body == nil { |
||||
return nil |
||||
} |
||||
// Reassemble the block and return
|
||||
return types.NewBlockWithHeader(header).WithBody(body.Transactions, body.Uncles) |
||||
} |
||||
|
||||
// GetBlockReceipts retrieves the receipts generated by the transactions included
|
||||
// in a block given by its hash.
|
||||
func GetBlockReceipts(db DatabaseReader, hash common.Hash, number uint64) types.Receipts { |
||||
data, _ := db.Get(append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash[:]...)) |
||||
if len(data) == 0 { |
||||
return nil |
||||
} |
||||
storageReceipts := []*types.ReceiptForStorage{} |
||||
if err := rlp.DecodeBytes(data, &storageReceipts); err != nil { |
||||
log.Error("Invalid receipt array RLP", "hash", hash, "err", err) |
||||
return nil |
||||
} |
||||
receipts := make(types.Receipts, len(storageReceipts)) |
||||
for i, receipt := range storageReceipts { |
||||
receipts[i] = (*types.Receipt)(receipt) |
||||
} |
||||
return receipts |
||||
} |
||||
|
||||
// GetTxLookupEntry retrieves the positional metadata associated with a transaction
|
||||
// hash to allow retrieving the transaction or receipt by hash.
|
||||
func GetTxLookupEntry(db DatabaseReader, hash common.Hash) (common.Hash, uint64, uint64) { |
||||
// Load the positional metadata from disk and bail if it fails
|
||||
data, _ := db.Get(append(lookupPrefix, hash.Bytes()...)) |
||||
if len(data) == 0 { |
||||
return common.Hash{}, 0, 0 |
||||
} |
||||
// Parse and return the contents of the lookup entry
|
||||
var entry TxLookupEntry |
||||
if err := rlp.DecodeBytes(data, &entry); err != nil { |
||||
log.Error("Invalid lookup entry RLP", "hash", hash, "err", err) |
||||
return common.Hash{}, 0, 0 |
||||
} |
||||
return entry.BlockHash, entry.BlockIndex, entry.Index |
||||
} |
||||
|
||||
// GetTransaction retrieves a specific transaction from the database, along with
|
||||
// its added positional metadata.
|
||||
func GetTransaction(db DatabaseReader, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) { |
||||
// Retrieve the lookup metadata and resolve the transaction from the body
|
||||
blockHash, blockNumber, txIndex := GetTxLookupEntry(db, hash) |
||||
|
||||
if blockHash != (common.Hash{}) { |
||||
body := GetBody(db, blockHash, blockNumber) |
||||
if body == nil || len(body.Transactions) <= int(txIndex) { |
||||
log.Error("Transaction referenced missing", "number", blockNumber, "hash", blockHash, "index", txIndex) |
||||
return nil, common.Hash{}, 0, 0 |
||||
} |
||||
return body.Transactions[txIndex], blockHash, blockNumber, txIndex |
||||
} |
||||
// Old transaction representation, load the transaction and it's metadata separately
|
||||
data, _ := db.Get(hash.Bytes()) |
||||
if len(data) == 0 { |
||||
return nil, common.Hash{}, 0, 0 |
||||
} |
||||
var tx types.Transaction |
||||
if err := rlp.DecodeBytes(data, &tx); err != nil { |
||||
return nil, common.Hash{}, 0, 0 |
||||
} |
||||
// Retrieve the blockchain positional metadata
|
||||
data, _ = db.Get(append(hash.Bytes(), oldTxMetaSuffix...)) |
||||
if len(data) == 0 { |
||||
return nil, common.Hash{}, 0, 0 |
||||
} |
||||
var entry TxLookupEntry |
||||
if err := rlp.DecodeBytes(data, &entry); err != nil { |
||||
return nil, common.Hash{}, 0, 0 |
||||
} |
||||
return &tx, entry.BlockHash, entry.BlockIndex, entry.Index |
||||
} |
||||
|
||||
// GetReceipt retrieves a specific transaction receipt from the database, along with
|
||||
// its added positional metadata.
|
||||
func GetReceipt(db DatabaseReader, hash common.Hash) (*types.Receipt, common.Hash, uint64, uint64) { |
||||
// Retrieve the lookup metadata and resolve the receipt from the receipts
|
||||
blockHash, blockNumber, receiptIndex := GetTxLookupEntry(db, hash) |
||||
|
||||
if blockHash != (common.Hash{}) { |
||||
receipts := GetBlockReceipts(db, blockHash, blockNumber) |
||||
if len(receipts) <= int(receiptIndex) { |
||||
log.Error("Receipt refereced missing", "number", blockNumber, "hash", blockHash, "index", receiptIndex) |
||||
return nil, common.Hash{}, 0, 0 |
||||
} |
||||
return receipts[receiptIndex], blockHash, blockNumber, receiptIndex |
||||
} |
||||
// Old receipt representation, load the receipt and set an unknown metadata
|
||||
data, _ := db.Get(append(oldReceiptsPrefix, hash[:]...)) |
||||
if len(data) == 0 { |
||||
return nil, common.Hash{}, 0, 0 |
||||
} |
||||
var receipt types.ReceiptForStorage |
||||
err := rlp.DecodeBytes(data, &receipt) |
||||
if err != nil { |
||||
log.Error("Invalid receipt RLP", "hash", hash, "err", err) |
||||
} |
||||
return (*types.Receipt)(&receipt), common.Hash{}, 0, 0 |
||||
} |
||||
|
||||
// GetBloomBits retrieves the compressed bloom bit vector belonging to the given
|
||||
// section and bit index from the.
|
||||
func GetBloomBits(db DatabaseReader, bit uint, section uint64, head common.Hash) ([]byte, error) { |
||||
key := append(append(bloomBitsPrefix, make([]byte, 10)...), head.Bytes()...) |
||||
|
||||
binary.BigEndian.PutUint16(key[1:], uint16(bit)) |
||||
binary.BigEndian.PutUint64(key[3:], section) |
||||
|
||||
return db.Get(key) |
||||
} |
||||
|
||||
// WriteCanonicalHash stores the canonical hash for the given block number.
|
||||
func WriteCanonicalHash(db ethdb.Putter, hash common.Hash, number uint64) error { |
||||
key := append(append(headerPrefix, encodeBlockNumber(number)...), numSuffix...) |
||||
if err := db.Put(key, hash.Bytes()); err != nil { |
||||
log.Crit("Failed to store number to hash mapping", "err", err) |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// WriteHeadHeaderHash stores the head header's hash.
|
||||
func WriteHeadHeaderHash(db ethdb.Putter, hash common.Hash) error { |
||||
if err := db.Put(headHeaderKey, hash.Bytes()); err != nil { |
||||
log.Crit("Failed to store last header's hash", "err", err) |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// WriteHeadBlockHash stores the head block's hash.
|
||||
func WriteHeadBlockHash(db ethdb.Putter, hash common.Hash) error { |
||||
if err := db.Put(headBlockKey, hash.Bytes()); err != nil { |
||||
log.Crit("Failed to store last block's hash", "err", err) |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// WriteHeadFastBlockHash stores the fast head block's hash.
|
||||
func WriteHeadFastBlockHash(db ethdb.Putter, hash common.Hash) error { |
||||
if err := db.Put(headFastKey, hash.Bytes()); err != nil { |
||||
log.Crit("Failed to store last fast block's hash", "err", err) |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// WriteTrieSyncProgress stores the fast sync trie process counter to support
|
||||
// retrieving it across restarts.
|
||||
func WriteTrieSyncProgress(db ethdb.Putter, count uint64) error { |
||||
if err := db.Put(trieSyncKey, new(big.Int).SetUint64(count).Bytes()); err != nil { |
||||
log.Crit("Failed to store fast sync trie progress", "err", err) |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// WriteHeader serializes a block header into the database.
|
||||
func WriteHeader(db ethdb.Putter, header *types.Header) error { |
||||
data, err := rlp.EncodeToBytes(header) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
hash := header.Hash().Bytes() |
||||
num := header.Number.Uint64() |
||||
encNum := encodeBlockNumber(num) |
||||
key := append(blockHashPrefix, hash...) |
||||
if err := db.Put(key, encNum); err != nil { |
||||
log.Crit("Failed to store hash to number mapping", "err", err) |
||||
} |
||||
key = append(append(headerPrefix, encNum...), hash...) |
||||
if err := db.Put(key, data); err != nil { |
||||
log.Crit("Failed to store header", "err", err) |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// WriteBody serializes the body of a block into the database.
|
||||
func WriteBody(db ethdb.Putter, hash common.Hash, number uint64, body *types.Body) error { |
||||
data, err := rlp.EncodeToBytes(body) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
return WriteBodyRLP(db, hash, number, data) |
||||
} |
||||
|
||||
// WriteBodyRLP writes a serialized body of a block into the database.
|
||||
func WriteBodyRLP(db ethdb.Putter, hash common.Hash, number uint64, rlp rlp.RawValue) error { |
||||
key := append(append(bodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...) |
||||
if err := db.Put(key, rlp); err != nil { |
||||
log.Crit("Failed to store block body", "err", err) |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// WriteTd serializes the total difficulty of a block into the database.
|
||||
func WriteTd(db ethdb.Putter, hash common.Hash, number uint64, td *big.Int) error { |
||||
data, err := rlp.EncodeToBytes(td) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
key := append(append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...), tdSuffix...) |
||||
if err := db.Put(key, data); err != nil { |
||||
log.Crit("Failed to store block total difficulty", "err", err) |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// WriteBlock serializes a block into the database, header and body separately.
|
||||
func WriteBlock(db ethdb.Putter, block *types.Block) error { |
||||
// Store the body first to retain database consistency
|
||||
if err := WriteBody(db, block.Hash(), block.NumberU64(), block.Body()); err != nil { |
||||
return err |
||||
} |
||||
// Store the header too, signaling full block ownership
|
||||
if err := WriteHeader(db, block.Header()); err != nil { |
||||
return err |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// WriteBlockReceipts stores all the transaction receipts belonging to a block
|
||||
// as a single receipt slice. This is used during chain reorganisations for
|
||||
// rescheduling dropped transactions.
|
||||
func WriteBlockReceipts(db ethdb.Putter, hash common.Hash, number uint64, receipts types.Receipts) error { |
||||
// Convert the receipts into their storage form and serialize them
|
||||
storageReceipts := make([]*types.ReceiptForStorage, len(receipts)) |
||||
for i, receipt := range receipts { |
||||
storageReceipts[i] = (*types.ReceiptForStorage)(receipt) |
||||
} |
||||
bytes, err := rlp.EncodeToBytes(storageReceipts) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
// Store the flattened receipt slice
|
||||
key := append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...) |
||||
if err := db.Put(key, bytes); err != nil { |
||||
log.Crit("Failed to store block receipts", "err", err) |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// WriteTxLookupEntries stores a positional metadata for every transaction from
|
||||
// a block, enabling hash based transaction and receipt lookups.
|
||||
func WriteTxLookupEntries(db ethdb.Putter, block *types.Block) error { |
||||
// Iterate over each transaction and encode its metadata
|
||||
for i, tx := range block.Transactions() { |
||||
entry := TxLookupEntry{ |
||||
BlockHash: block.Hash(), |
||||
BlockIndex: block.NumberU64(), |
||||
Index: uint64(i), |
||||
} |
||||
data, err := rlp.EncodeToBytes(entry) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
if err := db.Put(append(lookupPrefix, tx.Hash().Bytes()...), data); err != nil { |
||||
return err |
||||
} |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// WriteBloomBits writes the compressed bloom bits vector belonging to the given
|
||||
// section and bit index.
|
||||
func WriteBloomBits(db ethdb.Putter, bit uint, section uint64, head common.Hash, bits []byte) { |
||||
key := append(append(bloomBitsPrefix, make([]byte, 10)...), head.Bytes()...) |
||||
|
||||
binary.BigEndian.PutUint16(key[1:], uint16(bit)) |
||||
binary.BigEndian.PutUint64(key[3:], section) |
||||
|
||||
if err := db.Put(key, bits); err != nil { |
||||
log.Crit("Failed to store bloom bits", "err", err) |
||||
} |
||||
} |
||||
|
||||
// DeleteCanonicalHash removes the number to hash canonical mapping.
|
||||
func DeleteCanonicalHash(db DatabaseDeleter, number uint64) { |
||||
db.Delete(append(append(headerPrefix, encodeBlockNumber(number)...), numSuffix...)) |
||||
} |
||||
|
||||
// DeleteHeader removes all block header data associated with a hash.
|
||||
func DeleteHeader(db DatabaseDeleter, hash common.Hash, number uint64) { |
||||
db.Delete(append(blockHashPrefix, hash.Bytes()...)) |
||||
db.Delete(append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...)) |
||||
} |
||||
|
||||
// DeleteBody removes all block body data associated with a hash.
|
||||
func DeleteBody(db DatabaseDeleter, hash common.Hash, number uint64) { |
||||
db.Delete(append(append(bodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...)) |
||||
} |
||||
|
||||
// DeleteTd removes all block total difficulty data associated with a hash.
|
||||
func DeleteTd(db DatabaseDeleter, hash common.Hash, number uint64) { |
||||
db.Delete(append(append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...), tdSuffix...)) |
||||
} |
||||
|
||||
// DeleteBlock removes all block data associated with a hash.
|
||||
func DeleteBlock(db DatabaseDeleter, hash common.Hash, number uint64) { |
||||
DeleteBlockReceipts(db, hash, number) |
||||
DeleteHeader(db, hash, number) |
||||
DeleteBody(db, hash, number) |
||||
DeleteTd(db, hash, number) |
||||
} |
||||
|
||||
// DeleteBlockReceipts removes all receipt data associated with a block hash.
|
||||
func DeleteBlockReceipts(db DatabaseDeleter, hash common.Hash, number uint64) { |
||||
db.Delete(append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...)) |
||||
} |
||||
|
||||
// DeleteTxLookupEntry removes all transaction data associated with a hash.
|
||||
func DeleteTxLookupEntry(db DatabaseDeleter, hash common.Hash) { |
||||
db.Delete(append(lookupPrefix, hash.Bytes()...)) |
||||
} |
||||
|
||||
// PreimageTable returns a Database instance with the key prefix for preimage entries.
|
||||
func PreimageTable(db ethdb.Database) ethdb.Database { |
||||
return ethdb.NewTable(db, preimagePrefix) |
||||
} |
||||
|
||||
// WritePreimages writes the provided set of preimages to the database. `number` is the
|
||||
// current block number, and is used for debug messages only.
|
||||
func WritePreimages(db ethdb.Database, number uint64, preimages map[common.Hash][]byte) error { |
||||
table := PreimageTable(db) |
||||
batch := table.NewBatch() |
||||
hitCount := 0 |
||||
for hash, preimage := range preimages { |
||||
if _, err := table.Get(hash.Bytes()); err != nil { |
||||
batch.Put(hash.Bytes(), preimage) |
||||
hitCount++ |
||||
} |
||||
} |
||||
preimageCounter.Inc(int64(len(preimages))) |
||||
preimageHitCounter.Inc(int64(hitCount)) |
||||
if hitCount > 0 { |
||||
if err := batch.Write(); err != nil { |
||||
return fmt.Errorf("preimage write fail for block %d: %v", number, err) |
||||
} |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// GetBlockChainVersion reads the version number from db.
|
||||
func GetBlockChainVersion(db DatabaseReader) int { |
||||
var vsn uint |
||||
enc, _ := db.Get([]byte("BlockchainVersion")) |
||||
rlp.DecodeBytes(enc, &vsn) |
||||
return int(vsn) |
||||
} |
||||
|
||||
// WriteBlockChainVersion writes vsn as the version number to db.
|
||||
func WriteBlockChainVersion(db ethdb.Putter, vsn int) { |
||||
enc, _ := rlp.EncodeToBytes(uint(vsn)) |
||||
db.Put([]byte("BlockchainVersion"), enc) |
||||
} |
||||
|
||||
// WriteChainConfig writes the chain config settings to the database.
|
||||
func WriteChainConfig(db ethdb.Putter, hash common.Hash, cfg *params.ChainConfig) error { |
||||
// short circuit and ignore if nil config. GetChainConfig
|
||||
// will return a default.
|
||||
if cfg == nil { |
||||
return nil |
||||
} |
||||
|
||||
jsonChainConfig, err := json.Marshal(cfg) |
||||
if err != nil { |
||||
return err |
||||
} |
||||
|
||||
return db.Put(append(configPrefix, hash[:]...), jsonChainConfig) |
||||
} |
||||
|
||||
// GetChainConfig will fetch the network settings based on the given hash.
|
||||
func GetChainConfig(db DatabaseReader, hash common.Hash) (*params.ChainConfig, error) { |
||||
jsonChainConfig, _ := db.Get(append(configPrefix, hash[:]...)) |
||||
if len(jsonChainConfig) == 0 { |
||||
return nil, ErrChainConfigNotFound |
||||
} |
||||
|
||||
var config params.ChainConfig |
||||
if err := json.Unmarshal(jsonChainConfig, &config); err != nil { |
||||
return nil, err |
||||
} |
||||
|
||||
return &config, nil |
||||
} |
||||
|
||||
// FindCommonAncestor returns the last common ancestor of two block headers
|
||||
func FindCommonAncestor(db DatabaseReader, a, b *types.Header) *types.Header { |
||||
for bn := b.Number.Uint64(); a.Number.Uint64() > bn; { |
||||
a = GetHeader(db, a.ParentHash, a.Number.Uint64()-1) |
||||
if a == nil { |
||||
return nil |
||||
} |
||||
} |
||||
for an := a.Number.Uint64(); an < b.Number.Uint64(); { |
||||
b = GetHeader(db, b.ParentHash, b.Number.Uint64()-1) |
||||
if b == nil { |
||||
return nil |
||||
} |
||||
} |
||||
for a.Hash() != b.Hash() { |
||||
a = GetHeader(db, a.ParentHash, a.Number.Uint64()-1) |
||||
if a == nil { |
||||
return nil |
||||
} |
||||
b = GetHeader(db, b.ParentHash, b.Number.Uint64()-1) |
||||
if b == nil { |
||||
return nil |
||||
} |
||||
} |
||||
return a |
||||
} |
@ -0,0 +1,381 @@ |
||||
// 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 |
||||
|
||||
import ( |
||||
"bytes" |
||||
"encoding/binary" |
||||
"math/big" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/core/types" |
||||
"github.com/ethereum/go-ethereum/log" |
||||
"github.com/ethereum/go-ethereum/rlp" |
||||
) |
||||
|
||||
// ReadCanonicalHash retrieves the hash assigned to a canonical block number.
|
||||
func ReadCanonicalHash(db DatabaseReader, number uint64) common.Hash { |
||||
data, _ := db.Get(append(append(headerPrefix, encodeBlockNumber(number)...), headerHashSuffix...)) |
||||
if len(data) == 0 { |
||||
return common.Hash{} |
||||
} |
||||
return common.BytesToHash(data) |
||||
} |
||||
|
||||
// WriteCanonicalHash stores the hash assigned to a canonical block number.
|
||||
func WriteCanonicalHash(db DatabaseWriter, hash common.Hash, number uint64) { |
||||
key := append(append(headerPrefix, encodeBlockNumber(number)...), headerHashSuffix...) |
||||
if err := db.Put(key, hash.Bytes()); err != nil { |
||||
log.Crit("Failed to store number to hash mapping", "err", err) |
||||
} |
||||
} |
||||
|
||||
// DeleteCanonicalHash removes the number to hash canonical mapping.
|
||||
func DeleteCanonicalHash(db DatabaseDeleter, number uint64) { |
||||
if err := db.Delete(append(append(headerPrefix, encodeBlockNumber(number)...), headerHashSuffix...)); err != nil { |
||||
log.Crit("Failed to delete number to hash mapping", "err", err) |
||||
} |
||||
} |
||||
|
||||
// ReadHeaderNumber returns the header number assigned to a hash.
|
||||
func ReadHeaderNumber(db DatabaseReader, hash common.Hash) *uint64 { |
||||
data, _ := db.Get(append(headerNumberPrefix, hash.Bytes()...)) |
||||
if len(data) != 8 { |
||||
return nil |
||||
} |
||||
number := binary.BigEndian.Uint64(data) |
||||
return &number |
||||
} |
||||
|
||||
// ReadHeadHeaderHash retrieves the hash of the current canonical head header.
|
||||
func ReadHeadHeaderHash(db DatabaseReader) common.Hash { |
||||
data, _ := db.Get(headHeaderKey) |
||||
if len(data) == 0 { |
||||
return common.Hash{} |
||||
} |
||||
return common.BytesToHash(data) |
||||
} |
||||
|
||||
// WriteHeadHeaderHash stores the hash of the current canonical head header.
|
||||
func WriteHeadHeaderHash(db DatabaseWriter, hash common.Hash) { |
||||
if err := db.Put(headHeaderKey, hash.Bytes()); err != nil { |
||||
log.Crit("Failed to store last header's hash", "err", err) |
||||
} |
||||
} |
||||
|
||||
// ReadHeadBlockHash retrieves the hash of the current canonical head block.
|
||||
func ReadHeadBlockHash(db DatabaseReader) common.Hash { |
||||
data, _ := db.Get(headBlockKey) |
||||
if len(data) == 0 { |
||||
return common.Hash{} |
||||
} |
||||
return common.BytesToHash(data) |
||||
} |
||||
|
||||
// WriteHeadBlockHash stores the head block's hash.
|
||||
func WriteHeadBlockHash(db DatabaseWriter, hash common.Hash) { |
||||
if err := db.Put(headBlockKey, hash.Bytes()); err != nil { |
||||
log.Crit("Failed to store last block's hash", "err", err) |
||||
} |
||||
} |
||||
|
||||
// ReadHeadFastBlockHash retrieves the hash of the current fast-sync head block.
|
||||
func ReadHeadFastBlockHash(db DatabaseReader) common.Hash { |
||||
data, _ := db.Get(headFastBlockKey) |
||||
if len(data) == 0 { |
||||
return common.Hash{} |
||||
} |
||||
return common.BytesToHash(data) |
||||
} |
||||
|
||||
// WriteHeadFastBlockHash stores the hash of the current fast-sync head block.
|
||||
func WriteHeadFastBlockHash(db DatabaseWriter, hash common.Hash) { |
||||
if err := db.Put(headFastBlockKey, hash.Bytes()); err != nil { |
||||
log.Crit("Failed to store last fast block's hash", "err", err) |
||||
} |
||||
} |
||||
|
||||
// ReadFastTrieProgress retrieves the number of tries nodes fast synced to allow
|
||||
// reporting correct numbers across restarts.
|
||||
func ReadFastTrieProgress(db DatabaseReader) uint64 { |
||||
data, _ := db.Get(fastTrieProgressKey) |
||||
if len(data) == 0 { |
||||
return 0 |
||||
} |
||||
return new(big.Int).SetBytes(data).Uint64() |
||||
} |
||||
|
||||
// WriteFastTrieProgress stores the fast sync trie process counter to support
|
||||
// retrieving it across restarts.
|
||||
func WriteFastTrieProgress(db DatabaseWriter, count uint64) { |
||||
if err := db.Put(fastTrieProgressKey, new(big.Int).SetUint64(count).Bytes()); err != nil { |
||||
log.Crit("Failed to store fast sync trie progress", "err", err) |
||||
} |
||||
} |
||||
|
||||
// ReadHeaderRLP retrieves a block header in its raw RLP database encoding.
|
||||
func ReadHeaderRLP(db DatabaseReader, hash common.Hash, number uint64) rlp.RawValue { |
||||
data, _ := db.Get(append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...)) |
||||
return data |
||||
} |
||||
|
||||
// HasHeader verifies the existence of a block header corresponding to the hash.
|
||||
func HasHeader(db DatabaseReader, hash common.Hash, number uint64) bool { |
||||
key := append(append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...)) |
||||
if has, err := db.Has(key); !has || err != nil { |
||||
return false |
||||
} |
||||
return true |
||||
} |
||||
|
||||
// ReadHeader retrieves the block header corresponding to the hash.
|
||||
func ReadHeader(db DatabaseReader, hash common.Hash, number uint64) *types.Header { |
||||
data := ReadHeaderRLP(db, hash, number) |
||||
if len(data) == 0 { |
||||
return nil |
||||
} |
||||
header := new(types.Header) |
||||
if err := rlp.Decode(bytes.NewReader(data), header); err != nil { |
||||
log.Error("Invalid block header RLP", "hash", hash, "err", err) |
||||
return nil |
||||
} |
||||
return header |
||||
} |
||||
|
||||
// WriteHeader stores a block header into the database and also stores the hash-
|
||||
// to-number mapping.
|
||||
func WriteHeader(db DatabaseWriter, header *types.Header) { |
||||
// Write the hash -> number mapping
|
||||
var ( |
||||
hash = header.Hash().Bytes() |
||||
number = header.Number.Uint64() |
||||
encoded = encodeBlockNumber(number) |
||||
) |
||||
key := append(headerNumberPrefix, hash...) |
||||
if err := db.Put(key, encoded); err != nil { |
||||
log.Crit("Failed to store hash to number mapping", "err", err) |
||||
} |
||||
// Write the encoded header
|
||||
data, err := rlp.EncodeToBytes(header) |
||||
if err != nil { |
||||
log.Crit("Failed to RLP encode header", "err", err) |
||||
} |
||||
key = append(append(headerPrefix, encoded...), hash...) |
||||
if err := db.Put(key, data); err != nil { |
||||
log.Crit("Failed to store header", "err", err) |
||||
} |
||||
} |
||||
|
||||
// DeleteHeader removes all block header data associated with a hash.
|
||||
func DeleteHeader(db DatabaseDeleter, hash common.Hash, number uint64) { |
||||
if err := db.Delete(append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...)); err != nil { |
||||
log.Crit("Failed to delete header", "err", err) |
||||
} |
||||
if err := db.Delete(append(headerNumberPrefix, hash.Bytes()...)); err != nil { |
||||
log.Crit("Failed to delete hash to number mapping", "err", err) |
||||
} |
||||
} |
||||
|
||||
// ReadBodyRLP retrieves the block body (transactions and uncles) in RLP encoding.
|
||||
func ReadBodyRLP(db DatabaseReader, hash common.Hash, number uint64) rlp.RawValue { |
||||
data, _ := db.Get(append(append(blockBodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...)) |
||||
return data |
||||
} |
||||
|
||||
// WriteBodyRLP stores an RLP encoded block body into the database.
|
||||
func WriteBodyRLP(db DatabaseWriter, hash common.Hash, number uint64, rlp rlp.RawValue) { |
||||
key := append(append(blockBodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...) |
||||
if err := db.Put(key, rlp); err != nil { |
||||
log.Crit("Failed to store block body", "err", err) |
||||
} |
||||
} |
||||
|
||||
// HasBody verifies the existence of a block body corresponding to the hash.
|
||||
func HasBody(db DatabaseReader, hash common.Hash, number uint64) bool { |
||||
key := append(append(blockBodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...) |
||||
if has, err := db.Has(key); !has || err != nil { |
||||
return false |
||||
} |
||||
return true |
||||
} |
||||
|
||||
// ReadBody retrieves the block body corresponding to the hash.
|
||||
func ReadBody(db DatabaseReader, hash common.Hash, number uint64) *types.Body { |
||||
data := ReadBodyRLP(db, hash, number) |
||||
if len(data) == 0 { |
||||
return nil |
||||
} |
||||
body := new(types.Body) |
||||
if err := rlp.Decode(bytes.NewReader(data), body); err != nil { |
||||
log.Error("Invalid block body RLP", "hash", hash, "err", err) |
||||
return nil |
||||
} |
||||
return body |
||||
} |
||||
|
||||
// WriteBody storea a block body into the database.
|
||||
func WriteBody(db DatabaseWriter, hash common.Hash, number uint64, body *types.Body) { |
||||
data, err := rlp.EncodeToBytes(body) |
||||
if err != nil { |
||||
log.Crit("Failed to RLP encode body", "err", err) |
||||
} |
||||
WriteBodyRLP(db, hash, number, data) |
||||
} |
||||
|
||||
// DeleteBody removes all block body data associated with a hash.
|
||||
func DeleteBody(db DatabaseDeleter, hash common.Hash, number uint64) { |
||||
if err := db.Delete(append(append(blockBodyPrefix, encodeBlockNumber(number)...), hash.Bytes()...)); err != nil { |
||||
log.Crit("Failed to delete block body", "err", err) |
||||
} |
||||
} |
||||
|
||||
// ReadTd retrieves a block's total difficulty corresponding to the hash.
|
||||
func ReadTd(db DatabaseReader, hash common.Hash, number uint64) *big.Int { |
||||
data, _ := db.Get(append(append(append(headerPrefix, encodeBlockNumber(number)...), hash[:]...), headerTDSuffix...)) |
||||
if len(data) == 0 { |
||||
return nil |
||||
} |
||||
td := new(big.Int) |
||||
if err := rlp.Decode(bytes.NewReader(data), td); err != nil { |
||||
log.Error("Invalid block total difficulty RLP", "hash", hash, "err", err) |
||||
return nil |
||||
} |
||||
return td |
||||
} |
||||
|
||||
// WriteTd stores the total difficulty of a block into the database.
|
||||
func WriteTd(db DatabaseWriter, hash common.Hash, number uint64, td *big.Int) { |
||||
data, err := rlp.EncodeToBytes(td) |
||||
if err != nil { |
||||
log.Crit("Failed to RLP encode block total difficulty", "err", err) |
||||
} |
||||
key := append(append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...), headerTDSuffix...) |
||||
if err := db.Put(key, data); err != nil { |
||||
log.Crit("Failed to store block total difficulty", "err", err) |
||||
} |
||||
} |
||||
|
||||
// DeleteTd removes all block total difficulty data associated with a hash.
|
||||
func DeleteTd(db DatabaseDeleter, hash common.Hash, number uint64) { |
||||
if err := db.Delete(append(append(append(headerPrefix, encodeBlockNumber(number)...), hash.Bytes()...), headerTDSuffix...)); err != nil { |
||||
log.Crit("Failed to delete block total difficulty", "err", err) |
||||
} |
||||
} |
||||
|
||||
// ReadReceipts retrieves all the transaction receipts belonging to a block.
|
||||
func ReadReceipts(db DatabaseReader, hash common.Hash, number uint64) types.Receipts { |
||||
// Retrieve the flattened receipt slice
|
||||
data, _ := db.Get(append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash[:]...)) |
||||
if len(data) == 0 { |
||||
return nil |
||||
} |
||||
// Convert the revceipts from their storage form to their internal representation
|
||||
storageReceipts := []*types.ReceiptForStorage{} |
||||
if err := rlp.DecodeBytes(data, &storageReceipts); err != nil { |
||||
log.Error("Invalid receipt array RLP", "hash", hash, "err", err) |
||||
return nil |
||||
} |
||||
receipts := make(types.Receipts, len(storageReceipts)) |
||||
for i, receipt := range storageReceipts { |
||||
receipts[i] = (*types.Receipt)(receipt) |
||||
} |
||||
return receipts |
||||
} |
||||
|
||||
// WriteReceipts stores all the transaction receipts belonging to a block.
|
||||
func WriteReceipts(db DatabaseWriter, hash common.Hash, number uint64, receipts types.Receipts) { |
||||
// Convert the receipts into their storage form and serialize them
|
||||
storageReceipts := make([]*types.ReceiptForStorage, len(receipts)) |
||||
for i, receipt := range receipts { |
||||
storageReceipts[i] = (*types.ReceiptForStorage)(receipt) |
||||
} |
||||
bytes, err := rlp.EncodeToBytes(storageReceipts) |
||||
if err != nil { |
||||
log.Crit("Failed to encode block receipts", "err", err) |
||||
} |
||||
// Store the flattened receipt slice
|
||||
key := append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...) |
||||
if err := db.Put(key, bytes); err != nil { |
||||
log.Crit("Failed to store block receipts", "err", err) |
||||
} |
||||
} |
||||
|
||||
// DeleteReceipts removes all receipt data associated with a block hash.
|
||||
func DeleteReceipts(db DatabaseDeleter, hash common.Hash, number uint64) { |
||||
if err := db.Delete(append(append(blockReceiptsPrefix, encodeBlockNumber(number)...), hash.Bytes()...)); err != nil { |
||||
log.Crit("Failed to delete block receipts", "err", err) |
||||
} |
||||
} |
||||
|
||||
// ReadBlock retrieves an entire block corresponding to the hash, assembling it
|
||||
// back from the stored header and body. If either the header or body could not
|
||||
// be retrieved nil is returned.
|
||||
//
|
||||
// Note, due to concurrent download of header and block body the header and thus
|
||||
// canonical hash can be stored in the database but the body data not (yet).
|
||||
func ReadBlock(db DatabaseReader, hash common.Hash, number uint64) *types.Block { |
||||
header := ReadHeader(db, hash, number) |
||||
if header == nil { |
||||
return nil |
||||
} |
||||
body := ReadBody(db, hash, number) |
||||
if body == nil { |
||||
return nil |
||||
} |
||||
return types.NewBlockWithHeader(header).WithBody(body.Transactions, body.Uncles) |
||||
} |
||||
|
||||
// WriteBlock serializes a block into the database, header and body separately.
|
||||
func WriteBlock(db DatabaseWriter, block *types.Block) { |
||||
WriteBody(db, block.Hash(), block.NumberU64(), block.Body()) |
||||
WriteHeader(db, block.Header()) |
||||
} |
||||
|
||||
// DeleteBlock removes all block data associated with a hash.
|
||||
func DeleteBlock(db DatabaseDeleter, hash common.Hash, number uint64) { |
||||
DeleteReceipts(db, hash, number) |
||||
DeleteHeader(db, hash, number) |
||||
DeleteBody(db, hash, number) |
||||
DeleteTd(db, hash, number) |
||||
} |
||||
|
||||
// FindCommonAncestor returns the last common ancestor of two block headers
|
||||
func FindCommonAncestor(db DatabaseReader, a, b *types.Header) *types.Header { |
||||
for bn := b.Number.Uint64(); a.Number.Uint64() > bn; { |
||||
a = ReadHeader(db, a.ParentHash, a.Number.Uint64()-1) |
||||
if a == nil { |
||||
return nil |
||||
} |
||||
} |
||||
for an := a.Number.Uint64(); an < b.Number.Uint64(); { |
||||
b = ReadHeader(db, b.ParentHash, b.Number.Uint64()-1) |
||||
if b == nil { |
||||
return nil |
||||
} |
||||
} |
||||
for a.Hash() != b.Hash() { |
||||
a = ReadHeader(db, a.ParentHash, a.Number.Uint64()-1) |
||||
if a == nil { |
||||
return nil |
||||
} |
||||
b = ReadHeader(db, b.ParentHash, b.Number.Uint64()-1) |
||||
if b == nil { |
||||
return nil |
||||
} |
||||
} |
||||
return a |
||||
} |
@ -0,0 +1,119 @@ |
||||
// 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 |
||||
|
||||
import ( |
||||
"encoding/binary" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/core/types" |
||||
"github.com/ethereum/go-ethereum/log" |
||||
"github.com/ethereum/go-ethereum/rlp" |
||||
) |
||||
|
||||
// ReadTxLookupEntry retrieves the positional metadata associated with a transaction
|
||||
// hash to allow retrieving the transaction or receipt by hash.
|
||||
func ReadTxLookupEntry(db DatabaseReader, hash common.Hash) (common.Hash, uint64, uint64) { |
||||
data, _ := db.Get(append(txLookupPrefix, hash.Bytes()...)) |
||||
if len(data) == 0 { |
||||
return common.Hash{}, 0, 0 |
||||
} |
||||
var entry TxLookupEntry |
||||
if err := rlp.DecodeBytes(data, &entry); err != nil { |
||||
log.Error("Invalid transaction lookup entry RLP", "hash", hash, "err", err) |
||||
return common.Hash{}, 0, 0 |
||||
} |
||||
return entry.BlockHash, entry.BlockIndex, entry.Index |
||||
} |
||||
|
||||
// WriteTxLookupEntries stores a positional metadata for every transaction from
|
||||
// a block, enabling hash based transaction and receipt lookups.
|
||||
func WriteTxLookupEntries(db DatabaseWriter, block *types.Block) { |
||||
for i, tx := range block.Transactions() { |
||||
entry := TxLookupEntry{ |
||||
BlockHash: block.Hash(), |
||||
BlockIndex: block.NumberU64(), |
||||
Index: uint64(i), |
||||
} |
||||
data, err := rlp.EncodeToBytes(entry) |
||||
if err != nil { |
||||
log.Crit("Failed to encode transaction lookup entry", "err", err) |
||||
} |
||||
if err := db.Put(append(txLookupPrefix, tx.Hash().Bytes()...), data); err != nil { |
||||
log.Crit("Failed to store transaction lookup entry", "err", err) |
||||
} |
||||
} |
||||
} |
||||
|
||||
// DeleteTxLookupEntry removes all transaction data associated with a hash.
|
||||
func DeleteTxLookupEntry(db DatabaseDeleter, hash common.Hash) { |
||||
db.Delete(append(txLookupPrefix, hash.Bytes()...)) |
||||
} |
||||
|
||||
// ReadTransaction retrieves a specific transaction from the database, along with
|
||||
// its added positional metadata.
|
||||
func ReadTransaction(db DatabaseReader, hash common.Hash) (*types.Transaction, common.Hash, uint64, uint64) { |
||||
blockHash, blockNumber, txIndex := ReadTxLookupEntry(db, hash) |
||||
if blockHash == (common.Hash{}) { |
||||
return nil, common.Hash{}, 0, 0 |
||||
} |
||||
body := ReadBody(db, blockHash, blockNumber) |
||||
if body == nil || len(body.Transactions) <= int(txIndex) { |
||||
log.Error("Transaction referenced missing", "number", blockNumber, "hash", blockHash, "index", txIndex) |
||||
return nil, common.Hash{}, 0, 0 |
||||
} |
||||
return body.Transactions[txIndex], blockHash, blockNumber, txIndex |
||||
} |
||||
|
||||
// ReadReceipt retrieves a specific transaction receipt from the database, along with
|
||||
// its added positional metadata.
|
||||
func ReadReceipt(db DatabaseReader, hash common.Hash) (*types.Receipt, common.Hash, uint64, uint64) { |
||||
blockHash, blockNumber, receiptIndex := ReadTxLookupEntry(db, hash) |
||||
if blockHash == (common.Hash{}) { |
||||
return nil, common.Hash{}, 0, 0 |
||||
} |
||||
receipts := ReadReceipts(db, blockHash, blockNumber) |
||||
if len(receipts) <= int(receiptIndex) { |
||||
log.Error("Receipt refereced missing", "number", blockNumber, "hash", blockHash, "index", receiptIndex) |
||||
return nil, common.Hash{}, 0, 0 |
||||
} |
||||
return receipts[receiptIndex], blockHash, blockNumber, receiptIndex |
||||
} |
||||
|
||||
// ReadBloomBits retrieves the compressed bloom bit vector belonging to the given
|
||||
// section and bit index from the.
|
||||
func ReadBloomBits(db DatabaseReader, bit uint, section uint64, head common.Hash) ([]byte, error) { |
||||
key := append(append(bloomBitsPrefix, make([]byte, 10)...), head.Bytes()...) |
||||
|
||||
binary.BigEndian.PutUint16(key[1:], uint16(bit)) |
||||
binary.BigEndian.PutUint64(key[3:], section) |
||||
|
||||
return db.Get(key) |
||||
} |
||||
|
||||
// WriteBloomBits stores the compressed bloom bits vector belonging to the given
|
||||
// section and bit index.
|
||||
func WriteBloomBits(db DatabaseWriter, bit uint, section uint64, head common.Hash, bits []byte) { |
||||
key := append(append(bloomBitsPrefix, make([]byte, 10)...), head.Bytes()...) |
||||
|
||||
binary.BigEndian.PutUint16(key[1:], uint16(bit)) |
||||
binary.BigEndian.PutUint64(key[3:], section) |
||||
|
||||
if err := db.Put(key, bits); err != nil { |
||||
log.Crit("Failed to store bloom bits", "err", err) |
||||
} |
||||
} |
@ -0,0 +1,68 @@ |
||||
// 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 |
||||
|
||||
import ( |
||||
"math/big" |
||||
"testing" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/core/types" |
||||
"github.com/ethereum/go-ethereum/ethdb" |
||||
) |
||||
|
||||
// Tests that positional lookup metadata can be stored and retrieved.
|
||||
func TestLookupStorage(t *testing.T) { |
||||
db, _ := ethdb.NewMemDatabase() |
||||
|
||||
tx1 := types.NewTransaction(1, common.BytesToAddress([]byte{0x11}), big.NewInt(111), 1111, big.NewInt(11111), []byte{0x11, 0x11, 0x11}) |
||||
tx2 := types.NewTransaction(2, common.BytesToAddress([]byte{0x22}), big.NewInt(222), 2222, big.NewInt(22222), []byte{0x22, 0x22, 0x22}) |
||||
tx3 := types.NewTransaction(3, common.BytesToAddress([]byte{0x33}), big.NewInt(333), 3333, big.NewInt(33333), []byte{0x33, 0x33, 0x33}) |
||||
txs := []*types.Transaction{tx1, tx2, tx3} |
||||
|
||||
block := types.NewBlock(&types.Header{Number: big.NewInt(314)}, txs, nil, nil) |
||||
|
||||
// Check that no transactions entries are in a pristine database
|
||||
for i, tx := range txs { |
||||
if txn, _, _, _ := ReadTransaction(db, tx.Hash()); txn != nil { |
||||
t.Fatalf("tx #%d [%x]: non existent transaction returned: %v", i, tx.Hash(), txn) |
||||
} |
||||
} |
||||
// Insert all the transactions into the database, and verify contents
|
||||
WriteBlock(db, block) |
||||
WriteTxLookupEntries(db, block) |
||||
|
||||
for i, tx := range txs { |
||||
if txn, hash, number, index := ReadTransaction(db, tx.Hash()); txn == nil { |
||||
t.Fatalf("tx #%d [%x]: transaction not found", i, tx.Hash()) |
||||
} else { |
||||
if hash != block.Hash() || number != block.NumberU64() || index != uint64(i) { |
||||
t.Fatalf("tx #%d [%x]: positional metadata mismatch: have %x/%d/%d, want %x/%v/%v", i, tx.Hash(), hash, number, index, block.Hash(), block.NumberU64(), i) |
||||
} |
||||
if tx.Hash() != txn.Hash() { |
||||
t.Fatalf("tx #%d [%x]: transaction mismatch: have %v, want %v", i, tx.Hash(), txn, tx) |
||||
} |
||||
} |
||||
} |
||||
// Delete the transactions and check purge
|
||||
for i, tx := range txs { |
||||
DeleteTxLookupEntry(db, tx.Hash()) |
||||
if txn, _, _, _ := ReadTransaction(db, tx.Hash()); txn != nil { |
||||
t.Fatalf("tx #%d [%x]: deleted transaction returned: %v", i, tx.Hash(), txn) |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,90 @@ |
||||
// 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 |
||||
|
||||
import ( |
||||
"encoding/json" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/log" |
||||
"github.com/ethereum/go-ethereum/params" |
||||
"github.com/ethereum/go-ethereum/rlp" |
||||
) |
||||
|
||||
// ReadDatabaseVersion retrieves the version number of the database.
|
||||
func ReadDatabaseVersion(db DatabaseReader) int { |
||||
var version int |
||||
|
||||
enc, _ := db.Get(databaseVerisionKey) |
||||
rlp.DecodeBytes(enc, &version) |
||||
|
||||
return version |
||||
} |
||||
|
||||
// WriteDatabaseVersion stores the version number of the database
|
||||
func WriteDatabaseVersion(db DatabaseWriter, version int) { |
||||
enc, _ := rlp.EncodeToBytes(version) |
||||
if err := db.Put(databaseVerisionKey, enc); err != nil { |
||||
log.Crit("Failed to store the database version", "err", err) |
||||
} |
||||
} |
||||
|
||||
// ReadChainConfig retrieves the consensus settings based on the given genesis hash.
|
||||
func ReadChainConfig(db DatabaseReader, hash common.Hash) *params.ChainConfig { |
||||
data, _ := db.Get(append(configPrefix, hash[:]...)) |
||||
if len(data) == 0 { |
||||
return nil |
||||
} |
||||
var config params.ChainConfig |
||||
if err := json.Unmarshal(data, &config); err != nil { |
||||
log.Error("Invalid chain config JSON", "hash", hash, "err", err) |
||||
return nil |
||||
} |
||||
return &config |
||||
} |
||||
|
||||
// WriteChainConfig writes the chain config settings to the database.
|
||||
func WriteChainConfig(db DatabaseWriter, hash common.Hash, cfg *params.ChainConfig) { |
||||
if cfg == nil { |
||||
return |
||||
} |
||||
data, err := json.Marshal(cfg) |
||||
if err != nil { |
||||
log.Crit("Failed to JSON encode chain config", "err", err) |
||||
} |
||||
if err := db.Put(append(configPrefix, hash[:]...), data); err != nil { |
||||
log.Crit("Failed to store chain config", "err", err) |
||||
} |
||||
} |
||||
|
||||
// ReadPreimage retrieves a single preimage of the provided hash.
|
||||
func ReadPreimage(db DatabaseReader, hash common.Hash) []byte { |
||||
data, _ := db.Get(append(preimagePrefix, hash.Bytes()...)) |
||||
return data |
||||
} |
||||
|
||||
// WritePreimages writes the provided set of preimages to the database. `number` is the
|
||||
// current block number, and is used for debug messages only.
|
||||
func WritePreimages(db DatabaseWriter, number uint64, preimages map[common.Hash][]byte) { |
||||
for hash, preimage := range preimages { |
||||
if err := db.Put(append(preimagePrefix, hash.Bytes()...), preimage); err != nil { |
||||
log.Crit("Failed to store trie preimage", "err", err) |
||||
} |
||||
} |
||||
preimageCounter.Inc(int64(len(preimages))) |
||||
preimageHitCounter.Inc(int64(len(preimages))) |
||||
} |
@ -0,0 +1,33 @@ |
||||
// 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 |
||||
|
||||
// DatabaseReader wraps the Has and Get method of a backing data store.
|
||||
type DatabaseReader interface { |
||||
Has(key []byte) (bool, error) |
||||
Get(key []byte) ([]byte, error) |
||||
} |
||||
|
||||
// DatabaseWriter wraps the Put method of a backing data store.
|
||||
type DatabaseWriter interface { |
||||
Put(key []byte, value []byte) error |
||||
} |
||||
|
||||
// DatabaseDeleter wraps the Delete method of a backing data store.
|
||||
type DatabaseDeleter interface { |
||||
Delete(key []byte) error |
||||
} |
@ -0,0 +1,79 @@ |
||||
// 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") |
||||
|
||||
// headHeaderKey tracks the latest know header's hash.
|
||||
headHeaderKey = []byte("LastHeader") |
||||
|
||||
// headBlockKey tracks the latest know full block's hash.
|
||||
headBlockKey = []byte("LastBlock") |
||||
|
||||
// headFastBlockKey tracks the latest known incomplete block's hash duirng fast sync.
|
||||
headFastBlockKey = []byte("LastFast") |
||||
|
||||
// fastTrieProgressKey tracks the number of trie entries imported during fast sync.
|
||||
fastTrieProgressKey = []byte("TrieSync") |
||||
|
||||
// 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
|
||||
|
||||
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) |
||||
) |
||||
|
||||
// TxLookupEntry is a positional metadata to help looking up the data content of
|
||||
// a transaction or receipt given only its hash.
|
||||
type TxLookupEntry 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 |
||||
} |
@ -1,135 +0,0 @@ |
||||
// Copyright 2016 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 eth implements the Ethereum protocol.
|
||||
package eth |
||||
|
||||
import ( |
||||
"bytes" |
||||
"time" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/core" |
||||
"github.com/ethereum/go-ethereum/ethdb" |
||||
"github.com/ethereum/go-ethereum/log" |
||||
"github.com/ethereum/go-ethereum/rlp" |
||||
) |
||||
|
||||
var deduplicateData = []byte("dbUpgrade_20170714deduplicateData") |
||||
|
||||
// upgradeDeduplicateData checks the chain database version and
|
||||
// starts a background process to make upgrades if necessary.
|
||||
// Returns a stop function that blocks until the process has
|
||||
// been safely stopped.
|
||||
func upgradeDeduplicateData(db ethdb.Database) func() error { |
||||
// If the database is already converted or empty, bail out
|
||||
data, _ := db.Get(deduplicateData) |
||||
if len(data) > 0 && data[0] == 42 { |
||||
return nil |
||||
} |
||||
if data, _ := db.Get([]byte("LastHeader")); len(data) == 0 { |
||||
db.Put(deduplicateData, []byte{42}) |
||||
return nil |
||||
} |
||||
// Start the deduplication upgrade on a new goroutine
|
||||
log.Warn("Upgrading database to use lookup entries") |
||||
stop := make(chan chan error) |
||||
|
||||
go func() { |
||||
// Create an iterator to read the entire database and covert old lookup entires
|
||||
it := db.(*ethdb.LDBDatabase).NewIterator() |
||||
defer func() { |
||||
if it != nil { |
||||
it.Release() |
||||
} |
||||
}() |
||||
|
||||
var ( |
||||
converted uint64 |
||||
failed error |
||||
) |
||||
for failed == nil && it.Next() { |
||||
// Skip any entries that don't look like old transaction meta entries (<hash>0x01)
|
||||
key := it.Key() |
||||
if len(key) != common.HashLength+1 || key[common.HashLength] != 0x01 { |
||||
continue |
||||
} |
||||
// Skip any entries that don't contain metadata (name clash between <hash>0x01 and <some-prefix><hash>)
|
||||
var meta struct { |
||||
BlockHash common.Hash |
||||
BlockIndex uint64 |
||||
Index uint64 |
||||
} |
||||
if err := rlp.DecodeBytes(it.Value(), &meta); err != nil { |
||||
continue |
||||
} |
||||
// Skip any already upgraded entries (clash due to <hash> ending with 0x01 (old suffix))
|
||||
hash := key[:common.HashLength] |
||||
|
||||
if hash[0] == byte('l') { |
||||
// Potential clash, the "old" `hash` must point to a live transaction.
|
||||
if tx, _, _, _ := core.GetTransaction(db, common.BytesToHash(hash)); tx == nil || !bytes.Equal(tx.Hash().Bytes(), hash) { |
||||
continue |
||||
} |
||||
} |
||||
// Convert the old metadata to a new lookup entry, delete duplicate data
|
||||
if failed = db.Put(append([]byte("l"), hash...), it.Value()); failed == nil { // Write the new lookup entry
|
||||
if failed = db.Delete(hash); failed == nil { // Delete the duplicate transaction data
|
||||
if failed = db.Delete(append([]byte("receipts-"), hash...)); failed == nil { // Delete the duplicate receipt data
|
||||
if failed = db.Delete(key); failed != nil { // Delete the old transaction metadata
|
||||
break |
||||
} |
||||
} |
||||
} |
||||
} |
||||
// Bump the conversion counter, and recreate the iterator occasionally to
|
||||
// avoid too high memory consumption.
|
||||
converted++ |
||||
if converted%100000 == 0 { |
||||
it.Release() |
||||
it = db.(*ethdb.LDBDatabase).NewIterator() |
||||
it.Seek(key) |
||||
|
||||
log.Info("Deduplicating database entries", "deduped", converted) |
||||
} |
||||
// Check for termination, or continue after a bit of a timeout
|
||||
select { |
||||
case errc := <-stop: |
||||
errc <- nil |
||||
return |
||||
case <-time.After(time.Microsecond * 100): |
||||
} |
||||
} |
||||
// Upgrade finished, mark a such and terminate
|
||||
if failed == nil { |
||||
log.Info("Database deduplication successful", "deduped", converted) |
||||
db.Put(deduplicateData, []byte{42}) |
||||
} else { |
||||
log.Error("Database deduplication failed", "deduped", converted, "err", failed) |
||||
} |
||||
it.Release() |
||||
it = nil |
||||
|
||||
errc := <-stop |
||||
errc <- failed |
||||
}() |
||||
// Assembly the cancellation callback
|
||||
return func() error { |
||||
errc := make(chan error) |
||||
stop <- errc |
||||
return <-errc |
||||
} |
||||
} |
Loading…
Reference in new issue