2019-08-06 10:40:28 +00:00
|
|
|
// 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 (
|
2019-10-04 13:24:01 +00:00
|
|
|
"sync"
|
|
|
|
|
2019-08-06 10:40:28 +00:00
|
|
|
"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
|
|
|
|
|
2019-11-22 11:23:49 +00:00
|
|
|
root common.Hash // Root hash of the base snapshot
|
|
|
|
stale bool // Signals that the layer became stale (state progressed)
|
2019-10-04 13:24:01 +00:00
|
|
|
|
|
|
|
lock sync.RWMutex
|
2019-08-06 10:40:28 +00:00
|
|
|
}
|
|
|
|
|
2019-11-22 11:23:49 +00:00
|
|
|
// Root returns root hash for which this snapshot was made.
|
|
|
|
func (dl *diskLayer) Root() common.Hash {
|
|
|
|
return dl.root
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stale return whether this layer has become stale (was flattened across) or if
|
|
|
|
// it's still live.
|
|
|
|
func (dl *diskLayer) Stale() bool {
|
|
|
|
dl.lock.RLock()
|
|
|
|
defer dl.lock.RUnlock()
|
|
|
|
|
|
|
|
return dl.stale
|
2019-08-06 10:40:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Account directly retrieves the account associated with a particular hash in
|
|
|
|
// the snapshot slim data format.
|
2019-10-04 13:24:01 +00:00
|
|
|
func (dl *diskLayer) Account(hash common.Hash) (*Account, error) {
|
|
|
|
data, err := dl.AccountRLP(hash)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2019-08-06 10:40:28 +00:00
|
|
|
if len(data) == 0 { // can be both nil and []byte{}
|
2019-10-04 13:24:01 +00:00
|
|
|
return nil, nil
|
2019-08-06 10:40:28 +00:00
|
|
|
}
|
|
|
|
account := new(Account)
|
|
|
|
if err := rlp.DecodeBytes(data, account); err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
2019-10-04 13:24:01 +00:00
|
|
|
return account, nil
|
2019-08-06 10:40:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// AccountRLP directly retrieves the account RLP associated with a particular
|
|
|
|
// hash in the snapshot slim data format.
|
2019-10-04 13:24:01 +00:00
|
|
|
func (dl *diskLayer) AccountRLP(hash common.Hash) ([]byte, error) {
|
|
|
|
dl.lock.RLock()
|
|
|
|
defer dl.lock.RUnlock()
|
|
|
|
|
|
|
|
// If the layer was flattened into, consider it invalid (any live reference to
|
|
|
|
// the original should be marked as unusable).
|
|
|
|
if dl.stale {
|
|
|
|
return nil, ErrSnapshotStale
|
|
|
|
}
|
2019-08-06 10:40:28 +00:00
|
|
|
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)))
|
2019-10-04 13:24:01 +00:00
|
|
|
return blob, nil
|
2019-08-06 10:40:28 +00:00
|
|
|
}
|
|
|
|
// 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)))
|
|
|
|
|
2019-10-04 13:24:01 +00:00
|
|
|
return blob, nil
|
2019-08-06 10:40:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Storage directly retrieves the storage data associated with a particular hash,
|
|
|
|
// within a particular account.
|
2019-10-04 13:24:01 +00:00
|
|
|
func (dl *diskLayer) Storage(accountHash, storageHash common.Hash) ([]byte, error) {
|
|
|
|
dl.lock.RLock()
|
|
|
|
defer dl.lock.RUnlock()
|
|
|
|
|
|
|
|
// If the layer was flattened into, consider it invalid (any live reference to
|
|
|
|
// the original should be marked as unusable).
|
|
|
|
if dl.stale {
|
|
|
|
return nil, ErrSnapshotStale
|
|
|
|
}
|
2019-08-06 10:40:28 +00:00
|
|
|
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)))
|
2019-10-04 13:24:01 +00:00
|
|
|
return blob, nil
|
2019-08-06 10:40:28 +00:00
|
|
|
}
|
|
|
|
// 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)))
|
|
|
|
|
2019-10-04 13:24:01 +00:00
|
|
|
return blob, nil
|
2019-08-06 10:40:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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 {
|
2019-11-22 11:23:49 +00:00
|
|
|
return newDiffLayer(dl, blockHash, accounts, storage)
|
2019-08-06 10:40:28 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
}
|