From 92b3929a69fc7fa4e744999074e9cd6ed20e6896 Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Fri, 31 Aug 2018 23:49:56 -0700 Subject: [PATCH 1/4] add SYNC messageType --- node/node_handler.go | 4 +++- proto/node/node.go | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/node/node_handler.go b/node/node_handler.go index 7d86ca873..1ee3f6584 100644 --- a/node/node_handler.go +++ b/node/node_handler.go @@ -3,13 +3,13 @@ package node import ( "bytes" "encoding/gob" - "github.com/simple-rules/harmony-benchmark/log" "net" "os" "strconv" "time" "github.com/simple-rules/harmony-benchmark/blockchain" + "github.com/simple-rules/harmony-benchmark/log" "github.com/simple-rules/harmony-benchmark/p2p" "github.com/simple-rules/harmony-benchmark/proto" "github.com/simple-rules/harmony-benchmark/proto/client" @@ -80,6 +80,8 @@ func (node *Node) NodeHandler(conn net.Conn) { node.Client.UpdateBlocks(*blocks) } } + case proto_node.SYNC: + node.transactionMessageHandler(msgPayload) case proto_node.CLIENT: clientMsgType := proto_node.ClientMessageType(msgPayload[0]) switch clientMsgType { diff --git a/proto/node/node.go b/proto/node/node.go index 3b298ef77..1891ef635 100644 --- a/proto/node/node.go +++ b/proto/node/node.go @@ -17,6 +17,7 @@ const ( BLOCK CLIENT CONTROL + SYNC // TODO: add more types ) From f17ec205db4c067aee35e2d7b9742816ae878cb2 Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Tue, 4 Sep 2018 13:09:12 -0700 Subject: [PATCH 2/4] add SendMeassage and Test --- p2p/message_reader.go | 20 ++++++++++++++++ p2p/peer_test.go | 53 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 73 insertions(+) create mode 100644 p2p/peer_test.go diff --git a/p2p/message_reader.go b/p2p/message_reader.go index e04e4876b..493f4831d 100644 --- a/p2p/message_reader.go +++ b/p2p/message_reader.go @@ -94,3 +94,23 @@ ILOOP: } return contentBuf.Bytes(), nil } + +func CreateMessage(msgType byte, data []byte) []byte { + buffer := bytes.NewBuffer([]byte{}) + + buffer.WriteByte(msgType) + + fourBytes := make([]byte, 4) + binary.BigEndian.PutUint32(fourBytes, uint32(len(data))) + buffer.Write(fourBytes) + + buffer.Write(data) + return buffer.Bytes() +} + +func SendMessageContent(conn net.Conn, data []byte) { + msgToSend := CreateMessage(byte(1), data) + w := bufio.NewWriter(conn) + w.Write(msgToSend) + w.Flush() +} diff --git a/p2p/peer_test.go b/p2p/peer_test.go new file mode 100644 index 000000000..da0062b5f --- /dev/null +++ b/p2p/peer_test.go @@ -0,0 +1,53 @@ +package p2p + +import ( + "bufio" + "net" + "testing" +) + +func setUpTestServer(times int, t *testing.T, conCreated chan bool) { + t.Parallel() + ln, _ := net.Listen("tcp", ":8081") + conCreated <- true + conn, _ := ln.Accept() + defer conn.Close() + + var ( + w = bufio.NewWriter(conn) + ) + for times > 0 { + times-- + data, err := ReadMessageContent(conn) + if err != nil { + t.Fatalf("error when ReadMessageContent %v", err) + } + data = CreateMessage(byte(1), data) + w.Write(data) + w.Flush() + } +} +func TestNewNewNode(t *testing.T) { + times := 100 + conCreated := make(chan bool) + go setUpTestServer(times, t, conCreated) + <-conCreated + + conn, _ := net.Dial("tcp", "127.0.0.1:8081") + + for times > 0 { + times-- + + myMsg := "minhdoan" + SendMessageContent(conn, []byte(myMsg)) + + data, err := ReadMessageContent(conn) + if err != nil { + t.Error("got an error when trying to receive an expected message from server.") + } + if string(data) != myMsg { + t.Error("did not receive expected message") + } + } + conn.Close() +} From 2de914a3984401cac84dbaafe71247d13134f556 Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Tue, 4 Sep 2018 13:22:37 -0700 Subject: [PATCH 3/4] rename message_reader to helper and change the name of the test as well --- p2p/{message_reader.go => helper.go} | 0 p2p/{peer_test.go => helper_test.go} | 0 2 files changed, 0 insertions(+), 0 deletions(-) rename p2p/{message_reader.go => helper.go} (100%) rename p2p/{peer_test.go => helper_test.go} (100%) diff --git a/p2p/message_reader.go b/p2p/helper.go similarity index 100% rename from p2p/message_reader.go rename to p2p/helper.go diff --git a/p2p/peer_test.go b/p2p/helper_test.go similarity index 100% rename from p2p/peer_test.go rename to p2p/helper_test.go From d75bdc5566c256de7f3ff3f0d733d3807e78f1c1 Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Tue, 4 Sep 2018 13:54:09 -0700 Subject: [PATCH 4/4] fix small thing by good practice --- node/node.go | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/node/node.go b/node/node.go index 358c99764..cfce00b99 100644 --- a/node/node.go +++ b/node/node.go @@ -1,10 +1,11 @@ package node import ( - "github.com/simple-rules/harmony-benchmark/crypto/pki" "net" "sync" + "github.com/simple-rules/harmony-benchmark/crypto/pki" + "github.com/simple-rules/harmony-benchmark/blockchain" "github.com/simple-rules/harmony-benchmark/client" "github.com/simple-rules/harmony-benchmark/consensus" @@ -67,11 +68,11 @@ func (node *Node) StartServer(port string) { func (node *Node) listenOnPort(port string) { listen, err := net.Listen("tcp4", ":"+port) - defer func() { + defer func(listen net.Listener) { if listen != nil { listen.Close() } - }() + }(listen) if err != nil { node.log.Error("Socket listen port failed", "port", port, "err", err) return