From 5b1d0c1d81d70d142097042c71bbe5cb4197a2f7 Mon Sep 17 00:00:00 2001 From: ak Date: Wed, 14 Nov 2018 17:33:56 -0800 Subject: [PATCH] todo sustain node server to receive pingback --- beaconchain/beaconchain.go | 15 +++++++++------ beaconchain/beaconchain_handler.go | 2 ++ node/node.go | 6 +++--- p2p/peer.go | 5 +---- runid/run-identity.go | 12 ++++++++++++ runid/run_identity.go | 26 -------------------------- runnode/{run_node.go => run-node.go} | 21 ++++++++++----------- 7 files changed, 37 insertions(+), 50 deletions(-) create mode 100644 runid/run-identity.go delete mode 100644 runid/run_identity.go rename runnode/{run_node.go => run-node.go} (55%) diff --git a/beaconchain/beaconchain.go b/beaconchain/beaconchain.go index 98ca0994e..48b12c121 100644 --- a/beaconchain/beaconchain.go +++ b/beaconchain/beaconchain.go @@ -1,6 +1,7 @@ package beaconchain import ( + "fmt" "net" "os" "sync" @@ -35,7 +36,6 @@ func New(filename string) *BeaconChain { idc.log = log.New() idc.NumberOfShards = 2 idc.PubKey = generateIDCKeys() - idc.StartServer() return &idc } @@ -57,7 +57,8 @@ func (IDC *BeaconChain) AcceptConnections(b []byte) { func (IDC *BeaconChain) registerNode(Node *node.Node) { IDC.Identities = append(IDC.Identities, Node) - IDC.CommunicatePublicKeyToNode(Node.SelfPeer) + fmt.Println(IDC.Identities) + //IDC.CommunicatePublicKeyToNode(Node.SelfPeer) return } @@ -74,17 +75,19 @@ func (IDC *BeaconChain) StartServer() { } func (IDC *BeaconChain) listenOnPort() { - addr := net.JoinHostPort("", "8081") - listen, err := net.Listen("tcp4", addr) + addr := net.JoinHostPort("127.0.0.1", "8081") + listen, err := net.Listen("tcp", addr) if err != nil { - IDC.log.Crit("Socket listen port failed") //Log.Crit does nothing for me! + IDC.log.Crit("Socket listen port failed") os.Exit(1) } else { - IDC.log.Info("Identity chain is now listening ..") //log.Info does nothing for me! (ak) remove this + IDC.log.Info("Identity chain is now listening ..") } defer listen.Close() for { + IDC.log.Info("I am accepting connections now") conn, err := listen.Accept() + fmt.Println(conn) if err != nil { IDC.log.Crit("Error listening on port. Exiting", "8081") continue diff --git a/beaconchain/beaconchain_handler.go b/beaconchain/beaconchain_handler.go index add8b08a9..0dc66a55f 100644 --- a/beaconchain/beaconchain_handler.go +++ b/beaconchain/beaconchain_handler.go @@ -17,6 +17,8 @@ func (IDC *BeaconChain) BeaconChainHandler(conn net.Conn) { if err != nil { IDC.log.Error("Read p2p data failed") return + } else { + IDC.log.Info("received connection") } msgCategory, err := proto.GetMessageCategory(content) if err != nil { diff --git a/node/node.go b/node/node.go index 10ceb0f4b..f2e060001 100644 --- a/node/node.go +++ b/node/node.go @@ -81,8 +81,9 @@ func (node *Node) StartServer(port string) { node.listenOnPort(port) } -func (node *Node) SetLog() { +func (node *Node) SetLog() *Node { node.log = log.New() + return node } func (node *Node) listenOnPort(port string) { @@ -125,8 +126,7 @@ func (node *Node) countNumTransactionsInBlockchain() int { } //ConnectIdentityChain connects to identity chain -func (node *Node) ConnectIdentityChain() { - fmt.Println("In identity chain now") +func (node *Node) ConnectBeaconChain() { msg := node.SerializeNode() msgToSend := proto_identity.ConstructIdentityMessage(proto_identity.Register, msg) p2p.SendMessage(node.IDCPeer, msgToSend) diff --git a/p2p/peer.go b/p2p/peer.go index 7293a3cd9..9093d7c05 100644 --- a/p2p/peer.go +++ b/p2p/peer.go @@ -8,7 +8,6 @@ import ( "runtime" "time" - "github.com/simple-rules/harmony-benchmark/attack" "github.com/simple-rules/harmony-benchmark/log" "github.com/dedis/kyber" @@ -30,7 +29,6 @@ const MaxBroadCast = 20 func SendMessage(peer Peer, msg []byte) { // Construct normal p2p message content := ConstructP2pMessage(byte(0), msg) - go send(peer.Ip, peer.Port, content) } @@ -131,8 +129,7 @@ func sendWithSocketClient(ip, port string, message []byte) (err error) { // Send a message to another node with given port. func send(ip, port string, message []byte) { // Add attack code here. - attack.GetInstance().Run() - + //attack.GetInstance().Run() backoff := NewExpBackoff(250*time.Millisecond, 10*time.Second, 2) for trial := 0; trial < 10; trial++ { diff --git a/runid/run-identity.go b/runid/run-identity.go new file mode 100644 index 000000000..fa9db2c5e --- /dev/null +++ b/runid/run-identity.go @@ -0,0 +1,12 @@ +package main + +import ( + "github.com/simple-rules/harmony-benchmark/beaconchain" +) + +func main() { + + bc := beaconchain.New("temp") + bc.StartServer() + +} diff --git a/runid/run_identity.go b/runid/run_identity.go deleted file mode 100644 index b6fbb95d7..000000000 --- a/runid/run_identity.go +++ /dev/null @@ -1,26 +0,0 @@ -package main - -import ( - "flag" - "fmt" - - "github.com/simple-rules/harmony-benchmark/beaconchain" - "github.com/simple-rules/harmony-benchmark/node" - "github.com/simple-rules/harmony-benchmark/p2p" -) - -func main() { - bc := beaconchain.New("temp") - - ip := flag.String("ip", "127.0.0.0", "IP of the node") - port := flag.String("port", "8081", "port of the node.") - flag.Parse() - peer := p2p.Peer{Ip: *ip, Port: *port} - node := node.Node{} - node.SelfPeer = peer - msg := node.SerializeNode() - fmt.Print(msg) - // fmt.Println(ip) - // fmt.Println(peer) - fmt.Print(bc) -} diff --git a/runnode/run_node.go b/runnode/run-node.go similarity index 55% rename from runnode/run_node.go rename to runnode/run-node.go index abe8e86a0..c62296cc4 100644 --- a/runnode/run_node.go +++ b/runnode/run-node.go @@ -2,7 +2,7 @@ package main import ( "flag" - "fmt" + "time" "github.com/simple-rules/harmony-benchmark/node" "github.com/simple-rules/harmony-benchmark/p2p" @@ -10,21 +10,20 @@ import ( func main() { - ip := flag.String("ip", "127.0.0.0", "IP of the node") + ip := flag.String("ip", "127.0.0.1", "IP of the node") port := flag.String("port", "9000", "port of the node.") flag.Parse() peer := p2p.Peer{Ip: *ip, Port: *port} + // c, err := net.Dial("tcp4", "127.0.0.1:8081") + // fmt.Println(c) + // fmt.Println(err) node := node.Node{} node.SelfPeer = peer node.IDCPeer = p2p.Peer{Ip: *ip, Port: "8081"} - fmt.Println("now in go run mode") - msg := node.SerializeNode() - node.SetLog() - fmt.Println("set the log") - go node.StartServer(*port) - node.ConnectIdentityChain() - fmt.Print(msg) - // fmt.Println(ip) - // fmt.Println(peer) + // node.SetLog() + // go node.StartServer(*port) + // time.Sleep(5 * time.Second) + node.ConnectBeaconChain() + time.Sleep(5 * time.Second) }