From b6dd823812e3fc752e70189bef9a266f37e4d1c3 Mon Sep 17 00:00:00 2001 From: Minh Doan Date: Fri, 30 Nov 2018 15:48:54 -0800 Subject: [PATCH] write get peers to do syncing --- benchmark.go | 3 +++ node/node.go | 28 +++++++++++++++++++++------- node/node_handler.go | 6 ++++++ node/node_test.go | 38 +++++++++++++++++++++++++++----------- proto/node/node.go | 3 +-- 5 files changed, 58 insertions(+), 20 deletions(-) diff --git a/benchmark.go b/benchmark.go index 419d3d912..9ef1edfb5 100644 --- a/benchmark.go +++ b/benchmark.go @@ -203,6 +203,7 @@ func main() { currentNode.State = node.NodeWaitToJoin if consensus.IsLeader { + currentNode.State = node.NodeLeader if *accountModel { // Let consensus run go func() { @@ -225,6 +226,8 @@ func main() { } else { if *peerDisvoery { go currentNode.JoinShard(leader) + } else { + node.State = node.NodeDoingConsensus } } diff --git a/node/node.go b/node/node.go index c7ff2ac56..b39c57f69 100644 --- a/node/node.go +++ b/node/node.go @@ -38,13 +38,12 @@ type State byte // All constants except the NodeLeader below are for validators only. const ( - NodeInit State = iota // Node just started, before contacting BeaconChain - NodeWaitToJoin // Node contacted BeaconChain, wait to join Shard - NodeJoinedShard // Node joined Shard, ready for consensus - NodeOffline // Node is offline - NodeReadyForConsensus // Node is ready to do consensus - NodeDoingConsensus // Node is already doing consensus - NodeLeader // Node is the leader of some shard. + NodeInit State = iota // Node just started, before contacting BeaconChain + NodeWaitToJoin // Node contacted BeaconChain, wait to join Shard + NodeJoinedShard // Node joined Shard, ready for consensus + NodeOffline // Node is offline + NodeDoingConsensus // Node is already doing consensus + NodeLeader // Node is the leader of some shard. ) const ( @@ -324,6 +323,11 @@ func New(consensus *bft.Consensus, db *hdb.LDBDatabase, selfPeer p2p.Peer) *Node return &node } +// DoSyncing starts syncing. +func (node *Node) DoSyncing() { + +} + // AddPeers adds neighbors nodes func (node *Node) AddPeers(peers []p2p.Peer) int { count := 0 @@ -342,6 +346,16 @@ func (node *Node) AddPeers(peers []p2p.Peer) int { return count } +// GetPeers returns list of peers. +func (node *Node) GetPeers() []p2p.Peer { + res := []p2p.Peer{} + node.Neighbors.Range(func(k, v interface{}) bool { + res = append(res, v.(p2p.Peer)) + return true + }) + return res +} + // JoinShard helps a new node to join a shard. func (node *Node) JoinShard(leader p2p.Peer) { // try to join the shard, with 10 minutes time-out diff --git a/node/node_handler.go b/node/node_handler.go index 54626dab7..e1d9f7d81 100644 --- a/node/node_handler.go +++ b/node/node_handler.go @@ -90,6 +90,12 @@ func (node *Node) NodeHandler(conn net.Conn) { } } case proto.Consensus: + if !(node.State == NodeDoingConsensus || node.State == NodeLeader) { + if node.State == NodeJoinedShard { + + } + return + } actionType := consensus.ConMessageType(msgType) switch actionType { case consensus.Consensus: diff --git a/node/node_test.go b/node/node_test.go index bb0458b4b..d27a60cbc 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -15,34 +15,34 @@ import ( proto_node "github.com/harmony-one/harmony/proto/node" ) -func TestNewNewNode(test *testing.T) { +func TestNewNewNode(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) if node.Consensus == nil { - test.Error("Consensus is not initialized for the node") + t.Error("Consensus is not initialized for the node") } if node.blockchain == nil { - test.Error("Blockchain is not initialized for the node") + t.Error("Blockchain is not initialized for the node") } if len(node.blockchain.Blocks) != 1 { - test.Error("Genesis block is not initialized for the node") + t.Error("Genesis block is not initialized for the node") } if len(node.blockchain.Blocks[0].Transactions) != 1 { - test.Error("Coinbase TX is not initialized for the node") + t.Error("Coinbase TX is not initialized for the node") } if node.UtxoPool == nil { - test.Error("Utxo pool is not initialized for the node") + t.Error("Utxo pool is not initialized for the node") } } -func TestCountNumTransactionsInBlockchain(test *testing.T) { +func TestCountNumTransactionsInBlockchain(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) @@ -50,11 +50,27 @@ func TestCountNumTransactionsInBlockchain(test *testing.T) { node := New(consensus, nil, leader) node.AddTestingAddresses(1000) if node.countNumTransactionsInBlockchain() != 1001 { - test.Error("Count of transactions in the blockchain is incorrect") + t.Error("Count of transactions in the blockchain is incorrect") } } -func TestAddPeers(test *testing.T) { +func TestGetPeers(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"} + node.Neighbors.Store("minh", peer) + node.Neighbors.Store("mark", peer2) + res := node.GetPeers() + if len(res) != 2 || res[0] != peer || res[1] != peer2 { + t.Error("GetPeers should return list of {peer, peer2}") + } +} + +func TestAddPeers(t *testing.T) { priKey1 := crypto.Ed25519Curve.Scalar().SetInt64(int64(333)) pubKey1 := pki.GetPublicKeyFromScalar(priKey1) @@ -85,12 +101,12 @@ func TestAddPeers(test *testing.T) { r1 := node.AddPeers(peers1) e1 := 2 if r1 != e1 { - test.Errorf("Add %v peers, expectd %v", r1, e1) + t.Errorf("Add %v peers, expectd %v", r1, e1) } r2 := node.AddPeers(peers1) e2 := 0 if r2 != e2 { - test.Errorf("Add %v peers, expectd %v", r2, e2) + t.Errorf("Add %v peers, expectd %v", r2, e2) } } diff --git a/proto/node/node.go b/proto/node/node.go index d440231cd..32f480d9c 100644 --- a/proto/node/node.go +++ b/proto/node/node.go @@ -3,7 +3,6 @@ package node import ( "bytes" "encoding/gob" - "fmt" "log" "github.com/ethereum/go-ethereum/rlp" @@ -161,7 +160,7 @@ func ConstructTransactionListMessageAccount(transactions types.Transactions) []b txs, err := rlp.EncodeToBytes(transactions) if err != nil { - fmt.Errorf("ERROR RLP %s", err) + log.Fatal(err) return []byte{} // TODO(RJ): better handle of the error } byteBuffer.Write(txs)