netsync

package
v1.10.3 Latest Latest
Warning

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

Go to latest
Published: Dec 30, 2025 License: ISC Imports: 20 Imported by: 0

README

netsync

Build Status ISC License Doc

Package netsync implements a concurrency safe block syncing protocol.

Overview

The provided implementation of SyncManager communicates with connected peers to perform an initial chain sync, keep the chain in sync, and announce new blocks connected to the chain. Currently the sync manager selects a single sync peer that it downloads all blocks from until it is up to date with the longest chain the sync peer is aware of.

License

Package netsync is licensed under the copyfree ISC License.

Documentation

Overview

Package netsync implements a concurrency safe block syncing protocol.

The provided implementation of SyncManager communicates with connected peers to perform an initial chain sync, keep the chain in sync, and announce new blocks connected to the chain. Currently the sync manager selects a single sync peer that it downloads all blocks from until it is up to date with the longest chain the sync peer is aware of.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func UseLogger

func UseLogger(logger slog.Logger)

UseLogger uses a specified Logger to output package logging info. This should be used in preference to SetLogWriter if the caller is also using slog.

Types

type Config

type Config struct {
	// ChainParams identifies which chain parameters the manager is associated
	// with.
	ChainParams *chaincfg.Params

	// Chain specifies the chain instance to use for processing blocks and
	// transactions.
	Chain *blockchain.BlockChain

	// TimeSource defines the median time source which is used to retrieve the
	// current time adjusted by the median time offset.
	TimeSource blockchain.MedianTimeSource

	// TxMemPool specifies the mempool to use for processing transactions.
	TxMemPool *mempool.TxPool

	// NoMiningStateSync indicates whether or not the sync manager should
	// perform an initial mining state synchronization with peers once they are
	// believed to be fully synced.
	NoMiningStateSync bool

	// MaxOutboundPeers specifies the maximum number of outbound peer the server
	// is expected to be connected with.
	MaxOutboundPeers uint64

	// MaxPeers specifies the maximum number of peers the server is expected to
	// be connected with.  It is primarily used as a hint for more efficient
	// synchronization.
	MaxPeers int

	// MaxOrphanTxs specifies the maximum number of orphan transactions the
	// transaction pool associated with the server supports.
	MaxOrphanTxs int

	// RecentlyConfirmedTxns specifies a size limited set to use for tracking
	// and querying the most recently confirmed transactions.  It is useful for
	// preventing duplicate requests.
	RecentlyConfirmedTxns *apbf.Filter

	// MixPool specifies the mixing pool to use for transient mixing
	// messages broadcast across the network.
	MixPool *mixpool.Pool
}

Config holds the configuration options related to the network chain synchronization manager.

type Peer added in v1.9.0

type Peer struct {
	*peerpkg.Peer
	// contains filtered or unexported fields
}

Peer extends a common peer to maintain additional state needed by the sync manager. The internals are intentionally unexported to create an opaque type.

func NewPeer added in v1.9.0

func NewPeer(peer *peerpkg.Peer) *Peer

NewPeer returns a new instance of a peer that wraps the provided underlying common peer with additional state that is used throughout the package.

type SyncManager

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

SyncManager provides a concurrency safe sync manager for handling all incoming blocks.

func New

func New(config *Config) *SyncManager

New returns a new network chain synchronization manager. Use Run to begin processing asynchronous events.

func (*SyncManager) IsCurrent

func (m *SyncManager) IsCurrent() bool

IsCurrent returns whether or not the sync manager believes it is synced with the connected peers.

This function is safe for concurrent access.

func (*SyncManager) OnBlock added in v1.10.0

func (m *SyncManager) OnBlock(peer *Peer, block *dcrutil.Block)

OnBlock should be invoked with blocks that are received from remote peers.

Its primary purpose is processing blocks by validating them, adding them to the blockchain, logging relevant information, and requesting more if needed.

It also deals with some other things related to syncing such as participating in determining when the blockchain is initially synced and removing the transactions in the block from the transaction and mixing pools.

Ideally, this should be called from the same peer goroutine that received the message so the bulk of the processing is done concurrently and further reads from the peer are blocked until the message is processed.

This function is safe for concurrent access.

func (*SyncManager) OnHeaders added in v1.10.0

func (m *SyncManager) OnHeaders(peer *Peer, headersMsg *wire.MsgHeaders)

OnHeaders should be invoked with header messages that are received from remote peers.

Its primary purpose is processing headers by ensuring they properly connect, adding them to the blockchain, and requesting more if needed.

It also deals with some other things related to syncing such as determining when the headers are initially synced, disconnecting outbound peers that have less cumulative work compared to the known minimum work during the initial chain sync, and requesting blocks associated with the announced headers.

Ideally, this should be called from the same peer goroutine that received the message so the bulk of the processing is done concurrently and further reads from the peer are blocked until the message is processed.

This function is safe for the concurrent access.

func (*SyncManager) OnInv added in v1.10.0

func (m *SyncManager) OnInv(peer *Peer, inv *wire.MsgInv)

OnInv should be invoked with inventory announcements that are received from remote peers.

Its primary purpose is to learn about the inventory advertised by the remote peer and to use that information to guide decisions regarding whether or not and when to request the relevant associated data from the peer.

Ideally, this should be called from the same peer goroutine that received the message so the bulk of the processing is done concurrently and further reads from the peer are blocked until the message is processed.

This function is safe for the concurrent access.

func (*SyncManager) OnMixMsg added in v1.10.0

func (m *SyncManager) OnMixMsg(peer *Peer, msg mixing.Message) ([]mixing.Message, error)

OnMixMsg should be invoked with mix messages that are received from remote peers.

Its primary purpose is processing mix messages by validating them and adding them to the mixpool along with any orphan messages that depend on them.

It also deals with some other things related to syncing such as marking in-flight messages as received and preventing multiple requests for invalid messages.

It returns a slice of messages added to the mixpool which might include the passed message itself along with any additional orphan messages that were added as a result of the passed one being accepted.

Ideally, this should be called from the same peer goroutine that received the message so the bulk of the processing is done concurrently and further reads from the peer are blocked until the message is processed.

This function is safe for concurrent access.

func (*SyncManager) OnNotFound added in v1.10.0

func (m *SyncManager) OnNotFound(peer *Peer, notFound *wire.MsgNotFound)

OnNotFound should be invoked from the sync manager with notfound messages that are received from remote peers.

Currently, this primarily just removes the items from the request maps so they can eventually be requested from elsewhere. This could be improved in the future to immediately request the reported items from another peer that has announced them.

Ideally, this should be called from the same peer goroutine that received the message so the bulk of the processing is done concurrently and further reads from the peer are blocked until the message is processed.

This function is safe for the concurrent access.

func (*SyncManager) OnPeerConnected added in v1.10.0

func (m *SyncManager) OnPeerConnected(peer *Peer)

OnPeerConnected should be invoked with peers that have already gone through version negotiation and passed other initial validation checks that determine it is suitable for participation in staying synced with the network.

This function is safe for concurrent access.

func (*SyncManager) OnPeerDisconnected added in v1.10.0

func (m *SyncManager) OnPeerDisconnected(peer *Peer)

OnPeerDisconnected should be invoked when peers the sync manager was previously informed about have disconnected. It removes the peer as a candidate for syncing and, in the case it was the current sync peer, attempts to select a new best peer to sync from.

This function is safe for concurrent access.

func (*SyncManager) OnTx added in v1.10.0

func (m *SyncManager) OnTx(peer *Peer, tx *dcrutil.Tx) []*dcrutil.Tx

OnTx should be invoked with transactions that are received from remote peers.

Its primary purpose is processing transactions by validating them and adding them to the mempool along with any orphan transactions that depend on it.

It also deals with some other things related to syncing such as marking in-flight transactions as received and preventing multiple requests for invalid transactions.

It returns a slice of transactions added to the mempool which might include the passed transaction itself along with any additional orphan transactions that were added as a result of the passed one being accepted.

Ideally, this should be called from the same peer goroutine that received the message so the bulk of the processing is done concurrently and further reads from the peer are blocked until the message is processed.

This function is safe for concurrent access.

func (*SyncManager) ProcessBlock

func (m *SyncManager) ProcessBlock(block *dcrutil.Block) error

ProcessBlock processes the provided block using the chain instance associated with the sync manager.

Blocks from all sources, such as those submitted to the RPC server and those found by the CPU miner, are expected to be processed using this method as opposed to directly invoking the method on the chain instance.

Passing all blocks through the sync manager ensures they are all processed using the same code paths as blocks received from the network which helps enforce consistent handling and that the sync manager is always up-to-date in regards to the sync status of the chain.

This function is safe for concurrent access.

func (*SyncManager) RequestFromPeer

func (m *SyncManager) RequestFromPeer(peer *Peer, blocks, voteHashes, tSpendHashes []chainhash.Hash)

RequestFromPeer requests any combination of blocks, votes, and treasury spends from the given peer. It ensures all of the requests are tracked so the peer is not banned for sending unrequested data when it responds.

This function is safe for concurrent access.

func (*SyncManager) RequestMixMsgFromPeer added in v1.10.0

func (m *SyncManager) RequestMixMsgFromPeer(peer *Peer, mixHash *chainhash.Hash)

RequestMixMsgFromPeer requests the specified mix message from the given peer. It ensures all of the requests are tracked so the peer is not banned for sending unrequested data when it responds.

This function is safe for concurrent access.

func (*SyncManager) Run

func (m *SyncManager) Run(ctx context.Context)

Run starts the sync manager and all other goroutines necessary for it to function properly and blocks until the provided context is cancelled.

func (*SyncManager) SyncHeight

func (m *SyncManager) SyncHeight() int64

SyncHeight returns latest known block being synced to.

func (*SyncManager) SyncPeerID

func (m *SyncManager) SyncPeerID() int32

SyncPeerID returns the ID of the current sync peer, or 0 if there is none.

Jump to

Keyboard shortcuts

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