Documentation
¶
Overview ¶
Package netsync implements a concurrency safe block syncing protocol. The SyncManager communicates with connected peers to perform an initial block download, keep the chain and unconfirmed transaction pool 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 DisableLog()
- func UseLogger(logger bchlog.Logger)
- type Config
- type PeerNotifier
- type SyncManager
- func (sm *SyncManager) DonePeer(peer *peerpkg.Peer, done chan struct{})
- func (sm *SyncManager) IsCurrent() bool
- func (sm *SyncManager) NewPeer(peer *peerpkg.Peer, done chan struct{})
- func (sm *SyncManager) Pause() chan<- struct{}
- func (sm *SyncManager) ProcessBlock(block *bchutil.Block, flags blockchain.BehaviorFlags) (bool, error)
- func (sm *SyncManager) QueueBlock(block *bchutil.Block, peer *peerpkg.Peer, done chan struct{})
- func (sm *SyncManager) QueueBlockError(hash *chainhash.Hash, peer *peerpkg.Peer)
- func (sm *SyncManager) QueueCompactBlock(block *bchutil.Block, peer *peerpkg.Peer, done chan struct{})
- func (sm *SyncManager) QueueHeaders(headers *wire.MsgHeaders, peer *peerpkg.Peer)
- func (sm *SyncManager) QueueInv(inv *wire.MsgInv, peer *peerpkg.Peer)
- func (sm *SyncManager) QueueNotFound(notFound *wire.MsgNotFound, peer *peerpkg.Peer)
- func (sm *SyncManager) QueueTx(tx *bchutil.Tx, peer *peerpkg.Peer, done chan struct{})
- func (sm *SyncManager) Start()
- func (sm *SyncManager) Stop() error
- func (sm *SyncManager) SyncHeight() uint64
- func (sm *SyncManager) SyncPeerID() int32
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func DisableLog ¶
func DisableLog()
DisableLog disables all library log output. Logging output is disabled by default until either UseLogger or SetLogWriter are called.
Types ¶
type Config ¶
type Config struct {
PeerNotifier PeerNotifier
Chain *blockchain.BlockChain
TxMemPool *mempool.TxPool
ChainParams *chaincfg.Params
DisableCheckpoints bool
MaxPeers int
FeeEstimator *mempool.FeeEstimator
MinSyncPeerNetworkSpeed uint64
FastSyncMode bool
RegTestSyncAnyHost bool
}
Config is a configuration struct used to initialize a new SyncManager.
type PeerNotifier ¶
type PeerNotifier interface {
AnnounceNewTransactions(newTxs []*mempool.TxDesc)
UpdatePeerHeights(latestBlkHash *chainhash.Hash, latestHeight int32, updateSource *peer.Peer)
RelayInventory(invVect *wire.InvVect, data interface{})
TransactionConfirmed(tx *bchutil.Tx)
// Misbehaving adds score to the ban score of the peer, which sent an
// invalid transaction or block, for BCHN's reject reason.
Misbehaving(peer *peer.Peer, score uint32, reason string)
}
PeerNotifier exposes methods to notify peers of status changes to transactions, blocks, etc. Currently server (in the main package) implements this interface.
type SyncManager ¶
type SyncManager struct {
// contains filtered or unexported fields
}
SyncManager is used to communicate block related messages with peers. The SyncManager is started as by executing Start() in a goroutine. Once started, it selects peers to sync from and starts the initial block download. Once the chain is in sync, the SyncManager handles incoming block and header notifications and relays announcements of new blocks to peers.
func New ¶
func New(config *Config) (*SyncManager, error)
New constructs a new SyncManager. Use Start to begin processing asynchronous block, tx, and inv updates.
func (*SyncManager) DonePeer ¶
func (sm *SyncManager) DonePeer(peer *peerpkg.Peer, done chan struct{})
DonePeer informs the blockmanager that a peer has disconnected. A nil done channel is not signaled.
func (*SyncManager) IsCurrent ¶
func (sm *SyncManager) IsCurrent() bool
IsCurrent returns whether or not the sync manager believes it is synced with the connected peers. It returns false once the sync manager has stopped.
func (*SyncManager) NewPeer ¶
func (sm *SyncManager) NewPeer(peer *peerpkg.Peer, done chan struct{})
NewPeer informs the sync manager of a newly active peer. A nil done channel is not signaled.
func (*SyncManager) Pause ¶
func (sm *SyncManager) Pause() chan<- struct{}
Pause pauses the sync manager until the returned channel is closed. Once the sync manager has stopped, it returns without pausing it, as nothing is processed then.
Note that while paused, all peer and block processing is halted. The message sender should avoid pausing the sync manager for long durations.
func (*SyncManager) ProcessBlock ¶
func (sm *SyncManager) ProcessBlock(block *bchutil.Block, flags blockchain.BehaviorFlags) (bool, error)
ProcessBlock makes use of ProcessBlock on an internal instance of a block chain. It returns errStopped once the sync manager has stopped.
func (*SyncManager) QueueBlock ¶
func (sm *SyncManager) QueueBlock(block *bchutil.Block, peer *peerpkg.Peer, done chan struct{})
QueueBlock adds the passed block message and peer to the block handling queue. Responds to the done channel argument after the block message is processed. The peer sent the block in a block message, so it is punished if the block is invalid, as by BCHN.
func (*SyncManager) QueueBlockError ¶
func (sm *SyncManager) QueueBlockError(hash *chainhash.Hash, peer *peerpkg.Peer)
QueueBlockError adds the passed block message and peer to the block handling queue to remove the requested block for our queues.
func (*SyncManager) QueueCompactBlock ¶ added in v0.22.3
func (sm *SyncManager) QueueCompactBlock(block *bchutil.Block, peer *peerpkg.Peer, done chan struct{})
QueueCompactBlock is QueueBlock for a block rebuilt from a compact block the peer sent. BIP 152 lets a peer relay a compact block once it has checked only its header, so the peer is not punished if the block is invalid, as BCHN does not punish it (net_processing.cpp:3270-3274, 3368-3377).
func (*SyncManager) QueueHeaders ¶
func (sm *SyncManager) QueueHeaders(headers *wire.MsgHeaders, peer *peerpkg.Peer)
QueueHeaders adds the passed headers message and peer to the block handling queue.
func (*SyncManager) QueueInv ¶
func (sm *SyncManager) QueueInv(inv *wire.MsgInv, peer *peerpkg.Peer)
QueueInv adds the passed inv message and peer to the block handling queue.
func (*SyncManager) QueueNotFound ¶ added in v0.22.3
func (sm *SyncManager) QueueNotFound(notFound *wire.MsgNotFound, peer *peerpkg.Peer)
QueueNotFound adds the passed notfound message and peer to the block handling queue.
func (*SyncManager) QueueTx ¶
func (sm *SyncManager) QueueTx(tx *bchutil.Tx, peer *peerpkg.Peer, done chan struct{})
QueueTx adds the passed transaction message and peer to the block handling queue. Responds to the done channel argument after the tx message is processed.
func (*SyncManager) Start ¶
func (sm *SyncManager) Start()
Start begins the core block handler which processes block and inv messages.
func (*SyncManager) Stop ¶
func (sm *SyncManager) Stop() error
Stop gracefully shuts down the sync manager by stopping all asynchronous handlers and waiting for them to finish.
func (*SyncManager) SyncHeight ¶
func (sm *SyncManager) SyncHeight() uint64
SyncHeight returns latest known block being synced to, or 0 if there is no sync peer or the sync manager has stopped. The block handler answers it, as the sync peer is its own.
func (*SyncManager) SyncPeerID ¶
func (sm *SyncManager) SyncPeerID() int32
SyncPeerID returns the ID of the current sync peer, or 0 if there is none or the sync manager has stopped.