From e028236935216972e73dd0cc0b99b9ae7138a453 Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Sat, 1 Dec 2018 12:24:14 -0800 Subject: [PATCH] add syncing port logic for syncing --- node/node.go | 28 +++++++++++++++++++--------- node/node_test.go | 15 +++++++++------ 2 files changed, 28 insertions(+), 15 deletions(-) diff --git a/node/node.go b/node/node.go index f368b96ee..cb7d440b7 100644 --- a/node/node.go +++ b/node/node.go @@ -57,6 +57,8 @@ const ( const ( // TimeToSleepForSyncing is the time waiting for node transformed into NodeDoingConsensus TimeToSleepForSyncing = time.Second * 30 + // SyncingPortDifference is the difference between the node port and the syncing port. + SyncingPortDifference = 1000 ) // NetworkNode ... @@ -341,7 +343,7 @@ func (node *Node) DoSyncing() { if node.stateSync != nil { node.stateSync = syncing.GetStateSync() } - node.stateSync.StartStateSync(node.GetPeers(), node.blockchain) + node.stateSync.StartStateSync(node.GetSyncingPeers(), node.blockchain) } // AddPeers adds neighbors nodes @@ -362,13 +364,26 @@ func (node *Node) AddPeers(peers []p2p.Peer) int { return count } -// GetPeers returns list of peers. -func (node *Node) GetPeers() []p2p.Peer { +// GetSyncingPort returns the syncing port. +func GetSyncingPort(nodePort string) string { + if port, err := strconv.Atoi(nodePort); err == nil { + return fmt.Sprintf("%d", port-SyncingPortDifference) + } + os.Exit(1) + return "" +} + +// GetSyncingPeers returns list of peers. +func (node *Node) GetSyncingPeers() []p2p.Peer { res := []p2p.Peer{} node.Neighbors.Range(func(k, v interface{}) bool { res = append(res, v.(p2p.Peer)) return true }) + + for i := range res { + res[i].Port = GetSyncingPort(res[i].Port) + } return res } @@ -400,12 +415,7 @@ func (node *Node) InitSyncingServer() { // StartSyncingServer starts syncing server. func (node *Node) StartSyncingServer() { - if port, err := strconv.Atoi(node.SelfPeer.Port); err == nil { - node.downloaderServer.Start(node.SelfPeer.IP, fmt.Sprintf("%d", port-1000)) - } else { - node.log.Error("Wrong port format provided") - os.Exit(1) - } + node.downloaderServer.Start(node.SelfPeer.IP, GetSyncingPort(node.SelfPeer.Port)) } // CalculateResponse implements DownloadInterface on Node object. diff --git a/node/node_test.go b/node/node_test.go index bd3d59d50..81489fab2 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -54,19 +54,22 @@ func TestCountNumTransactionsInBlockchain(t *testing.T) { } } -func TestGetPeers(t *testing.T) { +func TestGetSyncingPeers(t *testing.T) { leader := p2p.Peer{IP: "1", Port: "2"} validator := p2p.Peer{IP: "3", Port: "5"} consensus := consensus.New(leader, "0", []p2p.Peer{leader, validator}, leader) node := New(consensus, nil, leader) - peer := p2p.Peer{IP: "1.1.1.1"} - peer2 := p2p.Peer{IP: "2.1.1.1"} + peer := p2p.Peer{IP: "1.1.1.1", Port: "2000"} + peer2 := p2p.Peer{IP: "2.1.1.1", Port: "2000"} node.Neighbors.Store("minh", peer) node.Neighbors.Store("mark", peer2) - res := node.GetPeers() - if len(res) != 2 || !((res[0] == peer && res[1] == peer2) || (res[1] == peer && res[0] == peer2)) { - t.Error("GetPeers should return list of {peer, peer2}") + res := node.GetSyncingPeers() + if len(res) != 2 || !((res[0].IP == peer.IP && res[1].IP == peer2.IP) || (res[1].IP == peer.IP && res[0].IP == peer2.IP)) { + t.Error("GetSyncingPeers should return list of {peer, peer2}") + } + if len(res) != 2 || res[0].Port != "1000" || res[1].Port != "1000" { + t.Error("Syncing ports should be 1000") } }