[HAR-5]: add validatorID in ping/pong messages

Signed-off-by: Leo Chen <leo@harmony.one>
pull/92/head
Leo Chen 6 years ago
parent 91253819dd
commit 400cfaea42
  1. 18
      proto/node/pingpong.go
  2. 19
      proto/node/pingpong_test.go

@ -19,10 +19,14 @@ import (
"log" "log"
) )
// refer to Peer struct in p2p/peer.go
// this is basically a simplified version of Peer
// for network transportation
type nodeInfo struct { type nodeInfo struct {
IP string IP string
Port string Port string
PubKey []byte PubKey []byte
ValidatorID int
} }
type PingMessageType struct { type PingMessageType struct {
@ -36,13 +40,13 @@ type PongMessageType struct {
} }
func (p PingMessageType) String() string { func (p PingMessageType) String() string {
return fmt.Sprintf("%v=>%v:%v/%v", p.Version, p.Node.IP, p.Node.Port, p.Node.PubKey) return fmt.Sprintf("ping:%v=>%v:%v:%v/%v", p.Version, p.Node.IP, p.Node.Port, p.Node.ValidatorID, p.Node.PubKey)
} }
func (p PongMessageType) String() string { func (p PongMessageType) String() string {
str := fmt.Sprintf("%v=># Peers: %v", p.Version, len(p.Peers)) str := fmt.Sprintf("pong:%v=>length:%v\n", p.Version, len(p.Peers))
for _, p := range p.Peers { for _, p := range p.Peers {
str = fmt.Sprintf("%v\n%v:%v/%v", str, p.IP, p.Port, p.PubKey) str = fmt.Sprintf("%v%v:%v:%v/%v\n", str, p.IP, p.Port, p.ValidatorID, p.PubKey)
} }
return str return str
} }
@ -54,6 +58,7 @@ func NewPingMessage(peer p2p.Peer) *PingMessageType {
ping.Version = PROTOCOL_VERSION ping.Version = PROTOCOL_VERSION
ping.Node.IP = peer.Ip ping.Node.IP = peer.Ip
ping.Node.Port = peer.Port ping.Node.Port = peer.Port
ping.Node.ValidatorID = peer.ValidatorID
ping.Node.PubKey, err = peer.PubKey.MarshalBinary() ping.Node.PubKey, err = peer.PubKey.MarshalBinary()
if err != nil { if err != nil {
@ -75,6 +80,7 @@ func NewPongMessage(peers []p2p.Peer) *PongMessageType {
n := nodeInfo{} n := nodeInfo{}
n.IP = p.Ip n.IP = p.Ip
n.Port = p.Port n.Port = p.Port
n.ValidatorID = p.ValidatorID
n.PubKey, err = p.PubKey.MarshalBinary() n.PubKey, err = p.PubKey.MarshalBinary()
if err != nil { if err != nil {
fmt.Printf("Error Marshall PubKey: %v", err) fmt.Printf("Error Marshall PubKey: %v", err)

@ -15,11 +15,12 @@ var (
priKey1 = crypto.Ed25519Curve.Scalar().SetInt64(int64(333)) priKey1 = crypto.Ed25519Curve.Scalar().SetInt64(int64(333))
pubKey1 = pki.GetPublicKeyFromScalar(priKey1) pubKey1 = pki.GetPublicKeyFromScalar(priKey1)
p1 = p2p.Peer{ p1 = p2p.Peer{
Ip: "127.0.0.1", Ip: "127.0.0.1",
Port: "9999", Port: "9999",
PubKey: pubKey1, ValidatorID: 8888,
PubKey: pubKey1,
} }
e1 = "1=>127.0.0.1:9999/[90 217 28 68 64 211 160 232 61 244 159 244 160 36 61 161 237 242 236 45 147 118 237 88 234 122 198 188 157 116 90 228]" e1 = "ping:1=>127.0.0.1:9999:8888/[90 217 28 68 64 211 160 232 61 244 159 244 160 36 61 161 237 242 236 45 147 118 237 88 234 122 198 188 157 116 90 228]"
priKey2 = crypto.Ed25519Curve.Scalar().SetInt64(int64(999)) priKey2 = crypto.Ed25519Curve.Scalar().SetInt64(int64(999))
pubKey2 = pki.GetPublicKeyFromScalar(priKey2) pubKey2 = pki.GetPublicKeyFromScalar(priKey2)
@ -40,7 +41,7 @@ var (
ValidatorID: 2, ValidatorID: 2,
}, },
} }
e2 = "1=># Peers: 2" e2 = "pong:1=>length:2"
buf1 []byte buf1 []byte
buf2 []byte buf2 []byte
@ -53,7 +54,7 @@ func TestString(test *testing.T) {
if strings.Compare(r1, e1) != 0 { if strings.Compare(r1, e1) != 0 {
test.Errorf("expect: %v, got: %v", e1, r1) test.Errorf("expect: %v, got: %v", e1, r1)
} else { } else {
fmt.Printf("Ping:%v\n", r1) fmt.Println(r1)
} }
pong1 := NewPongMessage(p2) pong1 := NewPongMessage(p2)
@ -62,7 +63,7 @@ func TestString(test *testing.T) {
if !strings.HasPrefix(r2, e2) { if !strings.HasPrefix(r2, e2) {
test.Errorf("expect: %v, got: %v", e2, r2) test.Errorf("expect: %v, got: %v", e2, r2)
} else { } else {
fmt.Printf("Pong:%v\n", r2) fmt.Println(r2)
} }
} }
@ -85,13 +86,13 @@ func TestDeserialize(test *testing.T) {
if err != nil { if err != nil {
test.Error("Ping failed!") test.Error("Ping failed!")
} }
fmt.Printf("Ping:%v\n", ping) fmt.Println(ping)
msg2, err := proto.GetMessagePayload(buf2) msg2, err := proto.GetMessagePayload(buf2)
pong, err := GetPongMessage(msg2) pong, err := GetPongMessage(msg2)
if err != nil { if err != nil {
test.Error("Pong failed!") test.Error("Pong failed!")
} }
fmt.Printf("Pong:%v\n", pong) fmt.Println(pong)
} }

Loading…
Cancel
Save