[utils] move GetAddressHex to utils module

* add test cases for GetAddressHex

Signed-off-by: Leo Chen <leo@harmony.one>
pull/646/head
Leo Chen 6 years ago
parent 957fdf0eac
commit 313ddac6ca
  1. 2
      api/proto/discovery/pingpong.go
  2. 12
      consensus/consensus.go
  3. 4
      consensus/consensus_leader.go
  4. 2
      consensus/consensus_validator.go
  5. 9
      drand/drand.go
  6. 8
      internal/utils/utils.go
  7. 28
      internal/utils/utils_test.go
  8. 10
      node/node_handler.go
  9. 10
      p2p/p2p.go

@ -93,7 +93,7 @@ func NewPongMessage(peers []p2p.Peer, pubKeys []*bls.PublicKey, leaderKey *bls.P
}
pong.LeaderPubKey = leaderKey.Serialize()
// utils.GetLogInstance().Info("[pong message]", "keys", len(pong.PubKeys), "peers", len(pong.Peers), "leader", len(pong.LeaderPubKey))
// utils.GetLogInstance().Info("[NewPongMessage]", "keys", len(pong.PubKeys), "peers", len(pong.Peers), "leaderPubKey", utils.GetAddressHex(leaderKey))
return pong
}

@ -44,8 +44,7 @@ type Consensus struct {
ChainReader consensus_engine.ChainReader
// map of nodeID to validator Peer object
// FIXME: should use PubKey of p2p.Peer as the hashkey
validators sync.Map // key is uint16, value is p2p.Peer
validators sync.Map // key is the hex string of the blsKey, value is p2p.Peer
// Minimal number of peers in the shard
// If the number of validators is less than minPeers, the consensus won't start
@ -56,6 +55,7 @@ type Consensus struct {
// Public keys of the committee including leader and validators
PublicKeys []*bls.PublicKey
pubKeyLock sync.Mutex
// private/public keys of current node
@ -168,7 +168,7 @@ func New(host p2p.Host, ShardID uint32, peers []p2p.Peer, leader p2p.Peer, blsPr
consensus.leader = leader
for _, peer := range peers {
consensus.validators.Store(peer.GetAddressHex(), peer)
consensus.validators.Store(utils.GetAddressHex(peer.ConsensusPubKey), peer)
}
consensus.prepareSigs = map[string]*bls.Sign{}
@ -193,7 +193,7 @@ func New(host p2p.Host, ShardID uint32, peers []p2p.Peer, leader p2p.Peer, blsPr
// For now use socket address as ID
// TODO: populate Id derived from address
consensus.SelfAddress = selfPeer.GetAddressHex()
consensus.SelfAddress = utils.GetAddressHex(selfPeer.ConsensusPubKey)
if blsPriKey != nil {
consensus.priKey = blsPriKey
@ -407,7 +407,7 @@ func (consensus *Consensus) AddPeers(peers []*p2p.Peer) int {
count := 0
for _, peer := range peers {
_, ok := consensus.validators.LoadOrStore(peer.GetAddressHex(), *peer)
_, ok := consensus.validators.LoadOrStore(utils.GetAddressHex(peer.ConsensusPubKey), *peer)
if !ok {
consensus.pubKeyLock.Lock()
consensus.PublicKeys = append(consensus.PublicKeys, peer.ConsensusPubKey)
@ -486,7 +486,7 @@ func (consensus *Consensus) DebugPrintValidators() {
consensus.validators.Range(func(k, v interface{}) bool {
if p, ok := v.(p2p.Peer); ok {
str2 := fmt.Sprintf("%s", p.ConsensusPubKey.Serialize())
utils.GetLogInstance().Debug("validator:", "IP", p.IP, "Port", p.Port, "address", p.GetAddressHex(), "Key", str2)
utils.GetLogInstance().Debug("validator:", "IP", p.IP, "Port", p.Port, "address", utils.GetAddressHex(p.ConsensusPubKey), "Key", str2)
count++
return true
}

@ -20,10 +20,6 @@ import (
"github.com/harmony-one/harmony/p2p/host"
)
const (
waitForEnoughValidators = 1000
)
var (
startTime time.Time
)

@ -68,7 +68,7 @@ func (consensus *Consensus) processAnnounceMessage(message *msg_pb.Message) {
consensus.block = block
if err := consensus.checkConsensusMessage(message, consensus.leader.ConsensusPubKey); err != nil {
utils.GetLogInstance().Debug("Failed to check the leader message")
utils.GetLogInstance().Debug("Failed to check the leader message", "key", utils.GetAddressHex(consensus.leader.ConsensusPubKey))
return
}

@ -81,7 +81,7 @@ func New(host p2p.Host, ShardID uint32, peers []p2p.Peer, leader p2p.Peer, confi
dRand.leader = leader
for _, peer := range peers {
dRand.validators.Store(peer.GetAddressHex(), peer)
dRand.validators.Store(utils.GetAddressHex(peer.ConsensusPubKey), peer)
}
dRand.vrfs = &map[string][]byte{}
@ -102,7 +102,7 @@ func New(host p2p.Host, ShardID uint32, peers []p2p.Peer, leader p2p.Peer, confi
dRand.rand = nil
// For now use socket address as ID
dRand.SelfAddress = selfPeer.GetAddressHex()
dRand.SelfAddress = utils.GetAddressHex(selfPeer.ConsensusPubKey)
// Set private key for myself so that I can sign messages.
if blsPriKey != nil {
@ -125,13 +125,12 @@ func (dRand *DRand) AddPeers(peers []*p2p.Peer) int {
count := 0
for _, peer := range peers {
_, ok := dRand.validators.Load(peer.GetAddressHex())
_, ok := dRand.validators.LoadOrStore(utils.GetAddressHex(peer.ConsensusPubKey), *peer)
if !ok {
dRand.validators.Store(peer.GetAddressHex(), *peer)
dRand.pubKeyLock.Lock()
dRand.PublicKeys = append(dRand.PublicKeys, peer.ConsensusPubKey)
dRand.pubKeyLock.Unlock()
utils.GetLogInstance().Debug("[DRAND]", "AddPeers", *peer)
// utils.GetLogInstance().Debug("[DRAND]", "AddPeers", *peer)
}
count++
}

@ -225,3 +225,11 @@ func IsPrivateIP(ip net.IP) bool {
}
return false
}
// GetAddressHex returns the hex string of the address of consensus pubKey.
func GetAddressHex(key *bls.PublicKey) string {
addr := common.Address{}
addrBytes := key.GetAddress()
addr.SetBytes(addrBytes[:])
return addr.Hex()
}

@ -6,6 +6,8 @@ import (
"reflect"
"testing"
"github.com/harmony-one/bls/ffi/go/bls"
"github.com/harmony-one/harmony/crypto/pki"
p2p "github.com/harmony-one/harmony/p2p"
crypto "github.com/libp2p/go-libp2p-crypto"
"github.com/stretchr/testify/assert"
@ -204,3 +206,29 @@ func TestStringsToPeers(t *testing.T) {
}
}
}
func TestGetAddressHex(t *testing.T) {
pubKey1 := pki.GetBLSPrivateKeyFromInt(333).GetPublicKey()
pubKey2 := pki.GetBLSPrivateKeyFromInt(1024).GetPublicKey()
tests := []struct {
key *bls.PublicKey
expected string
}{
{
pubKey1,
"0x8fAd8DAa0206a9a6710b05604a58e6EA1B3A160E",
},
{
pubKey2,
"0x91B5B75ddeb29085BF0490bc562e93059Ad1c254",
},
}
for _, test := range tests {
result := GetAddressHex(test.key)
if result != test.expected {
t.Errorf("Hex Of %v is: %v, got: %v", test.key, test.expected, result)
}
}
}

@ -347,10 +347,8 @@ func (node *Node) AddNewBlock(newBlock *types.Block) {
func (node *Node) pingMessageHandler(msgPayload []byte, sender string) int {
if sender != "" {
_, ok := node.duplicatedPing.Load(sender)
if !ok {
node.duplicatedPing.Store(sender, true)
} else {
_, ok := node.duplicatedPing.LoadOrStore(sender, true)
if ok {
// duplicated ping message return
return 0
}
@ -386,8 +384,8 @@ func (node *Node) pingMessageHandler(msgPayload []byte, sender string) int {
utils.GetLogInstance().Info("Add Client Peer to Node", "Address", node.Consensus.GetSelfAddress(), "Client", peer)
node.ClientPeer = peer
} else {
utils.GetLogInstance().Info("Add Peer to Node", "Address", node.Consensus.GetSelfAddress(), "Pear", peer)
node.AddPeers([]*p2p.Peer{peer})
utils.GetLogInstance().Info("Add Peer to Node", "Address", node.Consensus.GetSelfAddress(), "Peer", peer, "# Peers", node.Consensus.GetNumPeers())
}
return 1
@ -462,7 +460,7 @@ func (node *Node) pongMessageHandler(msgPayload []byte) int {
if err != nil {
utils.GetLogInstance().Error("Unmarshal Consensus Leader PubKey Failed", "error", err)
} else {
utils.GetLogInstance().Info("Set Consensus Leader PubKey")
utils.GetLogInstance().Info("Set Consensus Leader PubKey", "key", utils.GetAddressHex(node.Consensus.GetLeaderPubKey()))
}
err = node.DRand.SetLeaderPubKey(pong.LeaderPubKey)
if err != nil {

@ -4,8 +4,6 @@ import (
"fmt"
"net"
"github.com/ethereum/go-ethereum/common"
"github.com/harmony-one/bls/ffi/go/bls"
libp2p_peer "github.com/libp2p/go-libp2p-peer"
ma "github.com/multiformats/go-multiaddr"
@ -26,11 +24,3 @@ type Peer struct {
func (p Peer) String() string {
return fmt.Sprintf("%s/%s[%d]", net.JoinHostPort(p.IP, p.Port), p.PeerID, len(p.Addrs))
}
// GetAddressHex returns the hex string of the address of consensus pubKey.
func (p Peer) GetAddressHex() string {
addr := common.Address{}
addrBytes := p.ConsensusPubKey.GetAddress()
addr.SetBytes(addrBytes[:])
return addr.Hex()
}

Loading…
Cancel
Save