netsync

package
v1.0.4 Latest Latest
Warning

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

Go to latest
Published: Jan 6, 2026 License: ISC Imports: 21 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 {
	// PeerNotifier specifies an implementation to use for notifying peers of
	// status changes related to blocks and transactions.
	PeerNotifier PeerNotifier

	// 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

	// 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

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

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 PeerNotifier

type PeerNotifier interface {
	// AnnounceNewTransactions generates and relays inventory vectors and
	// notifies websocket clients of the passed transactions.
	AnnounceNewTransactions(txns []*dcrutil.Tx)

	// AnnounceMixMessages generates and relays inventory vectors of the
	// passed messages.
	AnnounceMixMessages(msgs []mixing.Message)
}

PeerNotifier provides an interface to notify peers of status changes related to blocks and transactions.

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

func (m *SyncManager) OnBlock(block *dcrutil.Block, peer *Peer, done chan struct{})

OnBlock adds the passed block message and peer to the event handling queue.

func (*SyncManager) OnHeaders

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

OnHeaders adds the passed headers message and peer to the event handling queue.

func (*SyncManager) OnInv

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

OnInv adds the passed inv message and peer to the event handling queue.

func (*SyncManager) OnMixMsg

func (m *SyncManager) OnMixMsg(msg mixing.Message, peer *Peer, done chan error)

OnMixMsg adds the passed mixing message and peer to the event handling queue.

func (*SyncManager) OnNotFound

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

OnNotFound adds the passed notfound message and peer to the event handling queue.

func (*SyncManager) OnTx

func (m *SyncManager) OnTx(tx *dcrutil.Tx, peer *Peer, done chan struct{})

OnTx adds the passed transaction message and peer to the event handling queue.

func (*SyncManager) PeerConnected

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

PeerConnected informs the sync manager of a newly active peer.

func (*SyncManager) PeerDisconnected

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

PeerDisconnected informs the sync manager that a peer has disconnected.

func (*SyncManager) ProcessBlock

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

ProcessBlock makes use of ProcessBlock on an internal instance of a block chain. It is funneled through the sync manager since blockchain is not safe for concurrent access.

func (*SyncManager) RequestFromPeer

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

RequestFromPeer allows an outside caller to request blocks or transactions from a peer. The requests are logged in the internal map of requests so the peer is not later banned for sending the respective data.

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