p2p

package
v0.17.0 Latest Latest
Warning

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

Go to latest
Published: Mar 27, 2018 License: Apache-2.0 Imports: 22 Imported by: 0

README

p2p

The p2p package provides an abstraction around peer-to-peer communication.

Docs:

  • Connection for details on how connections and multiplexing work
  • Peer for details on peer ID, handshakes, and peer exchange
  • Node for details about different types of nodes and how they should work
  • Pex for details on peer discovery and exchange
  • Config for details on some config option

Documentation

Index

Constants

View Source
const (
	// FuzzModeDrop is a mode in which we randomly drop reads/writes, connections or sleep
	FuzzModeDrop = iota
	// FuzzModeDelay is a mode in which we randomly sleep
	FuzzModeDelay
)
View Source
const IDByteLength = 20

IDByteLength is the length of a crypto.Address. Currently only 20. TODO: support other length addresses ?

View Source
const Version = "0.5.0"

Variables

View Source
var (
	ErrSwitchDuplicatePeer = errors.New("Duplicate peer")
	ErrSwitchConnectToSelf = errors.New("Connect to self")
)

Functions

func AddPeerToSwitch added in v0.16.0

func AddPeerToSwitch(sw *Switch, peer Peer)

func Connect2Switches

func Connect2Switches(switches []*Switch, i, j int)

Connect2Switches will connect switches i and j via net.Pipe(). Blocks until a connection is established. NOTE: caller ensures i and j are within bounds.

func CreateRandomPeer added in v0.16.0

func CreateRandomPeer(outbound bool) *peer

func FuzzConn

func FuzzConn(conn net.Conn) net.Conn

FuzzConn creates a new FuzzedConnection. Fuzzing starts immediately.

func FuzzConnAfter

func FuzzConnAfter(conn net.Conn, d time.Duration) net.Conn

FuzzConnAfter creates a new FuzzedConnection. Fuzzing starts when the duration elapses.

func FuzzConnAfterFromConfig

func FuzzConnAfterFromConfig(conn net.Conn, d time.Duration, config *FuzzConnConfig) net.Conn

FuzzConnAfterFromConfig creates a new FuzzedConnection from a config. Fuzzing starts when the duration elapses.

func FuzzConnFromConfig

func FuzzConnFromConfig(conn net.Conn, config *FuzzConnConfig) net.Conn

FuzzConnFromConfig creates a new FuzzedConnection from a config. Fuzzing starts immediately.

func IDAddressString added in v0.16.0

func IDAddressString(id ID, hostPort string) string

IDAddressString returns id@hostPort.

func MakePoWTarget added in v0.16.0

func MakePoWTarget(difficulty, targetBits uint) []byte

MakePoWTarget returns the big-endian encoding of 2^(targetBits - difficulty) - 1. It can be used as a Proof of Work target. NOTE: targetBits must be a multiple of 8 and difficulty must be less than targetBits.

func MaxNodeInfoSize added in v0.16.0

func MaxNodeInfoSize() int

func StartSwitches

func StartSwitches(switches []*Switch) error

StartSwitches calls sw.Start() for each given switch. It returns the first encountered error.

Types

type AddrBook

type AddrBook interface {
	AddAddress(addr *NetAddress, src *NetAddress) error
	MarkGood(*NetAddress)
	Save()
}

type BaseReactor

type BaseReactor struct {
	cmn.BaseService // Provides Start, Stop, .Quit
	Switch          *Switch
}

func NewBaseReactor

func NewBaseReactor(name string, impl Reactor) *BaseReactor

func (*BaseReactor) AddPeer

func (_ *BaseReactor) AddPeer(peer Peer)

func (*BaseReactor) GetChannels

func (_ *BaseReactor) GetChannels() []*conn.ChannelDescriptor

func (*BaseReactor) Receive

func (_ *BaseReactor) Receive(chID byte, peer Peer, msgBytes []byte)

func (*BaseReactor) RemovePeer

func (_ *BaseReactor) RemovePeer(peer Peer, reason interface{})

func (*BaseReactor) SetSwitch

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

type ChannelDescriptor

type ChannelDescriptor = conn.ChannelDescriptor

type ConnectionStatus

type ConnectionStatus = conn.ConnectionStatus

type DefaultListener

type DefaultListener struct {
	cmn.BaseService
	// contains filtered or unexported fields
}

Implements Listener

func (*DefaultListener) Connections

func (l *DefaultListener) Connections() <-chan net.Conn

A channel of inbound connections. It gets closed when the listener closes.

func (*DefaultListener) ExternalAddress

func (l *DefaultListener) ExternalAddress() *NetAddress

func (*DefaultListener) InternalAddress

func (l *DefaultListener) InternalAddress() *NetAddress

func (*DefaultListener) NetListener

func (l *DefaultListener) NetListener() net.Listener

NOTE: The returned listener is already Accept()'ing. So it's not suitable to pass into http.Serve().

func (*DefaultListener) OnStart

func (l *DefaultListener) OnStart() error

func (*DefaultListener) OnStop

func (l *DefaultListener) OnStop()

func (*DefaultListener) String

func (l *DefaultListener) String() string

type ErrSwitchAuthenticationFailure added in v0.16.0

type ErrSwitchAuthenticationFailure struct {
	Dialed *NetAddress
	Got    ID
}

func (ErrSwitchAuthenticationFailure) Error added in v0.16.0

type FuzzConnConfig

type FuzzConnConfig struct {
	Mode         int
	MaxDelay     time.Duration
	ProbDropRW   float64
	ProbDropConn float64
	ProbSleep    float64
}

FuzzConnConfig is a FuzzedConnection configuration.

func DefaultFuzzConnConfig

func DefaultFuzzConnConfig() *FuzzConnConfig

DefaultFuzzConnConfig returns the default config.

type FuzzedConnection

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

FuzzedConnection wraps any net.Conn and depending on the mode either delays reads/writes or randomly drops reads/writes/connections.

func (*FuzzedConnection) Close

func (fc *FuzzedConnection) Close() error

Close implements net.Conn.

func (*FuzzedConnection) Config

func (fc *FuzzedConnection) Config() *FuzzConnConfig

Config returns the connection's config.

func (*FuzzedConnection) LocalAddr

func (fc *FuzzedConnection) LocalAddr() net.Addr

LocalAddr implements net.Conn.

func (*FuzzedConnection) Read

func (fc *FuzzedConnection) Read(data []byte) (n int, err error)

Read implements net.Conn.

func (*FuzzedConnection) RemoteAddr

func (fc *FuzzedConnection) RemoteAddr() net.Addr

RemoteAddr implements net.Conn.

func (*FuzzedConnection) SetDeadline

func (fc *FuzzedConnection) SetDeadline(t time.Time) error

SetDeadline implements net.Conn.

func (*FuzzedConnection) SetReadDeadline

func (fc *FuzzedConnection) SetReadDeadline(t time.Time) error

SetReadDeadline implements net.Conn.

func (*FuzzedConnection) SetWriteDeadline

func (fc *FuzzedConnection) SetWriteDeadline(t time.Time) error

SetWriteDeadline implements net.Conn.

func (*FuzzedConnection) Write

func (fc *FuzzedConnection) Write(data []byte) (n int, err error)

Write implements net.Conn.

type ID added in v0.16.0

type ID string

ID is a hex-encoded crypto.Address

func PubKeyToID added in v0.16.0

func PubKeyToID(pubKey crypto.PubKey) ID

PubKeyToID returns the ID corresponding to the given PubKey. It's the hex-encoding of the pubKey.Address().

type IPeerSet

type IPeerSet interface {
	Has(key ID) bool
	Get(key ID) Peer
	List() []Peer
	Size() int
}

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

type Listener

type Listener interface {
	Connections() <-chan net.Conn
	InternalAddress() *NetAddress
	ExternalAddress() *NetAddress
	String() string
	Stop() error
}

func NewDefaultListener

func NewDefaultListener(protocol string, lAddr string, skipUPNP bool, logger log.Logger) Listener

skipUPNP: If true, does not try getUPNPExternalAddress()

type NetAddress

type NetAddress struct {
	ID   ID
	IP   net.IP
	Port uint16
	// contains filtered or unexported fields
}

NetAddress defines information about a peer on the network including its ID, IP address, and port.

func CreateRoutableAddr added in v0.16.0

func CreateRoutableAddr() (addr string, netAddr *NetAddress)

func NewNetAddress

func NewNetAddress(id ID, addr net.Addr) *NetAddress

NewNetAddress returns a new NetAddress using the provided TCP address. When testing, other net.Addr (except TCP) will result in using 0.0.0.0:0. When normal run, other net.Addr (except TCP) will panic. TODO: socks proxies?

func NewNetAddressIPPort

func NewNetAddressIPPort(ip net.IP, port uint16) *NetAddress

NewNetAddressIPPort returns a new NetAddress using the provided IP and port number.

func NewNetAddressString

func NewNetAddressString(addr string) (*NetAddress, error)

NewNetAddressString returns a new NetAddress using the provided address in the form of "ID@IP:Port", where the ID is optional. Also resolves the host if host is not an IP.

func NewNetAddressStrings

func NewNetAddressStrings(addrs []string) ([]*NetAddress, []error)

NewNetAddressStrings returns an array of NetAddress'es build using the provided strings.

func (*NetAddress) Dial

func (na *NetAddress) Dial() (net.Conn, error)

Dial calls net.Dial on the address.

func (*NetAddress) DialString added in v0.16.0

func (na *NetAddress) DialString() string

func (*NetAddress) DialTimeout

func (na *NetAddress) DialTimeout(timeout time.Duration) (net.Conn, error)

DialTimeout calls net.DialTimeout on the address.

func (*NetAddress) Equals

func (na *NetAddress) Equals(other interface{}) bool

Equals reports whether na and other are the same addresses, including their ID, IP, and Port.

func (*NetAddress) Local

func (na *NetAddress) Local() bool

Local returns true if it is a local address.

func (*NetAddress) RFC1918

func (na *NetAddress) RFC1918() bool

func (*NetAddress) RFC3849

func (na *NetAddress) RFC3849() bool

func (*NetAddress) RFC3927

func (na *NetAddress) RFC3927() bool

func (*NetAddress) RFC3964

func (na *NetAddress) RFC3964() bool

func (*NetAddress) RFC4193

func (na *NetAddress) RFC4193() bool

func (*NetAddress) RFC4380

func (na *NetAddress) RFC4380() bool

func (*NetAddress) RFC4843

func (na *NetAddress) RFC4843() bool

func (*NetAddress) RFC4862

func (na *NetAddress) RFC4862() bool

func (*NetAddress) RFC6052

func (na *NetAddress) RFC6052() bool

func (*NetAddress) RFC6145

func (na *NetAddress) RFC6145() bool

func (*NetAddress) ReachabilityTo

func (na *NetAddress) ReachabilityTo(o *NetAddress) int

ReachabilityTo checks whenever o can be reached from na.

func (*NetAddress) Routable

func (na *NetAddress) Routable() bool

Routable returns true if the address is routable.

func (*NetAddress) Same added in v0.16.0

func (na *NetAddress) Same(other interface{}) bool

Same returns true is na has the same non-empty ID or DialString as other.

func (*NetAddress) String

func (na *NetAddress) String() string

String representation: <ID>@<IP>:<PORT>

func (*NetAddress) Valid

func (na *NetAddress) Valid() bool

For IPv4 these are either a 0 or all bits set address. For IPv6 a zero address or one that matches the RFC3849 documentation address format.

type NodeInfo

type NodeInfo struct {
	// Authenticate
	PubKey     crypto.PubKey `json:"pub_key"`     // authenticated pubkey
	ListenAddr string        `json:"listen_addr"` // accepting incoming

	// Check compatibility
	Network  string `json:"network"`  // network/chain ID
	Version  string `json:"version"`  // major.minor.revision
	Channels []byte `json:"channels"` // channels this node knows about

	// Sanitize
	Moniker string   `json:"moniker"` // arbitrary moniker
	Other   []string `json:"other"`   // other application specific data
}

NodeInfo is the basic node information exchanged between two peers during the Tendermint P2P handshake.

func (NodeInfo) CompatibleWith

func (info NodeInfo) CompatibleWith(other NodeInfo) error

CompatibleWith checks if two NodeInfo are compatible with eachother. CONTRACT: two nodes are compatible if the major/minor versions match and network match and they have at least one channel in common.

func (NodeInfo) ID added in v0.16.0

func (info NodeInfo) ID() ID

func (NodeInfo) NetAddress added in v0.16.0

func (info NodeInfo) NetAddress() *NetAddress

NetAddress returns a NetAddress derived from the NodeInfo - it includes the authenticated peer ID and the self-reported ListenAddr. Note that the ListenAddr is not authenticated and may not match that address actually dialed if its an outbound peer.

func (NodeInfo) String

func (info NodeInfo) String() string

func (NodeInfo) Validate added in v0.16.0

func (info NodeInfo) Validate() error

Validate checks the self-reported NodeInfo is safe. It returns an error if there are too many Channels or any duplicate Channels. TODO: constraints for Moniker/Other? Or is that for the UI ?

type NodeKey added in v0.16.0

type NodeKey struct {
	PrivKey crypto.PrivKey `json:"priv_key"` // our priv key
}

NodeKey is the persistent peer key. It contains the nodes private key for authentication.

func LoadOrGenNodeKey added in v0.16.0

func LoadOrGenNodeKey(filePath string) (*NodeKey, error)

LoadOrGenNodeKey attempts to load the NodeKey from the given filePath. If the file does not exist, it generates and saves a new NodeKey.

func (*NodeKey) ID added in v0.16.0

func (nodeKey *NodeKey) ID() ID

ID returns the peer's canonical ID - the hash of its public key.

func (*NodeKey) PubKey added in v0.16.0

func (nodeKey *NodeKey) PubKey() crypto.PubKey

PubKey returns the peer's PubKey

type Peer

type Peer interface {
	cmn.Service

	ID() ID             // peer's cryptographic ID
	IsOutbound() bool   // did we dial the peer
	IsPersistent() bool // do we redial this peer when we disconnect
	NodeInfo() NodeInfo // peer's info
	Status() tmconn.ConnectionStatus

	Send(byte, interface{}) bool
	TrySend(byte, interface{}) bool

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

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

type PeerConfig

type PeerConfig struct {
	AuthEnc bool `mapstructure:"auth_enc"` // authenticated encryption

	// times are in seconds
	HandshakeTimeout time.Duration `mapstructure:"handshake_timeout"`
	DialTimeout      time.Duration `mapstructure:"dial_timeout"`

	MConfig *tmconn.MConnConfig `mapstructure:"connection"`

	Fuzz       bool            `mapstructure:"fuzz"` // fuzz connection (for testing)
	FuzzConfig *FuzzConnConfig `mapstructure:"fuzz_config"`
}

PeerConfig is a Peer configuration.

func DefaultPeerConfig

func DefaultPeerConfig() *PeerConfig

DefaultPeerConfig returns the default config.

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 ErrSwitchDuplicatePeer if the peer is already present.

func (*PeerSet) Get

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

Get looks up a peer by the provided peerKey.

func (*PeerSet) Has

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

Has returns true iff the PeerSet contains the peer referred to by this peerKey.

func (*PeerSet) List

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

List returns the threadsafe list of peers.

func (*PeerSet) Remove

func (ps *PeerSet) Remove(peer Peer)

Remove discards peer by its Key, if the peer was previously memoized.

func (*PeerSet) Size

func (ps *PeerSet) Size() int

Size returns the number of unique items in the peerSet.

type Reactor

type Reactor interface {
	cmn.Service // Start, Stop

	// SetSwitch allows setting a switch.
	SetSwitch(*Switch)

	// GetChannels returns the list of channel descriptors.
	GetChannels() []*conn.ChannelDescriptor

	// AddPeer is called by the switch when a new peer is added.
	AddPeer(peer Peer)

	// RemovePeer is called by the switch when the peer is stopped (due to error
	// or other reason).
	RemovePeer(peer Peer, reason interface{})

	// Receive is called when msgBytes is received from peer.
	//
	// NOTE reactor can not keep msgBytes around after Receive completes without
	// copying.
	//
	// CONTRACT: msgBytes are not nil.
	Receive(chID byte, peer Peer, msgBytes []byte)
}

type Switch

type Switch struct {
	cmn.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 MakeConnectedSwitches

func MakeConnectedSwitches(cfg *cfg.P2PConfig, n int, initSwitch func(int, *Switch) *Switch, connect func([]*Switch, int, int)) []*Switch

MakeConnectedSwitches returns n switches, connected according to the connect func. If connect==Connect2Switches, the switches will be fully connected. initSwitch defines how the i'th switch should be initialized (ie. with what reactors). NOTE: panics if any switch fails to start.

func MakeSwitch added in v0.16.0

func MakeSwitch(cfg *cfg.P2PConfig, i int, network, version string, initSwitch func(int, *Switch) *Switch) *Switch

func NewSwitch

func NewSwitch(config *cfg.P2PConfig) *Switch

func (*Switch) AddListener

func (sw *Switch) AddListener(l Listener)

AddListener adds the given listener to the switch for listening to incoming peer connections. NOTE: Not goroutine safe.

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

func (sw *Switch) Broadcast(chID byte, msg interface{}) 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 send to all peers.

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

func (*Switch) DialPeerWithAddress

func (sw *Switch) DialPeerWithAddress(addr *NetAddress, persistent bool) error

DialPeerWithAddress dials the given peer and runs sw.addPeer if it connects and authenticates successfully. If `persistent == true`, the switch will always try to reconnect to this peer if the connection ever fails.

func (*Switch) DialPeersAsync added in v0.16.0

func (sw *Switch) DialPeersAsync(addrBook AddrBook, peers []string, persistent bool) error

DialPeersAsync dials a list of peers asynchronously in random order (optionally, making them persistent).

func (*Switch) FilterConnByAddr

func (sw *Switch) FilterConnByAddr(addr net.Addr) error

FilterConnByAddr returns an error if connecting to the given address is forbidden.

func (*Switch) FilterConnByID added in v0.17.0

func (sw *Switch) FilterConnByID(id ID) error

FilterConnByID returns an error if connecting to the given peer ID is forbidden.

func (*Switch) IsDialing

func (sw *Switch) IsDialing(id ID) bool

IsDialing returns true if the switch is currently dialing the given ID.

func (*Switch) IsListening

func (sw *Switch) IsListening() bool

IsListening returns true if the switch has at least one listener. NOTE: Not goroutine safe.

func (*Switch) Listeners

func (sw *Switch) Listeners() []Listener

Listeners returns the list of listeners the switch listens on. NOTE: Not goroutine safe.

func (*Switch) MarkPeerAsGood added in v0.17.0

func (sw *Switch) MarkPeerAsGood(peer Peer)

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

func (*Switch) NodeInfo

func (sw *Switch) NodeInfo() 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.

func (*Switch) OnStart

func (sw *Switch) OnStart() error

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

func (*Switch) OnStop

func (sw *Switch) OnStop()

OnStop implements BaseService. It stops all listeners, 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) SetAddrBook added in v0.17.0

func (sw *Switch) SetAddrBook(addrBook AddrBook)

SetAddrBook allows to set address book on Switch.

func (*Switch) SetAddrFilter

func (sw *Switch) SetAddrFilter(f func(net.Addr) error)

SetAddrFilter sets the function for filtering connections by address.

func (*Switch) SetIDFilter added in v0.17.0

func (sw *Switch) SetIDFilter(f func(ID) error)

SetIDFilter sets the function for filtering connections by peer ID.

func (*Switch) SetNodeInfo

func (sw *Switch) SetNodeInfo(nodeInfo NodeInfo)

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

func (*Switch) SetNodeKey added in v0.16.0

func (sw *Switch) SetNodeKey(nodeKey *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. If the peer is persistent, it will attempt to reconnect. TODO: make record depending on reason.

func (*Switch) StopPeerGracefully

func (sw *Switch) StopPeerGracefully(peer Peer)

StopPeerGracefully disconnects from a peer gracefully. TODO: handle graceful disconnects.

Directories

Path Synopsis
Uses nacl's secret_box to encrypt a net.Conn.
Uses nacl's secret_box to encrypt a net.Conn.
Taken from taipei-torrent.
Taken from taipei-torrent.

Jump to

Keyboard shortcuts

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