netsync

package
v1.8.0 Latest Latest
Warning

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

Go to latest
Published: Jun 9, 2023 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 block download, 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 block download, 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
}

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

type PeerNotifier

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

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

func (m *SyncManager) DonePeer(peer *peerpkg.Peer)

DonePeer informs the sync manager that a peer has disconnected.

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

func (m *SyncManager) NewPeer(peer *peerpkg.Peer)

NewPeer informs the sync manager of a newly active peer.

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

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

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

func (*SyncManager) QueueHeaders

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

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

func (*SyncManager) QueueInv

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

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

func (*SyncManager) QueueNotFound

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

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

func (*SyncManager) QueueTx

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

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

func (*SyncManager) RequestFromPeer

func (m *SyncManager) RequestFromPeer(p *peerpkg.Peer, blocks, voteHashes,
	tSpendHashes []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