tendermint

package
v0.0.0-...-0244ac9 Latest Latest
Warning

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

Go to latest
Published: Mar 23, 2022 License: BlueOak-1.0.0 Imports: 35 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// MetricsSubsystem is a subsystem shared by all metrics exposed by this
	// package.
	MetricsSubsystem = "p2p"
)

Variables

This section is empty.

Functions

func DialPeers

func DialPeers(peers []string, r *SeedNodeReactor) []error

DialPeers Dials a peer and retrieve the resulting seed list

func NewBaseReactor

func NewBaseReactor(name string, impl Reactor) *p2p.BaseReactor

Types

type AddrBook

type AddrBook interface {
	AddAddress(addr *p2p.NetAddress, src *p2p.NetAddress) error
	AddPrivateIDs([]string)
	AddOurAddress(*p2p.NetAddress)
	OurAddress(*p2p.NetAddress) bool
	MarkGood(id p2p.ID)
	RemoveAddress(*p2p.NetAddress)
	HasAddress(*p2p.NetAddress) bool
	Save()
}

An AddrBook represents an address book from the pex package, which is used to store peer addresses.

type Channel

type Channel struct {
	Logger log.Logger
	// contains filtered or unexported fields
}

func (*Channel) SetLogger

func (ch *Channel) SetLogger(l log.Logger)

type ChannelDescriptor

type ChannelDescriptor struct {
	ID                  byte
	Priority            int
	SendQueueCapacity   int
	RecvBufferCapacity  int
	RecvMessageCapacity int
}

func (ChannelDescriptor) FillDefaults

func (chDesc ChannelDescriptor) FillDefaults() (filled ChannelDescriptor)

type ChannelStatus

type ChannelStatus struct {
	ID                byte
	SendQueueCapacity int
	SendQueueSize     int
	Priority          int
	RecentlySent      int64
}

type ConnFilterFunc

type ConnFilterFunc func(p2p.ConnSet, net.Conn, []net.IP) error

ConnFilterFunc to be implemented by filter hooks after a new connection has been established. The set of exisiting connections is passed along together with all resolved IPs for the new connection.

type ConnectionStatus

type ConnectionStatus struct {
	Duration    time.Duration
	SendMonitor flow.Status
	RecvMonitor flow.Status
	Channels    []ChannelStatus
}

type IPResolver

type IPResolver interface {
	LookupIPAddr(context.Context, string) ([]net.IPAddr, error)
}

IPResolver is a behaviour subset of net.Resolver.

type IPeerSet

type IPeerSet interface {
	Has(key p2p.ID) bool
	HasIP(ip net.IP) bool
	Get(key p2p.ID) Peer
	List() []Peer
	Size() int
}

IPeerSet has a (immutable) subset of the methods of PeerSet.

type MConnConfig

type MConnConfig struct {
	SendRate int64 `mapstructure:"send_rate"`
	RecvRate int64 `mapstructure:"recv_rate"`

	// Maximum payload size
	MaxPacketMsgPayloadSize int `mapstructure:"max_packet_msg_payload_size"`

	// Interval to flush writes (throttled)
	FlushThrottle time.Duration `mapstructure:"flush_throttle"`

	// Interval to send pings
	PingInterval time.Duration `mapstructure:"ping_interval"`

	// Maximum wait time for pongs
	PongTimeout time.Duration `mapstructure:"pong_timeout"`
}

MConnConfig is a MConnection configuration.

func DefaultMConnConfig

func DefaultMConnConfig() MConnConfig

DefaultMConnConfig returns the default config.

func NewMConnConfig

func NewMConnConfig(cfg *config.P2PConfig) MConnConfig

NewMConnConfig returns an MConnConfig with fields updated from the P2PConfig.

type MConnection

type MConnection struct {
	service.BaseService
	// contains filtered or unexported fields
}

MConnection Each peer has one `MConnection` (multiplex connection) instance.

__multiplex__ *noun* a system or signal involving simultaneous transmission of several messages along a single channel of communication.

Each `MConnection` handles message transmission on multiple abstract communication `Channel`s. Each channel has a globally unique byte id. The byte id and the relative priorities of each `Channel` are configured upon initialization of the connection.

There are two methods for sending messages:

func (m MConnection) Send(chID byte, msgBytes []byte) bool {}
func (m MConnection) TrySend(chID byte, msgBytes []byte}) bool {}

`Send(chID, msgBytes)` is a blocking call that waits until `msg` is successfully queued for the channel with the given id byte `chID`, or until the request times out. The message `msg` is serialized using Protobuf.

`TrySend(chID, msgBytes)` is a nonblocking call that returns false if the channel's queue is full.

Inbound message bytes are handled with an onReceive callback function.

func NewMConnection

func NewMConnection(
	conn net.Conn,
	chDescs []*ChannelDescriptor,
	onReceive receiveCbFunc,
	onError errorCbFunc,
) *MConnection

NewMConnection wraps net.Conn and creates multiplex connection

func NewMConnectionWithConfig

func NewMConnectionWithConfig(
	conn net.Conn,
	chDescs []*ChannelDescriptor,
	onReceive receiveCbFunc,
	onError errorCbFunc,
	config MConnConfig,
) *MConnection

NewMConnectionWithConfig wraps net.Conn and creates multiplex connection with a config

func (*MConnection) CanSend

func (c *MConnection) CanSend(chID byte) bool

CanSend returns true if you can send more data onto the chID, false otherwise. Use only as a heuristic.

func (*MConnection) FlushStop

func (c *MConnection) FlushStop()

FlushStop replicates the logic of OnStop. It additionally ensures that all successful .Send() calls will get flushed before closing the connection.

func (*MConnection) OnStart

func (c *MConnection) OnStart() error

OnStart implements BaseService

func (*MConnection) OnStop

func (c *MConnection) OnStop()

OnStop implements BaseService

func (*MConnection) Send

func (c *MConnection) Send(chID byte, msgBytes []byte) bool

Queues a message to be sent to channel.

func (*MConnection) SetLogger

func (c *MConnection) SetLogger(l log.Logger)

func (*MConnection) Status

func (c *MConnection) Status() ConnectionStatus

func (*MConnection) String

func (c *MConnection) String() string

func (*MConnection) TrySend

func (c *MConnection) TrySend(chID byte, msgBytes []byte) bool

Queues a message to be sent to channel. Nonblocking, returns true if successful.

type Metrics

type Metrics struct {
	// Number of peers received from a given seed.
	SeedReceivePeers metrics.Gauge
}

Metrics contains metrics exposed by this package.

func NopMetrics

func NopMetrics() *Metrics

NopMetrics returns no-op Metrics.

func PrometheusMetrics

func PrometheusMetrics(namespace string) *Metrics

PrometheusMetrics returns Metrics build using Prometheus client library. Optionally, labels can be provided along with their values ("foo", "fooValue").

type MultiplexTransport

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

MultiplexTransport accepts and dials tcp connections and upgrades them to multiplexed peers.

func NewMultiplexTransport

func NewMultiplexTransport(
	nodeInfo p2p.NodeInfo,
	nodeKey p2p.NodeKey,
	mConfig MConnConfig,
) *MultiplexTransport

NewMultiplexTransport returns a tcp connected multiplexed peer.

func (*MultiplexTransport) Accept

func (mt *MultiplexTransport) Accept(cfg peerConfig) (Peer, error)

Accept implements Transport.

func (*MultiplexTransport) AddChannel

func (mt *MultiplexTransport) AddChannel(chID byte)

AddChannel registers a channel to nodeInfo. NOTE: NodeInfo must be of type DefaultNodeInfo else channels won't be updated This is a bit messy at the moment but is cleaned up in the following version when NodeInfo changes from an interface to a concrete type

func (*MultiplexTransport) Cleanup

func (mt *MultiplexTransport) Cleanup(p Peer)

Cleanup removes the given address from the connections set and closes the connection.

func (*MultiplexTransport) Close

func (mt *MultiplexTransport) Close() error

Close implements transportLifecycle.

func (*MultiplexTransport) Dial

func (mt *MultiplexTransport) Dial(
	addr p2p.NetAddress,
	cfg peerConfig,
) (Peer, error)

Dial implements Transport.

func (*MultiplexTransport) Listen

func (mt *MultiplexTransport) Listen(addr p2p.NetAddress) error

Listen implements transportLifecycle.

func (*MultiplexTransport) NetAddress

func (mt *MultiplexTransport) NetAddress() p2p.NetAddress

NetAddress implements Transport.

type MultiplexTransportOption

type MultiplexTransportOption func(*MultiplexTransport)

MultiplexTransportOption sets an optional parameter on the MultiplexTransport.

type Peer

type Peer interface {
	service.Service
	FlushStop()

	ID() p2p.ID           // peer's cryptographic ID
	RemoteIP() net.IP     // remote IP of the connection
	RemoteAddr() net.Addr // remote address of the connection

	IsOutbound() bool   // did we dial the peer
	IsPersistent() bool // do we redial this peer when we disconnect

	CloseConn() error // close original connection

	NodeInfo() p2p.NodeInfo // peer's info
	Status() ConnectionStatus
	SocketAddr() *p2p.NetAddress // actual address of the socket

	Send(byte, []byte) bool
	TrySend(byte, []byte) bool

	Set(string, interface{})
	Get(string) interface{}

	// GetMetrics -------------- Additional attributes for the metrics -----------//
	GetMetrics() *Metrics
}

Peer is an interface representing a peer connected on a reactor.

type PeerFilterFunc

type PeerFilterFunc func(IPeerSet, Peer) error

PeerFilterFunc to be implemented by filter hooks after a new Peer has been fully setup.

type PeerOption

type PeerOption func(*peer)

type PeerSet

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

PeerSet is a special structure for keeping a table of peers. Iteration over the peers is super fast and thread-safe.

func NewPeerSet

func NewPeerSet() *PeerSet

NewPeerSet creates a new peerSet with a list of initial capacity of 256 items.

func (*PeerSet) Add

func (ps *PeerSet) Add(peer Peer) error

Add adds the peer to the PeerSet. It returns an error carrying the reason, if the peer is already present.

func (*PeerSet) Clear

func (ps *PeerSet) Clear()

func (*PeerSet) Get

func (ps *PeerSet) Get(peerKey p2p.ID) Peer

Get looks up a peer by the provided peerKey. Returns nil if peer is not found.

func (*PeerSet) Has

func (ps *PeerSet) Has(peerKey p2p.ID) bool

Has returns true if the set contains the peer referred to by this peerKey, otherwise false.

func (*PeerSet) HasIP

func (ps *PeerSet) HasIP(peerIP net.IP) bool

HasIP returns true if the set contains the peer referred to by this IP address, otherwise false.

func (*PeerSet) List

func (ps *PeerSet) List() []Peer

List returns the threadsafe list of peers.

func (*PeerSet) Remove

func (ps *PeerSet) Remove(peer Peer) bool

Remove discards peer by its Key, if the peer was previously memoized. Returns true if the peer was removed, and false if it was not found. in the set.

func (*PeerSet) Size

func (ps *PeerSet) Size() int

Size returns the number of unique items in the peerSet.

type Reactor

type Reactor interface {
	service.Service // Start, Stop
	SetSwitch(*Switch)
	GetChannels() []*ChannelDescriptor
	InitPeer(peer Peer) Peer
	AddPeer(peer Peer)
	RemovePeer(peer Peer, reason interface{})
	Receive(chID byte, peer Peer, msgBytes []byte)
}

type SeedNodeReactor

type SeedNodeReactor struct {
	p2p.BaseReactor
	Switch *Switch
}

func NewReactor

func NewReactor() *SeedNodeReactor

NewReactor creates new PEX reactor.

func (*SeedNodeReactor) AddPeer

func (br *SeedNodeReactor) AddPeer(p Peer)

AddPeer normally implements Reactor by adding peer to the address book (if inbound) or by requesting more addresses (if outbound). This version only request addressed

func (*SeedNodeReactor) GetChannels

func (br *SeedNodeReactor) GetChannels() []*ChannelDescriptor

GetChannels implements Reactor

func (*SeedNodeReactor) InitPeer

func (*SeedNodeReactor) InitPeer(peer Peer) Peer

func (*SeedNodeReactor) Receive

func (br *SeedNodeReactor) Receive(chID byte, src Peer, msgBytes []byte)

func (*SeedNodeReactor) RemovePeer

func (*SeedNodeReactor) RemovePeer(Peer, interface{})

func (*SeedNodeReactor) RequestAddrs

func (br *SeedNodeReactor) RequestAddrs(p Peer)

RequestAddrs asks peer for more addresses if we do not already have a request out for this peer.

func (*SeedNodeReactor) SetSwitch

func (br *SeedNodeReactor) SetSwitch(sw *Switch)

type Switch

type Switch struct {
	service.BaseService
	// contains filtered or unexported fields
}

Switch handles peer connections and exposes an API to receive incoming messages on `Reactors`. Each `Reactor` is responsible for handling incoming messages of one or more `Channels`. So while sending outgoing messages is typically performed on the peer, incoming messages are received on the reactor.

func NewSwitch

func NewSwitch(
	cfg *config.P2PConfig,
	transport Transport,
	options ...SwitchOption,
) *Switch

NewSwitch creates a new Switch with the given config.

func (*Switch) AddPersistentPeers

func (sw *Switch) AddPersistentPeers(addrs []string) error

AddPersistentPeers allows you to set persistent peers. It ignores ErrNetAddressLookup. However, if there are other errors, first encounter is returned.

func (*Switch) AddPrivatePeerIDs

func (sw *Switch) AddPrivatePeerIDs(ids []string) error

func (*Switch) AddReactor

func (sw *Switch) AddReactor(name string, reactor Reactor) Reactor

AddReactor adds the given reactor to the switch. NOTE: Not goroutine safe.

func (*Switch) AddUnconditionalPeerIDs

func (sw *Switch) AddUnconditionalPeerIDs(ids []string) error

func (*Switch) Broadcast

func (sw *Switch) Broadcast(chID byte, msgBytes []byte) chan bool

Broadcast runs a go routine for each attempted send, which will block trying to send for defaultSendTimeoutSeconds. Returns a channel which receives success values for each attempted send (false if times out). Channel will be closed once msg bytes are sent to all peers (or time out).

NOTE: Broadcast uses goroutines, so order of broadcast may not be preserved.

func (*Switch) DialPeerWithAddress

func (sw *Switch) DialPeerWithAddress(addr *p2p.NetAddress) error

DialPeerWithAddress dials the given peer and runs sw.addPeer if it connects and authenticates successfully. If we're currently dialing this address or it belongs to an existing peer, ErrCurrentlyDialingOrExistingAddress is returned.

func (*Switch) GetPersistentPeers

func (sw *Switch) GetPersistentPeers() *PeerSet

func (*Switch) IsDialingOrExistingAddress

func (sw *Switch) IsDialingOrExistingAddress(addr *p2p.NetAddress) bool

IsDialingOrExistingAddress returns true if switch has a peer with the given address or dialing it at the moment.

func (*Switch) IsPeerPersistent

func (sw *Switch) IsPeerPersistent(na *p2p.NetAddress) bool

func (*Switch) IsPeerUnconditional

func (sw *Switch) IsPeerUnconditional(id p2p.ID) bool

func (*Switch) MarkPeerAsGood

func (sw *Switch) MarkPeerAsGood(peer Peer)

MarkPeerAsGood marks the given peer as good when it did something useful like contributed to consensus.

func (*Switch) MaxNumOutboundPeers

func (sw *Switch) MaxNumOutboundPeers() int

MaxNumOutboundPeers returns a maximum number of outbound peers.

func (*Switch) NetAddress

func (sw *Switch) NetAddress() *p2p.NetAddress

NetAddress returns the address the switch is listening on.

func (*Switch) NodeInfo

func (sw *Switch) NodeInfo() p2p.NodeInfo

NodeInfo returns the switch's NodeInfo. NOTE: Not goroutine safe.

func (*Switch) NumPeers

func (sw *Switch) NumPeers() (outbound, inbound, dialing int)

NumPeers returns the count of outbound/inbound and outbound-dialing peers. unconditional peers are not counted here.

func (*Switch) OnStart

func (sw *Switch) OnStart() error

OnStart implements BaseService. It starts all the reactors and peers.

func (*Switch) OnStop

func (sw *Switch) OnStop()

OnStop implements BaseService. It stops all peers and reactors.

func (*Switch) Peers

func (sw *Switch) Peers() IPeerSet

Peers returns the set of peers that are connected to the switch.

func (*Switch) Reactor

func (sw *Switch) Reactor(name string) Reactor

Reactor returns the reactor with the given name. NOTE: Not goroutine safe.

func (*Switch) Reactors

func (sw *Switch) Reactors() map[string]Reactor

Reactors returns a map of reactors registered on the switch. NOTE: Not goroutine safe.

func (*Switch) RemoveReactor

func (sw *Switch) RemoveReactor(name string, reactor Reactor)

RemoveReactor removes the given Reactor from the Switch. NOTE: Not goroutine safe.

func (*Switch) SetAddrBook

func (sw *Switch) SetAddrBook(addrBook AddrBook)

SetAddrBook allows to set address book on Switch.

func (*Switch) SetNodeInfo

func (sw *Switch) SetNodeInfo(nodeInfo p2p.NodeInfo)

SetNodeInfo sets the switch's NodeInfo for checking compatibility and handshaking with other nodes. NOTE: Not goroutine safe.

func (*Switch) SetNodeKey

func (sw *Switch) SetNodeKey(nodeKey *p2p.NodeKey)

SetNodeKey sets the switch's private key for authenticated encryption. NOTE: Not goroutine safe.

func (*Switch) StopPeerForError

func (sw *Switch) StopPeerForError(peer Peer, reason interface{})

StopPeerForError disconnects from a peer due to external error.

type SwitchOption

type SwitchOption func(*Switch)

SwitchOption sets an optional parameter on the Switch.

type Transport

type Transport interface {
	NetAddress() p2p.NetAddress

	// Accept returns a newly connected Peer.
	Accept(peerConfig) (Peer, error)

	// Dial connects to the Peer for the address.
	Dial(p2p.NetAddress, peerConfig) (Peer, error)

	// Cleanup any resources associated with Peer.
	Cleanup(Peer)
}

Transport emits and connects to Peers. The implementation of Peer is left to the transport. Each transport is also responsible to filter establishing peers specific to its domain.

Jump to

Keyboard shortcuts

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