Merge pull request #117 from harmony-one/HAR-67_libp2p_integration_v2

Har 67 libp2p integration v2
pull/121/head
Richard Liu 6 years ago committed by GitHub
commit 7af438bc58
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 4
      benchmark.go
  2. 2
      blockchain/merkle_tree_test.go
  3. 9
      client/txgen/main.go
  4. 2
      client/wallet/main.go
  5. 2
      client/wallet_v2/main.go
  6. 10
      consensus/consensus.go
  7. 4
      consensus/consensus_leader_msg_test.go
  8. 4
      consensus/consensus_test.go
  9. 4
      consensus/consensus_validator_msg_test.go
  10. 5
      node/node.go
  11. 17
      node/node_test.go
  12. 13
      utils/utils.go

@ -168,7 +168,7 @@ func main() {
} }
// Consensus object. // Consensus object.
consensus := consensus.NewConsensus(*ip, *port, shardID, peers, leader) consensus := consensus.New(selfPeer, shardID, peers, leader)
consensus.MinPeers = *minPeers consensus.MinPeers = *minPeers
// Start Profiler for leader if profile argument is on // Start Profiler for leader if profile argument is on
@ -183,7 +183,7 @@ func main() {
// Set logger to attack model. // Set logger to attack model.
attack.GetInstance().SetLogger(consensus.Log) attack.GetInstance().SetLogger(consensus.Log)
// Current node. // Current node.
currentNode := node.New(consensus, ldb) currentNode := node.New(consensus, ldb, selfPeer)
// Add self peer. // Add self peer.
currentNode.SelfPeer = selfPeer currentNode.SelfPeer = selfPeer
// Add sync node configuration. // Add sync node configuration.

@ -13,7 +13,7 @@ func TestNewMerkleNode(t *testing.T) {
[]byte("node3"), []byte("node3"),
} }
fmt.Println("TEting") fmt.Println("Testing")
// Level 1 // Level 1
n1 := NewMerkleNode(nil, nil, data[0]) n1 := NewMerkleNode(nil, nil, data[0])

@ -80,7 +80,7 @@ func main() {
// Nodes containing utxopools to mirror the shards' data in the network // Nodes containing utxopools to mirror the shards' data in the network
nodes := []*node.Node{} nodes := []*node.Node{}
for shardID := range shardIDLeaderMap { for shardID := range shardIDLeaderMap {
node := node.New(&consensus.Consensus{ShardID: shardID}, nil) node := node.New(&consensus.Consensus{ShardID: shardID}, nil, p2p.Peer{})
// Assign many fake addresses so we have enough address to play with at first // Assign many fake addresses so we have enough address to play with at first
node.AddTestingAddresses(setting.NumOfAddress) node.AddTestingAddresses(setting.NumOfAddress)
nodes = append(nodes, node) nodes = append(nodes, node)
@ -88,11 +88,8 @@ func main() {
// Client/txgenerator server node setup // Client/txgenerator server node setup
clientPeer := config.GetClientPeer() clientPeer := config.GetClientPeer()
consensusObj := consensus.NewConsensus(clientPeer.IP, clientPeer.Port, "0", nil, p2p.Peer{}) consensusObj := consensus.New(*clientPeer, "0", nil, p2p.Peer{})
clientNode := node.New(consensusObj, nil) clientNode := node.New(consensusObj, nil, *clientPeer)
// Add self peer.
// TODO(ricl): setting self peer should be moved into consensus / node New!
clientNode.SelfPeer = *clientPeer
if clientPeer != nil { if clientPeer != nil {
p2pv2.InitHost(clientPeer.IP, clientPeer.Port) // TODO: this should be moved into client node. p2pv2.InitHost(clientPeer.IP, clientPeer.Port) // TODO: this should be moved into client node.

@ -257,7 +257,7 @@ func CreateWalletServerNode() *node.Node {
shardIDLeaderMap = getShardIDToLeaderMap() shardIDLeaderMap = getShardIDToLeaderMap()
clientPeer = &p2p.Peer{Port: "127.0.0.1", IP: "1234"} clientPeer = &p2p.Peer{Port: "127.0.0.1", IP: "1234"}
} }
walletNode := node.New(nil, nil) walletNode := node.New(nil, nil, *clientPeer) // TODO(ricl): shouldn't the selfPeer for client being clientPeer??
walletNode.Client = client.NewClient(&shardIDLeaderMap) walletNode.Client = client.NewClient(&shardIDLeaderMap)
walletNode.ClientPeer = clientPeer walletNode.ClientPeer = clientPeer
return walletNode return walletNode

@ -257,7 +257,7 @@ func CreateWalletServerNode() *node.Node {
shardIDLeaderMap = getShardIDToLeaderMap() shardIDLeaderMap = getShardIDToLeaderMap()
clientPeer = &p2p.Peer{Port: "127.0.0.1", IP: "1234"} clientPeer = &p2p.Peer{Port: "127.0.0.1", IP: "1234"}
} }
walletNode := node.New(nil, nil) walletNode := node.New(nil, nil, *clientPeer)
walletNode.Client = client.NewClient(&shardIDLeaderMap) walletNode.Client = client.NewClient(&shardIDLeaderMap)
walletNode.ClientPeer = clientPeer walletNode.ClientPeer = clientPeer
return walletNode return walletNode

@ -102,13 +102,11 @@ type BlockConsensusStatus struct {
state State // the latest state of the consensus state State // the latest state of the consensus
} }
// NewConsensus creates a new Consensus object // New creates a new Consensus object
// TODO(minhdoan): Maybe convert it into just New func New(selfPeer p2p.Peer, ShardID string, peers []p2p.Peer, leader p2p.Peer) *Consensus {
// FYI, see https://golang.org/doc/effective_go.html?#package-names
func NewConsensus(ip, port, ShardID string, peers []p2p.Peer, leader p2p.Peer) *Consensus {
consensus := Consensus{} consensus := Consensus{}
if leader.Port == port && leader.IP == ip { if leader.Port == selfPeer.Port && leader.IP == selfPeer.IP {
consensus.IsLeader = true consensus.IsLeader = true
} else { } else {
consensus.IsLeader = false consensus.IsLeader = false
@ -146,7 +144,7 @@ func NewConsensus(ip, port, ShardID string, peers []p2p.Peer, leader p2p.Peer) *
// For now use socket address as 16 byte Id // For now use socket address as 16 byte Id
// TODO: populate with correct Id // TODO: populate with correct Id
consensus.nodeID = utils.GetUniqueIDFromPeer(p2p.Peer{IP: ip, Port: port}) consensus.nodeID = utils.GetUniqueIDFromPeer(selfPeer)
// Set private key for myself so that I can sign messages. // Set private key for myself so that I can sign messages.
consensus.priKey = crypto.Ed25519Curve.Scalar().SetInt64(int64(consensus.nodeID)) consensus.priKey = crypto.Ed25519Curve.Scalar().SetInt64(int64(consensus.nodeID))

@ -12,7 +12,7 @@ import (
func TestConstructAnnounceMessage(test *testing.T) { func TestConstructAnnounceMessage(test *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 := NewConsensus("1", "2", "0", []p2p.Peer{leader, validator}, leader) consensus := New(leader, "0", []p2p.Peer{leader, validator}, leader)
consensus.blockHash = [32]byte{} consensus.blockHash = [32]byte{}
header := consensus.blockHeader header := consensus.blockHeader
msg := consensus.constructAnnounceMessage() msg := consensus.constructAnnounceMessage()
@ -35,7 +35,7 @@ func TestConstructChallengeMessage(test *testing.T) {
validatorPubKey := pki.GetPublicKeyFromScalar(leaderPriKey) validatorPubKey := pki.GetPublicKeyFromScalar(leaderPriKey)
validator := p2p.Peer{IP: "3", Port: "5", PubKey: validatorPubKey} validator := p2p.Peer{IP: "3", Port: "5", PubKey: validatorPubKey}
consensus := NewConsensus("1", "2", "0", []p2p.Peer{leader, validator}, leader) consensus := New(leader, "0", []p2p.Peer{leader, validator}, leader)
consensus.blockHash = [32]byte{} consensus.blockHash = [32]byte{}
(*consensus.commitments)[0] = leaderPubKey (*consensus.commitments)[0] = leaderPubKey
(*consensus.commitments)[1] = validatorPubKey (*consensus.commitments)[1] = validatorPubKey

@ -6,10 +6,10 @@ import (
"github.com/harmony-one/harmony/p2p" "github.com/harmony-one/harmony/p2p"
) )
func TestNewConsensus(test *testing.T) { func TestNew(test *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 := NewConsensus("1", "2", "0", []p2p.Peer{leader, validator}, leader) consensus := New(leader, "0", []p2p.Peer{leader, validator}, leader)
if consensus.consensusID != 0 { if consensus.consensusID != 0 {
test.Errorf("Consensus Id is initialized to the wrong value: %d", consensus.consensusID) test.Errorf("Consensus Id is initialized to the wrong value: %d", consensus.consensusID)
} }

@ -11,7 +11,7 @@ import (
func TestConstructCommitMessage(test *testing.T) { func TestConstructCommitMessage(test *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 := NewConsensus("1", "2", "0", []p2p.Peer{leader, validator}, leader) consensus := New(leader, "0", []p2p.Peer{leader, validator}, leader)
consensus.blockHash = [32]byte{} consensus.blockHash = [32]byte{}
_, msg := consensus.constructCommitMessage(consensus_proto.Commit) _, msg := consensus.constructCommitMessage(consensus_proto.Commit)
@ -23,7 +23,7 @@ func TestConstructCommitMessage(test *testing.T) {
func TestConstructResponseMessage(test *testing.T) { func TestConstructResponseMessage(test *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 := NewConsensus("1", "2", "0", []p2p.Peer{leader, validator}, leader) consensus := New(leader, "0", []p2p.Peer{leader, validator}, leader)
consensus.blockHash = [32]byte{} consensus.blockHash = [32]byte{}
msg := consensus.constructResponseMessage(consensus_proto.Response, crypto.Ed25519Curve.Scalar()) msg := consensus.constructResponseMessage(consensus_proto.Response, crypto.Ed25519Curve.Scalar())

@ -253,7 +253,7 @@ func DeserializeNode(d []byte) *NetworkNode {
} }
// New creates a new node. // New creates a new node.
func New(consensus *bft.Consensus, db *hdb.LDBDatabase) *Node { func New(consensus *bft.Consensus, db *hdb.LDBDatabase, selfPeer p2p.Peer) *Node {
node := Node{} node := Node{}
if consensus != nil { if consensus != nil {
@ -310,6 +310,9 @@ func New(consensus *bft.Consensus, db *hdb.LDBDatabase) *Node {
node.BlockChannelAccount = make(chan *types.Block) node.BlockChannelAccount = make(chan *types.Block)
node.Worker = worker.New(params.TestChainConfig, chain, bft.NewFaker()) node.Worker = worker.New(params.TestChainConfig, chain, bft.NewFaker())
} }
node.SelfPeer = selfPeer
// Logger // Logger
node.log = log.New() node.log = log.New()
if consensus.IsLeader { if consensus.IsLeader {

@ -18,9 +18,9 @@ import (
func TestNewNewNode(test *testing.T) { func TestNewNewNode(test *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.NewConsensus("1", "2", "0", []p2p.Peer{leader, validator}, leader) consensus := consensus.New(leader, "0", []p2p.Peer{leader, validator}, leader)
node := New(consensus, nil) node := New(consensus, nil, leader)
if node.Consensus == nil { if node.Consensus == nil {
test.Error("Consensus is not initialized for the node") test.Error("Consensus is not initialized for the node")
} }
@ -45,9 +45,9 @@ func TestNewNewNode(test *testing.T) {
func TestCountNumTransactionsInBlockchain(test *testing.T) { func TestCountNumTransactionsInBlockchain(test *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.NewConsensus("1", "2", "0", []p2p.Peer{leader, validator}, leader) consensus := consensus.New(leader, "0", []p2p.Peer{leader, validator}, leader)
node := New(consensus, nil) node := New(consensus, nil, leader)
node.AddTestingAddresses(1000) node.AddTestingAddresses(1000)
if node.countNumTransactionsInBlockchain() != 1001 { if node.countNumTransactionsInBlockchain() != 1001 {
test.Error("Count of transactions in the blockchain is incorrect") test.Error("Count of transactions in the blockchain is incorrect")
@ -79,9 +79,9 @@ func TestAddPeers(test *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.NewConsensus("1", "2", "0", []p2p.Peer{leader, validator}, leader) consensus := consensus.New(leader, "0", []p2p.Peer{leader, validator}, leader)
node := New(consensus, nil) node := New(consensus, nil, leader)
r1 := node.AddPeers(peers1) r1 := node.AddPeers(peers1)
e1 := 2 e1 := 2
if r1 != e1 { if r1 != e1 {
@ -149,12 +149,11 @@ func exitServer() {
// func TestPingPongHandler(test *testing.T) { // func TestPingPongHandler(test *testing.T) {
// leader := p2p.Peer{IP: "127.0.0.1", Port: "8881"} // leader := p2p.Peer{IP: "127.0.0.1", Port: "8881"}
// consensus := consensus.NewConsensus("127.0.0.1", "8881", "0", []p2p.Peer{leader}, leader) // // validator := p2p.Peer{IP: "127.0.0.1", Port: "9991"}
// consensus := consensus.New("127.0.0.1", "8881", "0", []p2p.Peer{leader}, leader)
// node := New(consensus, nil) // node := New(consensus, nil)
// // go sendPingMessage(leader) // // go sendPingMessage(leader)
// go sendPongMessage(leader) // go sendPongMessage(leader)
// go exitServer() // go exitServer()
// node.StartServer("8881") // node.StartServer("8881")
// } // }

@ -24,18 +24,13 @@ func ConvertFixedDataIntoByteArray(data interface{}) []byte {
return buff.Bytes() return buff.Bytes()
} }
// GetUniqueIDFromPeer returns unique id from peer. // TODO(minhdoan): this is probably a hack, probably needs some strong non-collision hash.
// GetUniqueIDFromPeer --
func GetUniqueIDFromPeer(peer p2p.Peer) uint16 { func GetUniqueIDFromPeer(peer p2p.Peer) uint16 {
reg, err := regexp.Compile("[^0-9]+") return GetUniqueIDFromIPPort(peer.IP, peer.Port)
if err != nil {
log.Panic("Regex Compilation Failed", "err", err)
}
socketID := reg.ReplaceAllString(peer.IP+peer.Port, "") // A integer Id formed by unique IP/PORT pair
value, _ := strconv.Atoi(socketID)
return uint16(value)
} }
// GetUniqueIDFromIPPort return unique id from ip port. // GetUniqueIDFromIPPort --
func GetUniqueIDFromIPPort(ip, port string) uint16 { func GetUniqueIDFromIPPort(ip, port string) uint16 {
reg, err := regexp.Compile("[^0-9]+") reg, err := regexp.Compile("[^0-9]+")
if err != nil { if err != nil {

Loading…
Cancel
Save