peer

package
v0.0.3 Latest Latest
Warning

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

Go to latest
Published: Feb 7, 2021 License: ISC Imports: 25 Imported by: 1

Documentation

Overview

Copyright (c) 2020-2021 The bitcoinpay developers Copyright (c) 2013-2016 The btcsuite developers Copyright (c) 2016-2018 The Decred developers Use of this source code is governed by an ISC license that can be found in the LICENSE file.

Copyright (c) 2020-2021 The bitcoinpay developers Copyright (c) 2013-2016 The btcsuite developers Copyright (c) 2016-2018 The Decred developers Use of this source code is governed by an ISC license that can be found in the LICENSE file.

Copyright (c) 2020-2021 The bitcoinpay developers Copyright (c) 2013-2016 The btcsuite developers Copyright (c) 2016-2018 The Decred developers Use of this source code is governed by an ISC license that can be found in the LICENSE file.

Copyright (c) 2020-2021 The bitcoinpay developers Copyright (c) 2013-2016 The btcsuite developers Copyright (c) 2016-2018 The Decred developers Use of this source code is governed by an ISC license that can be found in the LICENSE file.

Copyright (c) 2020-2021 The bitcoinpay developers Copyright (c) 2013-2016 The btcsuite developers Copyright (c) 2016-2018 The Decred developers Use of this source code is governed by an ISC license that can be found in the LICENSE file.

Copyright (c) 2020-2021 The bitcoinpay developers Copyright (c) 2013-2016 The btcsuite developers Copyright (c) 2016-2018 The Decred developers Use of this source code is governed by an ISC license that can be found in the LICENSE file.

Copyright (c) 2020-2021 The bitcoinpay developers Copyright (c) 2013-2016 The btcsuite developers Copyright (c) 2016-2018 The Decred developers Use of this source code is governed by an ISC license that can be found in the LICENSE file.

Copyright (c) 2020-2021 The bitcoinpay developers Copyright (c) 2013-2016 The btcsuite developers Copyright (c) 2016-2018 The Decred developers Use of this source code is governed by an ISC license that can be found in the LICENSE file.

Copyright (c) 2020-2021 The bitcoinpay developers Copyright (c) 2013-2016 The btcsuite developers Copyright (c) 2016-2018 The Decred developers Use of this source code is governed by an ISC license that can be found in the LICENSE file.

Index

Constants

View Source
const (
	// MaxProtocolVersion is the max protocol version the peer supports.
	MaxProtocolVersion = protocol.ProtocolVersion

	// trickleTimeout is the duration of the ticker which trickles down the
	// inventory to a peer.
	TrickleTimeout = 10 * time.Second
)

Variables

This section is empty.

Functions

This section is empty.

Types

type Config

type Config struct {
	// NewestGS specifies a callback which provides the newest GS
	NewestGS GSFunc

	// HostToNetAddress returns the netaddress for the given host. This can be
	// nil in  which case the host will be parsed as an IP address.
	HostToNetAddress HostToNetAddrFunc

	// Proxy indicates a proxy is being used for connections.  The only
	// effect this has is to prevent leaking the tor proxy address, so it
	// only needs to specified if using a tor proxy.
	Proxy string

	// UserAgentName specifies the user agent name to advertise.  It is
	// highly recommended to specify this value.
	UserAgentName string

	// UserAgentVersion specifies the user agent version to advertise.  It
	// is highly recommended to specify this value and that it follows the
	// form "major.minor.revision" e.g. "2.6.41".
	UserAgentVersion string

	// UserAgentComments specifies any additional comments to include the in
	// user agent to advertise.  This is optional, so it may be nil.  If
	// specified, the comments should only exist of characters from the
	// semantic alphabet [a-zA-Z0-9-].
	UserAgentComments []string

	// ChainParams identifies which chain parameters the peer is associated
	// with.  It is highly recommended to specify this field, however it can
	// be omitted in which case the test network will be used.
	ChainParams *params.Params

	// Services specifies which services to advertise as supported by the
	// local peer.  This field can be omitted in which case it will be 0
	// and therefore advertise no supported services.
	Services protocol.ServiceFlag

	// ProtocolVersion specifies the maximum protocol version to use and
	// advertise.  This field can be omitted in which case
	// peer.MaxProtocolVersion will be used.
	ProtocolVersion uint32

	// DisableRelayTx specifies if the remote peer should be informed to
	// not send inv messages for transactions.
	DisableRelayTx bool

	// Listeners houses callback functions to be invoked on receiving peer
	// messages.
	Listeners MessageListeners

	// TrickleInterval is the duration of the ticker which trickles down the
	// inventory to a peer.
	TrickleInterval time.Duration
}

Config is the struct to hold configuration options useful to Peer.

type GSFunc

type GSFunc func() (gs *blockdag.GraphState, err error)

HashFunc is a function which returns a block hash, height and error It is used as a callback to get newest block details.

type HostToNetAddrFunc

type HostToNetAddrFunc func(host string, port uint16,
	services protocol.ServiceFlag) (*types.NetAddress, error)

HostToNetAddrFunc is a func which takes a host, port, services and returns the netaddress.

type MessageListeners

type MessageListeners struct {
	// OnGetAddr is invoked when a peer receives a getaddr wire message.
	OnGetAddr func(p *Peer, msg *message.MsgGetAddr)

	// OnAddr is invoked when a peer receives an addr wire message.
	OnAddr func(p *Peer, msg *message.MsgAddr)

	// OnPing is invoked when a peer receives a ping wire message.
	OnPing func(p *Peer, msg *message.MsgPing)

	// OnPong is invoked when a peer receives a pong wire message.
	OnPong func(p *Peer, msg *message.MsgPong)

	// OnVersion is invoked when a peer receives a version wire message.
	// The caller may return a reject message in which case the message will
	// be sent to the peer and the peer will be disconnected.
	OnVersion func(p *Peer, msg *message.MsgVersion) *message.MsgReject

	// OnVerAck is invoked when a peer receives a verack wire message.
	OnVerAck func(p *Peer, msg *message.MsgVerAck)

	// OnReject is invoked when a peer receives a reject wire message.
	OnReject func(p *Peer, msg *message.MsgReject)

	// OnRead is invoked when a peer receives a wire message.  It consists
	// of the number of bytes read, the message, and whether or not an error
	// in the read occurred.  Typically, callers will opt to use the
	// callbacks for the specific message types, however this can be useful
	// for circumstances such as keeping track of server-wide byte counts or
	// working with custom message types for which the peer does not
	// directly provide a callback.
	OnRead func(p *Peer, bytesRead int, msg message.Message, err error)

	// OnWrite is invoked when we write a wire message to a peer.  It
	// consists of the number of bytes written, the message, and whether or
	// not an error in the write occurred.  This can be useful for
	// circumstances such as keeping track of server-wide byte counts.
	OnWrite func(p *Peer, bytesWritten int, msg message.Message, err error)

	// OnTx is invoked when a peer receives a tx wire message.
	OnTx func(p *Peer, msg *message.MsgTx)

	// OnGetBlocks is invoked when a peer receives a getblocks wire message.
	OnGetBlocks func(p *Peer, msg *message.MsgGetBlocks)

	// OnGetHeaders is invoked when a peer receives a getheaders
	// message.
	OnGetHeaders func(p *Peer, msg *message.MsgGetHeaders)

	// OnBlock is invoked when a peer receives a block wire message.
	OnBlock func(p *Peer, msg *message.MsgBlock, buf []byte)

	// OnInv is invoked when a peer receives an inv message.
	OnInv func(p *Peer, msg *message.MsgInv)

	// OnGetData is invoked when a peer receives a getdata wire message.
	OnGetData func(p *Peer, msg *message.MsgGetData)

	// OnNotFound is invoked when a peer receives a notfound message.
	OnNotFound func(p *Peer, msg *message.MsgNotFound)

	// OnGetMiningState is invoked when a peer receives a getminings wire
	// message.
	OnGetMiningState func(p *Peer, msg *message.MsgGetMiningState)

	// OnMiningState is invoked when a peer receives a miningstate wire
	// message.
	OnMiningState func(p *Peer, msg *message.MsgMiningState)

	// OnGraphState
	OnGraphState func(p *Peer, msg *message.MsgGraphState)

	// OnMemPool is invoked when a peer receives a mempool wire message.
	OnMemPool func(p *Peer, msg *message.MsgMemPool)

	// OnSyncResult is invoked when a peer receives a getblocks wire message.
	OnSyncResult func(p *Peer, msg *message.MsgSyncResult)

	// OnSyncDAG
	OnSyncDAG func(p *Peer, msg *message.MsgSyncDAG)

	// OnSyncPoint
	OnSyncPoint func(p *Peer, msg *message.MsgSyncPoint)

	// OnFeeFilter
	OnFeeFilter func(p *Peer, msg *message.MsgFeeFilter)
}

MessageListeners defines callback function pointers to invoke with message listeners for a peer. Any listener which is not set to a concrete callback during peer initialization is ignored. Execution of multiple message listeners occurs serially, so one callback blocks the execution of the next.

NOTE: Unless otherwise documented, these listeners must NOT directly call any blocking calls (such as WaitForShutdown) on the peer instance since the input handler goroutine blocks until the callback has completed. Doing so will result in a deadlock.

type Peer

type Peer struct {

	// prevget
	PrevGet PrevGet
	// contains filtered or unexported fields
}

Peer represents a connected p2p network remote node.

func NewInboundPeer

func NewInboundPeer(cfg *Config) *Peer

NewInboundPeer returns a new inbound peer. Use Start to begin processing incoming and outgoing messages.

func NewOutboundPeer

func NewOutboundPeer(cfg *Config, addr string) (*Peer, error)

NewOutboundPeer returns a new outbound peer.

func (*Peer) AddKnownInventory

func (p *Peer) AddKnownInventory(invVect *message.InvVect)

AddKnownInventory adds the passed inventory to the cache of known inventory for the peer.

This function is safe for concurrent access.

func (*Peer) Addr

func (p *Peer) Addr() string

Addr returns the peer address. This function is safe for concurrent access.

func (*Peer) AssociateConnection

func (p *Peer) AssociateConnection(c *connmgr.ConnReq)

AssociateConnection associates the given conn to the peer. Calling this function when the peer is already connected will have no effect.

func (*Peer) BytesReceived

func (p *Peer) BytesReceived() uint64

BytesReceived returns the total number of bytes received by the peer.

This function is safe for concurrent access.

func (*Peer) BytesSent

func (p *Peer) BytesSent() uint64

BytesSent returns the total number of bytes sent by the peer.

This function is safe for concurrent access.

func (*Peer) Cfg

func (p *Peer) Cfg() *Config

func (*Peer) CleanGetBlocksSet

func (p *Peer) CleanGetBlocksSet()

func (*Peer) Connected

func (p *Peer) Connected() bool

Connected returns whether or not the peer is currently connected.

This function is safe for concurrent access.

func (*Peer) Disconnect

func (p *Peer) Disconnect()

Disconnect disconnects the peer by closing the connection. Calling this function when the peer is already disconnected or in the process of disconnecting will have no effect.

func (*Peer) GetGraphState

func (p *Peer) GetGraphState() *blockdag.GraphState

func (*Peer) ID

func (p *Peer) ID() int32

ID returns the peer id.

This function is safe for concurrent access.

func (*Peer) Inbound

func (p *Peer) Inbound() bool

Inbound returns whether the peer is inbound.

This function is safe for concurrent access.

func (*Peer) LastAnnouncedBlock

func (p *Peer) LastAnnouncedBlock() *hash.Hash

LastAnnouncedBlock returns the last announced block of the remote peer.

This function is safe for concurrent access.

func (*Peer) LastGS

func (p *Peer) LastGS() *blockdag.GraphState

LastGS returns the last graph state of the peer.

This function is safe for concurrent access.

func (*Peer) LastPingNonce

func (p *Peer) LastPingNonce() uint64

LastPingNonce returns the last ping nonce of the remote peer.

func (*Peer) LastRecv

func (p *Peer) LastRecv() time.Time

LastRecv returns the last recv time of the peer.

This function is safe for concurrent access.

func (*Peer) LastSend

func (p *Peer) LastSend() time.Time

LastSend returns the last send time of the peer.

This function is safe for concurrent access.

func (*Peer) LocalAddr

func (p *Peer) LocalAddr() net.Addr

LocalAddr returns the local address of the connection.

This function is safe fo concurrent access.

func (*Peer) NA

func (p *Peer) NA() *types.NetAddress

NA returns the peer network address.

This function is safe for concurrent access.

func (*Peer) ProtocolVersion

func (p *Peer) ProtocolVersion() uint32

ProtocolVersion returns the negotiated peer protocol version.

This function is safe for concurrent access.

func (*Peer) PushAddrMsg

func (p *Peer) PushAddrMsg(addresses []*types.NetAddress) ([]*types.NetAddress, error)

PushAddrMsg sends an addr message to the connected peer using the provided addresses. This function is useful over manually sending the message via QueueMessage since it automatically limits the addresses to the maximum number allowed by the message and randomizes the chosen addresses when there are too many. It returns the addresses that were actually sent and no message will be sent if there are no entries in the provided addresses slice.

This function is safe for concurrent access.

func (*Peer) PushGetBlocksMsg

func (p *Peer) PushGetBlocksMsg(sgs *blockdag.GraphState, blocks []*hash.Hash) error

PushGetBlocksMsg sends a getblocks message for the provided block locator and stop hash. It will ignore back-to-back duplicate requests.

This function is safe for concurrent access.

func (*Peer) PushGetHeadersMsg

func (p *Peer) PushGetHeadersMsg(sgs *blockdag.GraphState, blocks []*hash.Hash) error

PushGetHeadersMsg sends a getblocks message

This function is safe for concurrent access.

func (*Peer) PushGraphStateMsg

func (p *Peer) PushGraphStateMsg(gs *blockdag.GraphState) error

func (*Peer) PushRejectMsg

func (p *Peer) PushRejectMsg(command string, code message.RejectCode, reason string, h *hash.Hash, wait bool)

PushRejectMsg sends a reject message for the provided command, reject code, reject reason, and hash. The hash will only be used when the command is a tx or block and should be nil in other cases. The wait parameter will cause the function to block until the reject message has actually been sent.

This function is safe for concurrent access.

func (*Peer) PushSyncDAGMsg

func (p *Peer) PushSyncDAGMsg(sgs *blockdag.GraphState, mainLocator []*hash.Hash) error

func (*Peer) QueueInventory

func (p *Peer) QueueInventory(invVect *message.InvVect)

QueueInventory adds the passed inventory to the inventory send queue which might not be sent right away, rather it is trickled to the peer in batches. Inventory that the peer is already known to have is ignored.

This function is safe for concurrent access.

func (*Peer) QueueInventoryImmediate

func (p *Peer) QueueInventoryImmediate(invVect *message.InvVect, gs *blockdag.GraphState)

QueueInventoryImmediate adds the passed inventory to the send queue to be sent immediately. This should typically only be used for inventory that is time sensitive such as new tip blocks or votes. Normal inventory should be announced via QueueInventory which instead trickles it to the peer in batches. Inventory that the peer is already known to have is ignored.

This function is safe for concurrent access.

func (*Peer) QueueMessage

func (p *Peer) QueueMessage(msg message.Message, doneChan chan<- struct{})

QueueMessage adds the passed wire message to the peer send queue.

This function is safe for concurrent access.

func (*Peer) Services

func (p *Peer) Services() protocol.ServiceFlag

Services returns the services flag of the remote peer.

This function is safe for concurrent access.

func (*Peer) StatsSnapshot

func (p *Peer) StatsSnapshot() *StatsSnap

StatsSnapshot returns a snapshot of the current peer flags and statistics.

This function is safe for concurrent access.

func (*Peer) String

func (p *Peer) String() string

func (*Peer) UUID

func (p *Peer) UUID() uuid.UUID

UUID returns the peer uuid.

This function is safe for concurrent access.

func (*Peer) UpdateLastAnnouncedBlock

func (p *Peer) UpdateLastAnnouncedBlock(blkHash *hash.Hash)

UpdateLastAnnouncedBlock updates meta-data about the last block hash this peer is known to have announced.

This function is safe for concurrent access.

func (*Peer) UpdateLastGS

func (p *Peer) UpdateLastGS(newGS *blockdag.GraphState)

UpdateLastGS updates the last known graph state for the peer.

This function is safe for concurrent access.

func (*Peer) UserAgent

func (p *Peer) UserAgent() string

UserAgent returns the user agent of the remote peer.

This function is safe for concurrent access.

func (*Peer) VerAckReceived

func (p *Peer) VerAckReceived() bool

VerAckReceived returns whether or not a verack message was received by the peer.

This function is safe for concurrent access.

func (*Peer) VersionKnown

func (p *Peer) VersionKnown() bool

VersionKnown returns the whether or not the version of a peer is known locally.

This function is safe for concurrent access.

func (*Peer) WaitForDisconnect

func (p *Peer) WaitForDisconnect()

WaitForDisconnect waits until the peer has completely disconnected and all resources are cleaned up. This will happen if either the local or remote side has been disconnected or the peer is forcibly disconnected via Disconnect.

func (*Peer) WantsHeaders

func (p *Peer) WantsHeaders() bool

WantsHeaders returns if the peer wants header messages instead of inventory vectors for blocks.

This function is safe for concurrent access.

type PrevGet

type PrevGet struct {
	sync.Mutex
	GS      *blockdag.GraphState
	Locator []*hash.Hash
	Point   *hash.Hash
	Blocks  *blockdag.HashSet
}

func (*PrevGet) CheckBlocks

func (pg *PrevGet) CheckBlocks(p *Peer, gs *blockdag.GraphState, blocks []*hash.Hash) (bool, *blockdag.HashSet)

func (*PrevGet) Clean

func (pg *PrevGet) Clean()

func (*PrevGet) Init

func (pg *PrevGet) Init(p *Peer)

func (*PrevGet) UpdateBlocks

func (pg *PrevGet) UpdateBlocks(blocks []*hash.Hash)

func (*PrevGet) UpdateGS

func (pg *PrevGet) UpdateGS(gs *blockdag.GraphState, locator []*hash.Hash)

func (*PrevGet) UpdatePoint

func (pg *PrevGet) UpdatePoint(point *hash.Hash)

type ServerPeer

type ServerPeer struct {
	// The following chans are used to sync blockmanager and server.
	*Peer
	TxProcessed     chan struct{}
	BlockProcessed  chan connmgr.BanScore
	RequestedBlocks map[hash.Hash]struct{}
	RequestedTxns   map[hash.Hash]struct{}
	RequestQueue    []*message.InvVect
	SyncCandidate   bool
}

ServerPeer extends the peer to maintain state shared by the p2p server and the blockmanager.

type StatsSnap

type StatsSnap struct {
	UUID           uuid.UUID
	ID             int32
	Addr           string
	Services       protocol.ServiceFlag
	LastSend       time.Time
	LastRecv       time.Time
	BytesSent      uint64
	BytesRecv      uint64
	ConnTime       time.Time
	TimeOffset     int64
	Version        uint32
	UserAgent      string
	Inbound        bool
	LastPingNonce  uint64
	LastPingTime   time.Time
	LastPingMicros int64
	GraphState     *blockdag.GraphState
}

StatsSnap is a snapshot of peer stats at a point in time.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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