netsync

package
v0.4.11 Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2023 License: ISC Imports: 17 Imported by: 0

README

netsync

Build Status ISC License GoDoc

Overview

This package 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.

Installation and Updating

$ go get -u gitlab.com/jaxnet/jaxnetd/netsync

License

Package netsync is licensed under the copyfree ISC License.

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

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.

func UseLogger

func UseLogger(logger zerolog.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 btclog.

Types

type Config

type Config struct {
	PeerNotifier PeerNotifier
	Chain        *blockchain.BlockChain
	TxMemPool    *mempool.TxPool
	ChainParams  *chaincfg.Params

	DisableCheckpoints bool
	MaxPeers           int

	FeeEstimator *mempool.FeeEstimator
}

Config is a configuration struct used to initialize a new SyncManager.

type P2PCom

type P2PCom interface {
	RelayTransactions(txns []*mempool.TxDesc)
	AddRebroadcastInventory(iv *wire.InvVect, data interface{})
	BroadcastMessage(msg wire.Message)
	Query(value interface{})
	NetTotals() (uint64, uint64)
	ConnectedCount() int32
}

type P2PConnManager

type P2PConnManager interface {
	// Connect adds the provided address as a new outbound peer.  The
	// permanent flag indicates whether or not to make the peer persistent
	// and reconnect if the connection is lost.  Attempting to connect to an
	// already existing peer will return an error.
	Connect(addr string, permanent bool) error

	// RemoveByID removes the peer associated with the provided id from the
	// list of persistent peers.  Attempting to remove an id that does not
	// exist will return an error.
	RemoveByID(id int32) error

	// RemoveByAddr removes the peer associated with the provided address
	// from the list of persistent peers.  Attempting to remove an address
	// that does not exist will return an error.
	RemoveByAddr(addr string) error

	// DisconnectByID disconnects the peer associated with the provided id.
	// This applies to both inbound and outbound peers.  Attempting to
	// remove an id that does not exist will return an error.
	DisconnectByID(id int32) error

	// DisconnectByAddr disconnects the peer associated with the provided
	// address.  This applies to both inbound and outbound peers.
	// Attempting to remove an address that does not exist will return an
	// error.
	DisconnectByAddr(addr string) error

	// ConnectedCount returns the number of currently connected peers.
	ConnectedCount() int32

	// NetTotals returns the sum of all bytes received and sent across the
	// network for all peers.
	NetTotals() (uint64, uint64)

	// ConnectedPeers returns an array consisting of all connected peers.
	ConnectedPeers() []ServerPeer

	// PersistentPeers returns an array consisting of all the persistent
	// peers.
	PersistentPeers() []ServerPeer

	// BroadcastMessage sends the provided message to all currently
	// connected peers.
	BroadcastMessage(msg wire.Message)

	// AddRebroadcastInventory adds the provided inventory to the list of
	// inventories to be rebroadcast at random intervals until they show up
	// in a block.
	AddRebroadcastInventory(iv *wire.InvVect, data interface{})

	// RelayTransactions generates and relays inventory vectors for all of
	// the passed transactions to all connected peers.
	RelayTransactions(txns []*mempool.TxDesc)
}

P2PConnManager represents a connection manager for use with the RPC server.

The interface contract requires that all of these methods are safe for concurrent access.

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 *jaxutil.Tx)
}

PeerNotifier exposes methods to notify peers of status changes to transactions, blocks, etc. Currently server (in the main package) implements this interface.

type RPCSyncMgr

type RPCSyncMgr struct {
	SyncMgr        *SyncManager
	HeadersLocator func(locators []*chainhash.Hash, hashStop *chainhash.Hash) []wire.BlockHeader
}

RPCSyncMgr provides a block manager for use with the RPC Server and implements the ServerSyncManager interface.

func (*RPCSyncMgr) IsCurrent

func (b *RPCSyncMgr) IsCurrent() bool

IsCurrent returns whether or not the sync manager believes the BlockChain is current as compared to the rest of the network.

This function is safe for concurrent access and is part of the ServerSyncManager interface implementation.

func (*RPCSyncMgr) LocateHeaders

func (b *RPCSyncMgr) LocateHeaders(locators []*chainhash.Hash, hashStop *chainhash.Hash) []wire.BlockHeader

LocateHeaders returns the hashes of the blocks after the first known block in the provided locators until the provided stop hash or the current tip is reached, up to a max of wire.MaxBlockHeadersPerMsg hashes.

This function is safe for concurrent access and is part of the ServerSyncManager interface implementation.

func (*RPCSyncMgr) Pause

func (b *RPCSyncMgr) Pause() chan<- struct{}

Pause pauses the sync manager until the returned channel is closed.

This function is safe for concurrent access and is part of the ServerSyncManager interface implementation.

func (*RPCSyncMgr) SubmitBlock

func (b *RPCSyncMgr) SubmitBlock(block *jaxutil.Block, flags chaindata.BehaviorFlags) (bool, error)

SubmitBlock submits the provided block to the network after processing it locally.

This function is safe for concurrent access and is part of the ServerSyncManager interface implementation.

func (*RPCSyncMgr) SyncPeerID

func (b *RPCSyncMgr) SyncPeerID() int32

SyncPeerID returns the peer that is currently the peer being used to sync from.

This function is safe for concurrent access and is part of the ServerSyncManager interface implementation.

type ServerPeer

type ServerPeer interface {
	// ToPeer returns the underlying peer instance.
	ToPeer() *peer.Peer

	// IsTxRelayDisabled returns whether or not the peer has disabled
	// transaction relay.
	IsTxRelayDisabled() bool

	// GetBanScore returns the current integer value that represents how close
	// the peer is to being banned.
	GetBanScore() uint32

	// GetFeeFilter returns the requested current minimum fee rate for which
	// transactions should be announced.
	GetFeeFilter() int64
}

ServerPeer represents a peer for use with the RPC server.

The interface contract requires that all of these methods are safe for concurrent access.

type ServerSyncManager

type ServerSyncManager interface {
	// IsCurrent returns whether or not the sync manager believes the BlockChain
	// is current as compared to the rest of the network.
	IsCurrent() bool

	// SubmitBlock submits the provided block to the network after
	// processing it locally.
	SubmitBlock(block *jaxutil.Block, flags chaindata.BehaviorFlags) (bool, error)

	// Pause pauses the sync manager until the returned channel is closed.
	Pause() chan<- struct{}

	// SyncPeerID returns the ID of the peer that is currently the peer being
	// used to sync from or 0 if there is none.
	SyncPeerID() int32

	// LocateHeaders returns the headers of the blocks after the first known
	// block in the provided locators until the provided stop hash or the
	// current tip is reached, up to a max of wire.MaxBlockHeadersPerMsg
	// hashes.
	LocateHeaders(locators []*chainhash.Hash, hashStop *chainhash.Hash) []wire.BlockHeader
}

ServerSyncManager represents a sync manager for use with the RPC server.

The interface contract requires that all of these methods are safe for concurrent access.

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. nolint: gomnd

func (*SyncManager) DonePeer

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

DonePeer informs the blockmanager that a peer has disconnected.

func (*SyncManager) IsCurrent

func (sm *SyncManager) IsCurrent() bool

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

func (*SyncManager) NewPeer

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

NewPeer informs the sync manager of a newly active peer.

func (*SyncManager) Pause

func (sm *SyncManager) Pause() chan<- struct{}

Pause pauses the sync manager until the returned channel is closed.

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 *jaxutil.Block, flags chaindata.BehaviorFlags) (bool, error)

ProcessBlock makes use of ProcessBlock on an internal instance of a block chain.

func (*SyncManager) QueueBlock

func (sm *SyncManager) QueueBlock(block *jaxutil.Block, blockActualMMR chainhash.Hash, 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.

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

func (sm *SyncManager) QueueTx(tx *jaxutil.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(ctx context.Context)

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

func (sm *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