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 ¶
- func UseLogger(logger slog.Logger)
- type Config
- type Peer
- type SyncManager
- func (m *SyncManager) IsCurrent() bool
- func (m *SyncManager) OnBlock(peer *Peer, block *dcrutil.Block)
- func (m *SyncManager) OnHeaders(peer *Peer, headersMsg *wire.MsgHeaders)
- func (m *SyncManager) OnInv(peer *Peer, inv *wire.MsgInv)
- func (m *SyncManager) OnMixMsg(peer *Peer, msg mixing.Message) ([]mixing.Message, error)
- func (m *SyncManager) OnNotFound(peer *Peer, notFound *wire.MsgNotFound)
- func (m *SyncManager) OnPeerConnected(peer *Peer)
- func (m *SyncManager) OnPeerDisconnected(peer *Peer)
- func (m *SyncManager) OnTx(peer *Peer, tx *dcrutil.Tx) []*dcrutil.Tx
- func (m *SyncManager) ProcessBlock(block *dcrutil.Block) error
- func (m *SyncManager) RequestFromPeer(peer *Peer, blocks, voteHashes, tSpendHashes []chainhash.Hash)
- func (m *SyncManager) RequestMixMsgFromPeer(peer *Peer, mixHash *chainhash.Hash)
- func (m *SyncManager) Run(ctx context.Context)
- func (m *SyncManager) SyncHeight() int64
- func (m *SyncManager) SyncPeerID() int32
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
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
Peer extends a common peer to maintain additional state needed by the sync manager. The internals are intentionally unexported to create an opaque type.
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
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
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.