-
Notifications
You must be signed in to change notification settings - Fork 166
Peer to Peer
The peer to peer package (go-ethereum/p2p) allows you to rapidly and easily add peer to peer networking to any type of application. The p2p package is set up in a modular structure and extending the p2p with your own additional sub protocols is easy and straight forward.
Starting the p2p service only requires you setup a p2p.Server{}
with a few settings:
srv := p2p.Server{
MaxPeers: 10,
Identity: p2p.NewSimpleClientIdentity("my-p2p", "1.0", "", str512rnd),
ListenAddr: ":30300",
Protocols: []p2p.Protocol{},
}
srv.Start()
If we wanted to extend the capabilities of our p2p server we'd need to pass it an additional sub protocol in the Protocol: []p2p.Protocol{}
array.
An additional sub protocol that has the ability to respond to the message "foo" with "bar" requires you to setup an p2p.Protocol{}
:
func MyProtocol() {
return p2p.Protocol{ // 1.
Name: "MyProtocol", // 2.
Version: 1, // 3.
Length: 1, // 4.
Run: func(peer *p2p.Peer, ws p2p.MsgReadWriter) error { return nil }, // 5.
}
}
- A sub-protocol object in the p2p package is called
Protocol{}
. Each time a peer connects with the capability of handling this type of protocol will use this; - The name of your protocol to identify the protocol on the network;
- The version of the protocol.
- The amount of messages this protocol relies on. Because the p2p is extendible and thus has the ability to send an arbitrary amount of messages (with a type, which we'll see later) the p2p handler needs to know how much space it needs to reverse for your protocol, this to ensure consensus can be reached between the peers doing a negotiation over the message IDs. Our protocol supports only one;
message
(as you'll see later). - The main handler of your protocol. We've left this intentionally blank for now. the
peer
variable is the peer connected to you and provides you with some basic information regarding the peer. thews
variable which is a reader and a writer allows you to communicate with the peer. If a message is being send to us by that peer theMsgReadWriter
will handle it and vice-verse.
Lets fill in the blanks and create a somewhat useful peer by allowing it to communicate with another peer:
const messageId = 0 // 1.
type Message string // 2.
func msgHandler(peer *p2p.Peer, ws p2p.MsgReadWriter) error {
for {
msg, err := ws.ReadMsg() // 3.
if err != nil { // 4.
return err // if reading fails return err which will disconnect the peer.
}
var myMessage [1]Message
err = msg.Decode(&myMessage) // 5.
if err != nil {
// handle decode error
continue
}
switch myMessage[0] {
case "foo":
err := ws.WriteMsg(p2p.NewMsg(messageId, "bar")) // 6.
if err != nil {
return err // return (and disconnect) error if writing fails.
}
default:
fmt.Println("recv:", myMessage)
}
}
return nil
}
- The one and only message we know about;
- A typed string we decode in to;
-
ReadMsg
waits on the line until it receives a message, an error or EOF. - In case of an error during reading it's best to return that error and let the p2p server handle it. This usually results in a disconnect from the peer.
-
msg
contains two fields and a decoding method:-
Code
contains the message id,Code == messageId
(i.e., 0) -
Payload
the contents of the message. -
Decode(<ptr>)
is a helper method for: takemsg.Payload
and decodes the rest of the message in to the given interface. If it fails it will return an error.
-
- If the message we decoded was
foo
respond with aNewMessage
using themessageId
message identifier and respond with the messagebar
. Thebar
message would be handled in thedefault
case in the same switch.
Now if we'd tie this all up we'd have a working p2p server with a message passing sub protocol.
package main
import (
"fmt"
"os"
"github.com/ethereum/go-ethereum/p2p"
"github.com/obscuren/secp256k1-go"
)
const messageId = 0
type Message string
func MyProtocol() p2p.Protocol {
return p2p.Protocol{
Name: "MyProtocol",
Version: 1,
Length: 1,
Run: msgHandler,
}
}
func main() {
pub, _ := secp256k1.GenerateKey()
srv := p2p.Server{
MaxPeers: 10,
Identity: p2p.NewSimpleClientIdentity("my-p2p", "1.0", "", string(pub)),
ListenAddr: ":30300",
Protocols: []p2p.Protocol{MyProtocol()},
}
if err := srv.Start(); err != nil {
fmt.Println(err)
os.Exit(1)
}
select {}
}
func msgHandler(peer *p2p.Peer, ws p2p.MsgReadWriter) error {
for {
msg, err := ws.ReadMsg()
if err != nil {
return err
}
var myMessage Message
err = msg.Decode(&myMessage)
if err != nil {
// handle decode error
continue
}
switch myMessage {
case "foo":
err := ws.WriteMsg(p2p.NewMsg(messageId, "bar"))
if err != nil {
return err
}
default:
fmt.Println("recv:", myMessage)
}
}
return nil
}
❤️ Stay Classy