mirror of https://github.com/ethereum/go-ethereum
parent
2a5ed1a1d3
commit
542df8898e
@ -0,0 +1,99 @@ |
||||
// Copyright 2019 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/ethdb" |
||||
"github.com/ethereum/go-ethereum/log" |
||||
) |
||||
|
||||
// ReadSnapshotBlock retrieves the number and root of the block whose state is
|
||||
// contained in the persisted snapshot.
|
||||
func ReadSnapshotBlock(db ethdb.KeyValueReader) (uint64, common.Hash) { |
||||
data, _ := db.Get(snapshotBlockKey) |
||||
if len(data) != 8+common.HashLength { |
||||
return 0, common.Hash{} |
||||
} |
||||
return binary.BigEndian.Uint64(data[:8]), common.BytesToHash(data[8:]) |
||||
} |
||||
|
||||
// WriteSnapshotBlock stores the number and root of the block whose state is
|
||||
// contained in the persisted snapshot.
|
||||
func WriteSnapshotBlock(db ethdb.KeyValueWriter, number uint64, root common.Hash) { |
||||
if err := db.Put(snapshotBlockKey, append(encodeBlockNumber(number), root.Bytes()...)); err != nil { |
||||
log.Crit("Failed to store snapsnot block's number and root", "err", err) |
||||
} |
||||
} |
||||
|
||||
// DeleteSnapshotBlock deletes the number and hash of the block whose state is
|
||||
// contained in the persisted snapshot. Since snapshots are not immutable, this
|
||||
// method can be used during updates, so a crash or failure will mark the entire
|
||||
// snapshot invalid.
|
||||
func DeleteSnapshotBlock(db ethdb.KeyValueWriter) { |
||||
if err := db.Delete(snapshotBlockKey); err != nil { |
||||
log.Crit("Failed to remove snapsnot block's number and hash", "err", err) |
||||
} |
||||
} |
||||
|
||||
// ReadAccountSnapshot retrieves the snapshot entry of an account trie leaf.
|
||||
func ReadAccountSnapshot(db ethdb.KeyValueReader, hash common.Hash) []byte { |
||||
data, _ := db.Get(accountSnapshotKey(hash)) |
||||
return data |
||||
} |
||||
|
||||
// WriteAccountSnapshot stores the snapshot entry of an account trie leaf.
|
||||
func WriteAccountSnapshot(db ethdb.KeyValueWriter, hash common.Hash, entry []byte) { |
||||
if err := db.Put(accountSnapshotKey(hash), entry); err != nil { |
||||
log.Crit("Failed to store account snapshot", "err", err) |
||||
} |
||||
} |
||||
|
||||
// DeleteAccountSnapshot removes the snapshot entry of an account trie leaf.
|
||||
func DeleteAccountSnapshot(db ethdb.KeyValueWriter, hash common.Hash) { |
||||
if err := db.Delete(accountSnapshotKey(hash)); err != nil { |
||||
log.Crit("Failed to delete account snapshot", "err", err) |
||||
} |
||||
} |
||||
|
||||
// ReadStorageSnapshot retrieves the snapshot entry of an storage trie leaf.
|
||||
func ReadStorageSnapshot(db ethdb.KeyValueReader, accountHash, storageHash common.Hash) []byte { |
||||
data, _ := db.Get(storageSnapshotKey(accountHash, storageHash)) |
||||
return data |
||||
} |
||||
|
||||
// WriteStorageSnapshot stores the snapshot entry of an storage trie leaf.
|
||||
func WriteStorageSnapshot(db ethdb.KeyValueWriter, accountHash, storageHash common.Hash, entry []byte) { |
||||
if err := db.Put(storageSnapshotKey(accountHash, storageHash), entry); err != nil { |
||||
log.Crit("Failed to store storage snapshot", "err", err) |
||||
} |
||||
} |
||||
|
||||
// DeleteStorageSnapshot removes the snapshot entry of an storage trie leaf.
|
||||
func DeleteStorageSnapshot(db ethdb.KeyValueWriter, accountHash, storageHash common.Hash) { |
||||
if err := db.Delete(storageSnapshotKey(accountHash, storageHash)); err != nil { |
||||
log.Crit("Failed to delete storage snapshot", "err", err) |
||||
} |
||||
} |
||||
|
||||
// IterateStorageSnapshots returns an iterator for walking the entire storage
|
||||
// space of a specific account.
|
||||
func IterateStorageSnapshots(db ethdb.Iteratee, accountHash common.Hash) ethdb.Iterator { |
||||
return db.NewIteratorWithPrefix(storageSnapshotsKey(accountHash)) |
||||
} |
@ -0,0 +1,54 @@ |
||||
// Copyright 2019 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 snapshot |
||||
|
||||
import ( |
||||
"bytes" |
||||
"math/big" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/rlp" |
||||
) |
||||
|
||||
// Account is a slim version of a state.Account, where the root and code hash
|
||||
// are replaced with a nil byte slice for empty accounts.
|
||||
type Account struct { |
||||
Nonce uint64 |
||||
Balance *big.Int |
||||
Root []byte |
||||
CodeHash []byte |
||||
} |
||||
|
||||
// AccountRLP converts a state.Account content into a slim snapshot version RLP
|
||||
// encoded.
|
||||
func AccountRLP(nonce uint64, balance *big.Int, root common.Hash, codehash []byte) []byte { |
||||
slim := Account{ |
||||
Nonce: nonce, |
||||
Balance: balance, |
||||
} |
||||
if root != emptyRoot { |
||||
slim.Root = root[:] |
||||
} |
||||
if !bytes.Equal(codehash, emptyCode[:]) { |
||||
slim.CodeHash = codehash |
||||
} |
||||
data, err := rlp.EncodeToBytes(slim) |
||||
if err != nil { |
||||
panic(err) |
||||
} |
||||
return data |
||||
} |
@ -0,0 +1,337 @@ |
||||
// Copyright 2019 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 snapshot |
||||
|
||||
import ( |
||||
"fmt" |
||||
"sort" |
||||
"sync" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/core/rawdb" |
||||
"github.com/ethereum/go-ethereum/ethdb" |
||||
"github.com/ethereum/go-ethereum/log" |
||||
"github.com/ethereum/go-ethereum/rlp" |
||||
) |
||||
|
||||
// diffLayer represents a collection of modifications made to a state snapshot
|
||||
// after running a block on top. It contains one sorted list for the account trie
|
||||
// and one-one list for each storage tries.
|
||||
//
|
||||
// The goal of a diff layer is to act as a journal, tracking recent modifications
|
||||
// made to the state, that have not yet graduated into a semi-immutable state.
|
||||
type diffLayer struct { |
||||
parent snapshot // Parent snapshot modified by this one, never nil
|
||||
memory uint64 // Approximate guess as to how much memory we use
|
||||
|
||||
number uint64 // Block number to which this snapshot diff belongs to
|
||||
root common.Hash // Root hash to which this snapshot diff belongs to
|
||||
|
||||
accountList []common.Hash // List of account for iteration, might not be sorted yet (lazy)
|
||||
accountSorted bool // Flag whether the account list has alreayd been sorted or not
|
||||
accountData map[common.Hash][]byte // Keyed accounts for direct retrival (nil means deleted)
|
||||
storageList map[common.Hash][]common.Hash // List of storage slots for iterated retrievals, one per account
|
||||
storageSorted map[common.Hash]bool // Flag whether the storage slot list has alreayd been sorted or not
|
||||
storageData map[common.Hash]map[common.Hash][]byte // Keyed storage slots for direct retrival. one per account (nil means deleted)
|
||||
|
||||
lock sync.RWMutex |
||||
} |
||||
|
||||
// newDiffLayer creates a new diff on top of an existing snapshot, whether that's a low
|
||||
// level persistent database or a hierarchical diff already.
|
||||
func newDiffLayer(parent snapshot, number uint64, root common.Hash, accounts map[common.Hash][]byte, storage map[common.Hash]map[common.Hash][]byte) *diffLayer { |
||||
// Create the new layer with some pre-allocated data segments
|
||||
dl := &diffLayer{ |
||||
parent: parent, |
||||
number: number, |
||||
root: root, |
||||
accountData: accounts, |
||||
storageData: storage, |
||||
} |
||||
// Fill the account hashes and sort them for the iterator
|
||||
accountList := make([]common.Hash, 0, len(accounts)) |
||||
for hash, data := range accounts { |
||||
accountList = append(accountList, hash) |
||||
dl.memory += uint64(len(data)) |
||||
} |
||||
sort.Sort(hashes(accountList)) |
||||
dl.accountList = accountList |
||||
dl.accountSorted = true |
||||
|
||||
dl.memory += uint64(len(dl.accountList) * common.HashLength) |
||||
|
||||
// Fill the storage hashes and sort them for the iterator
|
||||
dl.storageList = make(map[common.Hash][]common.Hash, len(storage)) |
||||
dl.storageSorted = make(map[common.Hash]bool, len(storage)) |
||||
|
||||
for accountHash, slots := range storage { |
||||
// If the slots are nil, sanity check that it's a deleted account
|
||||
if slots == nil { |
||||
// Ensure that the account was just marked as deleted
|
||||
if account, ok := accounts[accountHash]; account != nil || !ok { |
||||
panic(fmt.Sprintf("storage in %#x nil, but account conflicts (%#x, exists: %v)", accountHash, account, ok)) |
||||
} |
||||
// Everything ok, store the deletion mark and continue
|
||||
dl.storageList[accountHash] = nil |
||||
continue |
||||
} |
||||
// Storage slots are not nil so entire contract was not deleted, ensure the
|
||||
// account was just updated.
|
||||
if account, ok := accounts[accountHash]; account == nil || !ok { |
||||
log.Error(fmt.Sprintf("storage in %#x exists, but account nil (exists: %v)", accountHash, ok)) |
||||
//panic(fmt.Sprintf("storage in %#x exists, but account nil (exists: %v)", accountHash, ok))
|
||||
} |
||||
// Fill the storage hashes for this account and sort them for the iterator
|
||||
storageList := make([]common.Hash, 0, len(slots)) |
||||
for storageHash, data := range slots { |
||||
storageList = append(storageList, storageHash) |
||||
dl.memory += uint64(len(data)) |
||||
} |
||||
sort.Sort(hashes(storageList)) |
||||
dl.storageList[accountHash] = storageList |
||||
dl.storageSorted[accountHash] = true |
||||
|
||||
dl.memory += uint64(len(storageList) * common.HashLength) |
||||
} |
||||
dl.memory += uint64(len(dl.storageList) * common.HashLength) |
||||
|
||||
return dl |
||||
} |
||||
|
||||
// Info returns the block number and root hash for which this snapshot was made.
|
||||
func (dl *diffLayer) Info() (uint64, common.Hash) { |
||||
return dl.number, dl.root |
||||
} |
||||
|
||||
// Account directly retrieves the account associated with a particular hash in
|
||||
// the snapshot slim data format.
|
||||
func (dl *diffLayer) Account(hash common.Hash) *Account { |
||||
data := dl.AccountRLP(hash) |
||||
if len(data) == 0 { // can be both nil and []byte{}
|
||||
return nil |
||||
} |
||||
account := new(Account) |
||||
if err := rlp.DecodeBytes(data, account); err != nil { |
||||
panic(err) |
||||
} |
||||
return account |
||||
} |
||||
|
||||
// AccountRLP directly retrieves the account RLP associated with a particular
|
||||
// hash in the snapshot slim data format.
|
||||
func (dl *diffLayer) AccountRLP(hash common.Hash) []byte { |
||||
dl.lock.RLock() |
||||
defer dl.lock.RUnlock() |
||||
|
||||
// If the account is known locally, return it. Note, a nil account means it was
|
||||
// deleted, and is a different notion than an unknown account!
|
||||
if data, ok := dl.accountData[hash]; ok { |
||||
return data |
||||
} |
||||
// Account unknown to this diff, resolve from parent
|
||||
return dl.parent.AccountRLP(hash) |
||||
} |
||||
|
||||
// Storage directly retrieves the storage data associated with a particular hash,
|
||||
// within a particular account. If the slot is unknown to this diff, it's parent
|
||||
// is consulted.
|
||||
func (dl *diffLayer) Storage(accountHash, storageHash common.Hash) []byte { |
||||
dl.lock.RLock() |
||||
defer dl.lock.RUnlock() |
||||
|
||||
// If the account is known locally, try to resolve the slot locally. Note, a nil
|
||||
// account means it was deleted, and is a different notion than an unknown account!
|
||||
if storage, ok := dl.storageData[accountHash]; ok { |
||||
if storage == nil { |
||||
return nil |
||||
} |
||||
if data, ok := storage[storageHash]; ok { |
||||
return data |
||||
} |
||||
} |
||||
// Account - or slot within - unknown to this diff, resolve from parent
|
||||
return dl.parent.Storage(accountHash, storageHash) |
||||
} |
||||
|
||||
// Update creates a new layer on top of the existing snapshot diff tree with
|
||||
// the specified data items.
|
||||
func (dl *diffLayer) Update(blockRoot common.Hash, accounts map[common.Hash][]byte, storage map[common.Hash]map[common.Hash][]byte) *diffLayer { |
||||
return newDiffLayer(dl, dl.number+1, blockRoot, accounts, storage) |
||||
} |
||||
|
||||
// Cap traverses downwards the diff tree until the number of allowed layers are
|
||||
// crossed. All diffs beyond the permitted number are flattened downwards. If
|
||||
// the layer limit is reached, memory cap is also enforced (but not before). The
|
||||
// block numbers for the disk layer and first diff layer are returned for GC.
|
||||
func (dl *diffLayer) Cap(layers int, memory uint64) (uint64, uint64) { |
||||
// Dive until we run out of layers or reach the persistent database
|
||||
if layers > 2 { |
||||
// If we still have diff layers below, recurse
|
||||
if parent, ok := dl.parent.(*diffLayer); ok { |
||||
return parent.Cap(layers-1, memory) |
||||
} |
||||
// Diff stack too shallow, return block numbers without modifications
|
||||
return dl.parent.(*diskLayer).number, dl.number |
||||
} |
||||
// We're out of layers, flatten anything below, stopping if it's the disk or if
|
||||
// the memory limit is not yet exceeded.
|
||||
switch parent := dl.parent.(type) { |
||||
case *diskLayer: |
||||
return parent.number, dl.number |
||||
case *diffLayer: |
||||
dl.lock.Lock() |
||||
defer dl.lock.Unlock() |
||||
|
||||
dl.parent = parent.flatten() |
||||
if dl.parent.(*diffLayer).memory < memory { |
||||
diskNumber, _ := parent.parent.Info() |
||||
return diskNumber, parent.number |
||||
} |
||||
default: |
||||
panic(fmt.Sprintf("unknown data layer: %T", parent)) |
||||
} |
||||
// If the bottommost layer is larger than our memory cap, persist to disk
|
||||
var ( |
||||
parent = dl.parent.(*diffLayer) |
||||
base = parent.parent.(*diskLayer) |
||||
batch = base.db.NewBatch() |
||||
) |
||||
parent.lock.RLock() |
||||
defer parent.lock.RUnlock() |
||||
|
||||
// Start by temporarilly deleting the current snapshot block marker. This
|
||||
// ensures that in the case of a crash, the entire snapshot is invalidated.
|
||||
rawdb.DeleteSnapshotBlock(batch) |
||||
|
||||
// Push all the accounts into the database
|
||||
for hash, data := range parent.accountData { |
||||
if len(data) > 0 { |
||||
// Account was updated, push to disk
|
||||
rawdb.WriteAccountSnapshot(batch, hash, data) |
||||
base.cache.Set(string(hash[:]), data) |
||||
|
||||
if batch.ValueSize() > ethdb.IdealBatchSize { |
||||
if err := batch.Write(); err != nil { |
||||
log.Crit("Failed to write account snapshot", "err", err) |
||||
} |
||||
batch.Reset() |
||||
} |
||||
} else { |
||||
// Account was deleted, remove all storage slots too
|
||||
rawdb.DeleteAccountSnapshot(batch, hash) |
||||
base.cache.Set(string(hash[:]), nil) |
||||
|
||||
it := rawdb.IterateStorageSnapshots(base.db, hash) |
||||
for it.Next() { |
||||
if key := it.Key(); len(key) == 65 { // TODO(karalabe): Yuck, we should move this into the iterator
|
||||
batch.Delete(key) |
||||
base.cache.Delete(string(key[1:])) |
||||
} |
||||
} |
||||
it.Release() |
||||
} |
||||
} |
||||
// Push all the storage slots into the database
|
||||
for accountHash, storage := range parent.storageData { |
||||
for storageHash, data := range storage { |
||||
if len(data) > 0 { |
||||
rawdb.WriteStorageSnapshot(batch, accountHash, storageHash, data) |
||||
base.cache.Set(string(append(accountHash[:], storageHash[:]...)), data) |
||||
} else { |
||||
rawdb.DeleteStorageSnapshot(batch, accountHash, storageHash) |
||||
base.cache.Set(string(append(accountHash[:], storageHash[:]...)), nil) |
||||
} |
||||
} |
||||
if batch.ValueSize() > ethdb.IdealBatchSize { |
||||
if err := batch.Write(); err != nil { |
||||
log.Crit("Failed to write storage snapshot", "err", err) |
||||
} |
||||
batch.Reset() |
||||
} |
||||
} |
||||
// Update the snapshot block marker and write any remainder data
|
||||
base.number, base.root = parent.number, parent.root |
||||
|
||||
rawdb.WriteSnapshotBlock(batch, base.number, base.root) |
||||
if err := batch.Write(); err != nil { |
||||
log.Crit("Failed to write leftover snapshot", "err", err) |
||||
} |
||||
dl.parent = base |
||||
|
||||
return base.number, dl.number |
||||
} |
||||
|
||||
// flatten pushes all data from this point downwards, flattening everything into
|
||||
// a single diff at the bottom. Since usually the lowermost diff is the largest,
|
||||
// the flattening bulds up from there in reverse.
|
||||
func (dl *diffLayer) flatten() snapshot { |
||||
// If the parent is not diff, we're the first in line, return unmodified
|
||||
parent, ok := dl.parent.(*diffLayer) |
||||
if !ok { |
||||
return dl |
||||
} |
||||
// Parent is a diff, flatten it first (note, apart from weird corned cases,
|
||||
// flatten will realistically only ever merge 1 layer, so there's no need to
|
||||
// be smarter about grouping flattens together).
|
||||
parent = parent.flatten().(*diffLayer) |
||||
|
||||
// Overwrite all the updated accounts blindly, merge the sorted list
|
||||
for hash, data := range dl.accountData { |
||||
parent.accountData[hash] = data |
||||
} |
||||
parent.accountList = append(parent.accountList, dl.accountList...) // TODO(karalabe): dedup!!
|
||||
parent.accountSorted = false |
||||
|
||||
// Overwrite all the updates storage slots (individually)
|
||||
for accountHash, storage := range dl.storageData { |
||||
// If storage didn't exist (or was deleted) in the parent; or if the storage
|
||||
// was freshly deleted in the child, overwrite blindly
|
||||
if parent.storageData[accountHash] == nil || storage == nil { |
||||
parent.storageList[accountHash] = dl.storageList[accountHash] |
||||
parent.storageData[accountHash] = storage |
||||
continue |
||||
} |
||||
// Storage exists in both parent and child, merge the slots
|
||||
comboData := parent.storageData[accountHash] |
||||
for storageHash, data := range storage { |
||||
comboData[storageHash] = data |
||||
} |
||||
parent.storageData[accountHash] = comboData |
||||
parent.storageList[accountHash] = append(parent.storageList[accountHash], dl.storageList[accountHash]...) // TODO(karalabe): dedup!!
|
||||
parent.storageSorted[accountHash] = false |
||||
} |
||||
// Return the combo parent
|
||||
parent.number = dl.number |
||||
parent.root = dl.root |
||||
parent.memory += dl.memory |
||||
return parent |
||||
} |
||||
|
||||
// Journal commits an entire diff hierarchy to disk into a single journal file.
|
||||
// This is meant to be used during shutdown to persist the snapshot without
|
||||
// flattening everything down (bad for reorgs).
|
||||
func (dl *diffLayer) Journal() error { |
||||
dl.lock.RLock() |
||||
defer dl.lock.RUnlock() |
||||
|
||||
writer, err := dl.journal() |
||||
if err != nil { |
||||
return err |
||||
} |
||||
writer.Close() |
||||
return nil |
||||
} |
@ -0,0 +1,140 @@ |
||||
// Copyright 2019 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 snapshot |
||||
|
||||
import ( |
||||
"fmt" |
||||
"io" |
||||
"os" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/rlp" |
||||
) |
||||
|
||||
// journalAccount is an account entry in a diffLayer's disk journal.
|
||||
type journalAccount struct { |
||||
Hash common.Hash |
||||
Blob []byte |
||||
} |
||||
|
||||
// journalStorage is an account's storage map in a diffLayer's disk journal.
|
||||
type journalStorage struct { |
||||
Hash common.Hash |
||||
Keys []common.Hash |
||||
Vals [][]byte |
||||
} |
||||
|
||||
// loadDiffLayer reads the next sections of a snapshot journal, reconstructing a new
|
||||
// diff and verifying that it can be linked to the requested parent.
|
||||
func loadDiffLayer(parent snapshot, r *rlp.Stream) (snapshot, error) { |
||||
// Read the next diff journal entry
|
||||
var ( |
||||
number uint64 |
||||
root common.Hash |
||||
) |
||||
if err := r.Decode(&number); err != nil { |
||||
// The first read may fail with EOF, marking the end of the journal
|
||||
if err == io.EOF { |
||||
return parent, nil |
||||
} |
||||
return nil, fmt.Errorf("load diff number: %v", err) |
||||
} |
||||
if err := r.Decode(&root); err != nil { |
||||
return nil, fmt.Errorf("load diff root: %v", err) |
||||
} |
||||
var accounts []journalAccount |
||||
if err := r.Decode(&accounts); err != nil { |
||||
return nil, fmt.Errorf("load diff accounts: %v", err) |
||||
} |
||||
accountData := make(map[common.Hash][]byte) |
||||
for _, entry := range accounts { |
||||
accountData[entry.Hash] = entry.Blob |
||||
} |
||||
var storage []journalStorage |
||||
if err := r.Decode(&storage); err != nil { |
||||
return nil, fmt.Errorf("load diff storage: %v", err) |
||||
} |
||||
storageData := make(map[common.Hash]map[common.Hash][]byte) |
||||
for _, entry := range storage { |
||||
slots := make(map[common.Hash][]byte) |
||||
for i, key := range entry.Keys { |
||||
slots[key] = entry.Vals[i] |
||||
} |
||||
storageData[entry.Hash] = slots |
||||
} |
||||
// Validate the block number to avoid state corruption
|
||||
if parent, ok := parent.(*diffLayer); ok { |
||||
if number != parent.number+1 { |
||||
return nil, fmt.Errorf("snapshot chain broken: block #%d after #%d", number, parent.number) |
||||
} |
||||
} |
||||
return loadDiffLayer(newDiffLayer(parent, number, root, accountData, storageData), r) |
||||
} |
||||
|
||||
// journal is the internal version of Journal that also returns the journal file
|
||||
// so subsequent layers know where to write to.
|
||||
func (dl *diffLayer) journal() (io.WriteCloser, error) { |
||||
// If we've reached the bottom, open the journal
|
||||
var writer io.WriteCloser |
||||
if parent, ok := dl.parent.(*diskLayer); ok { |
||||
file, err := os.Create(parent.journal) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
writer = file |
||||
} |
||||
// If we haven't reached the bottom yet, journal the parent first
|
||||
if writer == nil { |
||||
file, err := dl.parent.(*diffLayer).journal() |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
writer = file |
||||
} |
||||
// Everything below was journalled, persist this layer too
|
||||
if err := rlp.Encode(writer, dl.number); err != nil { |
||||
writer.Close() |
||||
return nil, err |
||||
} |
||||
if err := rlp.Encode(writer, dl.root); err != nil { |
||||
writer.Close() |
||||
return nil, err |
||||
} |
||||
accounts := make([]journalAccount, 0, len(dl.accountData)) |
||||
for hash, blob := range dl.accountData { |
||||
accounts = append(accounts, journalAccount{Hash: hash, Blob: blob}) |
||||
} |
||||
if err := rlp.Encode(writer, accounts); err != nil { |
||||
writer.Close() |
||||
return nil, err |
||||
} |
||||
storage := make([]journalStorage, 0, len(dl.storageData)) |
||||
for hash, slots := range dl.storageData { |
||||
keys := make([]common.Hash, 0, len(slots)) |
||||
vals := make([][]byte, 0, len(slots)) |
||||
for key, val := range slots { |
||||
keys = append(keys, key) |
||||
vals = append(vals, val) |
||||
} |
||||
storage = append(storage, journalStorage{Hash: hash, Keys: keys, Vals: vals}) |
||||
} |
||||
if err := rlp.Encode(writer, storage); err != nil { |
||||
writer.Close() |
||||
return nil, err |
||||
} |
||||
return writer, nil |
||||
} |
@ -0,0 +1,115 @@ |
||||
// Copyright 2019 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 snapshot |
||||
|
||||
import ( |
||||
"github.com/allegro/bigcache" |
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/core/rawdb" |
||||
"github.com/ethereum/go-ethereum/ethdb" |
||||
"github.com/ethereum/go-ethereum/rlp" |
||||
) |
||||
|
||||
// diskLayer is a low level persistent snapshot built on top of a key-value store.
|
||||
type diskLayer struct { |
||||
journal string // Path of the snapshot journal to use on shutdown
|
||||
db ethdb.KeyValueStore // Key-value store containing the base snapshot
|
||||
cache *bigcache.BigCache // Cache to avoid hitting the disk for direct access
|
||||
|
||||
number uint64 // Block number of the base snapshot
|
||||
root common.Hash // Root hash of the base snapshot
|
||||
} |
||||
|
||||
// Info returns the block number and root hash for which this snapshot was made.
|
||||
func (dl *diskLayer) Info() (uint64, common.Hash) { |
||||
return dl.number, dl.root |
||||
} |
||||
|
||||
// Account directly retrieves the account associated with a particular hash in
|
||||
// the snapshot slim data format.
|
||||
func (dl *diskLayer) Account(hash common.Hash) *Account { |
||||
data := dl.AccountRLP(hash) |
||||
if len(data) == 0 { // can be both nil and []byte{}
|
||||
return nil |
||||
} |
||||
account := new(Account) |
||||
if err := rlp.DecodeBytes(data, account); err != nil { |
||||
panic(err) |
||||
} |
||||
return account |
||||
} |
||||
|
||||
// AccountRLP directly retrieves the account RLP associated with a particular
|
||||
// hash in the snapshot slim data format.
|
||||
func (dl *diskLayer) AccountRLP(hash common.Hash) []byte { |
||||
key := string(hash[:]) |
||||
|
||||
// Try to retrieve the account from the memory cache
|
||||
if blob, err := dl.cache.Get(key); err == nil { |
||||
snapshotCleanHitMeter.Mark(1) |
||||
snapshotCleanReadMeter.Mark(int64(len(blob))) |
||||
return blob |
||||
} |
||||
// Cache doesn't contain account, pull from disk and cache for later
|
||||
blob := rawdb.ReadAccountSnapshot(dl.db, hash) |
||||
dl.cache.Set(key, blob) |
||||
|
||||
snapshotCleanMissMeter.Mark(1) |
||||
snapshotCleanWriteMeter.Mark(int64(len(blob))) |
||||
|
||||
return blob |
||||
} |
||||
|
||||
// Storage directly retrieves the storage data associated with a particular hash,
|
||||
// within a particular account.
|
||||
func (dl *diskLayer) Storage(accountHash, storageHash common.Hash) []byte { |
||||
key := string(append(accountHash[:], storageHash[:]...)) |
||||
|
||||
// Try to retrieve the storage slot from the memory cache
|
||||
if blob, err := dl.cache.Get(key); err == nil { |
||||
snapshotCleanHitMeter.Mark(1) |
||||
snapshotCleanReadMeter.Mark(int64(len(blob))) |
||||
return blob |
||||
} |
||||
// Cache doesn't contain storage slot, pull from disk and cache for later
|
||||
blob := rawdb.ReadStorageSnapshot(dl.db, accountHash, storageHash) |
||||
dl.cache.Set(key, blob) |
||||
|
||||
snapshotCleanMissMeter.Mark(1) |
||||
snapshotCleanWriteMeter.Mark(int64(len(blob))) |
||||
|
||||
return blob |
||||
} |
||||
|
||||
// Update creates a new layer on top of the existing snapshot diff tree with
|
||||
// the specified data items. Note, the maps are retained by the method to avoid
|
||||
// copying everything.
|
||||
func (dl *diskLayer) Update(blockHash common.Hash, accounts map[common.Hash][]byte, storage map[common.Hash]map[common.Hash][]byte) *diffLayer { |
||||
return newDiffLayer(dl, dl.number+1, blockHash, accounts, storage) |
||||
} |
||||
|
||||
// Cap traverses downwards the diff tree until the number of allowed layers are
|
||||
// crossed. All diffs beyond the permitted number are flattened downwards.
|
||||
func (dl *diskLayer) Cap(layers int, memory uint64) (uint64, uint64) { |
||||
return dl.number, dl.number |
||||
} |
||||
|
||||
// Journal commits an entire diff hierarchy to disk into a single journal file.
|
||||
func (dl *diskLayer) Journal() error { |
||||
// There's no journalling a disk layer
|
||||
return nil |
||||
} |
@ -0,0 +1,212 @@ |
||||
// Copyright 2019 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 snapshot |
||||
|
||||
import ( |
||||
"bytes" |
||||
"fmt" |
||||
"math/big" |
||||
"time" |
||||
|
||||
"github.com/allegro/bigcache" |
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/core/rawdb" |
||||
"github.com/ethereum/go-ethereum/crypto" |
||||
"github.com/ethereum/go-ethereum/ethdb" |
||||
"github.com/ethereum/go-ethereum/log" |
||||
"github.com/ethereum/go-ethereum/rlp" |
||||
"github.com/ethereum/go-ethereum/trie" |
||||
) |
||||
|
||||
var ( |
||||
// emptyRoot is the known root hash of an empty trie.
|
||||
emptyRoot = common.HexToHash("56e81f171bcc55a6ff8345e692c0f86e5b48e01b996cadc001622fb5e363b421") |
||||
|
||||
// emptyCode is the known hash of the empty EVM bytecode.
|
||||
emptyCode = crypto.Keccak256Hash(nil) |
||||
) |
||||
|
||||
// wipeSnapshot iterates over the entire key-value database and deletes all the
|
||||
// data associated with the snapshot (accounts, storage, metadata). After all is
|
||||
// done, the snapshot range of the database is compacted to free up unused data
|
||||
// blocks.
|
||||
func wipeSnapshot(db ethdb.KeyValueStore) error { |
||||
// Batch deletions together to avoid holding an iterator for too long
|
||||
var ( |
||||
batch = db.NewBatch() |
||||
items int |
||||
) |
||||
// Iterate over the snapshot key-range and delete all of them
|
||||
log.Info("Deleting previous snapshot leftovers") |
||||
start, logged := time.Now(), time.Now() |
||||
|
||||
it := db.NewIteratorWithStart(rawdb.StateSnapshotPrefix) |
||||
for it.Next() { |
||||
// Skip any keys with the correct prefix but wrong lenth (trie nodes)
|
||||
key := it.Key() |
||||
if !bytes.HasPrefix(key, rawdb.StateSnapshotPrefix) { |
||||
break |
||||
} |
||||
if len(key) != len(rawdb.StateSnapshotPrefix)+common.HashLength && len(key) != len(rawdb.StateSnapshotPrefix)+2*common.HashLength { |
||||
continue |
||||
} |
||||
// Delete the key and periodically recreate the batch and iterator
|
||||
batch.Delete(key) |
||||
items++ |
||||
|
||||
if items%10000 == 0 { |
||||
// Batch too large (or iterator too long lived, flush and recreate)
|
||||
it.Release() |
||||
if err := batch.Write(); err != nil { |
||||
return err |
||||
} |
||||
batch.Reset() |
||||
it = db.NewIteratorWithStart(key) |
||||
|
||||
if time.Since(logged) > 8*time.Second { |
||||
log.Info("Deleting previous snapshot leftovers", "wiped", items, "elapsed", time.Since(start)) |
||||
logged = time.Now() |
||||
} |
||||
} |
||||
} |
||||
it.Release() |
||||
|
||||
rawdb.DeleteSnapshotBlock(batch) |
||||
if err := batch.Write(); err != nil { |
||||
return err |
||||
} |
||||
log.Info("Deleted previous snapshot leftovers", "wiped", items, "elapsed", time.Since(start)) |
||||
|
||||
// Compact the snapshot section of the database to get rid of unused space
|
||||
log.Info("Compacting snapshot area in database") |
||||
start = time.Now() |
||||
|
||||
end := common.CopyBytes(rawdb.StateSnapshotPrefix) |
||||
end[len(end)-1]++ |
||||
|
||||
if err := db.Compact(rawdb.StateSnapshotPrefix, end); err != nil { |
||||
return err |
||||
} |
||||
log.Info("Compacted snapshot area in database", "elapsed", time.Since(start)) |
||||
|
||||
return nil |
||||
} |
||||
|
||||
// generateSnapshot regenerates a brand new snapshot based on an existing state database and head block.
|
||||
func generateSnapshot(db ethdb.KeyValueStore, journal string, headNumber uint64, headRoot common.Hash) (snapshot, error) { |
||||
// Wipe any previously existing snapshot from the database
|
||||
if err := wipeSnapshot(db); err != nil { |
||||
return nil, err |
||||
} |
||||
// Iterate the entire storage trie and re-generate the state snapshot
|
||||
var ( |
||||
accountCount int |
||||
storageCount int |
||||
storageNodes int |
||||
accountSize common.StorageSize |
||||
storageSize common.StorageSize |
||||
logged time.Time |
||||
) |
||||
batch := db.NewBatch() |
||||
triedb := trie.NewDatabase(db) |
||||
|
||||
accTrie, err := trie.NewSecure(headRoot, triedb) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
accIt := trie.NewIterator(accTrie.NodeIterator(nil)) |
||||
for accIt.Next() { |
||||
var ( |
||||
curStorageCount int |
||||
curStorageNodes int |
||||
curAccountSize common.StorageSize |
||||
curStorageSize common.StorageSize |
||||
) |
||||
var acc struct { |
||||
Nonce uint64 |
||||
Balance *big.Int |
||||
Root common.Hash |
||||
CodeHash []byte |
||||
} |
||||
if err := rlp.DecodeBytes(accIt.Value, &acc); err != nil { |
||||
return nil, err |
||||
} |
||||
data := AccountRLP(acc.Nonce, acc.Balance, acc.Root, acc.CodeHash) |
||||
curAccountSize += common.StorageSize(1 + common.HashLength + len(data)) |
||||
|
||||
rawdb.WriteAccountSnapshot(batch, common.BytesToHash(accIt.Key), data) |
||||
if batch.ValueSize() > ethdb.IdealBatchSize { |
||||
batch.Write() |
||||
batch.Reset() |
||||
} |
||||
if acc.Root != emptyRoot { |
||||
storeTrie, err := trie.NewSecure(acc.Root, triedb) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
storeIt := trie.NewIterator(storeTrie.NodeIterator(nil)) |
||||
for storeIt.Next() { |
||||
curStorageSize += common.StorageSize(1 + 2*common.HashLength + len(storeIt.Value)) |
||||
curStorageCount++ |
||||
|
||||
rawdb.WriteStorageSnapshot(batch, common.BytesToHash(accIt.Key), common.BytesToHash(storeIt.Key), storeIt.Value) |
||||
if batch.ValueSize() > ethdb.IdealBatchSize { |
||||
batch.Write() |
||||
batch.Reset() |
||||
} |
||||
} |
||||
curStorageNodes = storeIt.Nodes |
||||
} |
||||
accountCount++ |
||||
storageCount += curStorageCount |
||||
accountSize += curAccountSize |
||||
storageSize += curStorageSize |
||||
storageNodes += curStorageNodes |
||||
|
||||
if time.Since(logged) > 8*time.Second { |
||||
fmt.Printf("%#x: %9s + %9s (%6d slots, %6d nodes), total %9s (%d accs, %d nodes) + %9s (%d slots, %d nodes)\n", accIt.Key, curAccountSize.TerminalString(), curStorageSize.TerminalString(), curStorageCount, curStorageNodes, accountSize.TerminalString(), accountCount, accIt.Nodes, storageSize.TerminalString(), storageCount, storageNodes) |
||||
logged = time.Now() |
||||
} |
||||
} |
||||
fmt.Printf("Totals: %9s (%d accs, %d nodes) + %9s (%d slots, %d nodes)\n", accountSize.TerminalString(), accountCount, accIt.Nodes, storageSize.TerminalString(), storageCount, storageNodes) |
||||
|
||||
// Update the snapshot block marker and write any remainder data
|
||||
rawdb.WriteSnapshotBlock(batch, headNumber, headRoot) |
||||
batch.Write() |
||||
batch.Reset() |
||||
|
||||
// Compact the snapshot section of the database to get rid of unused space
|
||||
log.Info("Compacting snapshot in chain database") |
||||
if err := db.Compact([]byte{'s'}, []byte{'s' + 1}); err != nil { |
||||
return nil, err |
||||
} |
||||
// New snapshot generated, construct a brand new base layer
|
||||
cache, _ := bigcache.NewBigCache(bigcache.Config{ // TODO(karalabe): dedup
|
||||
Shards: 1024, |
||||
LifeWindow: time.Hour, |
||||
MaxEntriesInWindow: 512 * 1024, |
||||
MaxEntrySize: 512, |
||||
HardMaxCacheSize: 512, |
||||
}) |
||||
return &diskLayer{ |
||||
journal: journal, |
||||
db: db, |
||||
cache: cache, |
||||
number: headNumber, |
||||
root: headRoot, |
||||
}, nil |
||||
} |
@ -0,0 +1,111 @@ |
||||
// Copyright 2019 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 snapshot |
||||
|
||||
import ( |
||||
"math/rand" |
||||
"testing" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/core/rawdb" |
||||
"github.com/ethereum/go-ethereum/ethdb/memorydb" |
||||
) |
||||
|
||||
// randomHash generates a random blob of data and returns it as a hash.
|
||||
func randomHash() common.Hash { |
||||
var hash common.Hash |
||||
if n, err := rand.Read(hash[:]); n != common.HashLength || err != nil { |
||||
panic(err) |
||||
} |
||||
return hash |
||||
} |
||||
|
||||
// Tests that given a database with random data content, all parts of a snapshot
|
||||
// can be crrectly wiped without touching anything else.
|
||||
func TestWipe(t *testing.T) { |
||||
// Create a database with some random snapshot data
|
||||
db := memorydb.New() |
||||
|
||||
for i := 0; i < 128; i++ { |
||||
account := randomHash() |
||||
rawdb.WriteAccountSnapshot(db, account, randomHash().Bytes()) |
||||
for j := 0; j < 1024; j++ { |
||||
rawdb.WriteStorageSnapshot(db, account, randomHash(), randomHash().Bytes()) |
||||
} |
||||
} |
||||
rawdb.WriteSnapshotBlock(db, 123, randomHash()) |
||||
|
||||
// Add some random non-snapshot data too to make wiping harder
|
||||
for i := 0; i < 65536; i++ { |
||||
// Generate a key that's the wrong length for a state snapshot item
|
||||
var keysize int |
||||
for keysize == 0 || keysize == 32 || keysize == 64 { |
||||
keysize = 8 + rand.Intn(64) // +8 to ensure we will "never" randomize duplicates
|
||||
} |
||||
// Randomize the suffix, dedup and inject it under the snapshot namespace
|
||||
keysuffix := make([]byte, keysize) |
||||
rand.Read(keysuffix) |
||||
db.Put(append(rawdb.StateSnapshotPrefix, keysuffix...), randomHash().Bytes()) |
||||
} |
||||
// Sanity check that all the keys are present
|
||||
var items int |
||||
|
||||
it := db.NewIteratorWithPrefix(rawdb.StateSnapshotPrefix) |
||||
defer it.Release() |
||||
|
||||
for it.Next() { |
||||
key := it.Key() |
||||
if len(key) == len(rawdb.StateSnapshotPrefix)+32 || len(key) == len(rawdb.StateSnapshotPrefix)+64 { |
||||
items++ |
||||
} |
||||
} |
||||
if items != 128+128*1024 { |
||||
t.Fatalf("snapshot size mismatch: have %d, want %d", items, 128+128*1024) |
||||
} |
||||
if number, hash := rawdb.ReadSnapshotBlock(db); number != 123 || hash == (common.Hash{}) { |
||||
t.Errorf("snapshot block marker mismatch: have #%d [%#x], want #%d [<not-nil>]", number, hash, 123) |
||||
} |
||||
// Wipe all snapshot entries from the database
|
||||
if err := wipeSnapshot(db); err != nil { |
||||
t.Fatalf("failed to wipe snapshot: %v", err) |
||||
} |
||||
// Iterate over the database end ensure no snapshot information remains
|
||||
it = db.NewIteratorWithPrefix(rawdb.StateSnapshotPrefix) |
||||
defer it.Release() |
||||
|
||||
for it.Next() { |
||||
key := it.Key() |
||||
if len(key) == len(rawdb.StateSnapshotPrefix)+32 || len(key) == len(rawdb.StateSnapshotPrefix)+64 { |
||||
t.Errorf("snapshot entry remained after wipe: %x", key) |
||||
} |
||||
} |
||||
if number, hash := rawdb.ReadSnapshotBlock(db); number != 0 || hash != (common.Hash{}) { |
||||
t.Errorf("snapshot block marker remained after wipe: #%d [%#x]", number, hash) |
||||
} |
||||
// Iterate over the database and ensure miscellaneous items are present
|
||||
items = 0 |
||||
|
||||
it = db.NewIterator() |
||||
defer it.Release() |
||||
|
||||
for it.Next() { |
||||
items++ |
||||
} |
||||
if items != 65536 { |
||||
t.Fatalf("misc item count mismatch: have %d, want %d", items, 65536) |
||||
} |
||||
} |
@ -0,0 +1,244 @@ |
||||
// Copyright 2019 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 snapshot implements a journalled, dynamic state dump.
|
||||
package snapshot |
||||
|
||||
import ( |
||||
"errors" |
||||
"fmt" |
||||
"os" |
||||
"sync" |
||||
"time" |
||||
|
||||
"github.com/allegro/bigcache" |
||||
"github.com/ethereum/go-ethereum/common" |
||||
"github.com/ethereum/go-ethereum/core/rawdb" |
||||
"github.com/ethereum/go-ethereum/ethdb" |
||||
"github.com/ethereum/go-ethereum/log" |
||||
"github.com/ethereum/go-ethereum/metrics" |
||||
"github.com/ethereum/go-ethereum/rlp" |
||||
) |
||||
|
||||
var ( |
||||
snapshotCleanHitMeter = metrics.NewRegisteredMeter("state/snapshot/clean/hit", nil) |
||||
snapshotCleanMissMeter = metrics.NewRegisteredMeter("state/snapshot/clean/miss", nil) |
||||
snapshotCleanReadMeter = metrics.NewRegisteredMeter("state/snapshot/clean/read", nil) |
||||
snapshotCleanWriteMeter = metrics.NewRegisteredMeter("state/snapshot/clean/write", nil) |
||||
) |
||||
|
||||
// Snapshot represents the functionality supported by a snapshot storage layer.
|
||||
type Snapshot interface { |
||||
// Info returns the block number and root hash for which this snapshot was made.
|
||||
Info() (uint64, common.Hash) |
||||
|
||||
// Account directly retrieves the account associated with a particular hash in
|
||||
// the snapshot slim data format.
|
||||
Account(hash common.Hash) *Account |
||||
|
||||
// AccountRLP directly retrieves the account RLP associated with a particular
|
||||
// hash in the snapshot slim data format.
|
||||
AccountRLP(hash common.Hash) []byte |
||||
|
||||
// Storage directly retrieves the storage data associated with a particular hash,
|
||||
// within a particular account.
|
||||
Storage(accountHash, storageHash common.Hash) []byte |
||||
} |
||||
|
||||
// snapshot is the internal version of the snapshot data layer that supports some
|
||||
// additional methods compared to the public API.
|
||||
type snapshot interface { |
||||
Snapshot |
||||
|
||||
// Update creates a new layer on top of the existing snapshot diff tree with
|
||||
// the specified data items. Note, the maps are retained by the method to avoid
|
||||
// copying everything.
|
||||
Update(blockRoot common.Hash, accounts map[common.Hash][]byte, storage map[common.Hash]map[common.Hash][]byte) *diffLayer |
||||
|
||||
// Cap traverses downwards the diff tree until the number of allowed layers are
|
||||
// crossed. All diffs beyond the permitted number are flattened downwards. The
|
||||
// block numbers for the disk layer and first diff layer are returned for GC.
|
||||
Cap(layers int, memory uint64) (uint64, uint64) |
||||
|
||||
// Journal commits an entire diff hierarchy to disk into a single journal file.
|
||||
// This is meant to be used during shutdown to persist the snapshot without
|
||||
// flattening everything down (bad for reorgs).
|
||||
Journal() error |
||||
} |
||||
|
||||
// SnapshotTree is an Ethereum state snapshot tree. It consists of one persistent
|
||||
// base layer backed by a key-value store, on top of which arbitrarilly many in-
|
||||
// memory diff layers are topped. The memory diffs can form a tree with branching,
|
||||
// but the disk layer is singleton and common to all. If a reorg goes deeper than
|
||||
// the disk layer, everything needs to be deleted.
|
||||
//
|
||||
// The goal of a state snapshot is twofold: to allow direct access to account and
|
||||
// storage data to avoid expensive multi-level trie lookups; and to allow sorted,
|
||||
// cheap iteration of the account/storage tries for sync aid.
|
||||
type SnapshotTree struct { |
||||
layers map[common.Hash]snapshot // Collection of all known layers // TODO(karalabe): split Clique overlaps
|
||||
lock sync.RWMutex |
||||
} |
||||
|
||||
// New attempts to load an already existing snapshot from a persistent key-value
|
||||
// store (with a number of memory layers from a journal), ensuring that the head
|
||||
// of the snapshot matches the expected one.
|
||||
//
|
||||
// If the snapshot is missing or inconsistent, the entirety is deleted and will
|
||||
// be reconstructed from scratch based on the tries in the key-value store.
|
||||
func New(db ethdb.KeyValueStore, journal string, headNumber uint64, headRoot common.Hash) (*SnapshotTree, error) { |
||||
// Attempt to load a previously persisted snapshot
|
||||
head, err := loadSnapshot(db, journal, headNumber, headRoot) |
||||
if err != nil { |
||||
log.Warn("Failed to load snapshot, regenerating", "err", err) |
||||
if head, err = generateSnapshot(db, journal, headNumber, headRoot); err != nil { |
||||
return nil, err |
||||
} |
||||
} |
||||
// Existing snapshot loaded or one regenerated, seed all the layers
|
||||
snap := &SnapshotTree{ |
||||
layers: make(map[common.Hash]snapshot), |
||||
} |
||||
for head != nil { |
||||
_, root := head.Info() |
||||
snap.layers[root] = head |
||||
|
||||
switch self := head.(type) { |
||||
case *diffLayer: |
||||
head = self.parent |
||||
case *diskLayer: |
||||
head = nil |
||||
default: |
||||
panic(fmt.Sprintf("unknown data layer: %T", self)) |
||||
} |
||||
} |
||||
return snap, nil |
||||
} |
||||
|
||||
// Snapshot retrieves a snapshot belonging to the given block root, or nil if no
|
||||
// snapshot is maintained for that block.
|
||||
func (st *SnapshotTree) Snapshot(blockRoot common.Hash) Snapshot { |
||||
st.lock.RLock() |
||||
defer st.lock.RUnlock() |
||||
|
||||
return st.layers[blockRoot] |
||||
} |
||||
|
||||
// Update adds a new snapshot into the tree, if that can be linked to an existing
|
||||
// old parent. It is disallowed to insert a disk layer (the origin of all).
|
||||
func (st *SnapshotTree) Update(blockRoot common.Hash, parentRoot common.Hash, accounts map[common.Hash][]byte, storage map[common.Hash]map[common.Hash][]byte) error { |
||||
// Generate a new snapshot on top of the parent
|
||||
parent := st.Snapshot(parentRoot).(snapshot) |
||||
if parent == nil { |
||||
return fmt.Errorf("parent [%#x] snapshot missing", parentRoot) |
||||
} |
||||
snap := parent.Update(blockRoot, accounts, storage) |
||||
|
||||
// Save the new snapshot for later
|
||||
st.lock.Lock() |
||||
defer st.lock.Unlock() |
||||
|
||||
st.layers[snap.root] = snap |
||||
return nil |
||||
} |
||||
|
||||
// Cap traverses downwards the snapshot tree from a head block hash until the
|
||||
// number of allowed layers are crossed. All layers beyond the permitted number
|
||||
// are flattened downwards.
|
||||
func (st *SnapshotTree) Cap(blockRoot common.Hash, layers int, memory uint64) error { |
||||
// Retrieve the head snapshot to cap from
|
||||
snap := st.Snapshot(blockRoot).(snapshot) |
||||
if snap == nil { |
||||
return fmt.Errorf("snapshot [%#x] missing", blockRoot) |
||||
} |
||||
// Run the internal capping and discard all stale layers
|
||||
st.lock.Lock() |
||||
defer st.lock.Unlock() |
||||
|
||||
diskNumber, diffNumber := snap.Cap(layers, memory) |
||||
for root, snap := range st.layers { |
||||
if number, _ := snap.Info(); number != diskNumber && number < diffNumber { |
||||
delete(st.layers, root) |
||||
} |
||||
} |
||||
return nil |
||||
} |
||||
|
||||
// Journal commits an entire diff hierarchy to disk into a single journal file.
|
||||
// This is meant to be used during shutdown to persist the snapshot without
|
||||
// flattening everything down (bad for reorgs).
|
||||
func (st *SnapshotTree) Journal(blockRoot common.Hash) error { |
||||
// Retrieve the head snapshot to journal from
|
||||
snap := st.Snapshot(blockRoot).(snapshot) |
||||
if snap == nil { |
||||
return fmt.Errorf("snapshot [%#x] missing", blockRoot) |
||||
} |
||||
// Run the journaling
|
||||
st.lock.Lock() |
||||
defer st.lock.Unlock() |
||||
|
||||
return snap.Journal() |
||||
} |
||||
|
||||
// loadSnapshot loads a pre-existing state snapshot backed by a key-value store.
|
||||
func loadSnapshot(db ethdb.KeyValueStore, journal string, headNumber uint64, headRoot common.Hash) (snapshot, error) { |
||||
// Retrieve the block number and hash of the snapshot, failing if no snapshot
|
||||
// is present in the database (or crashed mid-update).
|
||||
number, root := rawdb.ReadSnapshotBlock(db) |
||||
if root == (common.Hash{}) { |
||||
return nil, errors.New("missing or corrupted snapshot") |
||||
} |
||||
cache, _ := bigcache.NewBigCache(bigcache.Config{ // TODO(karalabe): dedup
|
||||
Shards: 1024, |
||||
LifeWindow: time.Hour, |
||||
MaxEntriesInWindow: 512 * 1024, |
||||
MaxEntrySize: 512, |
||||
HardMaxCacheSize: 512, |
||||
}) |
||||
base := &diskLayer{ |
||||
journal: journal, |
||||
db: db, |
||||
cache: cache, |
||||
number: number, |
||||
root: root, |
||||
} |
||||
// Load all the snapshot diffs from the journal, failing if their chain is broken
|
||||
// or does not lead from the disk snapshot to the specified head.
|
||||
if _, err := os.Stat(journal); os.IsNotExist(err) { |
||||
// Journal doesn't exist, don't worry if it's not supposed to
|
||||
if number != headNumber || root != headRoot { |
||||
return nil, fmt.Errorf("snapshot journal missing, head does't match snapshot: #%d [%#x] vs. #%d [%#x]", |
||||
headNumber, headRoot, number, root) |
||||
} |
||||
return base, nil |
||||
} |
||||
file, err := os.Open(journal) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
snapshot, err := loadDiffLayer(base, rlp.NewStream(file, 0)) |
||||
if err != nil { |
||||
return nil, err |
||||
} |
||||
// Entire snapshot journal loaded, sanity check the head and return
|
||||
// Journal doesn't exist, don't worry if it's not supposed to
|
||||
number, root = snapshot.Info() |
||||
if number != headNumber || root != headRoot { |
||||
return nil, fmt.Errorf("head does't match snapshot: #%d [%#x] vs. #%d [%#x]", |
||||
headNumber, headRoot, number, root) |
||||
} |
||||
return snapshot, nil |
||||
} |
@ -0,0 +1,17 @@ |
||||
// Copyright 2019 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 snapshot |
@ -0,0 +1,62 @@ |
||||
// Copyright 2019 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 snapshot |
||||
|
||||
import ( |
||||
"bytes" |
||||
|
||||
"github.com/ethereum/go-ethereum/common" |
||||
) |
||||
|
||||
// hashes is a helper to implement sort.Interface.
|
||||
type hashes []common.Hash |
||||
|
||||
// Len is the number of elements in the collection.
|
||||
func (hs hashes) Len() int { return len(hs) } |
||||
|
||||
// Less reports whether the element with index i should sort before the element
|
||||
// with index j.
|
||||
func (hs hashes) Less(i, j int) bool { return bytes.Compare(hs[i][:], hs[j][:]) < 0 } |
||||
|
||||
// Swap swaps the elements with indexes i and j.
|
||||
func (hs hashes) Swap(i, j int) { hs[i], hs[j] = hs[j], hs[i] } |
||||
|
||||
// merge combines two sorted lists of hashes into a combo sorted one.
|
||||
func merge(a, b []common.Hash) []common.Hash { |
||||
result := make([]common.Hash, len(a)+len(b)) |
||||
|
||||
i := 0 |
||||
for len(a) > 0 && len(b) > 0 { |
||||
if bytes.Compare(a[0][:], b[0][:]) < 0 { |
||||
result[i] = a[0] |
||||
a = a[1:] |
||||
} else { |
||||
result[i] = b[0] |
||||
b = b[1:] |
||||
} |
||||
i++ |
||||
} |
||||
for j := 0; j < len(a); j++ { |
||||
result[i] = a[j] |
||||
i++ |
||||
} |
||||
for j := 0; j < len(b); j++ { |
||||
result[i] = b[j] |
||||
i++ |
||||
} |
||||
return result |
||||
} |
Loading…
Reference in new issue