statesync

package
v0.111.0 Latest Latest
Warning

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

Go to latest
Published: Jul 18, 2025 License: MIT Imports: 19 Imported by: 0

Documentation

Overview

Package statesync implements module for the P2P state synchronisation process. The module manages state synchronisation for non-archival nodes which are joining the network and don't have the ability to resync from the genesis block.

Given the currently available state synchronisation point P, the state sync process includes the following stages:

1. Fetching headers starting from height 0 up to P+1. 2. Fetching state data for height P, starting from the corresponding state root. It can either be raw contract storage items or MPT nodes depending on the StorageSyncMode. 3. Fetching blocks starting from height P-MaxTraceableBlocks(P) (or 0) up to P.

All steps are being performed sequentially. Once all the data are collected and stored in the db, an atomic state jump is occurred to the state sync point P. Further node operation process is performed using standard sync mechanism until the node reaches synchronised state.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func TemporaryPrefix added in v0.98.0

func TemporaryPrefix(currPrefix storage.KeyPrefix) storage.KeyPrefix

TemporaryPrefix accepts current storage prefix and returns prefix to use for storing intermediate items during synchronization.

Types

type Ledger added in v0.98.1

type Ledger interface {
	AddHeaders(...*block.Header) error
	BlockHeight() uint32
	IsHardforkEnabled(hf *config.Hardfork, blockHeight uint32) bool
	GetConfig() config.Blockchain
	GetHeader(hash util.Uint256) (*block.Header, error)
	GetHeaderHash(uint32) util.Uint256
	HeaderHeight() uint32
}

Ledger is the interface required from Blockchain for Module to operate.

type Module

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

Module represents state sync module and aimed to gather state-related data to perform an atomic state jump.

func NewModule

func NewModule(bc Ledger, stateMod *stateroot.Module, log *zap.Logger, s *dao.Simple, jumpCallback func(p uint32) error) *Module

NewModule returns new instance of statesync module.

func (*Module) AddBlock

func (s *Module) AddBlock(block *block.Block) error

AddBlock verifies and saves block skipping executable scripts.

func (*Module) AddContractStorageItems added in v0.110.0

func (s *Module) AddContractStorageItems(kvs []storage.KeyValue, syncHeight uint32, expectedRoot util.Uint256) error

AddContractStorageItems adds a batch of key-value pairs for storage-based sync.

func (*Module) AddHeaders

func (s *Module) AddHeaders(hdrs ...*block.Header) error

AddHeaders validates and adds specified headers to the chain.

func (*Module) AddMPTNodes

func (s *Module) AddMPTNodes(nodes [][]byte) error

AddMPTNodes tries to add provided set of MPT nodes to the MPT billet if they are not yet collected.

func (*Module) BlockHeight

func (s *Module) BlockHeight() uint32

BlockHeight returns index of the last stored block. It's a no-op to call this method until MPT is in sync since block height initialization requires access to the node state at state synchronisation point.

func (*Module) GetConfig added in v0.108.0

func (s *Module) GetConfig() config.Blockchain

GetConfig returns current blockchain configuration.

func (*Module) GetLastStoredKey added in v0.110.0

func (s *Module) GetLastStoredKey() []byte

GetLastStoredKey returns the last processed storage key iff operating in ContractStorageBased mode.

func (*Module) GetStateSyncPoint added in v0.110.0

func (s *Module) GetStateSyncPoint() uint32

GetStateSyncPoint returns the current state synchronisation point P.

func (*Module) GetUnknownMPTNodesBatch

func (s *Module) GetUnknownMPTNodesBatch(limit int) []util.Uint256

GetUnknownMPTNodesBatch returns set of currently unknown MPT nodes (`limit` at max).

func (*Module) HeaderHeight added in v0.108.0

func (s *Module) HeaderHeight() uint32

HeaderHeight returns the height of the latest stored header.

func (*Module) Init

func (s *Module) Init(currChainHeight uint32) error

Init initializes state sync module for the current chain's height with given callback for MPT nodes requests.

func (*Module) IsActive

func (s *Module) IsActive() bool

IsActive tells whether state sync module is on and still gathering state synchronisation data (headers, blocks or MPT nodes).

func (*Module) IsInitialized

func (s *Module) IsInitialized() bool

IsInitialized tells whether state sync module does not require initialization. If `false` is returned then Init can be safely called.

func (*Module) NeedBlocks added in v0.108.0

func (s *Module) NeedBlocks() bool

NeedBlocks returns whether the module hasn't completed blocks synchronisation.

func (*Module) NeedHeaders

func (s *Module) NeedHeaders() bool

NeedHeaders tells whether the module hasn't completed headers synchronisation.

func (*Module) NeedStorageData added in v0.110.0

func (s *Module) NeedStorageData() bool

NeedStorageData returns whether the module hasn't completed contract storage state synchronization.

func (*Module) SetOnStageChanged added in v0.108.0

func (s *Module) SetOnStageChanged(cb func())

SetOnStageChanged sets callback that is triggered whenever the sync stage changes.

func (*Module) Traverse

func (s *Module) Traverse(root util.Uint256, process func(node mpt.Node, nodeBytes []byte) bool) error

Traverse traverses local MPT nodes starting from the specified root down to its children calling `process` for each serialised node until stop condition is satisfied.

type Pool

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

Pool stores unknown MPT nodes along with the corresponding paths (single node is allowed to have multiple MPT paths).

func NewPool

func NewPool() *Pool

NewPool returns new MPT node hashes pool.

func (*Pool) Add

func (mp *Pool) Add(hash util.Uint256, path []byte)

Add adds path to the set of paths for the specified node.

func (*Pool) ContainsKey

func (mp *Pool) ContainsKey(hash util.Uint256) bool

ContainsKey checks if MPT node with the specified hash is in the Pool.

func (*Pool) Count

func (mp *Pool) Count() int

Count returns the number of nodes in the pool.

func (*Pool) GetAll

func (mp *Pool) GetAll() map[util.Uint256][][]byte

GetAll returns all MPT nodes with the corresponding paths from the pool.

func (*Pool) GetBatch

func (mp *Pool) GetBatch(limit int) []util.Uint256

GetBatch returns set of unknown MPT nodes hashes (`limit` at max).

func (*Pool) Remove

func (mp *Pool) Remove(hash util.Uint256)

Remove removes MPT node from the pool by the specified hash.

func (*Pool) TryGet

func (mp *Pool) TryGet(hash util.Uint256) ([][]byte, bool)

TryGet returns a set of MPT paths for the specified HashNode.

func (*Pool) Update

func (mp *Pool) Update(remove map[util.Uint256][][]byte, add map[util.Uint256][][]byte)

Update is an atomic operation and removes/adds specified nodes from/to the pool.

type StorageSyncMode added in v0.110.0

type StorageSyncMode byte

StorageSyncMode is an enum that denotes the operation mode of contract storage synchronisation. It can be either ContractStorageBased or MPTBased.

const (
	// MPTBased denotes that the Module recovers contract storage state based on
	// the MPT state at the state synchronisation point.
	MPTBased StorageSyncMode = iota
	// ContractStorageBased denotes that the Module recovers contract storage state
	// based on the raw contract storage items at the state synchronisation point.
	ContractStorageBased
)

func (StorageSyncMode) String added in v0.110.0

func (i StorageSyncMode) String() string

Jump to

Keyboard shortcuts

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