p2p

package
v3.1.0 Latest Latest
Warning

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

Go to latest
Published: Sep 27, 2023 License: ISC Imports: 24 Imported by: 3

Documentation

Index

Constants

Pver is the maximum protocol version implemented by the LocalPeer.

Variables

View Source
var ErrDisconnected = errors.New("peer has been disconnected")

ErrDisconnected describes the error of a remote peer being disconnected by the local peer. While the disconnection may be clean, other methods currently being called on the peer must return this as a non-nil error.

View Source
var ErrNotFound = errors.E(errors.NotExist, "transaction not found")

ErrNotFound describes one or more transactions not being returned by a remote peer, indicated with notfound.

Functions

func UseLogger

func UseLogger(l slog.Logger)

UseLogger sets the package logger, which is slog.Disabled by default. This should only be called during init before main since access is unsynchronized.

Types

type DialFunc

type DialFunc func(ctx context.Context, net, addr string) (net.Conn, error)

DialFunc provides a method to dial a network connection.

type LocalPeer

type LocalPeer struct {
	// contains filtered or unexported fields
}

LocalPeer represents the local peer that can send and receive wire protocol messages with remote peers on the network.

func NewLocalPeer

func NewLocalPeer(params *chaincfg.Params, extaddr *net.TCPAddr, amgr *addrmgr.AddrManager) *LocalPeer

NewLocalPeer creates a LocalPeer that is externally reachable to remote peers through extaddr.

func (*LocalPeer) AddHandledMessages

func (lp *LocalPeer) AddHandledMessages(mask MessageMask)

AddHandledMessages adds all messages defined by the bitmask. This operation is concurrent-safe.

func (*LocalPeer) AddrManager

func (lp *LocalPeer) AddrManager() *addrmgr.AddrManager

AddrManager returns the local peer's address manager.

func (*LocalPeer) ConnectOutbound

func (lp *LocalPeer) ConnectOutbound(ctx context.Context, addr string, reqSvcs wire.ServiceFlag) (*RemotePeer, error)

ConnectOutbound establishes a connection to a remote peer by their remote TCP address. The peer is serviced in the background until the context is cancelled, the RemotePeer disconnects, times out, misbehaves, or the LocalPeer disconnects all peers.

func (*LocalPeer) ReceiveGetData

func (lp *LocalPeer) ReceiveGetData(ctx context.Context) (*RemotePeer, *wire.MsgGetData, error)

ReceiveGetData waits for a getdata message from a remote peer, returning the peer that sent the message, and the message itself.

func (*LocalPeer) ReceiveHeadersAnnouncement

func (lp *LocalPeer) ReceiveHeadersAnnouncement(ctx context.Context) (*RemotePeer, []*wire.BlockHeader, error)

ReceiveHeadersAnnouncement returns any unrequested headers that were announced without an inventory message due to a previous sendheaders request.

func (*LocalPeer) ReceiveInv

func (lp *LocalPeer) ReceiveInv(ctx context.Context) (*RemotePeer, *wire.MsgInv, error)

ReceiveInv waits for an inventory message from a remote peer, returning the peer that sent the message, and the message itself.

func (*LocalPeer) RemoveHandledMessages

func (lp *LocalPeer) RemoveHandledMessages(mask MessageMask)

RemoveHandledMessages removes all messages defined by the bitmask. This operation is concurrent safe.

func (*LocalPeer) RequirePeerHeight

func (lp *LocalPeer) RequirePeerHeight(requiredHeight int32)

RequirePeerHeight sets the minimum height a peer must advertise during its handshake. Peers advertising below this height will error during the handshake, and will not be marked as good peers in the address manager.

func (*LocalPeer) SeedPeers

func (lp *LocalPeer) SeedPeers(ctx context.Context, services wire.ServiceFlag)

SeedPeers seeds the local peer with remote addresses matching the services.

func (*LocalPeer) SetDialFunc

func (lp *LocalPeer) SetDialFunc(dial DialFunc)

SetDialFunc sets the function used to dial peer and seeder connections.

type MessageMask

type MessageMask uint64

MessageMask is a bitmask of message types that can be received and handled by consumers of this package by calling various Receive* methods on a LocalPeer. Received messages not in the mask are ignored and not receiving messages in the mask will leak goroutines. Handled messages can be added and removed by using the AddHandledMessages and RemoveHandledMessages methods of a LocalPeer.

const (
	MaskGetData MessageMask = 1 << iota
	MaskInv
)

Message mask constants

type RemotePeer

type RemotePeer struct {
	// contains filtered or unexported fields
}

RemotePeer represents a remote peer that can send and receive wire protocol messages with the local peer. RemotePeers must be created by dialing the peer's address with a LocalPeer.

func (*RemotePeer) Addrs

func (rp *RemotePeer) Addrs(ctx context.Context) error

Addrs requests a list of known active peers from a RemotePeer using getaddr. As many addr responses may be received for a single getaddr request, received address messages are handled asynchronously by the local peer and at least the stall timeout should be waited before disconnecting a remote peer while waiting for addr messages.

func (*RemotePeer) BanScore

func (rp *RemotePeer) BanScore() uint32

BanScore returns the banScore of the peer's.

func (*RemotePeer) Block

func (rp *RemotePeer) Block(ctx context.Context, blockHash *chainhash.Hash) (*wire.MsgBlock, error)

Block requests a block from a RemotePeer using getdata.

func (*RemotePeer) Blocks

func (rp *RemotePeer) Blocks(ctx context.Context, blockHashes []*chainhash.Hash) ([]*wire.MsgBlock, error)

Blocks requests multiple blocks at a time from a RemotePeer using a single getdata message. It returns when all of the blocks have been received.

func (*RemotePeer) CFilterV2

func (rp *RemotePeer) CFilterV2(ctx context.Context, blockHash *chainhash.Hash) (*gcs.FilterV2, uint32, []chainhash.Hash, error)

CFilterV2 requests a version 2 regular compact filter from a RemotePeer using getcfilterv2. The same block can not be requested concurrently from the same peer.

The inclusion proof data that ensures the cfilter is committed to in the header is returned as well.

func (*RemotePeer) CFiltersV2

func (rp *RemotePeer) CFiltersV2(ctx context.Context, blockHashes []*chainhash.Hash) ([]filterProof, error)

CFiltersV2 requests version 2 cfilters for all blocks described by blockHashes. This is currently implemented by making many separate getcfilter requests concurrently and waiting on every result.

Note: returning a []func() is an ugly hack to prevent a cyclical dependency between the rpc package and the wallet package.

func (*RemotePeer) Disconnect

func (rp *RemotePeer) Disconnect(reason error)

Disconnect closes the underlying TCP connection to a RemotePeer. A nil reason is replaced with ErrDisconnected.

func (*RemotePeer) Err

func (rp *RemotePeer) Err() error

Err blocks until the RemotePeer disconnects, returning the reason for disconnection.

func (*RemotePeer) GetInitState

func (rp *RemotePeer) GetInitState(ctx context.Context, msg *wire.MsgGetInitState) (*wire.MsgInitState, error)

GetInitState attempts to get initial state by sending a GetInitState message.

func (*RemotePeer) Headers

func (rp *RemotePeer) Headers(ctx context.Context, blockLocators []*chainhash.Hash, hashStop *chainhash.Hash) ([]*wire.BlockHeader, error)

Headers requests block headers from the RemotePeer with getheaders. Block headers can not be requested concurrently from the same peer. Sending a getheaders message and synchronously waiting for the result is not possible if a sendheaders message has been sent to the remote peer.

func (*RemotePeer) HeadersAsync

func (rp *RemotePeer) HeadersAsync(ctx context.Context, blockLocators []*chainhash.Hash, hashStop *chainhash.Hash) error

HeadersAsync requests block headers from the RemotePeer with getheaders. Block headers can not be requested concurrently from the same peer. This can only be used _after_ the sendheaders msg was sent and does _not_ wait for a reply from the remote peer. Headers will be delivered via the ReceiveHeaderAnnouncements call.

func (*RemotePeer) ID

func (rp *RemotePeer) ID() uint64

ID returns the remote ID.

func (*RemotePeer) InitialHeight

func (rp *RemotePeer) InitialHeight() int32

InitialHeight returns the current height the peer advertised in its version message.

func (*RemotePeer) InvsRecv

func (rp *RemotePeer) InvsRecv() *lru.Cache

InvsRecv returns an LRU cache of inventory hashes received by the remote peer.

func (*RemotePeer) InvsSent

func (rp *RemotePeer) InvsSent() *lru.Cache

InvsSent returns an LRU cache of inventory hashes sent to the remote peer.

func (*RemotePeer) KnownHeaders

func (rp *RemotePeer) KnownHeaders() *lru.Cache

KnownHeaders returns an LRU cache of block hashes from received headers messages.

func (*RemotePeer) LocalAddr

func (rp *RemotePeer) LocalAddr() net.Addr

LocalAddr returns the local address of the peer's TCP connection.

func (*RemotePeer) NA

func (rp *RemotePeer) NA() *addrmgr.NetAddress

NA returns the remote peer's net address.

func (*RemotePeer) PublishTransactions

func (rp *RemotePeer) PublishTransactions(ctx context.Context, txs ...*wire.MsgTx) error

PublishTransactions pushes an inventory message advertising transaction hashes of txs.

func (*RemotePeer) Pver

func (rp *RemotePeer) Pver() uint32

Pver returns the negotiated protocol version.

func (*RemotePeer) ReceivedOrphanHeader

func (rp *RemotePeer) ReceivedOrphanHeader() error

ReceivedOrphanHeader increases the banscore for a peer due to them sending an orphan header. Returns an error if the banscore has been breached.

func (*RemotePeer) RemoteAddr

func (rp *RemotePeer) RemoteAddr() net.Addr

RemoteAddr returns the remote address of the peer's TCP connection.

func (*RemotePeer) SendHeaders

func (rp *RemotePeer) SendHeaders(ctx context.Context) error

SendHeaders sends the remote peer a sendheaders message. This informs the peer to announce new blocks by immediately sending them in a headers message rather than sending an inv message containing the block hash.

Once this is called, it is no longer permitted to use the synchronous GetHeaders method, as there is no guarantee that the next received headers message corresponds with any getheaders request.

func (*RemotePeer) SendHeadersSent

func (rp *RemotePeer) SendHeadersSent() bool

SendHeadersSent returns whether this peer was already instructed to send new headers via the sendheaders message.

func (*RemotePeer) SendMessage

func (rp *RemotePeer) SendMessage(ctx context.Context, msg wire.Message) error

SendMessage sends an message to the remote peer. Use this method carefully, as calling this with an unexpected message that changes the protocol state may cause problems with the convenience methods implemented by this package.

func (*RemotePeer) Services

func (rp *RemotePeer) Services() wire.ServiceFlag

Services returns the remote peer's advertised service flags.

func (*RemotePeer) String

func (rp *RemotePeer) String() string

func (*RemotePeer) Transactions

func (rp *RemotePeer) Transactions(ctx context.Context, hashes []*chainhash.Hash) ([]*wire.MsgTx, error)

Transactions requests multiple transactions at a time from a RemotePeer using a single getdata message. It returns when all of the transactions and/or notfound messages have been received. The same transaction may not be requested multiple times concurrently from the same peer. Returns ErrNotFound with a slice of one or more nil transactions if any notfound messages are received for requested transactions.

func (*RemotePeer) UA

func (rp *RemotePeer) UA() string

UA returns the remote peer's user agent.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL