tendermint/p2p
Ethan Buchman 58b4a8395b
Merge pull request #917 from tendermint/trust-metric-cleanup
Trust metric cleanup
2017-12-06 02:11:38 -05:00
..
trust p2p/trust: lock on Copy() 2017-12-01 23:35:17 -05:00
upnp linting: moar fixes 2017-11-27 22:39:11 +00:00
CHANGELOG.md move into p2p package 2017-04-21 18:07:52 -04:00
Dockerfile go-p2p -> tendermint/p2p 2017-04-21 18:19:41 -04:00
README.md p2p: update readme, some minor things 2017-11-07 23:00:49 +00:00
addrbook.go lil fixes 2017-11-27 22:39:12 +00:00
addrbook_test.go minor fixes from review 2017-11-21 15:30:19 +00:00
conn_go110.go p2p: use fake net.Pipe since only >=Go1.10 implements SetDeadline 2017-11-14 22:03:23 -07:00
conn_notgo110.go p2p: netPipe for <Go1.10 in own file with own build tag 2017-11-14 22:23:48 -07:00
connection.go fix for legacy gowire 2017-12-06 01:21:14 -05:00
connection_test.go service#Start, service#Stop signatures were changed 2017-11-29 10:38:58 -06:00
fuzz.go more linting 2017-11-27 22:39:12 +00:00
listener.go service#Start, service#Stop signatures were changed 2017-11-29 10:38:58 -06:00
listener_test.go linting errors: tackle p2p package 2017-11-27 22:39:11 +00:00
netaddress.go core: apply megacheck vet tool (unused, gosimple, staticcheck) 2017-05-29 23:11:40 -04:00
netaddress_test.go fix test using uncommon names 2017-10-28 20:29:11 -07:00
peer.go service#Start, service#Stop signatures were changed 2017-11-29 10:38:58 -06:00
peer_set.go peer interface 2017-09-15 18:40:59 -04:00
peer_set_test.go linting errors: tackle p2p package 2017-11-27 22:39:11 +00:00
peer_test.go service#Start, service#Stop signatures were changed 2017-11-29 10:38:58 -06:00
pex_reactor.go ignore ErrAlreadyStarted when starting addrbook in PEXReactor 2017-11-29 10:53:30 -06:00
pex_reactor_test.go ignore ErrAlreadyStarted when starting addrbook in PEXReactor 2017-11-29 10:53:30 -06:00
secret_connection.go more linting 2017-11-27 22:39:12 +00:00
secret_connection_test.go linting errors: tackle p2p package 2017-11-27 22:39:11 +00:00
switch.go ignore ErrAlreadyStarted when starting addrbook in PEXReactor 2017-11-29 10:53:30 -06:00
switch_test.go service#Start, service#Stop signatures were changed 2017-11-29 10:38:58 -06:00
types.go more linting 2017-11-27 22:39:12 +00:00
util.go more linting 2017-11-27 22:39:12 +00:00
version.go move into p2p package 2017-04-21 18:07:52 -04:00

README.md

tendermint/tendermint/p2p

CircleCI

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

MConnection

MConnection is a multiplex connection:

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.

The MConnection supports three packet types: Ping, Pong, and Msg.

Ping and Pong

The ping and pong messages consist of writing a single byte to the connection; 0x1 and 0x2, respectively

When we haven't received any messages on an MConnection in a time pingTimeout, we send a ping message. When a ping is received on the MConnection, a pong is sent in response.

If a pong is not received in sufficient time, the peer's score should be decremented (TODO).

Msg

Messages in channels are chopped into smaller msgPackets for multiplexing.

type msgPacket struct {
	ChannelID byte
	EOF       byte // 1 means message ends here.
	Bytes     []byte
}

The msgPacket is serialized using go-wire, and prefixed with a 0x3. The received Bytes of a sequential set of packets are appended together until a packet with EOF=1 is received, at which point the complete serialized message is returned for processing by the corresponding channels onReceive function.

Multiplexing

Messages are sent from a single sendRoutine, which loops over a select statement that results in the sending of a ping, a pong, or a batch of data messages. The batch of data messages may include messages from multiple channels. Message bytes are queued for sending in their respective channel, with each channel holding one unsent message at a time. Messages are chosen for a batch one a time from the channel with the lowest ratio of recently sent bytes to channel priority.

Sending Messages

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.

Peer

Each peer has one MConnection instance, and includes other information such as whether the connection was outbound, whether the connection should be recreated if it closes, various identity information about the node, and other higher level thread-safe data used by the reactors.

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, ...})