tendermint/p2p
Jae Kwon fa956596dc Merge remote-tracking branch 'origin/config' into develop 2015-10-11 18:18:53 -07:00
..
upnp BaseService and BaseReactor refactor and unification 2015-07-20 14:52:24 -07:00
README.md Blockpool better timeouts, download rate observation, quicker switching to consensus; Id -> ID (sorry, this doesn't belong here) 2015-09-11 21:09:00 -07:00
addrbook.go Use rpc/client/ws_client; OnStart() returns error 2015-08-11 11:01:17 -07:00
addrbook_test.go addrbook cleanup 2014-07-10 02:19:50 -07:00
config.go RPCResponse.Result && EventData are registered interfaces; -skip_upnp option 2015-08-11 11:01:18 -07:00
connection.go blockchain parameter && log tweaks 2015-09-11 21:09:27 -07:00
listener.go RPCResponse.Result && EventData are registered interfaces; -skip_upnp option 2015-08-11 11:01:18 -07:00
log.go Package import path change 2015-04-01 17:30:16 -07:00
netaddress.go more config options 2015-09-25 11:58:11 -04:00
peer.go mempool tests 2015-09-29 12:30:46 -04:00
peer_set.go UUID -> PubKeyEd25519 2015-07-15 14:31:03 -07:00
peer_set_test.go various bug fixes 2015-09-11 21:09:01 -07:00
pex_reactor.go Blockpool better timeouts, download rate observation, quicker switching to consensus; Id -> ID (sorry, this doesn't belong here) 2015-09-11 21:09:00 -07:00
secret_connection.go tendermint/binary -> tendermint/wire 2015-07-28 12:18:17 -07:00
secret_connection_test.go crypto byte arrays are fixed length 2015-07-17 17:19:16 -04:00
switch.go precommit nil if locked and no POL 2015-09-15 14:25:50 -04:00
switch_test.go more versioning 2015-09-16 05:08:06 +00:00
util.go cleanup 2014-07-09 14:27:32 -07:00
version.go more versioning 2015-09-16 05:08:06 +00:00

README.md

tendermint/p2p

tendermint/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(config.App.GetString("AddrBookFile"))
pexReactor := p2p.NewPEXReactor(book)
...
switch := NewSwitch([]Reactor{pexReactor, myReactor, ...})