add syncing port logic for syncing

pull/125/head
Minh Doan 6 years ago
parent 6cbb0f73bf
commit e028236935
  1. 28
      node/node.go
  2. 15
      node/node_test.go

@ -57,6 +57,8 @@ const (
const ( const (
// TimeToSleepForSyncing is the time waiting for node transformed into NodeDoingConsensus // TimeToSleepForSyncing is the time waiting for node transformed into NodeDoingConsensus
TimeToSleepForSyncing = time.Second * 30 TimeToSleepForSyncing = time.Second * 30
// SyncingPortDifference is the difference between the node port and the syncing port.
SyncingPortDifference = 1000
) )
// NetworkNode ... // NetworkNode ...
@ -341,7 +343,7 @@ func (node *Node) DoSyncing() {
if node.stateSync != nil { if node.stateSync != nil {
node.stateSync = syncing.GetStateSync() node.stateSync = syncing.GetStateSync()
} }
node.stateSync.StartStateSync(node.GetPeers(), node.blockchain) node.stateSync.StartStateSync(node.GetSyncingPeers(), node.blockchain)
} }
// AddPeers adds neighbors nodes // AddPeers adds neighbors nodes
@ -362,13 +364,26 @@ func (node *Node) AddPeers(peers []p2p.Peer) int {
return count return count
} }
// GetPeers returns list of peers. // GetSyncingPort returns the syncing port.
func (node *Node) GetPeers() []p2p.Peer { 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{} res := []p2p.Peer{}
node.Neighbors.Range(func(k, v interface{}) bool { node.Neighbors.Range(func(k, v interface{}) bool {
res = append(res, v.(p2p.Peer)) res = append(res, v.(p2p.Peer))
return true return true
}) })
for i := range res {
res[i].Port = GetSyncingPort(res[i].Port)
}
return res return res
} }
@ -400,12 +415,7 @@ func (node *Node) InitSyncingServer() {
// StartSyncingServer starts syncing server. // StartSyncingServer starts syncing server.
func (node *Node) StartSyncingServer() { func (node *Node) StartSyncingServer() {
if port, err := strconv.Atoi(node.SelfPeer.Port); err == nil { node.downloaderServer.Start(node.SelfPeer.IP, GetSyncingPort(node.SelfPeer.Port))
node.downloaderServer.Start(node.SelfPeer.IP, fmt.Sprintf("%d", port-1000))
} else {
node.log.Error("Wrong port format provided")
os.Exit(1)
}
} }
// CalculateResponse implements DownloadInterface on Node object. // CalculateResponse implements DownloadInterface on Node object.

@ -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"} leader := p2p.Peer{IP: "1", Port: "2"}
validator := p2p.Peer{IP: "3", Port: "5"} validator := p2p.Peer{IP: "3", Port: "5"}
consensus := consensus.New(leader, "0", []p2p.Peer{leader, validator}, leader) consensus := consensus.New(leader, "0", []p2p.Peer{leader, validator}, leader)
node := New(consensus, nil, leader) node := New(consensus, nil, leader)
peer := p2p.Peer{IP: "1.1.1.1"} peer := p2p.Peer{IP: "1.1.1.1", Port: "2000"}
peer2 := p2p.Peer{IP: "2.1.1.1"} peer2 := p2p.Peer{IP: "2.1.1.1", Port: "2000"}
node.Neighbors.Store("minh", peer) node.Neighbors.Store("minh", peer)
node.Neighbors.Store("mark", peer2) node.Neighbors.Store("mark", peer2)
res := node.GetPeers() res := node.GetSyncingPeers()
if len(res) != 2 || !((res[0] == peer && res[1] == peer2) || (res[1] == peer && res[0] == peer2)) { 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("GetPeers should return list of {peer, peer2}") 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")
} }
} }

Loading…
Cancel
Save