tendermint/p2p
Petabyte Storage ceedd4d968 remove unnecessary plus [ci skip] 2017-10-25 22:28:20 -04:00
..
upnp upnp: keep a link 2017-10-04 17:19:49 -04: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 go-p2p -> tendermint/p2p 2017-04-21 18:19:41 -04:00
addrbook.go Merge branch 'apply-megacheck' into unstable 2017-06-23 21:38:22 -04:00
addrbook_test.go new logging 2017-05-13 10:24:58 +02:00
connection.go mempool: reactor test 2017-09-05 16:25:02 -04:00
connection_test.go new logging 2017-05-13 10:24:58 +02:00
fuzz.go move into p2p package 2017-04-21 18:07:52 -04:00
listener.go rpc/lib: remove dead files, closes #710 2017-10-04 17:45:15 -04:00
listener_test.go new logging 2017-05-13 10:24:58 +02:00
netaddress.go core: apply megacheck vet tool (unused, gosimple, staticcheck) 2017-05-29 23:11:40 -04:00
netaddress_test.go One silly tests passes on osx, fails on linux... comment out so i can develop 2017-05-05 18:48:39 +02:00
peer.go remove unnecessary plus [ci skip] 2017-10-25 22:28:20 -04:00
peer_set.go peer interface 2017-09-15 18:40:59 -04:00
peer_set_test.go peer interface 2017-09-15 18:40:59 -04:00
peer_test.go peer interface 2017-09-15 18:40:59 -04:00
pex_reactor.go blockchain/pool: some comments and small changes 2017-10-23 10:13:46 -04:00
pex_reactor_test.go peer interface 2017-09-15 18:40:59 -04:00
secret_connection.go core: apply megacheck vet tool (unused, gosimple, staticcheck) 2017-05-29 23:11:40 -04:00
secret_connection_test.go p2p: use cmn instead of . 2017-05-04 22:43:55 -04:00
switch.go p2p: sw.AddPeer -> sw.addPeer 2017-09-21 15:47:41 -04:00
switch_test.go p2p: sw.AddPeer -> sw.addPeer 2017-09-21 15:47:41 -04:00
types.go changes as per Bucky's review 2017-05-13 16:22:51 +02:00
util.go move into p2p package 2017-04-21 18:07:52 -04: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.

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