Documentation
¶
Overview ¶
Package snapshot provides functionality for managing snapshots of a blockchain key-value storage. It includes an interface for creating and applying snapshots, as well as an implementation using leveldb.
Package snapshot provides functionality for managing snapshots of blockchain state and storage in the context of decentralized applications (DApps). It offers utilities for creating, encoding, decoding, and applying snapshots of the blockchain's key-value storage and state storage.
The package includes interfaces, implementations, and helper functions for creating and manipulating snapshots. It supports various storage backends, including in-memory storage, LevelDB storage, and other key-value storage implementations. Snapshots can be used to capture and restore the blockchain state at specific points in time, enabling efficient state management, data integrity, and data recovery.
Key Features ¶
- Snapshotting of blockchain key-value storage and state storage.
- Encoding and decoding of snapshots using the Substrate scale format.
- Support for in-memory storage and LevelDB storage.
- Snapshot creation, application, and retrieval.
Usage ¶
To use the snapshot package, import it as follows:
import "github.com/example/snapshot"
This package provides several types and functions for working with snapshots, including the Snapshotter interface, which defines methods for managing snapshots. The package also includes implementations of the Snapshotter interface, such as NewMemoryDBSnapshotStorage and NewLevelDBSnapshotStorage, which create instances of snapshot storage backends.
Example:
logger := hclog.Default() // Create a logger instance
stateStorage := ... // Create an instance of state storage
blockchainStoragePath := ... // Path to LevelDB storage
snapshotter, blockchainStorage, _, err := snapshot.NewSnapshotter(logger, stateStorage, blockchainStoragePath)
if err != nil {
// Handle error
}
// Use the snapshotter and storage backend
Refer to the package's documentation and individual function/method documentation for more details on each component and its usage.
Index ¶
- type BlockchainKVSnapshotter
- type BlockchainSnapshot
- type Distributor
- type KVBatch
- type Snapshot
- type Snapshotter
- type StateStorage
- func (ss *StateStorage) Apply(snapshot *StateStorageSnapshot) error
- func (ss *StateStorage) Batch() itrie.Batch
- func (ss *StateStorage) Begin()
- func (ss *StateStorage) Close() error
- func (ss *StateStorage) End() *StateStorageSnapshot
- func (ss *StateStorage) Get(k []byte) ([]byte, bool)
- func (ss *StateStorage) GetCode(hash types.Hash) ([]byte, bool)
- func (ss *StateStorage) Put(k, v []byte)
- func (ss *StateStorage) SetCode(hash types.Hash, code []byte)
- type StateStorageSnapshot
- type StateStorageSnapshotter
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
type BlockchainKVSnapshotter ¶
type BlockchainKVSnapshotter interface {
storage.KV
// Begin starts a new transaction to create a snapshot.
Begin()
// End finalizes the transaction and returns the created snapshot.
End() *BlockchainSnapshot
// Apply applies the changes from the given snapshot to the underlying key-value storage.
Apply(snapshot *BlockchainSnapshot) error
}
BlockchainKVSnapshotter is an interface that extends storage.KV and provides additional snapshot-related functionality.
func NewLevelDBSnapshotStorage ¶
func NewLevelDBSnapshotStorage(path string) (BlockchainKVSnapshotter, error)
NewLevelDBSnapshotStorage creates a new storage reference with leveldb and snapshot support. It opens the leveldb database at the specified path and returns a BlockchainKVSnapshotter interface.
func NewMemoryDBSnapshotStorage ¶
func NewMemoryDBSnapshotStorage() (BlockchainKVSnapshotter, error)
NewMemoryDBSnapshotStorage creates a new storage reference with an in-memory key-value store and snapshot support.
type BlockchainSnapshot ¶
BlockchainSnapshot represents a snapshot of a blockchain key-value storage.
type Distributor ¶
Distributor is responsible for distributing snapshots among network peers.
func NewDistributor ¶
NewDistributor creates a new distributor instance that uses the given logger and network server. It subscribes to the gossip topic for snapshot distribution.
type KVBatch ¶
type KVBatch struct {
// contains filtered or unexported fields
}
KVBatch is a batch write for leveldb.
type Snapshot ¶
type Snapshot struct {
BlockNumber uint64 // BlockNumber is the number of the block at which the snapshot was taken.
BlockHash types.Hash // BlockHash is the hash of the block at which the snapshot was taken.
StateRoot types.Hash // StateRoot is the root hash of the state at which the snapshot was taken.
BlockchainSnapshot *BlockchainSnapshot // BlockchainSnapshot represents a snapshot of the blockchain key-value storage.
StateSnapshot *StateStorageSnapshot // StateSnapshot represents a snapshot of the state storage.
}
Snapshot represents a snapshot of the blockchain state.
type Snapshotter ¶
type Snapshotter interface {
Begin() // Begin starts a new transaction to create a snapshot.
End() *Snapshot // End finalizes the transaction and returns the created snapshot.
Apply(*Snapshot) error // Apply applies the changes from the given snapshot to the underlying storage.
}
Snapshotter is responsible for creating and applying snapshots.
func NewSnapshotter ¶
func NewSnapshotter(logger hclog.Logger, stateStorage itrie.Storage, blockchainStoragePath string) (Snapshotter, storage.Storage, itrie.Storage, error)
NewSnapshotter creates a new Snapshotter instance that uses the provided logger, state storage, and blockchain storage path. It returns the Snapshotter, blockchain storage, state storage, and any error that occurred.
type StateStorage ¶
type StateStorage struct {
// contains filtered or unexported fields
}
StateStorage is a wrapper around itrie.Storage that provides snapshotting functionality.
func (*StateStorage) Apply ¶
func (ss *StateStorage) Apply(snapshot *StateStorageSnapshot) error
Apply applies the changes from the given state storage snapshot to the underlying storage.
func (*StateStorage) Batch ¶
func (ss *StateStorage) Batch() itrie.Batch
Batch returns a new batch write for the state storage.
func (*StateStorage) Begin ¶
func (ss *StateStorage) Begin()
Begin starts a new transaction to create a state storage snapshot.
func (*StateStorage) End ¶
func (ss *StateStorage) End() *StateStorageSnapshot
End finalizes the transaction and returns the created state storage snapshot.
func (*StateStorage) Get ¶
func (ss *StateStorage) Get(k []byte) ([]byte, bool)
Get retrieves the value associated with the given key from the state storage.
func (*StateStorage) GetCode ¶
func (ss *StateStorage) GetCode(hash types.Hash) ([]byte, bool)
GetCode retrieves the code associated with the given hash from the state storage.
func (*StateStorage) Put ¶
func (ss *StateStorage) Put(k, v []byte)
Put adds a key-value pair to the state storage and the current transaction.
type StateStorageSnapshot ¶
StateStorageSnapshot represents a snapshot of the state storage.
type StateStorageSnapshotter ¶
type StateStorageSnapshotter interface {
itrie.Storage
Begin()
End() *StateStorageSnapshot
Apply(snapshot *StateStorageSnapshot) error
}
StateStorageSnapshotter is responsible for managing snapshots of the state storage.
func StateWrapper ¶
func StateWrapper(s itrie.Storage) StateStorageSnapshotter
StateWrapper wraps the given itrie.Storage with snapshotting functionality.