tendermint/p2p/README.md

144 lines
3.1 KiB
Markdown
Raw Normal View History

2014-09-11 11:17:59 -07:00
# P2P Module
P2P provides an abstraction around peer-to-peer communication.<br/>
Communication happens via Agents that react to messages from peers.<br/>
Each Agent has one or more Channels of communication for each Peer.<br/>
Channels are multiplexed automatically and can be configured.<br/>
A Switch is started upon app start, and handles Peer management.<br/>
A PEXAgent implementation is provided to automate peer discovery.<br/>
## Usage
MempoolAgent started from the following template code.<br/>
Modify the snippet below according to your needs.<br/>
Check out the ConsensusAgent for an example of tracking peer state.<br/>
```golang
package mempool
import (
"bytes"
"fmt"
"io"
"sync/atomic"
. "github.com/tendermint/tendermint/binary"
. "github.com/tendermint/tendermint/blocks"
"github.com/tendermint/tendermint/p2p"
)
var (
MempoolCh = byte(0x30)
)
// MempoolAgent handles mempool tx broadcasting amongst peers.
type MempoolAgent struct {
sw *p2p.Switch
swEvents chan interface{}
quit chan struct{}
started uint32
stopped uint32
}
func NewMempoolAgent(sw *p2p.Switch) *MempoolAgent {
swEvents := make(chan interface{})
sw.AddEventListener("MempoolAgent.swEvents", swEvents)
memA := &MempoolAgent{
sw: sw,
swEvents: swEvents,
quit: make(chan struct{}),
}
return memA
}
func (memA *MempoolAgent) Start() {
if atomic.CompareAndSwapUint32(&memA.started, 0, 1) {
log.Info("Starting MempoolAgent")
go memA.switchEventsRoutine()
go memA.gossipTxRoutine()
}
}
func (memA *MempoolAgent) Stop() {
if atomic.CompareAndSwapUint32(&memA.stopped, 0, 1) {
log.Info("Stopping MempoolAgent")
close(memA.quit)
close(memA.swEvents)
}
}
// Handle peer new/done events
func (memA *MempoolAgent) switchEventsRoutine() {
for {
swEvent, ok := <-memA.swEvents
if !ok {
break
}
switch swEvent.(type) {
case p2p.SwitchEventNewPeer:
// event := swEvent.(p2p.SwitchEventNewPeer)
2014-09-11 22:44:59 -07:00
// NOTE: set up peer state
2014-09-11 11:17:59 -07:00
case p2p.SwitchEventDonePeer:
// event := swEvent.(p2p.SwitchEventDonePeer)
2014-09-11 22:44:59 -07:00
// NOTE: tear down peer state
2014-09-11 11:17:59 -07:00
default:
log.Warning("Unhandled switch event type")
}
}
}
func (memA *MempoolAgent) gossipTxRoutine() {
OUTER_LOOP:
for {
// Receive incoming message on MempoolCh
inMsg, ok := memA.sw.Receive(MempoolCh)
if !ok {
break OUTER_LOOP // Client has stopped
}
_, msg_ := decodeMessage(inMsg.Bytes)
log.Info("gossipMempoolRoutine received %v", msg_)
switch msg_.(type) {
case *TxMessage:
// msg := msg_.(*TxMessage)
2014-09-11 22:44:59 -07:00
// handle msg
2014-09-11 11:17:59 -07:00
default:
// Ignore unknown message
// memA.sw.StopPeerForError(inMsg.MConn.Peer, errInvalidMessage)
}
}
// Cleanup
}
```
2014-06-25 11:58:34 -07:00
## Channels
2014-06-26 20:51:19 -07:00
Each peer connection is multiplexed into channels.
2014-09-10 02:43:16 -07:00
The p2p module comes with a channel implementation used for peer
discovery (called PEX, short for "peer exchange").
2014-06-24 23:49:06 -07:00
2014-06-24 23:58:38 -07:00
<table>
2014-06-25 11:58:34 -07:00
<tr>
<td><b>Channel</b></td>
2014-07-06 16:08:25 -07:00
<td>"PEX"</td>
2014-06-25 11:58:34 -07:00
</tr>
2014-06-24 23:58:38 -07:00
<tr>
2014-06-25 11:58:34 -07:00
<td><b>Messages</b></td>
2014-06-24 23:58:38 -07:00
<td>
<ul>
2014-07-06 16:08:25 -07:00
<li>pexRequestMsg</li>
<li>pexResponseMsg</li>
2014-06-24 23:58:38 -07:00
</ul>
</td>
</tr>
</table>
2014-06-25 11:58:34 -07:00
<hr />
2014-06-24 23:49:06 -07:00
## Resources
* http://www.upnp-hacks.org/upnp.html