write get peers to do syncing

pull/125/head
Minh Doan 6 years ago
parent e11d4c2b72
commit b6dd823812
  1. 3
      benchmark.go
  2. 28
      node/node.go
  3. 6
      node/node_handler.go
  4. 38
      node/node_test.go
  5. 3
      proto/node/node.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
}
}

@ -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

@ -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:

@ -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)
}
}

@ -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)

Loading…
Cancel
Save