snapshot

package
v1.10.17 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Jan 19, 2021 License: GPL-3.0, GPL-3.0 Imports: 24 Imported by: 0

Documentation

Overview

Package snapshot implements a journaled, dynamic state dump.

Index

Constants

This section is empty.

Variables

View Source
var (

	// ErrSnapshotStale is returned from data accessors if the underlying snapshot
	// layer had been invalidated due to the chain progressing forward far enough
	// to not maintain the layer's original state.
	ErrSnapshotStale = errors.New("snapshot stale")

	// ErrNotCoveredYet is returned from data accessors if the underlying snapshot
	// is being generated currently and the requested data item is not yet in the
	// range of accounts covered.
	ErrNotCoveredYet = errors.New("not covered yet")

	// ErrNotConstructed is returned if the callers want to iterate the snapshot
	// while the generation is not finished yet.
	ErrNotConstructed = errors.New("snapshot is not constructed")
)

Functions

func FullAccountRLP added in v1.9.55

func FullAccountRLP(data []byte) ([]byte, error)

FullAccountRLP converts data on the 'slim RLP' format into the full RLP-format.

func GenerateAccountTrieRoot added in v1.9.55

func GenerateAccountTrieRoot(it AccountIterator) (common.Hash, error)

GenerateAccountTrieRoot takes an account iterator and reproduces the root hash.

func GenerateStorageTrieRoot added in v1.9.55

func GenerateStorageTrieRoot(account common.Hash, it StorageIterator) (common.Hash, error)

GenerateStorageTrieRoot takes a storage iterator and reproduces the root hash.

func SlimAccountRLP added in v1.9.55

func SlimAccountRLP(nonce uint64, balance *big.Int, root common.Hash, codehash []byte, upload *big.Int, num *big.Int) []byte

SlimAccountRLP converts a state.Account content into a slim snapshot version RLP encoded.

func VerifyState added in v1.9.55

func VerifyState(snaptree *Tree, root common.Hash) error

VerifyState takes the whole snapshot tree as the input, traverses all the accounts as well as the corresponding storages and compares the re-computed hash with the original one(state root and the storage root).

Types

type Account

type Account struct {
	Nonce    uint64
	Balance  *big.Int
	Root     []byte
	CodeHash []byte
	Upload   *big.Int //bytes
	Num      *big.Int
}

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.

func FullAccount added in v1.9.55

func FullAccount(data []byte) (Account, error)

FullAccount decodes the data on the 'slim RLP' format and return the consensus format account.

func SlimAccount added in v1.9.55

func SlimAccount(nonce uint64, balance *big.Int, root common.Hash, codehash []byte, upload *big.Int, num *big.Int) Account

AccountRLP converts a state.Account content into a slim snapshot version RLP encoded.

type AccountIterator

type AccountIterator interface {
	Iterator

	// Account returns the RLP encoded slim account the iterator is currently at.
	// An error will be returned if the iterator becomes invalid
	Account() []byte
}

AccountIterator is a iterator to step over all the accounts in a snapshot, which may or may not be composed of multiple layers.

type Iterator added in v1.9.55

type Iterator interface {
	// Next steps the iterator forward one element, returning false if exhausted,
	// or an error if iteration failed for some reason (e.g. root being iterated
	// becomes stale and garbage collected).
	Next() bool

	// Error returns any failure that occurred during iteration, which might have
	// caused a premature iteration exit (e.g. snapshot stack becoming stale).
	Error() error

	// Hash returns the hash of the account or storage slot the iterator is
	// currently at.
	Hash() common.Hash

	// Release releases associated resources. Release should always succeed and
	// can be called multiple times without causing error.
	Release()
}

Iterator is a iterator to step over all the accounts or the specific storage in a snapshot which may or may not be composed of multiple layers.

type Snapshot

type Snapshot interface {
	// Root returns the root hash for which this snapshot was made.
	Root() common.Hash

	// Account directly retrieves the account associated with a particular hash in
	// the snapshot slim data format.
	Account(hash common.Hash) (*Account, error)

	// AccountRLP directly retrieves the account RLP associated with a particular
	// hash in the snapshot slim data format.
	AccountRLP(hash common.Hash) ([]byte, error)

	// Storage directly retrieves the storage data associated with a particular hash,
	// within a particular account.
	Storage(accountHash, storageHash common.Hash) ([]byte, error)
}

Snapshot represents the functionality supported by a snapshot storage layer.

type StorageIterator added in v1.9.55

type StorageIterator interface {
	Iterator

	// Slot returns the storage slot the iterator is currently at. An error will
	// be returned if the iterator becomes invalid
	Slot() []byte
}

StorageIterator is a iterator to step over the specific storage in a snapshot, which may or may not be composed of multiple layers.

type Tree

type Tree struct {
	// contains filtered or unexported fields
}

SnapshotTree is an Cortex state snapshot tree. It consists of one persistent base layer backed by a key-value store, on top of which arbitrarily 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.

func New

func New(diskdb ctxcdb.KeyValueStore, triedb *trie.Database, cache int, root common.Hash, async bool, recovery bool) *Tree

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 the disk layer is broken, the entire is deleted and will be reconstructed from scratch based on the tries in the key-value store, on a background thread. If the memory layers from the journal is not continuous with disk layer or the journal is missing, all diffs will be discarded iff it's in "recovery" mode, otherwise rebuild is mandatory.

func (*Tree) AccountIterator

func (t *Tree) AccountIterator(root common.Hash, seek common.Hash) (AccountIterator, error)

AccountIterator creates a new account iterator for the specified root hash and seeks to a starting account hash.

func (*Tree) Cap

func (t *Tree) Cap(root common.Hash, layers int) error

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 (*Tree) DiskRoot added in v1.10.13

func (t *Tree) DiskRoot() common.Hash

diskRoot is a external helper function to return the disk layer root.

func (*Tree) Journal

func (t *Tree) Journal(root common.Hash) (common.Hash, error)

Journal commits an entire diff hierarchy to disk into a single journal entry. This is meant to be used during shutdown to persist the snapshot without flattening everything down (bad for reorgs).

The method returns the root hash of the base layer that needs to be persisted to disk as a trie too to allow continuing any pending generation op.

func (*Tree) LegacyJournal added in v1.10.13

func (t *Tree) LegacyJournal(root common.Hash) (common.Hash, error)

LegacyJournal is basically identical to Journal. it's the legacy version for flushing legacy journal. Now the only purpose of this function is for testing.

func (*Tree) Rebuild

func (t *Tree) Rebuild(root common.Hash)

Rebuild wipes all available snapshot data from the persistent database and discard all caches and diff layers. Afterwards, it starts a new snapshot generator with the given root hash.

func (*Tree) Snapshot

func (t *Tree) Snapshot(blockRoot common.Hash) Snapshot

Snapshot retrieves a snapshot belonging to the given block root, or nil if no snapshot is maintained for that block.

func (*Tree) StorageIterator added in v1.9.55

func (t *Tree) StorageIterator(root common.Hash, account common.Hash, seek common.Hash) (StorageIterator, error)

StorageIterator creates a new storage iterator for the specified root hash and account. The iterator will be move to the specific start position.

func (*Tree) Update

func (t *Tree) Update(blockRoot common.Hash, parentRoot common.Hash, destructs map[common.Hash]struct{}, accounts map[common.Hash][]byte, storage map[common.Hash]map[common.Hash][]byte) error

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).

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL