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.
190 lines
4.4 KiB
190 lines
4.4 KiB
6 years ago
|
/*
|
||
|
Package proto/node implements the communication protocol among nodes.
|
||
|
|
||
|
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
|
||
|
|
||
|
*/
|
||
|
|
||
|
package node
|
||
|
|
||
|
import (
|
||
|
"bytes"
|
||
|
"encoding/gob"
|
||
|
"fmt"
|
||
6 years ago
|
"log"
|
||
|
|
||
6 years ago
|
"github.com/dedis/kyber"
|
||
6 years ago
|
"github.com/harmony-one/harmony/api/proto"
|
||
6 years ago
|
"github.com/harmony-one/harmony/p2p"
|
||
6 years ago
|
)
|
||
|
|
||
6 years ago
|
// RoleType defines the role of the node
|
||
|
type RoleType int
|
||
|
|
||
|
// Type of roles of a node
|
||
|
const (
|
||
|
ValidatorRole RoleType = iota
|
||
|
ClientRole
|
||
|
)
|
||
|
|
||
|
func (r RoleType) String() string {
|
||
|
switch r {
|
||
|
case ValidatorRole:
|
||
|
return "Validator"
|
||
|
case ClientRole:
|
||
|
return "Client"
|
||
|
}
|
||
|
return "Unknown"
|
||
|
}
|
||
|
|
||
6 years ago
|
// refer to Peer struct in p2p/peer.go
|
||
|
// this is basically a simplified version of Peer
|
||
|
// for network transportation
|
||
6 years ago
|
type nodeInfo struct {
|
||
6 years ago
|
IP string
|
||
|
Port string
|
||
|
PubKey []byte
|
||
|
ValidatorID int
|
||
6 years ago
|
Role RoleType
|
||
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
|
||
|
Node nodeInfo
|
||
|
}
|
||
|
|
||
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
|
||
|
Peers []nodeInfo
|
||
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
|
var err error
|
||
6 years ago
|
ping.Version = ProtocolVersion
|
||
6 years ago
|
ping.Node.IP = peer.IP
|
||
6 years ago
|
ping.Node.Port = peer.Port
|
||
6 years ago
|
ping.Node.ValidatorID = peer.ValidatorID
|
||
6 years ago
|
ping.Node.PubKey, err = peer.PubKey.MarshalBinary()
|
||
6 years ago
|
ping.Node.Role = ValidatorRole
|
||
6 years ago
|
|
||
|
if err != nil {
|
||
6 years ago
|
fmt.Printf("Error Marshal PubKey: %v", err)
|
||
6 years ago
|
return nil
|
||
|
}
|
||
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 []kyber.Point) *PongMessageType {
|
||
6 years ago
|
pong := new(PongMessageType)
|
||
6 years ago
|
pong.PubKeys = make([][]byte, 0)
|
||
6 years ago
|
|
||
6 years ago
|
pong.Version = ProtocolVersion
|
||
6 years ago
|
pong.Peers = make([]nodeInfo, 0)
|
||
|
|
||
6 years ago
|
var err error
|
||
6 years ago
|
for _, p := range peers {
|
||
|
n := nodeInfo{}
|
||
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.PubKey, err = p.PubKey.MarshalBinary()
|
||
|
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 {
|
||
|
key, err := p.MarshalBinary()
|
||
|
if err != nil {
|
||
|
fmt.Printf("Error Marshal PublicKeys: %v", err)
|
||
|
continue
|
||
|
}
|
||
|
|
||
|
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([]nodeInfo, 0)
|
||
|
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(PING))
|
||
|
|
||
|
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(PONG))
|
||
|
|
||
|
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()
|
||
|
}
|