Categorygithub.com/tendermint/go-p2p
modulepackage
0.0.0-20170421170644-e8f33a478467
Repository: https://github.com/tendermint/go-p2p.git
Documentation: pkg.go.dev

# README

tendermint/go-p2p

CircleCI

tendermint/go-p2p provides an abstraction around peer-to-peer communication.

Peer/MConnection/Channel

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 Channels. 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, msg interface{}) bool {}
func (m MConnection) TrySend(chID byte, msg interface{}) bool {}

Send(chID, msg) is a blocking call that waits until msg is successfully queued for the channel with the given id byte chID. The message msg is serialized using the tendermint/wire submodule's WriteBinary() reflection routine.

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

Send() and TrySend() are also exposed for each Peer.

Switch/Reactor

The 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.

// Declare a MyReactor reactor that handles messages on MyChannelID.
type MyReactor struct{}

func (reactor MyReactor) GetChannels() []*ChannelDescriptor {
    return []*ChannelDescriptor{ChannelDescriptor{ID:MyChannelID, Priority: 1}}
}

func (reactor MyReactor) Receive(chID byte, peer *Peer, msgBytes []byte) {
    r, n, err := bytes.NewBuffer(msgBytes), new(int64), new(error)
    msgString := ReadString(r, n, err)
    fmt.Println(msgString)
}

// Other Reactor methods omitted for brevity
...

switch := NewSwitch([]Reactor{MyReactor{}})

...

// Send a random message to all outbound connections
for _, peer := range switch.Peers().List() {
    if peer.IsOutbound() {
        peer.Send(MyChannelID, "Here's a random message")
    }
}

PexReactor/AddrBook

A PEXReactor reactor implementation is provided to automate peer discovery.

book := p2p.NewAddrBook(addrBookFilePath)
pexReactor := p2p.NewPEXReactor(book)
...
switch := NewSwitch([]Reactor{pexReactor, myReactor, ...})

# Packages

Taken from taipei-torrent Just enough UPnP to be able to forward ports */.

# Functions

TODO Test.
TODO Test.
Will connect switches i and j via net.Pipe() Blocks until a conection is established.
DecodeMessage implements interface registered above.
DefaultFuzzConnConfig returns the default config.
DefaultMConnConfig returns the default config.
DefaultPeerConfig returns the default config.
FuzzConn creates a new FuzzedConnection.
FuzzConnAfter creates a new FuzzedConnection.
FuzzConnAfterFromConfig creates a new FuzzedConnection from a config.
FuzzConnFromConfig creates a new FuzzedConnection from a config.
Returns n switches, connected according to the connect func.
Performs handshake and returns a new authenticated SecretConnection.
NewAddrBook creates a new address book.
No description provided by the author
skipUPNP: If true, does not try getUPNPExternalAddress().
NewMConnection wraps net.Conn and creates multiplex connection.
NewMConnectionWithConfig wraps net.Conn and creates multiplex connection with a config.
NewNetAddress returns a new NetAddress using the provided TCP address.
NewNetAddressIPPort returns a new NetAddress using the provided IP and port number.
NewNetAddressString returns a new NetAddress using the provided address in the form of "IP:Port".
NewNetAddressStrings returns an array of NetAddress'es build using the provided strings.
No description provided by the author
NewPEXReactor creates new PEX reactor.
No description provided by the author
No description provided by the author

# Constants

FuzzModeDelay is a mode in which we randomly sleep.
FuzzModeDrop is a mode in which we randomly drop reads/writes, connections or sleep.
PexChannel is a channel for PEX messages.
No description provided by the author

# Variables

No description provided by the author
No description provided by the author
No description provided by the author

# Structs

AddrBook - concurrency safe peer address manager.
No description provided by the author
TODO: lowercase.
No description provided by the author
No description provided by the author
No description provided by the author
Implements Listener.
FuzzConnConfig is a FuzzedConnection configuration.
FuzzedConnection wraps any net.Conn and depending on the mode either delays reads/writes or randomly drops reads/writes/connections.
MConnConfig is a MConnection configuration.
Each peer has one `MConnection` (multiplex connection) instance.
NetAddress defines information about a peer on the network including its IP address, and port.
No description provided by the author
Peer could be marked as persistent, in which case you can use Redial function to reconnect.
PeerConfig is a Peer configuration.
PeerSet is a special structure for keeping a table of peers.
PEXReactor handles PEX (peer exchange) and ensures that an adequate number of peers are connected to the switch.
Implements net.Conn.
The `Switch` handles peer connections and exposes an API to receive incoming messages on `Reactors`.
No description provided by the author
No description provided by the author

# Interfaces

IPeerSet has a (immutable) subset of the methods of PeerSet.
No description provided by the author
PexMessage is a primary type for PEX messages.
No description provided by the author