From 400cfaea421c47fe7cfefc961808018b05a67501 Mon Sep 17 00:00:00 2001 From: Leo Chen Date: Sat, 24 Nov 2018 12:52:37 -0800 Subject: [PATCH] [HAR-5]: add validatorID in ping/pong messages Signed-off-by: Leo Chen --- proto/node/pingpong.go | 18 ++++++++++++------ proto/node/pingpong_test.go | 19 ++++++++++--------- 2 files changed, 22 insertions(+), 15 deletions(-) diff --git a/proto/node/pingpong.go b/proto/node/pingpong.go index 3f17b5fac..c652c3045 100644 --- a/proto/node/pingpong.go +++ b/proto/node/pingpong.go @@ -19,10 +19,14 @@ import ( "log" ) +// refer to Peer struct in p2p/peer.go +// this is basically a simplified version of Peer +// for network transportation type nodeInfo struct { - IP string - Port string - PubKey []byte + IP string + Port string + PubKey []byte + ValidatorID int } type PingMessageType struct { @@ -36,13 +40,13 @@ type PongMessageType struct { } 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 { - 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 { - 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 } @@ -54,6 +58,7 @@ func NewPingMessage(peer p2p.Peer) *PingMessageType { ping.Version = PROTOCOL_VERSION ping.Node.IP = peer.Ip ping.Node.Port = peer.Port + ping.Node.ValidatorID = peer.ValidatorID ping.Node.PubKey, err = peer.PubKey.MarshalBinary() if err != nil { @@ -75,6 +80,7 @@ func NewPongMessage(peers []p2p.Peer) *PongMessageType { n := nodeInfo{} n.IP = p.Ip n.Port = p.Port + n.ValidatorID = p.ValidatorID n.PubKey, err = p.PubKey.MarshalBinary() if err != nil { fmt.Printf("Error Marshall PubKey: %v", err) diff --git a/proto/node/pingpong_test.go b/proto/node/pingpong_test.go index 72542fde4..d33abfe08 100644 --- a/proto/node/pingpong_test.go +++ b/proto/node/pingpong_test.go @@ -15,11 +15,12 @@ var ( priKey1 = crypto.Ed25519Curve.Scalar().SetInt64(int64(333)) pubKey1 = pki.GetPublicKeyFromScalar(priKey1) p1 = p2p.Peer{ - Ip: "127.0.0.1", - Port: "9999", - PubKey: pubKey1, + Ip: "127.0.0.1", + Port: "9999", + 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)) pubKey2 = pki.GetPublicKeyFromScalar(priKey2) @@ -40,7 +41,7 @@ var ( ValidatorID: 2, }, } - e2 = "1=># Peers: 2" + e2 = "pong:1=>length:2" buf1 []byte buf2 []byte @@ -53,7 +54,7 @@ func TestString(test *testing.T) { if strings.Compare(r1, e1) != 0 { test.Errorf("expect: %v, got: %v", e1, r1) } else { - fmt.Printf("Ping:%v\n", r1) + fmt.Println(r1) } pong1 := NewPongMessage(p2) @@ -62,7 +63,7 @@ func TestString(test *testing.T) { if !strings.HasPrefix(r2, e2) { test.Errorf("expect: %v, got: %v", e2, r2) } else { - fmt.Printf("Pong:%v\n", r2) + fmt.Println(r2) } } @@ -85,13 +86,13 @@ func TestDeserialize(test *testing.T) { if err != nil { test.Error("Ping failed!") } - fmt.Printf("Ping:%v\n", ping) + fmt.Println(ping) msg2, err := proto.GetMessagePayload(buf2) pong, err := GetPongMessage(msg2) if err != nil { test.Error("Pong failed!") } - fmt.Printf("Pong:%v\n", pong) + fmt.Println(pong) }