snapshot

package
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 27, 2023 License: Apache-2.0 Imports: 11 Imported by: 0

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

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

type BlockchainSnapshot struct {
	Keys   [][]byte
	Values [][]byte
}

BlockchainSnapshot represents a snapshot of a blockchain key-value storage.

func (*BlockchainSnapshot) Decode

func (bs *BlockchainSnapshot) Decode(d scale.Decoder) error

Decode decodes the BlockchainSnapshot using the provided scale.Decoder.

func (*BlockchainSnapshot) Encode

func (bs *BlockchainSnapshot) Encode(e scale.Encoder) error

Encode encodes the BlockchainSnapshot using the provided scale.Encoder.

type Distributor

type Distributor interface {
	Receive() <-chan *Snapshot
	Send(s *Snapshot) error
	Close() error
}

Distributor is responsible for distributing snapshots among network peers.

func NewDistributor

func NewDistributor(logger hclog.Logger, network *network.Server) (Distributor, error)

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.

func (*KVBatch) Put

func (b *KVBatch) Put(k, v []byte)

Put adds a key-value pair to the batch write.

func (*KVBatch) Write

func (b *KVBatch) Write()

Write applies the batch write to the state storage.

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.

func (*Snapshot) Decode

func (s *Snapshot) Decode(d scale.Decoder) error

Decode decodes the Snapshot using the provided scale.Decoder.

func (*Snapshot) Encode

func (s *Snapshot) Encode(e scale.Encoder) error

Encode encodes the Snapshot using the provided scale.Encoder.

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

func (ss *StateStorage) Close() error

Close closes the state storage.

func (*StateStorage) End

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.

func (*StateStorage) SetCode

func (ss *StateStorage) SetCode(hash types.Hash, code []byte)

SetCode sets the code associated with the given hash in the state storage.

type StateStorageSnapshot

type StateStorageSnapshot struct {
	Keys   [][]byte
	Values [][]byte
}

StateStorageSnapshot represents a snapshot of the state storage.

func (*StateStorageSnapshot) Decode

func (ss *StateStorageSnapshot) Decode(d scale.Decoder) error

Decode decodes the StateStorageSnapshot using the provided scale.Decoder.

func (*StateStorageSnapshot) Encode

func (ss *StateStorageSnapshot) Encode(e scale.Encoder) error

Encode encodes the StateStorageSnapshot using the provided scale.Encoder.

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.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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