statesync

package
v1.2.1 Latest Latest
Warning

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

Go to latest
Published: Aug 29, 2024 License: Apache-2.0 Imports: 46 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// SnapshotChannel exchanges snapshot metadata
	SnapshotChannel = p2p.ChannelID(0x60)

	// ChunkChannel exchanges chunk contents
	ChunkChannel = p2p.ChannelID(0x61)

	// LightBlockChannel exchanges light blocks
	LightBlockChannel = p2p.ChannelID(0x62)

	// ParamsChannel exchanges consensus params
	ParamsChannel = p2p.ChannelID(0x63)
)
View Source
const (
	// MetricsSubsystem is a subsystem shared by all metrics exposed by this package.
	MetricsSubsystem = "statesync"
)

Variables

This section is empty.

Functions

func NewPeerStore

func NewPeerStore() *store.InMemStore[types.NodeID, PeerData]

NewPeerStore returns a new in-memory peer store

Types

type BlockProvider

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

BlockProvider is a p2p based light provider which uses a dispatcher connected to the state sync reactor to serve light blocks to the light client

TODO: This should probably be moved over to the light package but as we're not yet officially supporting p2p light clients we'll leave this here for now.

NOTE: BlockProvider will return an error with concurrent calls. However, we don't need a mutex because a light client (and the backfill process) will never call a method more than once at the same time

func NewBlockProvider

func NewBlockProvider(peer types.NodeID, chainID string, dispatcher *Dispatcher) *BlockProvider

Creates a block provider which implements the light client Provider interface.

func (*BlockProvider) ID

func (p *BlockProvider) ID() string

Returns the ID address of the provider (NodeID of peer)

func (*BlockProvider) LightBlock

func (p *BlockProvider) LightBlock(ctx context.Context, height int64) (*types.LightBlock, error)

LightBlock fetches a light block from the peer at a specified height returning either a light block or an appropriate error.

func (*BlockProvider) ReportEvidence

func (p *BlockProvider) ReportEvidence(_ctx context.Context, _ev types.Evidence) error

ReportEvidence should allow for the light client to report any light client attacks. This is a no op as there currently isn't a way to wire this up to the evidence reactor (we should endeavor to do this in the future but for now it's not critical for backwards verification)

func (*BlockProvider) String

func (p *BlockProvider) String() string

String implements stringer interface

type Dispatcher

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

A Dispatcher multiplexes concurrent requests by multiple peers for light blocks. Only one request per peer can be sent at a time. Subsequent concurrent requests will report an error from the LightBlock method. NOTE: It is not the responsibility of the dispatcher to verify the light blocks.

func NewDispatcher

func NewDispatcher(requestChannel p2p.Channel, logger log.Logger) *Dispatcher

func (*Dispatcher) Close

func (d *Dispatcher) Close()

Close shuts down the dispatcher and cancels any pending calls awaiting responses. Peers awaiting responses that have not arrived are delivered a nil block.

func (*Dispatcher) LightBlock

func (d *Dispatcher) LightBlock(ctx context.Context, height int64, peer types.NodeID) (*types.LightBlock, error)

LightBlock uses the request channel to fetch a light block from a given peer tracking, the call and waiting for the reactor to pass back the response. A nil LightBlock response is used to signal that the peer doesn't have the requested LightBlock.

func (*Dispatcher) Respond

func (d *Dispatcher) Respond(ctx context.Context, lb *tmproto.LightBlock, peer types.NodeID) error

Respond allows the underlying process which receives requests on the requestCh to respond with the respective light block. A nil response is used to represent that the receiver of the request does not have a light block at that height.

type HandlerFunc

type HandlerFunc func(ctx context.Context, update p2p.PeerUpdate) error

HandlerFunc is peer update event handler

type LightBlockRepository

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

LightBlockRepository is a repository for light blocks

func (*LightBlockRepository) Get

func (r *LightBlockRepository) Get(height uint64) (*types.LightBlock, error)

Get works out whether the node has a light block at a particular height and if so returns it so it can be gossiped to peers

type Metricer

type Metricer interface {
	TotalSnapshots() int64
	ChunkProcessAvgTime() time.Duration
	SnapshotHeight() int64
	SnapshotChunksCount() int64
	SnapshotChunksTotal() int64
	BackFilledBlocks() int64
	BackFillBlocksTotal() int64
}

Metricer defines an interface used for the rpc sync info query, please see statesync.metrics for the details.

type Metrics

type Metrics struct {
	// The total number of snapshots discovered.
	TotalSnapshots metrics.Counter
	// The average processing time per chunk.
	ChunkProcessAvgTime metrics.Gauge
	// The height of the current snapshot the has been processed.
	SnapshotHeight metrics.Gauge
	// The current number of chunks that have been processed.
	SnapshotChunk metrics.Counter
	// The current number of blocks that have been back-filled.
	BackFilledBlocks metrics.Counter
	// The total number of blocks that need to be back-filled.
	BackFillBlocksTotal metrics.Gauge
}

Metrics contains metrics exposed by this package.

func NopMetrics

func NopMetrics() *Metrics

func PrometheusMetrics

func PrometheusMetrics(namespace string, labelsAndValues ...string) *Metrics

type PeerData

type PeerData struct {
	Snapshots []Snapshot
	Status    PeerStatus
}

PeerData is a data of a peer

type PeerManager

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

PeerManager is a manager for peers

func NewPeerManager

func NewPeerManager(
	logger log.Logger,
	client client.SnapshotClient,
	peerStore store.Store[types.NodeID, PeerData],
	peerSubs *PeerSubscriber,
) *PeerManager

NewPeerManager creates a new peer manager

func (*PeerManager) Start

func (p *PeerManager) Start(ctx context.Context)

Start starts the peer manager and its peer update listeners

func (*PeerManager) Stop

func (p *PeerManager) Stop(ctx context.Context)

Stop stops the peer manager and its peer update listeners

type PeerStatus

type PeerStatus int

PeerStatus is a status of a peer

const (
	PeerNotReady PeerStatus = iota
	PeerReady
)

List of peer statuses

type PeerSubscriber

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

PeerSubscriber is a subscriber for peer events

func NewPeerSubscriber

func NewPeerSubscriber(logger log.Logger, sub p2p.PeerEventSubscriber) *PeerSubscriber

NewPeerSubscriber creates a new peer subscriber

func (*PeerSubscriber) On

func (p *PeerSubscriber) On(eventName p2p.PeerStatus, handler HandlerFunc)

On adds a handler for a peer update event

func (*PeerSubscriber) Start

func (p *PeerSubscriber) Start(ctx context.Context)

Start starts the peer subscriber

func (*PeerSubscriber) Stop

func (p *PeerSubscriber) Stop(ctx context.Context)

Stop stops the peer subscriber

type Reactor

type Reactor struct {
	service.BaseService
	// contains filtered or unexported fields
}

Reactor handles state sync, both restoring snapshots for the local node and serving snapshots for other nodes.

func NewReactor

func NewReactor(
	chainID string,
	initialHeight int64,
	cfg config.StateSyncConfig,
	logger log.Logger,
	conn abciclient.Client,
	channelCreator p2p.ChannelCreator,
	peerEvents p2p.PeerEventSubscriber,
	stateStore sm.Store,
	blockStore *store.BlockStore,
	tempDir string,
	ssMetrics *Metrics,
	eventBus *eventbus.EventBus,
	postSyncHook func(context.Context, sm.State) error,
	needsStateSync bool,
	client dashcore.Client,
	csState *consensus.State,
) *Reactor

NewReactor returns a reference to a new state sync reactor, which implements the service.Service interface. It accepts a logger, connections for snapshots and querying, references to p2p Channels and a channel to listen for peer updates on. Note, the reactor will close all p2p Channels when stopping.

func (*Reactor) BackFillBlocksTotal

func (r *Reactor) BackFillBlocksTotal() int64

func (*Reactor) BackFilledBlocks

func (r *Reactor) BackFilledBlocks() int64

func (*Reactor) Backfill

func (r *Reactor) Backfill(ctx context.Context, state sm.State) error

Backfill sequentially fetches, verifies and stores light blocks in reverse order. It does not stop verifying blocks until reaching a block with a height and time that is less or equal to the stopHeight and stopTime. The trustedBlockID should be of the header at startHeight.

func (*Reactor) ChunkProcessAvgTime

func (r *Reactor) ChunkProcessAvgTime() time.Duration

func (*Reactor) OnStart

func (r *Reactor) OnStart(ctx context.Context) error

OnStart starts separate go routines for each p2p Channel and listens for envelopes on each. In addition, it also listens for peer updates and handles messages on that p2p channel accordingly. Note, we do not launch a go-routine to handle individual envelopes as to not have to deal with bounding workers or pools. The caller must be sure to execute OnStop to ensure the outbound p2p Channels are closed. No error is returned.

func (*Reactor) OnStop

func (r *Reactor) OnStop()

OnStop stops the reactor by signaling to all spawned goroutines to exit and blocking until they all exit.

func (*Reactor) SnapshotHeight

func (r *Reactor) SnapshotHeight() int64

func (*Reactor) Sync

func (r *Reactor) Sync(ctx context.Context) (sm.State, error)

Sync runs a state sync, fetching snapshots and providing chunks to the application. At the close of the operation, Sync will bootstrap the state store and persist the commit at that height so that either consensus or blocksync can commence. It will then proceed to backfill the necessary amount of historical blocks before participating in consensus

func (*Reactor) TotalSnapshots

func (r *Reactor) TotalSnapshots() int64

type Snapshot

type Snapshot struct {
	Height int64
}

Snapshot is a snapshot of a peer

type StateProvider

type StateProvider interface {
	// AppHash returns the app hash after the given height has been committed.
	AppHash(ctx context.Context, height uint64) (tmbytes.HexBytes, error)
	// Commit returns the commit at the given height.
	Commit(ctx context.Context, height uint64) (*types.Commit, error)
	// State returns a state object at the given height.
	State(ctx context.Context, height uint64) (sm.State, error)
}

StateProvider is a provider of trusted state data for bootstrapping a node. This refers to the state.State object, not the state machine. There are two implementations. One uses the P2P layer and the other uses the RPC layer. Both use light client verification.

func NewP2PStateProvider

func NewP2PStateProvider(
	ctx context.Context,
	chainID string,
	initialHeight int64,
	trustHeight int64,
	providers []lightprovider.Provider,
	paramsSendCh p2p.Channel,
	logger log.Logger,
	dashCoreClient dashcore.Client,
) (StateProvider, error)

NewP2PStateProvider creates a light client state provider but uses a dispatcher connected to the P2P layer

func NewRPCStateProvider

func NewRPCStateProvider(
	ctx context.Context,
	chainID string,
	initialHeight int64,
	servers []string,
	trustHeight int64,
	logger log.Logger,
	dashCoreClient dashcore.Client,
) (StateProvider, error)

NewRPCStateProvider creates a new StateProvider using a light client and RPC clients.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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