You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
153 lines
3.9 KiB
153 lines
3.9 KiB
6 years ago
|
/*
|
||
6 years ago
|
Package proto/discovery implements the discovery ping/pong protocol among nodes.
|
||
6 years ago
|
|
||
|
pingpong.go adds support of ping/pong messages.
|
||
|
|
||
|
ping: from node to peers, sending IP/Port/PubKey info
|
||
|
pong: peer responds to ping messages, sending all pubkeys known by peer
|
||
|
|
||
|
*/
|
||
|
|
||
6 years ago
|
package discovery
|
||
6 years ago
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"encoding/gob"
|
||
|
"fmt"
|
||
6 years ago
|
"log"
|
||
|
|
||
6 years ago
|
"github.com/harmony-one/bls/ffi/go/bls"
|
||
6 years ago
|
"github.com/harmony-one/harmony/api/proto"
|
||
6 years ago
|
"github.com/harmony-one/harmony/api/proto/node"
|
||
6 years ago
|
"github.com/harmony-one/harmony/p2p"
|
||
6 years ago
|
)
|
||
|
|
||
6 years ago
|
// PingMessageType defines the data structure of the Ping message
|
||
6 years ago
|
type PingMessageType struct {
|
||
|
Version uint16 // version of the protocol
|
||
6 years ago
|
Node node.Info
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
// PongMessageType defines the data structure of the Pong message
|
||
6 years ago
|
type PongMessageType struct {
|
||
6 years ago
|
Version uint16 // version of the protocol
|
||
6 years ago
|
Peers []node.Info
|
||
6 years ago
|
PubKeys [][]byte // list of publickKeys, has to be identical among all validators/leaders
|
||
6 years ago
|
}
|
||
|
|
||
|
func (p PingMessageType) String() string {
|
||
6 years ago
|
return fmt.Sprintf("ping:%v/%v=>%v:%v:%v/%v", p.Node.Role, p.Version, p.Node.IP, p.Node.Port, p.Node.ValidatorID, p.Node.PubKey)
|
||
6 years ago
|
}
|
||
|
|
||
|
func (p PongMessageType) String() string {
|
||
6 years ago
|
str := fmt.Sprintf("pong:%v=>length:%v, keys:%v\n", p.Version, len(p.Peers), len(p.PubKeys))
|
||
6 years ago
|
return str
|
||
|
}
|
||
|
|
||
6 years ago
|
// NewPingMessage creates a new Ping message based on the p2p.Peer input
|
||
6 years ago
|
func NewPingMessage(peer p2p.Peer) *PingMessageType {
|
||
|
ping := new(PingMessageType)
|
||
|
|
||
6 years ago
|
ping.Version = proto.ProtocolVersion
|
||
6 years ago
|
ping.Node.IP = peer.IP
|
||
6 years ago
|
ping.Node.Port = peer.Port
|
||
6 years ago
|
ping.Node.PeerID = peer.PeerID
|
||
6 years ago
|
ping.Node.ValidatorID = peer.ValidatorID
|
||
6 years ago
|
ping.Node.PubKey = peer.PubKey.Serialize()
|
||
6 years ago
|
ping.Node.Role = node.ValidatorRole
|
||
6 years ago
|
|
||
6 years ago
|
return ping
|
||
|
}
|
||
|
|
||
6 years ago
|
// NewPongMessage creates a new Pong message based on a list of p2p.Peer and a list of publicKeys
|
||
6 years ago
|
func NewPongMessage(peers []p2p.Peer, pubKeys []*bls.PublicKey) *PongMessageType {
|
||
6 years ago
|
pong := new(PongMessageType)
|
||
6 years ago
|
pong.PubKeys = make([][]byte, 0)
|
||
6 years ago
|
|
||
6 years ago
|
pong.Version = proto.ProtocolVersion
|
||
|
pong.Peers = make([]node.Info, 0)
|
||
6 years ago
|
|
||
6 years ago
|
var err error
|
||
6 years ago
|
for _, p := range peers {
|
||
6 years ago
|
n := node.Info{}
|
||
6 years ago
|
n.IP = p.IP
|
||
6 years ago
|
n.Port = p.Port
|
||
6 years ago
|
n.ValidatorID = p.ValidatorID
|
||
6 years ago
|
n.PeerID = p.PeerID
|
||
6 years ago
|
n.PubKey = p.PubKey.Serialize()
|
||
6 years ago
|
if err != nil {
|
||
6 years ago
|
fmt.Printf("Error Marshal PubKey: %v", err)
|
||
6 years ago
|
continue
|
||
|
}
|
||
6 years ago
|
pong.Peers = append(pong.Peers, n)
|
||
|
}
|
||
|
|
||
6 years ago
|
for _, p := range pubKeys {
|
||
6 years ago
|
key := p.Serialize()
|
||
6 years ago
|
|
||
|
pong.PubKeys = append(pong.PubKeys, key)
|
||
|
}
|
||
|
|
||
6 years ago
|
return pong
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
// GetPingMessage deserializes the Ping Message from a list of byte
|
||
6 years ago
|
func GetPingMessage(payload []byte) (*PingMessageType, error) {
|
||
|
ping := new(PingMessageType)
|
||
|
|
||
|
r := bytes.NewBuffer(payload)
|
||
|
decoder := gob.NewDecoder(r)
|
||
|
err := decoder.Decode(ping)
|
||
|
|
||
|
if err != nil {
|
||
|
log.Panic("Can't deserialize Ping message")
|
||
|
}
|
||
|
|
||
|
return ping, nil
|
||
|
}
|
||
|
|
||
6 years ago
|
// GetPongMessage deserializes the Pong Message from a list of byte
|
||
6 years ago
|
func GetPongMessage(payload []byte) (*PongMessageType, error) {
|
||
|
pong := new(PongMessageType)
|
||
6 years ago
|
pong.Peers = make([]node.Info, 0)
|
||
6 years ago
|
pong.PubKeys = make([][]byte, 0)
|
||
6 years ago
|
|
||
|
r := bytes.NewBuffer(payload)
|
||
|
decoder := gob.NewDecoder(r)
|
||
|
err := decoder.Decode(pong)
|
||
|
|
||
|
if err != nil {
|
||
|
log.Panic("Can't deserialize Pong message")
|
||
|
}
|
||
|
|
||
|
return pong, nil
|
||
|
}
|
||
|
|
||
|
// ConstructPingMessage contructs ping message from node to leader
|
||
6 years ago
|
func (p PingMessageType) ConstructPingMessage() []byte {
|
||
6 years ago
|
byteBuffer := bytes.NewBuffer([]byte{byte(proto.Node)})
|
||
6 years ago
|
byteBuffer.WriteByte(byte(node.PING))
|
||
6 years ago
|
|
||
|
encoder := gob.NewEncoder(byteBuffer)
|
||
6 years ago
|
err := encoder.Encode(p)
|
||
6 years ago
|
if err != nil {
|
||
|
log.Panic("Can't serialize Ping message", "error:", err)
|
||
|
return nil
|
||
|
}
|
||
|
return byteBuffer.Bytes()
|
||
|
}
|
||
|
|
||
|
// ConstructPongMessage contructs pong message from leader to node
|
||
6 years ago
|
func (p PongMessageType) ConstructPongMessage() []byte {
|
||
6 years ago
|
byteBuffer := bytes.NewBuffer([]byte{byte(proto.Node)})
|
||
6 years ago
|
byteBuffer.WriteByte(byte(node.PONG))
|
||
6 years ago
|
|
||
|
encoder := gob.NewEncoder(byteBuffer)
|
||
6 years ago
|
err := encoder.Encode(p)
|
||
6 years ago
|
if err != nil {
|
||
|
log.Panic("Can't serialize Pong message", "error:", err)
|
||
|
return nil
|
||
|
}
|
||
|
return byteBuffer.Bytes()
|
||
|
}
|