From ba47c85faf145072bea43c8ef0f4993488e854d9 Mon Sep 17 00:00:00 2001 From: Richard Liu Date: Thu, 29 Nov 2018 00:04:12 -0800 Subject: [PATCH 1/2] ip+port => peer; update signature of consensus.New and node.New as preparation for p2p --- benchmark.go | 4 ++-- client/txgen/main.go | 9 +++------ client/wallet/main.go | 2 +- client/wallet_v2/main.go | 2 +- consensus/consensus.go | 16 +++++++--------- consensus/consensus_leader_msg_test.go | 4 ++-- consensus/consensus_test.go | 4 ++-- consensus/consensus_validator.go | 6 +++--- consensus/consensus_validator_msg_test.go | 4 ++-- node/node.go | 5 ++++- node/node_test.go | 14 +++++++------- utils/distribution_config.go | 2 +- utils/utils.go | 16 ++++++---------- 13 files changed, 41 insertions(+), 47 deletions(-) diff --git a/benchmark.go b/benchmark.go index 5b698c651..5560264eb 100644 --- a/benchmark.go +++ b/benchmark.go @@ -168,7 +168,7 @@ func main() { } // Consensus object. - consensus := consensus.NewConsensus(*ip, *port, shardID, peers, leader) + consensus := consensus.New(selfPeer, shardID, peers, leader) consensus.MinPeers = *minPeers // Start Profiler for leader if profile argument is on @@ -183,7 +183,7 @@ func main() { // Set logger to attack model. attack.GetInstance().SetLogger(consensus.Log) // Current node. - currentNode := node.New(consensus, ldb) + currentNode := node.New(consensus, ldb, selfPeer) // Add self peer. currentNode.SelfPeer = selfPeer // Add sync node configuration. diff --git a/client/txgen/main.go b/client/txgen/main.go index 33e3bdd30..c5501876b 100644 --- a/client/txgen/main.go +++ b/client/txgen/main.go @@ -81,7 +81,7 @@ func main() { // Nodes containing utxopools to mirror the shards' data in the network nodes := []*node.Node{} 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 node.AddTestingAddresses(setting.NumOfAddress) nodes = append(nodes, node) @@ -89,11 +89,8 @@ func main() { // Client/txgenerator server node setup clientPeer := config.GetClientPeer() - consensusObj := consensus.NewConsensus(clientPeer.Ip, clientPeer.Port, "0", nil, p2p.Peer{}) - clientNode := node.New(consensusObj, nil) - // Add self peer. - // TODO(ricl): setting self peer should be moved into consensus / node New! - clientNode.SelfPeer = *clientPeer + consensusObj := consensus.New(*clientPeer, "0", nil, p2p.Peer{}) + clientNode := node.New(consensusObj, nil, *clientPeer) if clientPeer != nil { p2pv2.InitHost(clientPeer.Ip, clientPeer.Port) // TODO: this should be moved into client node. diff --git a/client/wallet/main.go b/client/wallet/main.go index ed15f4e9e..338370247 100644 --- a/client/wallet/main.go +++ b/client/wallet/main.go @@ -256,7 +256,7 @@ func CreateWalletServerNode() *node.Node { shardIDLeaderMap = getShardIDToLeaderMap() 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.ClientPeer = clientPeer return walletNode diff --git a/client/wallet_v2/main.go b/client/wallet_v2/main.go index ed15f4e9e..467bd49b4 100644 --- a/client/wallet_v2/main.go +++ b/client/wallet_v2/main.go @@ -256,7 +256,7 @@ func CreateWalletServerNode() *node.Node { shardIDLeaderMap = getShardIDToLeaderMap() 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.ClientPeer = clientPeer return walletNode diff --git a/consensus/consensus.go b/consensus/consensus.go index 1c8fd8cd1..11f11e2a4 100644 --- a/consensus/consensus.go +++ b/consensus/consensus.go @@ -102,13 +102,11 @@ type BlockConsensusStatus struct { state State // the latest state of the consensus } -// NewConsensus creates a new Consensus object -// TODO(minhdoan): Maybe convert it into just New -// FYI, see https://golang.org/doc/effective_go.html?#package-names -func NewConsensus(ip, port, ShardID string, peers []p2p.Peer, leader p2p.Peer) *Consensus { +// New creates a new Consensus object +func New(selfPeer p2p.Peer, ShardID string, peers []p2p.Peer, leader p2p.Peer) *Consensus { consensus := Consensus{} - if leader.Port == port && leader.Ip == ip { + if leader.Port == selfPeer.Port && leader.Ip == selfPeer.Ip { consensus.IsLeader = true } else { consensus.IsLeader = false @@ -121,7 +119,7 @@ func NewConsensus(ip, port, ShardID string, peers []p2p.Peer, leader p2p.Peer) * consensus.leader = leader for _, peer := range peers { - consensus.validators.Store(utils.GetUniqueIdFromPeer(peer), peer) + consensus.validators.Store(utils.GetUniqueIDFromPeer(peer), peer) } // Initialize cosign bitmap @@ -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 // 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. consensus.priKey = crypto.Ed25519Curve.Scalar().SetInt64(int64(consensus.nodeID)) @@ -239,12 +237,12 @@ func (consensus *Consensus) AddPeers(peers []p2p.Peer) int { count := 0 for _, peer := range peers { - _, ok := consensus.validators.Load(utils.GetUniqueIdFromPeer(peer)) + _, ok := consensus.validators.Load(utils.GetUniqueIDFromPeer(peer)) if !ok { if peer.ValidatorID == -1 { peer.ValidatorID = int(consensus.uniqueIDInstance.GetUniqueId()) } - consensus.validators.Store(utils.GetUniqueIdFromPeer(peer), peer) + consensus.validators.Store(utils.GetUniqueIDFromPeer(peer), peer) consensus.PublicKeys = append(consensus.PublicKeys, peer.PubKey) } count++ diff --git a/consensus/consensus_leader_msg_test.go b/consensus/consensus_leader_msg_test.go index 471357781..eab46485f 100644 --- a/consensus/consensus_leader_msg_test.go +++ b/consensus/consensus_leader_msg_test.go @@ -12,7 +12,7 @@ import ( func TestConstructAnnounceMessage(test *testing.T) { leader := p2p.Peer{Ip: "1", Port: "2"} 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{} header := consensus.blockHeader msg := consensus.constructAnnounceMessage() @@ -35,7 +35,7 @@ func TestConstructChallengeMessage(test *testing.T) { validatorPubKey := pki.GetPublicKeyFromScalar(leaderPriKey) 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.commitments)[0] = leaderPubKey (*consensus.commitments)[1] = validatorPubKey diff --git a/consensus/consensus_test.go b/consensus/consensus_test.go index ec6b6dd87..0d6528097 100644 --- a/consensus/consensus_test.go +++ b/consensus/consensus_test.go @@ -6,10 +6,10 @@ import ( "github.com/harmony-one/harmony/p2p" ) -func TestNewConsensus(test *testing.T) { +func TestNew(test *testing.T) { leader := p2p.Peer{Ip: "1", Port: "2"} 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 { test.Errorf("Consensus Id is initialized to the wrong value: %d", consensus.consensusID) } diff --git a/consensus/consensus_validator.go b/consensus/consensus_validator.go index fa25af968..fa1bcfe65 100644 --- a/consensus/consensus_validator.go +++ b/consensus/consensus_validator.go @@ -72,7 +72,7 @@ func (consensus *Consensus) processAnnounceMessage(payload []byte) { // Verify block data // check leader Id - myLeaderID := utils.GetUniqueIdFromPeer(consensus.leader) + myLeaderID := utils.GetUniqueIDFromPeer(consensus.leader) if leaderID != myLeaderID { consensus.Log.Warn("Received message from wrong leader", "myLeaderID", myLeaderID, "receivedLeaderId", leaderID, "consensus", consensus) return @@ -175,7 +175,7 @@ func (consensus *Consensus) processChallengeMessage(payload []byte, targetState // Verify block data and the aggregated signatures // check leader Id - myLeaderID := utils.GetUniqueIdFromPeer(consensus.leader) + myLeaderID := utils.GetUniqueIDFromPeer(consensus.leader) if leaderID != myLeaderID { consensus.Log.Warn("Received message from wrong leader", "myLeaderID", myLeaderID, "receivedLeaderId", leaderID, "consensus", consensus) return @@ -325,7 +325,7 @@ func (consensus *Consensus) processCollectiveSigMessage(payload []byte) { // Verify block data // check leader Id - myLeaderID := utils.GetUniqueIdFromPeer(consensus.leader) + myLeaderID := utils.GetUniqueIDFromPeer(consensus.leader) if leaderID != myLeaderID { consensus.Log.Warn("Received message from wrong leader", "myLeaderID", myLeaderID, "receivedLeaderId", leaderID, "consensus", consensus) return diff --git a/consensus/consensus_validator_msg_test.go b/consensus/consensus_validator_msg_test.go index 07d263c54..0ea798a35 100644 --- a/consensus/consensus_validator_msg_test.go +++ b/consensus/consensus_validator_msg_test.go @@ -11,7 +11,7 @@ import ( func TestConstructCommitMessage(test *testing.T) { leader := p2p.Peer{Ip: "1", Port: "2"} 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{} _, msg := consensus.constructCommitMessage(consensus_proto.Commit) @@ -23,7 +23,7 @@ func TestConstructCommitMessage(test *testing.T) { func TestConstructResponseMessage(test *testing.T) { leader := p2p.Peer{Ip: "1", Port: "2"} 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{} msg := consensus.constructResponseMessage(consensus_proto.Response, crypto.Ed25519Curve.Scalar()) diff --git a/node/node.go b/node/node.go index bd0d0533d..232eeeabf 100644 --- a/node/node.go +++ b/node/node.go @@ -261,7 +261,7 @@ func DeserializeNode(d []byte) *NetworkNode { } // 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{} if consensus != nil { @@ -315,6 +315,9 @@ func New(consensus *bft.Consensus, db *hdb.LDBDatabase) *Node { node.BlockChannelAccount = make(chan *types.Block) node.Worker = worker.New(params.TestChainConfig, chain, bft.NewFaker()) } + + node.SelfPeer = selfPeer + // Logger node.log = log.New() if consensus.IsLeader { diff --git a/node/node_test.go b/node/node_test.go index f76aa29b0..5640e9d6d 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -18,9 +18,9 @@ import ( func TestNewNewNode(test *testing.T) { leader := p2p.Peer{Ip: "1", Port: "2"} 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 { test.Error("Consensus is not initialized for the node") } @@ -45,9 +45,9 @@ func TestNewNewNode(test *testing.T) { func TestCountNumTransactionsInBlockchain(test *testing.T) { leader := p2p.Peer{Ip: "1", Port: "2"} 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) if node.countNumTransactionsInBlockchain() != 1001 { 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"} 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) e1 := 2 if r1 != e1 { @@ -150,7 +150,7 @@ func exitServer() { // func TestPingPongHandler(test *testing.T) { // leader := p2p.Peer{Ip: "127.0.0.1", Port: "8881"} // // validator := p2p.Peer{Ip: "127.0.0.1", Port: "9991"} -// consensus := consensus.NewConsensus("127.0.0.1", "8881", "0", []p2p.Peer{leader}, leader) +// consensus := consensus.New("127.0.0.1", "8881", "0", []p2p.Peer{leader}, leader) // node := New(consensus, nil) diff --git a/utils/distribution_config.go b/utils/distribution_config.go index 55ccb8cfa..ed93c8657 100644 --- a/utils/distribution_config.go +++ b/utils/distribution_config.go @@ -165,6 +165,6 @@ func (config *DistributionConfig) GetMyConfigEntry(ip string, port string) *Conf func setKey(peer *p2p.Peer) { // Get public key deterministically based on ip and port - priKey := crypto.Ed25519Curve.Scalar().SetInt64(int64(GetUniqueIdFromPeer(*peer))) // TODO: figure out why using a random hash value doesn't work for private key (schnorr) + priKey := crypto.Ed25519Curve.Scalar().SetInt64(int64(GetUniqueIDFromPeer(*peer))) // TODO: figure out why using a random hash value doesn't work for private key (schnorr) peer.PubKey = pki.GetPublicKeyFromScalar(priKey) } diff --git a/utils/utils.go b/utils/utils.go index e35e0756a..ec56a541c 100644 --- a/utils/utils.go +++ b/utils/utils.go @@ -25,17 +25,13 @@ func ConvertFixedDataIntoByteArray(data interface{}) []byte { } // TODO(minhdoan): this is probably a hack, probably needs some strong non-collision hash. -func GetUniqueIdFromPeer(peer p2p.Peer) uint16 { - reg, err := regexp.Compile("[^0-9]+") - 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) +// GetUniqueIDFromPeer -- +func GetUniqueIDFromPeer(peer p2p.Peer) uint16 { + return GetUniqueIDFromIPPort(peer.Ip, peer.Port) } -func GetUniqueIdFromIpPort(ip, port string) uint16 { +// GetUniqueIDFromIPPort -- +func GetUniqueIDFromIPPort(ip, port string) uint16 { reg, err := regexp.Compile("[^0-9]+") if err != nil { log.Panic("Regex Compilation Failed", "err", err) @@ -65,7 +61,7 @@ func RunCmd(name string, args ...string) error { } func GenKey(ip, port string) (kyber.Scalar, kyber.Point) { - priKey := crypto.Ed25519Curve.Scalar().SetInt64(int64(GetUniqueIdFromIpPort(ip, port))) // TODO: figure out why using a random hash value doesn't work for private key (schnorr) + priKey := crypto.Ed25519Curve.Scalar().SetInt64(int64(GetUniqueIDFromIPPort(ip, port))) // TODO: figure out why using a random hash value doesn't work for private key (schnorr) pubKey := pki.GetPublicKeyFromScalar(priKey) return priKey, pubKey From f2c3290ccd14b07f7936ebc4ec1da803b875ac43 Mon Sep 17 00:00:00 2001 From: Richard Liu Date: Fri, 30 Nov 2018 01:23:30 -0800 Subject: [PATCH 2/2] fix test issues --- blockchain/merkle_tree_test.go | 2 +- node/node_test.go | 12 ++---------- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/blockchain/merkle_tree_test.go b/blockchain/merkle_tree_test.go index 04a7adbee..70ce44ddc 100644 --- a/blockchain/merkle_tree_test.go +++ b/blockchain/merkle_tree_test.go @@ -13,7 +13,7 @@ func TestNewMerkleNode(t *testing.T) { []byte("node3"), } - fmt.Println("TEting") + fmt.Println("Testing") // Level 1 n1 := NewMerkleNode(nil, nil, data[0]) diff --git a/node/node_test.go b/node/node_test.go index 57a52c385..bb0458b4b 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -148,20 +148,12 @@ func exitServer() { } // func TestPingPongHandler(test *testing.T) { -<<<<<<< HEAD -// leader := p2p.Peer{Ip: "127.0.0.1", Port: "8881"} -// // validator := p2p.Peer{Ip: "127.0.0.1", Port: "9991"} -// consensus := consensus.New("127.0.0.1", "8881", "0", []p2p.Peer{leader}, leader) - -======= // leader := p2p.Peer{IP: "127.0.0.1", Port: "8881"} -// consensus := consensus.NewConsensus("127.0.0.1", "8881", "0", []p2p.Peer{leader}, leader) ->>>>>>> 8cddd1548aa2057a4d17d149addc5f579146fddd +// // 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) - // // go sendPingMessage(leader) // go sendPongMessage(leader) // go exitServer() - // node.StartServer("8881") // }