remove validator IDs

pull/597/head
Rongjian Lan 6 years ago
parent 0767b687dd
commit 1dbb1a9250
  1. 5
      api/proto/discovery/pingpong.go
  2. 7
      api/proto/discovery/pingpong_test.go
  3. 111
      api/proto/message/message.pb.go
  4. 4
      api/proto/message/message.proto
  5. 13
      api/proto/node/node.go
  6. 9
      api/proto/node/node_test.go
  7. 7
      api/service/discovery/discovery_test.go
  8. 7
      api/service/networkinfo/service_test.go
  9. 9
      api/service/syncing/downloader/client.go
  10. 6
      api/service/syncing/syncing.go
  11. 8
      cmd/client/txgen/main.go
  12. 4
      cmd/harmony/main.go
  13. 68
      consensus/consensus.go
  14. 49
      consensus/consensus_leader.go
  15. 10
      consensus/consensus_leader_msg_test.go
  16. 37
      consensus/consensus_leader_test.go
  17. 33
      consensus/consensus_test.go
  18. 10
      consensus/consensus_validator.go
  19. 56
      consensus/consensus_validator_test.go
  20. 4
      core/blockchain.go
  21. 43
      crypto/bls/bls_test.go
  22. 29
      drand/drand.go
  23. 16
      drand/drand_leader.go
  24. 2
      drand/drand_leader_msg.go
  25. 33
      drand/drand_test.go
  26. 2
      drand/drand_validator_msg.go
  27. 29
      internal/utils/utils.go
  28. 27
      internal/utils/utils_test.go
  29. 4
      node/contract_test.go
  30. 32
      node/node.go
  31. 6
      node/node_handler.go
  32. 6
      node/node_handler_test.go
  33. 8
      node/node_syncing.go
  34. 20
      node/node_test.go
  35. 5
      node/staking_test.go
  36. 11
      p2p/p2p.go

@ -37,7 +37,7 @@ type PongMessageType struct {
}
func (p PingMessageType) String() string {
return fmt.Sprintf("ping:%v/%v=>%v:%v:%v/%v", p.Node.Role, p.Version, p.Node.IP, p.Node.Port, p.Node.ValidatorID, p.Node.PubKey)
return fmt.Sprintf("ping:%v/%v=>%v:%v/%v", p.Node.Role, p.Version, p.Node.IP, p.Node.Port, p.Node.PubKey)
}
func (p PongMessageType) String() string {
@ -54,11 +54,9 @@ func NewPingMessage(peer p2p.Peer, isClient bool) *PingMessageType {
ping.Node.Port = peer.Port
ping.Node.PeerID = peer.PeerID
if !isClient {
ping.Node.ValidatorID = peer.ValidatorID
ping.Node.PubKey = peer.ConsensusPubKey.Serialize()
ping.Node.Role = node.ValidatorRole
} else {
ping.Node.ValidatorID = -1
ping.Node.PubKey = nil
ping.Node.Role = node.ClientRole
}
@ -79,7 +77,6 @@ func NewPongMessage(peers []p2p.Peer, pubKeys []*bls.PublicKey, leaderKey *bls.P
n := node.Info{}
n.IP = p.IP
n.Port = p.Port
n.ValidatorID = p.ValidatorID
n.PeerID = p.PeerID
n.PubKey = p.ConsensusPubKey.Serialize()
if err != nil {

@ -18,11 +18,10 @@ var (
p1 = p2p.Peer{
IP: "127.0.0.1",
Port: "9999",
ValidatorID: -1,
ConsensusPubKey: pubKey1,
}
e1 = "ping:Validator/1=>127.0.0.1:9999:-1/[120 1 130 197 30 202 78 236 84 249 5 230 132 208 242 242 246 63 100 123 96 11 211 228 4 56 64 94 57 133 3 226 254 222 231 160 178 81 252 205 40 28 45 2 90 74 207 15 68 86 138 68 143 176 221 161 108 105 133 6 64 121 92 25 134 255 9 209 156 209 119 187 13 160 23 147 240 24 196 152 100 20 163 51 118 45 100 26 179 227 184 166 147 113 50 139]"
e3 = "ping:Client/1=>127.0.0.1:9999:-1/[120 1 130 197 30 202 78 236 84 249 5 230 132 208 242 242 246 63 100 123 96 11 211 228 4 56 64 94 57 133 3 226 254 222 231 160 178 81 252 205 40 28 45 2 90 74 207 15 68 86 138 68 143 176 221 161 108 105 133 6 64 121 92 25 134 255 9 209 156 209 119 187 13 160 23 147 240 24 196 152 100 20 163 51 118 45 100 26 179 227 184 166 147 113 50 139]"
e1 = "ping:Validator/1=>127.0.0.1:9999/[120 1 130 197 30 202 78 236 84 249 5 230 132 208 242 242 246 63 100 123 96 11 211 228 4 56 64 94 57 133 3 226 254 222 231 160 178 81 252 205 40 28 45 2 90 74 207 15 68 86 138 68 143 176 221 161 108 105 133 6 64 121 92 25 134 255 9 209 156 209 119 187 13 160 23 147 240 24 196 152 100 20 163 51 118 45 100 26 179 227 184 166 147 113 50 139]"
e3 = "ping:Client/1=>127.0.0.1:9999/[120 1 130 197 30 202 78 236 84 249 5 230 132 208 242 242 246 63 100 123 96 11 211 228 4 56 64 94 57 133 3 226 254 222 231 160 178 81 252 205 40 28 45 2 90 74 207 15 68 86 138 68 143 176 221 161 108 105 133 6 64 121 92 25 134 255 9 209 156 209 119 187 13 160 23 147 240 24 196 152 100 20 163 51 118 45 100 26 179 227 184 166 147 113 50 139]"
pubKey2 = pki.GetBLSPrivateKeyFromInt(999).GetPublicKey()
@ -31,13 +30,11 @@ var (
IP: "127.0.0.1",
Port: "8888",
ConsensusPubKey: pubKey1,
ValidatorID: -1,
},
{
IP: "127.0.0.1",
Port: "9999",
ConsensusPubKey: pubKey2,
ValidatorID: -2,
},
}
e2 = "pong:1=>length:2"

@ -533,7 +533,7 @@ func (m *StakingRequest) GetNodeId() string {
type ConsensusRequest struct {
ConsensusId uint32 `protobuf:"varint,1,opt,name=consensus_id,json=consensusId,proto3" json:"consensus_id,omitempty"`
BlockHash []byte `protobuf:"bytes,2,opt,name=block_hash,json=blockHash,proto3" json:"block_hash,omitempty"`
SenderId uint32 `protobuf:"varint,3,opt,name=sender_id,json=senderId,proto3" json:"sender_id,omitempty"`
SenderAddress string `protobuf:"bytes,3,opt,name=sender_address,json=senderAddress,proto3" json:"sender_address,omitempty"`
Payload []byte `protobuf:"bytes,4,opt,name=payload,proto3" json:"payload,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
XXX_unrecognized []byte `json:"-"`
@ -579,11 +579,11 @@ func (m *ConsensusRequest) GetBlockHash() []byte {
return nil
}
func (m *ConsensusRequest) GetSenderId() uint32 {
func (m *ConsensusRequest) GetSenderAddress() string {
if m != nil {
return m.SenderId
return m.SenderAddress
}
return 0
return ""
}
func (m *ConsensusRequest) GetPayload() []byte {
@ -594,7 +594,7 @@ func (m *ConsensusRequest) GetPayload() []byte {
}
type DrandRequest struct {
SenderId uint32 `protobuf:"varint,1,opt,name=sender_id,json=senderId,proto3" json:"sender_id,omitempty"`
SenderAddress string `protobuf:"bytes,1,opt,name=sender_address,json=senderAddress,proto3" json:"sender_address,omitempty"`
BlockHash []byte `protobuf:"bytes,2,opt,name=block_hash,json=blockHash,proto3" json:"block_hash,omitempty"`
Payload []byte `protobuf:"bytes,3,opt,name=payload,proto3" json:"payload,omitempty"`
XXX_NoUnkeyedLiteral struct{} `json:"-"`
@ -627,11 +627,11 @@ func (m *DrandRequest) XXX_DiscardUnknown() {
var xxx_messageInfo_DrandRequest proto.InternalMessageInfo
func (m *DrandRequest) GetSenderId() uint32 {
func (m *DrandRequest) GetSenderAddress() string {
if m != nil {
return m.SenderId
return m.SenderAddress
}
return 0
return ""
}
func (m *DrandRequest) GetBlockHash() []byte {
@ -665,53 +665,54 @@ func init() {
func init() { proto.RegisterFile("message.proto", fileDescriptor_33c57e4bae7b9afd) }
var fileDescriptor_33c57e4bae7b9afd = []byte{
// 730 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xcc, 0x95, 0x4d, 0x6f, 0x9b, 0x4c,
0x10, 0xc7, 0x8d, 0xb1, 0x8d, 0x19, 0xb0, 0xb3, 0xcf, 0xea, 0x69, 0x43, 0xd3, 0x44, 0x75, 0x39,
0x59, 0x91, 0x1a, 0x45, 0x4e, 0xa5, 0xaa, 0xbd, 0x61, 0x83, 0x12, 0x14, 0x07, 0x47, 0x6b, 0xd2,
0x1e, 0x11, 0x31, 0xab, 0x04, 0x85, 0x82, 0xcb, 0x92, 0x48, 0xbe, 0xf6, 0xda, 0x43, 0xbf, 0x4c,
0x3f, 0x5e, 0x0f, 0x15, 0x0b, 0x36, 0xd8, 0x55, 0xd5, 0x6b, 0x6f, 0xcc, 0x7f, 0xf6, 0x37, 0x6f,
0x3b, 0x6b, 0x43, 0xef, 0x33, 0x65, 0xcc, 0xbf, 0xa3, 0x27, 0xcb, 0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, 0x2a, 0x0d, 0x13, 0x89, 0xbc, 0xac, 0xd9, 0xd5, 0x95, 0xed,
0xa2, 0x56, 0x51, 0x47, 0xfe, 0xed, 0x5a, 0x26, 0x6a, 0xe3, 0x3e, 0x00, 0x4f, 0xed, 0xd9, 0x8e,
0xed, 0xa2, 0x0e, 0x46, 0xa0, 0x16, 0x76, 0x09, 0x48, 0x23, 0x03, 0x7a, 0x93, 0x28, 0xa4, 0x71,
0x56, 0x76, 0x83, 0x4f, 0x41, 0xba, 0x4e, 0x93, 0x05, 0x65, 0x0c, 0xa3, 0xdd, 0x57, 0x7a, 0xf0,
0x5f, 0xed, 0x37, 0xa2, 0x7c, 0x5b, 0x8d, 0xdb, 0x0e, 0xff, 0x8f, 0x38, 0xfb, 0x15, 0x00, 0x00,
0xff, 0xff, 0x47, 0x7e, 0x88, 0xb5, 0x34, 0x06, 0x00, 0x00,
// 741 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xcc, 0x95, 0x41, 0x6f, 0x9b, 0x4a,
0x10, 0xc7, 0x8d, 0xb1, 0x8d, 0x19, 0xb0, 0xb3, 0x6f, 0xf5, 0xde, 0x0b, 0x2f, 0x4a, 0xf4, 0x5c,
0xa4, 0x4a, 0x56, 0xa4, 0x46, 0x91, 0x53, 0xa9, 0x6a, 0x6f, 0xd8, 0xa0, 0x04, 0xc5, 0xc1, 0xd1,
0x9a, 0xb4, 0x47, 0x44, 0xcc, 0x2a, 0x41, 0xa1, 0xe0, 0xb2, 0x24, 0x92, 0xaf, 0xfd, 0x02, 0x3d,
0xf5, 0x9b, 0xf4, 0xe3, 0xf5, 0x50, 0xb1, 0x60, 0x83, 0xdd, 0x54, 0xbd, 0xf6, 0xc6, 0xfc, 0x67,
0x7f, 0x33, 0xb3, 0x33, 0x3b, 0x36, 0xf4, 0x3e, 0x52, 0xc6, 0xfc, 0x3b, 0x7a, 0xb2, 0x4c, 0x93,
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6b, 0x62, 0x5d, 0x1b, 0xc4, 0x42, 0xcd,
0xdc, 0x55, 0x1a, 0x26, 0x12, 0x79, 0x59, 0xb3, 0xab, 0x2b, 0xdb, 0x45, 0xad, 0xa2, 0x8e, 0xfc,
0xdb, 0xb5, 0x4c, 0xd4, 0xc6, 0x7d, 0x00, 0x9e, 0xda, 0xb3, 0x1d, 0xdb, 0x45, 0x1d, 0x8c, 0x40,
0x2d, 0xec, 0x12, 0x90, 0x46, 0x06, 0xf4, 0x26, 0x51, 0x48, 0xe3, 0xac, 0xbc, 0x0d, 0x3e, 0x05,
0xe9, 0x3a, 0x4d, 0x16, 0x79, 0x83, 0xd1, 0xee, 0xe6, 0x1e, 0xfc, 0x55, 0xfb, 0xdd, 0x28, 0xf7,
0xad, 0x71, 0xdb, 0xe1, 0xff, 0x1b, 0x67, 0x3f, 0x02, 0x00, 0x00, 0xff, 0xff, 0xef, 0x2a, 0xe5,
0x7e, 0x48, 0x06, 0x00, 0x00,
}
// Reference imports to suppress errors if they are not otherwise used.

@ -85,12 +85,12 @@ message StakingRequest {
message ConsensusRequest {
uint32 consensus_id = 1;
bytes block_hash = 2;
uint32 sender_id = 3;
string sender_address = 3;
bytes payload = 4;
}
message DrandRequest {
uint32 sender_id = 1; // TODO: make it public key
string sender_address = 1;
bytes block_hash = 2;
bytes payload = 3;
}

@ -77,16 +77,15 @@ func (r RoleType) String() string {
// this is basically a simplified version of Peer
// for network transportation
type Info struct {
IP string
Port string
PubKey []byte
ValidatorID int
Role RoleType
PeerID peer.ID // Peerstore ID
IP string
Port string
PubKey []byte
Role RoleType
PeerID peer.ID // Peerstore ID
}
func (info Info) String() string {
return fmt.Sprintf("Info:%v/%v=>%v/%v", info.IP, info.Port, info.ValidatorID, info.PeerID.Pretty())
return fmt.Sprintf("Info:%v/%v=>%v", info.IP, info.Port, info.PeerID.Pretty())
}
// BlockMessageType represents the type of messages used for Node/Block

@ -125,12 +125,11 @@ func TestRoleTypeToString(t *testing.T) {
func TestInfoToString(t *testing.T) {
info := Info{
IP: "127.0.0.1",
Port: "81",
ValidatorID: 1,
PeerID: "peer",
IP: "127.0.0.1",
Port: "81",
PeerID: "peer",
}
if strings.Compare(info.String(), "Info:127.0.0.1/81=>1/3sdfvR") != 0 {
if strings.Compare(info.String(), "Info:127.0.0.1/81=>3sdfvR") != 0 {
t.Errorf("Info string mismatch: %v", info.String())
}
}

@ -4,6 +4,8 @@ import (
"testing"
"time"
"github.com/harmony-one/harmony/crypto/bls"
"github.com/harmony-one/harmony/api/service"
"github.com/harmony-one/harmony/internal/utils"
"github.com/harmony-one/harmony/p2p"
@ -21,11 +23,12 @@ func TestDiscoveryService(t *testing.T) {
if err != nil {
t.Fatal(err)
}
peerPriKey, peerPubKey := utils.GenKey("127.0.0.1", "12345")
peerPriKey := bls.RandPrivateKey()
peerPubKey := peerPriKey.GetPublicKey()
if peerPriKey == nil || peerPubKey == nil {
t.Fatal("generate key error")
}
selfPeer := p2p.Peer{IP: "127.0.0.1", Port: "12345", ValidatorID: -1, ConsensusPubKey: peerPubKey}
selfPeer := p2p.Peer{IP: "127.0.0.1", Port: "12345", ConsensusPubKey: peerPubKey}
host, err := p2pimpl.NewHost(&selfPeer, nodePriKey)
if err != nil {

@ -4,6 +4,8 @@ import (
"testing"
"time"
"github.com/harmony-one/harmony/crypto/bls"
"github.com/harmony-one/harmony/internal/utils"
"github.com/harmony-one/harmony/p2p"
"github.com/harmony-one/harmony/p2p/p2pimpl"
@ -14,11 +16,12 @@ func TestService(t *testing.T) {
if err != nil {
t.Fatal(err)
}
peerPriKey, peerPubKey := utils.GenKey("127.0.0.1", "12345")
peerPriKey := bls.RandPrivateKey()
peerPubKey := peerPriKey.GetPublicKey()
if peerPriKey == nil || peerPubKey == nil {
t.Fatal("generate key error")
}
selfPeer := p2p.Peer{IP: "127.0.0.1", Port: "12345", ValidatorID: -1, ConsensusPubKey: peerPubKey}
selfPeer := p2p.Peer{IP: "127.0.0.1", Port: "12345", ConsensusPubKey: peerPubKey}
host, err := p2pimpl.NewHost(&selfPeer, nodePriKey)
if err != nil {

@ -2,7 +2,6 @@ package downloader
import (
"context"
"encoding/binary"
"fmt"
"time"
@ -86,17 +85,15 @@ func (client *Client) Register(hash []byte) *pb.DownloaderResponse {
}
// PushNewBlock will send the lastest verified blow to registered nodes
func (client *Client) PushNewBlock(peerID uint32, blockHash []byte, timeout bool) *pb.DownloaderResponse {
func (client *Client) PushNewBlock(peerAddress [20]byte, blockHash []byte, timeout bool) *pb.DownloaderResponse {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
peerHash := make([]byte, 4)
binary.BigEndian.PutUint32(peerHash, peerID)
request := &pb.DownloaderRequest{Type: pb.DownloaderRequest_NEWBLOCK}
request.BlockHash = make([]byte, len(blockHash))
copy(request.BlockHash, blockHash)
request.PeerHash = make([]byte, len(peerHash))
copy(request.PeerHash, peerHash)
request.PeerHash = make([]byte, len(peerAddress))
copy(request.PeerHash, peerAddress[:])
if timeout {
request.Type = pb.DownloaderRequest_REGISTERTIMEOUT

@ -35,6 +35,7 @@ const (
type SyncPeerConfig struct {
ip string
port string
peerHash []byte
client *downloader.Client
blockHashes [][]byte // block hashes before node doing sync
newBlocks []*types.Block // blocks after node doing sync
@ -109,10 +110,7 @@ func GetServicePort(nodePort string) string {
// AddNewBlock will add newly received block into state syncing queue
func (ss *StateSync) AddNewBlock(peerHash []byte, block *types.Block) {
for i, pc := range ss.syncConfig.peers {
pid := utils.GetUniqueIDFromIPPort(pc.ip, GetServicePort(pc.port))
ph := make([]byte, 4)
binary.BigEndian.PutUint32(ph, pid)
if bytes.Compare(ph, peerHash) != 0 {
if bytes.Compare(pc.peerHash, peerHash) != 0 {
continue
}
pc.mux.Lock()

@ -9,6 +9,8 @@ import (
"sync"
"time"
"github.com/harmony-one/harmony/crypto/bls"
"github.com/ethereum/go-ethereum/log"
"github.com/harmony-one/harmony/api/client"
proto_node "github.com/harmony-one/harmony/api/proto/node"
@ -78,12 +80,12 @@ func main() {
panic(err)
}
peerPriKey, peerPubKey := utils.GenKey(*ip, *port)
if peerPriKey == nil || peerPubKey == nil {
peerPubKey := bls.RandPrivateKey().GetPublicKey()
if peerPubKey == nil {
panic(fmt.Errorf("generate key error"))
}
selfPeer := p2p.Peer{IP: *ip, Port: *port, ValidatorID: -1, ConsensusPubKey: peerPubKey}
selfPeer := p2p.Peer{IP: *ip, Port: *port, ConsensusPubKey: peerPubKey}
// Init with LibP2P enabled, FIXME: (leochen) right now we support only one shard
shardIDs = append(shardIDs, 0)

@ -140,7 +140,7 @@ func createGlobalConfig() *nodeconfig.ConfigType {
} else {
stakingPriKey = contract.NewNodeAccounts[*accountIndex].Private
// TODO: use user supplied key
consensusPriKey, _ = utils.GenKey(*ip, *port)
consensusPriKey.SetByCSPRNG()
}
nodeConfig.StakingPriKey = node.StoreStakingKeyFromFile(*stakingKeyFile, stakingPriKey)
@ -167,7 +167,7 @@ func createGlobalConfig() *nodeconfig.ConfigType {
}
}
nodeConfig.SelfPeer = p2p.Peer{IP: *ip, Port: *port, ValidatorID: -1, ConsensusPubKey: nodeConfig.ConsensusPubKey}
nodeConfig.SelfPeer = p2p.Peer{IP: *ip, Port: *port, ConsensusPubKey: nodeConfig.ConsensusPubKey}
if *isLeader {
nodeConfig.StringRole = "leader"
nodeConfig.Leader = nodeConfig.SelfPeer

@ -34,8 +34,8 @@ type Consensus struct {
state State
// Commits collected from validators.
prepareSigs map[uint32]*bls.Sign
commitSigs map[uint32]*bls.Sign
prepareSigs map[string]*bls.Sign // key is the validator's address
commitSigs map[string]*bls.Sign // key is the validator's address
aggregatedPrepareSig *bls.Sign
aggregatedCommitSig *bls.Sign
prepareBitmap *bls_cosi.Mask
@ -65,8 +65,8 @@ type Consensus struct {
// Whether I am leader. False means I am validator
IsLeader bool
// Leader or validator Id - 4 byte
nodeID uint32
// Leader or validator address in hex
SelfAddress string
// Consensus Id (View Id) - 4 byte
consensusID uint32
// Blockhash - 32 byte
@ -182,11 +182,11 @@ func New(host p2p.Host, ShardID string, peers []p2p.Peer, leader p2p.Peer, blsPr
consensus.leader = leader
for _, peer := range peers {
consensus.validators.Store(utils.GetUniqueIDFromPeer(peer), peer)
consensus.validators.Store(peer.GetAddressHex(), peer)
}
consensus.prepareSigs = map[uint32]*bls.Sign{}
consensus.commitSigs = map[uint32]*bls.Sign{}
consensus.prepareSigs = map[string]*bls.Sign{}
consensus.commitSigs = map[string]*bls.Sign{}
// Initialize cosign bitmap
allPublicKeys := make([]*bls.PublicKey, 0)
@ -207,7 +207,7 @@ func New(host p2p.Host, ShardID string, peers []p2p.Peer, leader p2p.Peer, blsPr
// For now use socket address as ID
// TODO: populate Id derived from address
consensus.nodeID = utils.GetUniqueIDFromPeer(selfPeer)
consensus.SelfAddress = selfPeer.GetAddressHex()
if blsPriKey != nil {
consensus.priKey = blsPriKey
@ -278,16 +278,16 @@ func (consensus *Consensus) checkConsensusMessage(message *msg_pb.Message, publi
return nil
}
// Gets the validator peer based on validator ID.
func (consensus *Consensus) getValidatorPeerByID(validatorID uint32) *p2p.Peer {
v, ok := consensus.validators.Load(validatorID)
// GetPeerByAddress the validator peer based on validator Address.
func (consensus *Consensus) GetPeerByAddress(validatorAddress string) *p2p.Peer {
v, ok := consensus.validators.Load(validatorAddress)
if !ok {
utils.GetLogInstance().Warn("Unrecognized validator", "validatorID", validatorID, "consensus", consensus)
utils.GetLogInstance().Warn("Unrecognized validator", "validatorAddress", validatorAddress, "consensus", consensus)
return nil
}
value, ok := v.(p2p.Peer)
if !ok {
utils.GetLogInstance().Warn("Invalid validator", "validatorID", validatorID, "consensus", consensus)
utils.GetLogInstance().Warn("Invalid validator", "validatorAddress", validatorAddress, "consensus", consensus)
return nil
}
return &value
@ -377,8 +377,8 @@ func (consensus *Consensus) GetCommitSigsArray() []*bls.Sign {
// ResetState resets the state of the consensus
func (consensus *Consensus) ResetState() {
consensus.state = Finished
consensus.prepareSigs = map[uint32]*bls.Sign{}
consensus.commitSigs = map[uint32]*bls.Sign{}
consensus.prepareSigs = map[string]*bls.Sign{}
consensus.commitSigs = map[string]*bls.Sign{}
prepareBitmap, _ := bls_cosi.NewMask(consensus.PublicKeys, consensus.leader.ConsensusPubKey)
commitBitmap, _ := bls_cosi.NewMask(consensus.PublicKeys, consensus.leader.ConsensusPubKey)
@ -400,8 +400,8 @@ func (consensus *Consensus) String() string {
} else {
duty = "VLD" // validator
}
return fmt.Sprintf("[duty:%s, PubKey:%s, ShardID:%v, nodeID:%v, state:%s]",
duty, hex.EncodeToString(consensus.PubKey.Serialize()), consensus.ShardID, consensus.nodeID, consensus.state)
return fmt.Sprintf("[duty:%s, PubKey:%s, ShardID:%v, Address:%v, state:%s]",
duty, hex.EncodeToString(consensus.PubKey.Serialize()), consensus.ShardID, consensus.SelfAddress, consensus.state)
}
// AddPeers adds new peers into the validator map of the consensus
@ -410,12 +410,9 @@ 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(peer.GetAddressHex())
if !ok {
if peer.ValidatorID == -1 {
peer.ValidatorID = int(consensus.uniqueIDInstance.GetUniqueID())
}
consensus.validators.Store(utils.GetUniqueIDFromPeer(*peer), *peer)
consensus.validators.Store(peer.GetAddressHex(), *peer)
consensus.pubKeyLock.Lock()
consensus.PublicKeys = append(consensus.PublicKeys, peer.ConsensusPubKey)
consensus.pubKeyLock.Unlock()
@ -494,7 +491,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, "VID", p.ValidatorID, "Key", str2)
utils.GetLogInstance().Debug("validator:", "IP", p.IP, "Port", p.Port, "address", p.GetAddressHex(), "Key", str2)
count++
return true
}
@ -600,22 +597,9 @@ func accumulateRewards(config *params.ChainConfig, state *state.DB, header *type
// TODO: implement mining rewards
}
// GetNodeID returns the nodeID
func (consensus *Consensus) GetNodeID() uint32 {
return consensus.nodeID
}
// GetPeerFromID will get peer from peerID, bool value in return true means success and false means fail
func (consensus *Consensus) GetPeerFromID(peerID uint32) (p2p.Peer, bool) {
v, ok := consensus.validators.Load(peerID)
if !ok {
return p2p.Peer{}, false
}
value, ok := v.(p2p.Peer)
if !ok {
return p2p.Peer{}, false
}
return value, true
// GetSelfAddress returns the address in hex
func (consensus *Consensus) GetSelfAddress() string {
return consensus.SelfAddress
}
// Populates the common basic fields for all consensus message.
@ -627,10 +611,10 @@ func (consensus *Consensus) populateMessageFields(request *msg_pb.ConsensusReque
// 32 byte block hash
request.BlockHash = consensus.blockHash[:]
// 4 byte sender id
request.SenderId = uint32(consensus.nodeID)
// sender address
request.SenderAddress = consensus.SelfAddress
utils.GetLogInstance().Debug("[populateMessageFields]", "myConsensusID", consensus.consensusID, "SenderId", request.SenderId)
utils.GetLogInstance().Debug("[populateMessageFields]", "myConsensusID", consensus.consensusID, "SenderAddress", request.SenderAddress)
}
// Signs the consensus message and returns the marshaled message.

@ -2,7 +2,6 @@ package consensus
import (
"encoding/hex"
"strconv"
"time"
"github.com/ethereum/go-ethereum/rlp"
@ -139,7 +138,7 @@ func (consensus *Consensus) startConsensus(newBlock *types.Block) {
consensus.state = AnnounceDone
// Leader sign the block hash itself
consensus.prepareSigs[consensus.nodeID] = consensus.priKey.SignHash(consensus.blockHash[:])
consensus.prepareSigs[consensus.SelfAddress] = consensus.priKey.SignHash(consensus.blockHash[:])
// Construct broadcast p2p message
utils.GetLogInstance().Warn("[Consensus]", "sent announce message", len(msgToSend))
@ -150,7 +149,7 @@ func (consensus *Consensus) startConsensus(newBlock *types.Block) {
func (consensus *Consensus) processPrepareMessage(message *msg_pb.Message) {
consensusMsg := message.GetConsensus()
validatorID := consensusMsg.SenderId
validatorAddress := consensusMsg.SenderAddress
prepareSig := consensusMsg.Payload
prepareSigs := consensus.prepareSigs
@ -159,27 +158,27 @@ func (consensus *Consensus) processPrepareMessage(message *msg_pb.Message) {
consensus.mutex.Lock()
defer consensus.mutex.Unlock()
validatorPeer := consensus.getValidatorPeerByID(validatorID)
validatorPeer := consensus.GetPeerByAddress(validatorAddress)
if validatorPeer == nil {
utils.GetLogInstance().Error("Invalid validator", "validatorID", validatorID)
utils.GetLogInstance().Error("Invalid validator", "validatorAddress", validatorAddress)
return
}
if err := consensus.checkConsensusMessage(message, validatorPeer.ConsensusPubKey); err != nil {
utils.GetLogInstance().Debug("Failed to check the validator message", "validatorID", validatorID)
utils.GetLogInstance().Debug("Failed to check the validator message", "validatorAddress", validatorAddress)
return
}
// proceed only when the message is not received before
_, ok := prepareSigs[validatorID]
_, ok := prepareSigs[validatorAddress]
if ok {
utils.GetLogInstance().Debug("Already received prepare message from the validator", "validatorID", validatorID)
utils.GetLogInstance().Debug("Already received prepare message from the validator", "validatorAddress", validatorAddress)
return
}
if len(prepareSigs) >= ((len(consensus.PublicKeys)*2)/3 + 1) {
utils.GetLogInstance().Debug("Received additional prepare message", "validatorID", validatorID)
utils.GetLogInstance().Debug("Received additional prepare message", "validatorAddress", validatorAddress)
return
}
@ -187,17 +186,17 @@ func (consensus *Consensus) processPrepareMessage(message *msg_pb.Message) {
var sign bls.Sign
err := sign.Deserialize(prepareSig)
if err != nil {
utils.GetLogInstance().Error("Failed to deserialize bls signature", "validatorID", validatorID)
utils.GetLogInstance().Error("Failed to deserialize bls signature", "validatorAddress", validatorAddress)
return
}
if !sign.VerifyHash(validatorPeer.ConsensusPubKey, consensus.blockHash[:]) {
utils.GetLogInstance().Error("Received invalid BLS signature", "validatorID", validatorID)
utils.GetLogInstance().Error("Received invalid BLS signature", "validatorAddress", validatorAddress)
return
}
utils.GetLogInstance().Debug("Received new prepare signature", "numReceivedSoFar", len(prepareSigs), "validatorID", validatorID, "PublicKeys", len(consensus.PublicKeys))
prepareSigs[validatorID] = &sign
utils.GetLogInstance().Debug("Received new prepare signature", "numReceivedSoFar", len(prepareSigs), "validatorAddress", validatorAddress, "PublicKeys", len(consensus.PublicKeys))
prepareSigs[validatorAddress] = &sign
prepareBitmap.SetKey(validatorPeer.ConsensusPubKey, true) // Set the bitmap indicating that this validator signed.
targetState := PreparedDone
@ -216,7 +215,7 @@ func (consensus *Consensus) processPrepareMessage(message *msg_pb.Message) {
// Leader sign the multi-sig and bitmap (for commit phase)
multiSigAndBitmap := append(aggSig.Serialize(), prepareBitmap.Bitmap...)
consensus.commitSigs[consensus.nodeID] = consensus.priKey.SignHash(multiSigAndBitmap)
consensus.commitSigs[consensus.SelfAddress] = consensus.priKey.SignHash(multiSigAndBitmap)
}
}
@ -224,21 +223,21 @@ func (consensus *Consensus) processPrepareMessage(message *msg_pb.Message) {
func (consensus *Consensus) processCommitMessage(message *msg_pb.Message) {
consensusMsg := message.GetConsensus()
validatorID := consensusMsg.SenderId
validatorAddress := consensusMsg.SenderAddress
commitSig := consensusMsg.Payload
consensus.mutex.Lock()
defer consensus.mutex.Unlock()
validatorPeer := consensus.getValidatorPeerByID(validatorID)
validatorPeer := consensus.GetPeerByAddress(validatorAddress)
if validatorPeer == nil {
utils.GetLogInstance().Error("Invalid validator", "validatorID", validatorID)
utils.GetLogInstance().Error("Invalid validator", "validatorAddress", validatorAddress)
return
}
if err := consensus.checkConsensusMessage(message, validatorPeer.ConsensusPubKey); err != nil {
utils.GetLogInstance().Debug("Failed to check the validator message", "validatorID", validatorID)
utils.GetLogInstance().Debug("Failed to check the validator message", "validatorAddress", validatorAddress)
return
}
@ -246,14 +245,14 @@ func (consensus *Consensus) processCommitMessage(message *msg_pb.Message) {
commitBitmap := consensus.commitBitmap
// proceed only when the message is not received before
_, ok := commitSigs[validatorID]
_, ok := commitSigs[validatorAddress]
if ok {
utils.GetLogInstance().Debug("Already received commit message from the validator", "validatorID", validatorID)
utils.GetLogInstance().Debug("Already received commit message from the validator", "validatorAddress", validatorAddress)
return
}
if len((commitSigs)) >= ((len(consensus.PublicKeys)*2)/3 + 1) {
utils.GetLogInstance().Debug("Received additional new commit message", "validatorID", strconv.Itoa(int(validatorID)))
utils.GetLogInstance().Debug("Received additional new commit message", "validatorAddress", validatorAddress)
return
}
@ -261,17 +260,17 @@ func (consensus *Consensus) processCommitMessage(message *msg_pb.Message) {
var sign bls.Sign
err := sign.Deserialize(commitSig)
if err != nil {
utils.GetLogInstance().Debug("Failed to deserialize bls signature", "validatorID", validatorID)
utils.GetLogInstance().Debug("Failed to deserialize bls signature", "validatorAddress", validatorAddress)
return
}
aggSig := bls_cosi.AggregateSig(consensus.GetPrepareSigsArray())
if !sign.VerifyHash(validatorPeer.ConsensusPubKey, append(aggSig.Serialize(), consensus.prepareBitmap.Bitmap...)) {
utils.GetLogInstance().Error("Received invalid BLS signature", "validatorID", validatorID)
utils.GetLogInstance().Error("Received invalid BLS signature", "validatorAddress", validatorAddress)
return
}
utils.GetLogInstance().Debug("Received new commit message", "numReceivedSoFar", len(commitSigs), "validatorID", strconv.Itoa(int(validatorID)))
commitSigs[validatorID] = &sign
utils.GetLogInstance().Debug("Received new commit message", "numReceivedSoFar", len(commitSigs), "validatorAddress", validatorAddress)
commitSigs[validatorAddress] = &sign
// Set the bitmap indicating that this validator signed.
commitBitmap.SetKey(validatorPeer.ConsensusPubKey, true)

@ -37,10 +37,12 @@ func TestConstructAnnounceMessage(test *testing.T) {
}
func TestConstructPreparedMessage(test *testing.T) {
leaderPriKey, leaderPubKey := utils.GenKey("127.0.0.1", "6000")
leaderPriKey := bls.RandPrivateKey()
leaderPubKey := leaderPriKey.GetPublicKey()
leader := p2p.Peer{IP: "127.0.0.1", Port: "6000", ConsensusPubKey: leaderPubKey}
validatorPriKey, validatorPubKey := utils.GenKey("127.0.0.1", "5555")
validatorPriKey := bls.RandPrivateKey()
validatorPubKey := leaderPriKey.GetPublicKey()
validator := p2p.Peer{IP: "127.0.0.1", Port: "5555", ConsensusPubKey: validatorPubKey}
priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902")
host, err := p2pimpl.NewHost(&leader, priKey)
@ -51,8 +53,8 @@ func TestConstructPreparedMessage(test *testing.T) {
consensus.blockHash = [32]byte{}
message := "test string"
consensus.prepareSigs[0] = leaderPriKey.Sign(message)
consensus.prepareSigs[1] = validatorPriKey.Sign(message)
consensus.prepareSigs["0"] = leaderPriKey.Sign(message)
consensus.prepareSigs["1"] = validatorPriKey.Sign(message)
consensus.prepareBitmap.SetKey(leaderPubKey, true)
consensus.prepareBitmap.SetKey(validatorPubKey, true)

@ -32,8 +32,10 @@ func TestProcessMessageLeaderPrepare(test *testing.T) {
defer ctrl.Finish()
leader := p2p.Peer{IP: ip, Port: "7777"}
var priKey *bls.SecretKey
priKey, leader.ConsensusPubKey = utils.GenKey(leader.IP, leader.Port)
leaderPriKey := bls_cosi.RandPrivateKey()
leaderPubKey := leaderPriKey.GetPublicKey()
leader.ConsensusPubKey = leaderPubKey
validators := make([]p2p.Peer, 3)
hosts := make([]p2p.Host, 3)
@ -41,8 +43,9 @@ func TestProcessMessageLeaderPrepare(test *testing.T) {
validatorsPriKeys := [3]*bls.SecretKey{}
for i := 0; i < 3; i++ {
port := fmt.Sprintf("%d", 7788+i)
validators[i] = p2p.Peer{IP: ip, Port: port, ValidatorID: i + 1}
validatorsPriKeys[i], validators[i].ConsensusPubKey = utils.GenKey(validators[i].IP, validators[i].Port)
validators[i] = p2p.Peer{IP: ip, Port: port}
validatorsPriKeys[i] = bls_cosi.RandPrivateKey()
validators[i].ConsensusPubKey = validatorsPriKeys[i].GetPublicKey()
}
m := mock_host.NewMockHost(ctrl)
@ -51,7 +54,7 @@ func TestProcessMessageLeaderPrepare(test *testing.T) {
m.EXPECT().GetSelfPeer().Return(leader)
m.EXPECT().SendMessageToGroups([]p2p.GroupID{p2p.GroupIDBeacon}, gomock.Any())
consensusLeader := New(m, "0", validators, leader, priKey)
consensusLeader := New(m, "0", validators, leader, leaderPriKey)
consensusLeader.blockHash = blockHash
consensusValidators := make([]*Consensus, 3)
@ -80,8 +83,8 @@ func TestProcessMessageLeaderPrepareInvalidSignature(test *testing.T) {
defer ctrl.Finish()
leader := p2p.Peer{IP: ip, Port: "7777"}
var priKey *bls.SecretKey
priKey, leader.ConsensusPubKey = utils.GenKey(leader.IP, leader.Port)
leaderPriKey := bls_cosi.RandPrivateKey()
leader.ConsensusPubKey = leaderPriKey.GetPublicKey()
validators := make([]p2p.Peer, 3)
hosts := make([]p2p.Host, 3)
@ -89,8 +92,9 @@ func TestProcessMessageLeaderPrepareInvalidSignature(test *testing.T) {
validatorKeys := [3]*bls.SecretKey{}
for i := 0; i < 3; i++ {
port := fmt.Sprintf("%d", 7788+i)
validators[i] = p2p.Peer{IP: ip, Port: port, ValidatorID: i + 1}
validatorKeys[i], validators[i].ConsensusPubKey = utils.GenKey(validators[i].IP, validators[i].Port)
validators[i] = p2p.Peer{IP: ip, Port: port}
validatorKeys[i] = bls_cosi.RandPrivateKey()
validators[i].ConsensusPubKey = validatorKeys[i].GetPublicKey()
}
m := mock_host.NewMockHost(ctrl)
@ -98,7 +102,7 @@ func TestProcessMessageLeaderPrepareInvalidSignature(test *testing.T) {
// Anything else will fail.
m.EXPECT().GetSelfPeer().Return(leader)
consensusLeader := New(m, "0", validators, leader, priKey)
consensusLeader := New(m, "0", validators, leader, leaderPriKey)
consensusLeader.blockHash = blockHash
consensusValidators := make([]*Consensus, 3)
@ -136,8 +140,8 @@ func TestProcessMessageLeaderCommit(test *testing.T) {
defer ctrl.Finish()
leader := p2p.Peer{IP: ip, Port: "8889"}
var priKey *bls.SecretKey
priKey, leader.ConsensusPubKey = utils.GenKey(leader.IP, leader.Port)
leaderPriKey := bls_cosi.RandPrivateKey()
leader.ConsensusPubKey = leaderPriKey.GetPublicKey()
validators := make([]p2p.Peer, 3)
hosts := make([]p2p.Host, 3)
@ -145,8 +149,9 @@ func TestProcessMessageLeaderCommit(test *testing.T) {
validatorKeys := [3]*bls.SecretKey{}
for i := 0; i < 3; i++ {
port := fmt.Sprintf("%d", 8788+i)
validators[i] = p2p.Peer{IP: ip, Port: port, ValidatorID: i + 1}
validatorKeys[i], validators[i].ConsensusPubKey = utils.GenKey(validators[i].IP, validators[i].Port)
validators[i] = p2p.Peer{IP: ip, Port: port}
validatorKeys[i] = bls_cosi.RandPrivateKey()
validators[i].ConsensusPubKey = validatorKeys[i].GetPublicKey()
}
m := mock_host.NewMockHost(ctrl)
@ -164,12 +169,12 @@ func TestProcessMessageLeaderCommit(test *testing.T) {
hosts[i] = host
}
consensusLeader := New(m, "0", validators, leader, priKey)
consensusLeader := New(m, "0", validators, leader, leaderPriKey)
consensusLeader.state = PreparedDone
consensusLeader.blockHash = blockHash
consensusLeader.OnConsensusDone = func(newBlock *types.Block) {}
consensusLeader.block, _ = rlp.EncodeToBytes(types.NewBlock(&types.Header{}, nil, nil))
consensusLeader.prepareSigs[consensusLeader.nodeID] = consensusLeader.priKey.SignHash(consensusLeader.blockHash[:])
consensusLeader.prepareSigs[consensusLeader.SelfAddress] = consensusLeader.priKey.SignHash(consensusLeader.blockHash[:])
aggSig := bls_cosi.AggregateSig(consensusLeader.GetPrepareSigsArray())
multiSigAndBitmap := append(aggSig.Serialize(), consensusLeader.prepareBitmap.Bitmap...)

@ -35,11 +35,11 @@ func TestNew(test *testing.T) {
}
func TestRemovePeers(t *testing.T) {
_, pk1 := utils.GenKey("1", "1")
_, pk2 := utils.GenKey("2", "2")
_, pk3 := utils.GenKey("3", "3")
_, pk4 := utils.GenKey("4", "4")
_, pk5 := utils.GenKey("5", "5")
pk1 := bls.RandPrivateKey().GetPublicKey()
pk2 := bls.RandPrivateKey().GetPublicKey()
pk3 := bls.RandPrivateKey().GetPublicKey()
pk4 := bls.RandPrivateKey().GetPublicKey()
pk5 := bls.RandPrivateKey().GetPublicKey()
p1 := p2p.Peer{IP: "127.0.0.1", Port: "19901", ConsensusPubKey: pk1}
p2 := p2p.Peer{IP: "127.0.0.1", Port: "19902", ConsensusPubKey: pk2}
@ -67,18 +67,21 @@ func TestRemovePeers(t *testing.T) {
}
func TestGetPeerFromID(t *testing.T) {
leader := p2p.Peer{IP: "127.0.0.1", Port: "9902"}
validator := p2p.Peer{IP: "127.0.0.1", Port: "9905"}
leaderPriKey := bls.RandPrivateKey()
leaderPubKey := leaderPriKey.GetPublicKey()
validatorPubKey := bls.RandPrivateKey().GetPublicKey()
leader := p2p.Peer{IP: "127.0.0.1", Port: "9902", ConsensusPubKey: leaderPubKey}
validator := p2p.Peer{IP: "127.0.0.1", Port: "9905", ConsensusPubKey: validatorPubKey}
priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902")
host, err := p2pimpl.NewHost(&leader, priKey)
if err != nil {
t.Fatalf("newhost failure: %v", err)
}
consensus := New(host, "0", []p2p.Peer{leader, validator}, leader, bls.RandPrivateKey())
leaderID := utils.GetUniqueIDFromIPPort(leader.IP, leader.Port)
validatorID := utils.GetUniqueIDFromIPPort(validator.IP, validator.Port)
l, _ := consensus.GetPeerFromID(leaderID)
v, _ := consensus.GetPeerFromID(validatorID)
consensus := New(host, "0", []p2p.Peer{leader, validator}, leader, leaderPriKey)
leaderAddress := utils.GetAddressFromBlsPubKey(leader.ConsensusPubKey)
validatorAddress := utils.GetAddressFromBlsPubKey(validator.ConsensusPubKey)
l := consensus.GetPeerByAddress(leaderAddress.Hex())
v := consensus.GetPeerByAddress(validatorAddress.Hex())
if l.IP != leader.IP || l.Port != leader.Port {
t.Errorf("leader IP not equal")
}
@ -98,7 +101,7 @@ func TestPopulateMessageFields(t *testing.T) {
consensus := New(host, "0", []p2p.Peer{leader, validator}, leader, bls.RandPrivateKey())
consensus.consensusID = 2
consensus.blockHash = blockHash
consensus.nodeID = 3
consensus.SelfAddress = "fake address"
msg := &msg_pb.Message{
Request: &msg_pb.Message_Consensus{
@ -114,7 +117,7 @@ func TestPopulateMessageFields(t *testing.T) {
if !bytes.Equal(consensusMsg.BlockHash[:], blockHash[:]) {
t.Errorf("Block hash is not populated correctly")
}
if consensusMsg.SenderId != 3 {
if consensusMsg.SenderAddress != "fake address" {
t.Errorf("Sender ID is not populated correctly")
}
}
@ -130,7 +133,7 @@ func TestSignAndMarshalConsensusMessage(t *testing.T) {
consensus := New(host, "0", []p2p.Peer{leader, validator}, leader, bls.RandPrivateKey())
consensus.consensusID = 2
consensus.blockHash = blockHash
consensus.nodeID = 3
consensus.SelfAddress = "fake address"
msg := &msg_pb.Message{}
marshaledMessage, err := consensus.signAndMarshalConsensusMessage(msg)

@ -75,7 +75,7 @@ func (consensus *Consensus) ProcessMessageValidator(payload []byte) {
// Processes the announce message sent from the leader
func (consensus *Consensus) processAnnounceMessage(message *msg_pb.Message) {
utils.GetLogInstance().Info("Received Announce Message", "nodeID", consensus.nodeID)
utils.GetLogInstance().Info("Received Announce Message", "ValidatorAddress", consensus.SelfAddress)
consensusMsg := message.GetConsensus()
@ -130,13 +130,13 @@ func (consensus *Consensus) processAnnounceMessage(message *msg_pb.Message) {
// Processes the prepared message sent from the leader
func (consensus *Consensus) processPreparedMessage(message *msg_pb.Message) {
utils.GetLogInstance().Info("Received Prepared Message", "nodeID", consensus.nodeID)
utils.GetLogInstance().Info("Received Prepared Message", "ValidatorAddress", consensus.SelfAddress)
consensusMsg := message.GetConsensus()
consensusID := consensusMsg.ConsensusId
blockHash := consensusMsg.BlockHash
leaderID := consensusMsg.SenderId
leaderID := consensusMsg.SenderAddress
messagePayload := consensusMsg.Payload
//#### Read payload data
@ -193,11 +193,11 @@ func (consensus *Consensus) processPreparedMessage(message *msg_pb.Message) {
// Processes the committed message sent from the leader
func (consensus *Consensus) processCommittedMessage(message *msg_pb.Message) {
utils.GetLogInstance().Warn("Received Committed Message", "nodeID", consensus.nodeID)
utils.GetLogInstance().Warn("Received Committed Message", "ValidatorAddress", consensus.SelfAddress)
consensusMsg := message.GetConsensus()
consensusID := consensusMsg.ConsensusId
leaderID := consensusMsg.SenderId
leaderID := consensusMsg.SenderAddress
messagePayload := consensusMsg.Payload
//#### Read payload data

@ -5,8 +5,6 @@ import (
"testing"
"time"
"github.com/harmony-one/bls/ffi/go/bls"
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/params"
@ -55,15 +53,15 @@ func TestProcessMessageValidatorAnnounce(test *testing.T) {
defer ctrl.Finish()
leader := p2p.Peer{IP: "127.0.0.1", Port: "9982"}
var leaderPriKey *bls.SecretKey
leaderPriKey, leader.ConsensusPubKey = utils.GenKey(leader.IP, leader.Port)
leaderPriKey := bls_cosi.RandPrivateKey()
leader.ConsensusPubKey = leaderPriKey.GetPublicKey()
validator1 := p2p.Peer{IP: "127.0.0.1", Port: "9984", ValidatorID: 1}
_, validator1.ConsensusPubKey = utils.GenKey(validator1.IP, validator1.Port)
validator2 := p2p.Peer{IP: "127.0.0.1", Port: "9986", ValidatorID: 2}
_, validator2.ConsensusPubKey = utils.GenKey(validator2.IP, validator2.Port)
validator3 := p2p.Peer{IP: "127.0.0.1", Port: "9988", ValidatorID: 3}
_, validator3.ConsensusPubKey = utils.GenKey(validator3.IP, validator3.Port)
validator1 := p2p.Peer{IP: "127.0.0.1", Port: "9984"}
validator1.ConsensusPubKey = bls_cosi.RandPrivateKey().GetPublicKey()
validator2 := p2p.Peer{IP: "127.0.0.1", Port: "9986"}
validator2.ConsensusPubKey = bls_cosi.RandPrivateKey().GetPublicKey()
validator3 := p2p.Peer{IP: "127.0.0.1", Port: "9988"}
validator3.ConsensusPubKey = bls_cosi.RandPrivateKey().GetPublicKey()
m := mock_host.NewMockHost(ctrl)
// Asserts that the first and only call to Bar() is passed 99.
@ -110,15 +108,15 @@ func TestProcessMessageValidatorPrepared(test *testing.T) {
defer ctrl.Finish()
leader := p2p.Peer{IP: "127.0.0.1", Port: "7782"}
var leaderPriKey *bls.SecretKey
leaderPriKey, leader.ConsensusPubKey = utils.GenKey(leader.IP, leader.Port)
leaderPriKey := bls_cosi.RandPrivateKey()
leader.ConsensusPubKey = leaderPriKey.GetPublicKey()
validator1 := p2p.Peer{IP: "127.0.0.1", Port: "7784", ValidatorID: 1}
_, validator1.ConsensusPubKey = utils.GenKey(validator1.IP, validator1.Port)
validator2 := p2p.Peer{IP: "127.0.0.1", Port: "7786", ValidatorID: 2}
_, validator2.ConsensusPubKey = utils.GenKey(validator2.IP, validator2.Port)
validator3 := p2p.Peer{IP: "127.0.0.1", Port: "7788", ValidatorID: 3}
_, validator3.ConsensusPubKey = utils.GenKey(validator3.IP, validator3.Port)
validator1 := p2p.Peer{IP: "127.0.0.1", Port: "7784"}
validator1.ConsensusPubKey = bls_cosi.RandPrivateKey().GetPublicKey()
validator2 := p2p.Peer{IP: "127.0.0.1", Port: "7786"}
validator2.ConsensusPubKey = bls_cosi.RandPrivateKey().GetPublicKey()
validator3 := p2p.Peer{IP: "127.0.0.1", Port: "7788"}
validator3.ConsensusPubKey = bls_cosi.RandPrivateKey().GetPublicKey()
m := mock_host.NewMockHost(ctrl)
// Asserts that the first and only call to Bar() is passed 99.
@ -139,7 +137,7 @@ func TestProcessMessageValidatorPrepared(test *testing.T) {
copy(consensusLeader.blockHash[:], hashBytes[:])
announceMsg := consensusLeader.constructAnnounceMessage()
consensusLeader.prepareSigs[consensusLeader.nodeID] = consensusLeader.priKey.SignHash(consensusLeader.blockHash[:])
consensusLeader.prepareSigs[consensusLeader.SelfAddress] = consensusLeader.priKey.SignHash(consensusLeader.blockHash[:])
preparedMsg, _ := consensusLeader.constructPreparedMessage()
@ -179,15 +177,15 @@ func TestProcessMessageValidatorCommitted(test *testing.T) {
defer ctrl.Finish()
leader := p2p.Peer{IP: "127.0.0.1", Port: "7782"}
var leaderPriKey *bls.SecretKey
leaderPriKey, leader.ConsensusPubKey = utils.GenKey(leader.IP, leader.Port)
leaderPriKey := bls_cosi.RandPrivateKey()
leader.ConsensusPubKey = leaderPriKey.GetPublicKey()
validator1 := p2p.Peer{IP: "127.0.0.1", Port: "7784", ValidatorID: 1}
_, validator1.ConsensusPubKey = utils.GenKey(validator1.IP, validator1.Port)
validator2 := p2p.Peer{IP: "127.0.0.1", Port: "7786", ValidatorID: 2}
_, validator2.ConsensusPubKey = utils.GenKey(validator2.IP, validator2.Port)
validator3 := p2p.Peer{IP: "127.0.0.1", Port: "7788", ValidatorID: 3}
_, validator3.ConsensusPubKey = utils.GenKey(validator3.IP, validator3.Port)
validator1 := p2p.Peer{IP: "127.0.0.1", Port: "7784"}
validator1.ConsensusPubKey = bls_cosi.RandPrivateKey().GetPublicKey()
validator2 := p2p.Peer{IP: "127.0.0.1", Port: "7786"}
validator2.ConsensusPubKey = bls_cosi.RandPrivateKey().GetPublicKey()
validator3 := p2p.Peer{IP: "127.0.0.1", Port: "7788"}
validator3.ConsensusPubKey = bls_cosi.RandPrivateKey().GetPublicKey()
m := mock_host.NewMockHost(ctrl)
// Asserts that the first and only call to Bar() is passed 99.
@ -209,13 +207,13 @@ func TestProcessMessageValidatorCommitted(test *testing.T) {
copy(consensusLeader.blockHash[:], hashBytes[:])
announceMsg := consensusLeader.constructAnnounceMessage()
consensusLeader.prepareSigs[consensusLeader.nodeID] = consensusLeader.priKey.SignHash(consensusLeader.blockHash[:])
consensusLeader.prepareSigs[consensusLeader.SelfAddress] = consensusLeader.priKey.SignHash(consensusLeader.blockHash[:])
preparedMsg, _ := consensusLeader.constructPreparedMessage()
aggSig := bls_cosi.AggregateSig(consensusLeader.GetPrepareSigsArray())
multiSigAndBitmap := append(aggSig.Serialize(), consensusLeader.prepareBitmap.Bitmap...)
consensusLeader.commitSigs[consensusLeader.nodeID] = consensusLeader.priKey.SignHash(multiSigAndBitmap)
consensusLeader.commitSigs[consensusLeader.SelfAddress] = consensusLeader.priKey.SignHash(multiSigAndBitmap)
committedMsg, _ := consensusLeader.constructCommittedMessage()
// Get actual consensus messages.

@ -1728,7 +1728,7 @@ func (bc *BlockChain) ValidateNewShardState(block *types.Block, stakeInfo *map[c
if shardState.Hash() != block.Header().ShardStateHash {
return ErrShardStateNotMatch
}
utils.GetLogInstance().Debug("[resharding] validate new shard state success", "shardStateHash", shardState.Hash())
utils.GetLogInstance().Debug("[resharding] validate new shard state successfully", "shardStateHash", shardState.Hash())
return nil
}
@ -1740,7 +1740,7 @@ func (bc *BlockChain) StoreNewShardState(block *types.Block, stakeInfo *map[comm
hash := block.Hash()
number := block.NumberU64()
rawdb.WriteShardState(bc.db, hash, number, shardState)
utils.GetLogInstance().Debug("[Resharding] Saved new shard state success", "state", shardState)
utils.GetLogInstance().Debug("[Resharding] Saved new shard state successfully", "state", shardState)
}
return shardState
}

@ -5,14 +5,13 @@ import (
"testing"
"github.com/harmony-one/bls/ffi/go/bls"
"github.com/harmony-one/harmony/internal/utils"
)
// Test the basic functionality of a BLS multi-sig mask.
func TestNewMask(test *testing.T) {
_, pubKey1 := utils.GenKey("127.0.0.1", "5555")
_, pubKey2 := utils.GenKey("127.0.0.1", "6666")
_, pubKey3 := utils.GenKey("127.0.0.1", "7777")
pubKey1 := RandPrivateKey().GetPublicKey()
pubKey2 := RandPrivateKey().GetPublicKey()
pubKey3 := RandPrivateKey().GetPublicKey()
mask, err := NewMask([]*bls.PublicKey{pubKey1, pubKey2, pubKey3}, pubKey1)
@ -39,10 +38,10 @@ func TestNewMask(test *testing.T) {
}
func TestNewMaskWithAbsentPublicKey(test *testing.T) {
_, pubKey1 := utils.GenKey("127.0.0.1", "5555")
_, pubKey2 := utils.GenKey("127.0.0.1", "6666")
_, pubKey3 := utils.GenKey("127.0.0.1", "7777")
_, pubKey4 := utils.GenKey("127.0.0.1", "8190")
pubKey1 := RandPrivateKey().GetPublicKey()
pubKey2 := RandPrivateKey().GetPublicKey()
pubKey3 := RandPrivateKey().GetPublicKey()
pubKey4 := RandPrivateKey().GetPublicKey()
mask, err := NewMask([]*bls.PublicKey{pubKey1, pubKey2, pubKey3}, pubKey4)
@ -57,9 +56,9 @@ func TestNewMaskWithAbsentPublicKey(test *testing.T) {
}
func TestThreshHoldPolicy(test *testing.T) {
_, pubKey1 := utils.GenKey("127.0.0.1", "5555")
_, pubKey2 := utils.GenKey("127.0.0.1", "6666")
_, pubKey3 := utils.GenKey("127.0.0.1", "7777")
pubKey1 := RandPrivateKey().GetPublicKey()
pubKey2 := RandPrivateKey().GetPublicKey()
pubKey3 := RandPrivateKey().GetPublicKey()
mask, err := NewMask([]*bls.PublicKey{pubKey1, pubKey2, pubKey3}, pubKey1)
@ -93,9 +92,9 @@ func TestThreshHoldPolicy(test *testing.T) {
}
func TestCompletePolicy(test *testing.T) {
_, pubKey1 := utils.GenKey("127.0.0.1", "5555")
_, pubKey2 := utils.GenKey("127.0.0.1", "6666")
_, pubKey3 := utils.GenKey("127.0.0.1", "7777")
pubKey1 := RandPrivateKey().GetPublicKey()
pubKey2 := RandPrivateKey().GetPublicKey()
pubKey3 := RandPrivateKey().GetPublicKey()
mask, err := NewMask([]*bls.PublicKey{pubKey1, pubKey2, pubKey3}, pubKey1)
@ -162,10 +161,10 @@ func TestAggregateMasks(test *testing.T) {
}
func TestEnableKeyFunctions(test *testing.T) {
_, pubKey1 := utils.GenKey("127.0.0.1", "5555")
_, pubKey2 := utils.GenKey("127.0.0.1", "6666")
_, pubKey3 := utils.GenKey("127.0.0.1", "7777")
_, pubKey4 := utils.GenKey("127.0.0.1", "1234")
pubKey1 := RandPrivateKey().GetPublicKey()
pubKey2 := RandPrivateKey().GetPublicKey()
pubKey3 := RandPrivateKey().GetPublicKey()
pubKey4 := RandPrivateKey().GetPublicKey()
mask, err := NewMask([]*bls.PublicKey{pubKey1, pubKey2, pubKey3}, pubKey1)
@ -212,8 +211,8 @@ func TestEnableKeyFunctions(test *testing.T) {
}
func TestCopyParticipatingMask(test *testing.T) {
_, pubKey1 := utils.GenKey("127.0.0.1", "5555")
_, pubKey2 := utils.GenKey("127.0.0.1", "6666")
pubKey1 := RandPrivateKey().GetPublicKey()
pubKey2 := RandPrivateKey().GetPublicKey()
mask, _ := NewMask([]*bls.PublicKey{pubKey1, pubKey2}, pubKey1)
@ -226,8 +225,8 @@ func TestCopyParticipatingMask(test *testing.T) {
}
func TestSetMask(test *testing.T) {
_, pubKey1 := utils.GenKey("127.0.0.1", "5555")
_, pubKey2 := utils.GenKey("127.0.0.1", "6666")
pubKey1 := RandPrivateKey().GetPublicKey()
pubKey2 := RandPrivateKey().GetPublicKey()
mask, _ := NewMask([]*bls.PublicKey{pubKey1, pubKey2}, pubKey1)

@ -19,7 +19,7 @@ import (
// DRand is the main struct which contains state for the distributed randomness protocol.
type DRand struct {
vrfs *map[uint32][]byte
vrfs *map[string][]byte // Key is the address hex
bitmap *bls_cosi.Mask
pRand *[32]byte
rand *[32]byte
@ -32,7 +32,7 @@ type DRand struct {
// 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 string, value is p2p.Peer
// Leader's address
leader p2p.Peer
@ -52,8 +52,8 @@ type DRand struct {
// Whether I am leader. False means I am validator
IsLeader bool
// Leader or validator Id - 4 byte
nodeID uint32
// Leader or validator address
SelfAddress string
// The p2p host used to send/receive p2p messages
host p2p.Host
@ -82,10 +82,10 @@ func New(host p2p.Host, ShardID string, peers []p2p.Peer, leader p2p.Peer, confi
dRand.leader = leader
for _, peer := range peers {
dRand.validators.Store(utils.GetUniqueIDFromPeer(peer), peer)
dRand.validators.Store(peer.GetAddressHex(), peer)
}
dRand.vrfs = &map[uint32][]byte{}
dRand.vrfs = &map[string][]byte{}
// Initialize cosign bitmap
allPublicKeys := make([]*bls.PublicKey, 0)
@ -103,8 +103,7 @@ func New(host p2p.Host, ShardID string, peers []p2p.Peer, leader p2p.Peer, confi
dRand.rand = nil
// For now use socket address as ID
// TODO: populate Id derived from address
dRand.nodeID = utils.GetUniqueIDFromPeer(selfPeer)
dRand.SelfAddress = selfPeer.GetAddressHex()
// Set private key for myself so that I can sign messages.
if blsPriKey != nil {
@ -132,9 +131,9 @@ func (dRand *DRand) AddPeers(peers []*p2p.Peer) int {
count := 0
for _, peer := range peers {
_, ok := dRand.validators.Load(utils.GetUniqueIDFromPeer(*peer))
_, ok := dRand.validators.Load(peer.GetAddressHex())
if !ok {
dRand.validators.Store(utils.GetUniqueIDFromPeer(*peer), *peer)
dRand.validators.Store(peer.GetAddressHex(), *peer)
dRand.pubKeyLock.Lock()
dRand.PublicKeys = append(dRand.PublicKeys, peer.ConsensusPubKey)
dRand.pubKeyLock.Unlock()
@ -216,15 +215,15 @@ func verifyMessageSig(signerPubKey *bls.PublicKey, message *msg_pb.Message) erro
}
// Gets the validator peer based on validator ID.
func (dRand *DRand) getValidatorPeerByID(validatorID uint32) *p2p.Peer {
v, ok := dRand.validators.Load(validatorID)
func (dRand *DRand) getValidatorPeerByAddress(validatorAddress string) *p2p.Peer {
v, ok := dRand.validators.Load(validatorAddress)
if !ok {
utils.GetLogInstance().Warn("Unrecognized validator", "validatorID", validatorID, "dRand", dRand)
utils.GetLogInstance().Warn("Unrecognized validator", "validatorAddress", validatorAddress, "dRand", dRand)
return nil
}
value, ok := v.(p2p.Peer)
if !ok {
utils.GetLogInstance().Warn("Invalid validator", "validatorID", validatorID, "dRand", dRand)
utils.GetLogInstance().Warn("Invalid validator", "validatorAddress", validatorAddress, "dRand", dRand)
return nil
}
return &value
@ -232,7 +231,7 @@ func (dRand *DRand) getValidatorPeerByID(validatorID uint32) *p2p.Peer {
// ResetState resets the state of the randomness protocol
func (dRand *DRand) ResetState() {
dRand.vrfs = &map[uint32][]byte{}
dRand.vrfs = &map[string][]byte{}
bitmap, _ := bls_cosi.NewMask(dRand.PublicKeys, dRand.leader.ConsensusPubKey)
dRand.bitmap = bitmap

@ -72,7 +72,7 @@ func (dRand *DRand) init(epochBlock *types.Block) {
// Leader commit vrf itself
rand, proof := dRand.vrf(dRand.blockHash)
(*dRand.vrfs)[dRand.nodeID] = append(rand[:], proof...)
(*dRand.vrfs)[dRand.SelfAddress] = append(rand[:], proof...)
utils.GetLogInstance().Info("[DRG] sent init", "msg", msgToSend, "leader.PubKey", dRand.leader.ConsensusPubKey)
dRand.host.SendMessageToGroups([]p2p.GroupID{p2p.GroupIDBeacon}, host.ConstructP2pMessage(byte(17), msgToSend))
@ -106,11 +106,11 @@ func (dRand *DRand) processCommitMessage(message *msg_pb.Message) {
defer dRand.mutex.Unlock()
drandMsg := message.GetDrand()
validatorID := drandMsg.SenderId
validatorPeer := dRand.getValidatorPeerByID(validatorID)
validatorAddress := drandMsg.SenderAddress
validatorPeer := dRand.getValidatorPeerByAddress(validatorAddress)
vrfs := dRand.vrfs
if len((*vrfs)) >= ((len(dRand.PublicKeys))/3 + 1) {
utils.GetLogInstance().Debug("Received additional randomness commit message", "validatorID", validatorID)
utils.GetLogInstance().Debug("Received additional randomness commit message", "validatorAddress", validatorAddress)
return
}
@ -130,18 +130,18 @@ func (dRand *DRand) processCommitMessage(message *msg_pb.Message) {
expectedRand, err := pubKey.ProofToHash(dRand.blockHash[:], proof)
if err != nil || !bytes.Equal(expectedRand[:], rand) {
utils.GetLogInstance().Error("[DRAND] Failed to verify the VRF", "error", err, "validatorID", validatorID, "expectedRand", expectedRand, "receivedRand", rand)
utils.GetLogInstance().Error("[DRAND] Failed to verify the VRF", "error", err, "validatorAddress", validatorAddress, "expectedRand", expectedRand, "receivedRand", rand)
return
}
utils.GetLogInstance().Debug("Received new VRF commit", "numReceivedSoFar", len((*vrfs)), "validatorID", validatorID, "PublicKeys", len(dRand.PublicKeys))
utils.GetLogInstance().Debug("Received new VRF commit", "numReceivedSoFar", len((*vrfs)), "validatorAddress", validatorAddress, "PublicKeys", len(dRand.PublicKeys))
(*vrfs)[validatorID] = drandMsg.Payload
(*vrfs)[validatorAddress] = drandMsg.Payload
dRand.bitmap.SetKey(validatorPeer.ConsensusPubKey, true) // Set the bitmap indicating that this validator signed.
if len((*vrfs)) >= ((len(dRand.PublicKeys))/3 + 1) {
// Construct pRand and initiate consensus on it
utils.GetLogInstance().Debug("[DRAND] {BINGO} Received enough randomness commit", "numReceivedSoFar", len((*vrfs)), "validatorID", validatorID, "PublicKeys", len(dRand.PublicKeys))
utils.GetLogInstance().Debug("[DRAND] {BINGO} Received enough randomness commit", "numReceivedSoFar", len((*vrfs)), "validatorAddress", validatorAddress, "PublicKeys", len(dRand.PublicKeys))
pRnd := [32]byte{}
// Bitwise XOR on all the submitted vrfs

@ -18,7 +18,7 @@ func (dRand *DRand) constructInitMessage() []byte {
}
drandMsg := message.GetDrand()
drandMsg.SenderId = dRand.nodeID
drandMsg.SenderAddress = dRand.SelfAddress
drandMsg.BlockHash = dRand.blockHash[:]
// Don't need the payload in init message
marshaledMessage, err := dRand.signAndMarshalDRandMessage(message)

@ -32,8 +32,8 @@ func TestNew(test *testing.T) {
}
func TestGetValidatorPeers(test *testing.T) {
leader := p2p.Peer{IP: "127.0.0.1", Port: "9902"}
validator := p2p.Peer{IP: "127.0.0.1", Port: "9905"}
leader := p2p.Peer{IP: "127.0.0.1", Port: "9902", ConsensusPubKey: bls2.RandPrivateKey().GetPublicKey()}
validator := p2p.Peer{IP: "127.0.0.1", Port: "9905", ConsensusPubKey: bls2.RandPrivateKey().GetPublicKey()}
priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902")
host, err := p2pimpl.NewHost(&leader, priKey)
if err != nil {
@ -48,13 +48,13 @@ func TestGetValidatorPeers(test *testing.T) {
countValidatorPeers := len(dRand.GetValidatorPeers())
if countValidatorPeers != 2 {
test.Error("Count of validator peers doesn't match")
test.Error("Count of validator peers doesn't match, got", countValidatorPeers)
}
}
func TestAddPeers(test *testing.T) {
leader := p2p.Peer{IP: "127.0.0.1", Port: "9902"}
validator := p2p.Peer{IP: "127.0.0.1", Port: "9905"}
leader := p2p.Peer{IP: "127.0.0.1", Port: "9902", ConsensusPubKey: bls2.RandPrivateKey().GetPublicKey()}
validator := p2p.Peer{IP: "127.0.0.1", Port: "9905", ConsensusPubKey: bls2.RandPrivateKey().GetPublicKey()}
priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902")
host, err := p2pimpl.NewHost(&leader, priKey)
if err != nil {
@ -79,28 +79,29 @@ func TestAddPeers(test *testing.T) {
}
func TestGetValidatorByPeerId(test *testing.T) {
leader := p2p.Peer{IP: "127.0.0.1", Port: "9902"}
validator := p2p.Peer{IP: "127.0.0.1", Port: "9905"}
leaderPriKey := bls2.RandPrivateKey()
leaderPubKey := bls2.RandPrivateKey().GetPublicKey()
validatorPubKey := bls2.RandPrivateKey().GetPublicKey()
leader := p2p.Peer{IP: "127.0.0.1", Port: "9902", ConsensusPubKey: leaderPubKey}
validator := p2p.Peer{IP: "127.0.0.1", Port: "9905", ConsensusPubKey: validatorPubKey}
priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902")
host, err := p2pimpl.NewHost(&leader, priKey)
if err != nil {
test.Fatalf("newhost failure: %v", err)
}
dRand := New(host, "0", []p2p.Peer{leader, validator}, leader, nil, true, bls2.RandPrivateKey())
dRand := New(host, "0", []p2p.Peer{leader, validator}, leader, nil, true, leaderPriKey)
if !dRand.IsLeader {
test.Error("dRand should belong to a leader")
}
validatorID := utils.GetUniqueIDFromPeer(validator)
validatorAddress := utils.GetAddressFromBlsPubKey(validatorPubKey).Hex()
if dRand.getValidatorPeerByID(validatorID) == nil {
if dRand.getValidatorPeerByAddress(validatorAddress) == nil {
test.Error("Unable to get validator by Peerid")
}
newPeer := p2p.Peer{IP: "127.0.0.1", Port: "9907"}
newPeerID := utils.GetUniqueIDFromPeer(newPeer)
if dRand.getValidatorPeerByID(newPeerID) != nil {
if dRand.getValidatorPeerByAddress("random address") != nil {
test.Error("Found validator for absent validatorId")
}
}
@ -144,8 +145,8 @@ func TestUpdatePublicKeys(test *testing.T) {
}
dRand := New(host, "0", []p2p.Peer{leader, validator}, leader, nil, true, bls2.RandPrivateKey())
_, pubKey1 := utils.GenKey("127.0.0.1", "5555")
_, pubKey2 := utils.GenKey("127.0.0.1", "6666")
pubKey1 := bls2.RandPrivateKey().GetPublicKey()
pubKey2 := bls2.RandPrivateKey().GetPublicKey()
publicKeys := []*bls.PublicKey{pubKey1, pubKey2}
@ -179,7 +180,7 @@ func TestVerifyMessageSig(test *testing.T) {
},
}
drandMsg := message.GetDrand()
drandMsg.SenderId = dRand.nodeID
drandMsg.SenderAddress = dRand.SelfAddress
drandMsg.BlockHash = dRand.blockHash[:]
dRand.signDRandMessage(message)

@ -18,7 +18,7 @@ func (dRand *DRand) constructCommitMessage(vrf [32]byte, proof []byte) []byte {
}
drandMsg := message.GetDrand()
drandMsg.SenderId = dRand.nodeID
drandMsg.SenderAddress = dRand.SelfAddress
drandMsg.BlockHash = dRand.blockHash[:]
drandMsg.BlockHash = dRand.blockHash[:]
drandMsg.Payload = append(vrf[:], proof...)

@ -14,8 +14,9 @@ import (
"strconv"
"sync"
"github.com/ethereum/go-ethereum/common"
"github.com/harmony-one/bls/ffi/go/bls"
"github.com/harmony-one/harmony/p2p"
p2p_crypto "github.com/libp2p/go-libp2p-crypto"
)
@ -69,12 +70,6 @@ func ConvertFixedDataIntoByteArray(data interface{}) []byte {
return buff.Bytes()
}
// GetUniqueIDFromPeer ...
// TODO(minhdoan): this is probably a hack, probably needs some strong non-collision hash.
func GetUniqueIDFromPeer(peer p2p.Peer) uint32 {
return GetUniqueIDFromIPPort(peer.IP, peer.Port)
}
// GetUniqueIDFromIPPort --
func GetUniqueIDFromIPPort(ip, port string) uint32 {
reg, err := regexp.Compile("[^0-9]+")
@ -86,20 +81,12 @@ func GetUniqueIDFromIPPort(ip, port string) uint32 {
return uint32(value)
}
// GenKey generates a bls key pair given ip and port.
func GenKey(ip, port string) (*bls.SecretKey, *bls.PublicKey) {
nodeIDBytes := make([]byte, 32)
binary.LittleEndian.PutUint32(nodeIDBytes, GetUniqueIDFromIPPort(ip, port))
privateKey := bls.SecretKey{}
err := privateKey.SetLittleEndian(nodeIDBytes)
if err != nil {
log.Print("failed to set private key", err)
return nil, nil
}
priKey := &privateKey
pubKey := privateKey.GetPublicKey()
return priKey, pubKey
// GetAddressFromBlsPubKey return the address object from bls pub key.
func GetAddressFromBlsPubKey(pubKey *bls.PublicKey) common.Address {
addr := common.Address{}
addrBytes := pubKey.GetAddress()
addr.SetBytes(addrBytes[:])
return addr
}
// GenKeyP2P generates a pair of RSA keys used in libp2p host

@ -1,13 +1,10 @@
package utils
import (
"bytes"
"encoding/hex"
"net"
"os"
"testing"
"github.com/harmony-one/harmony/p2p"
crypto "github.com/libp2p/go-libp2p-crypto"
"github.com/stretchr/testify/assert"
)
@ -47,19 +44,6 @@ func TestAllocateShard(t *testing.T) {
assert.Equal(t, num, 3, "error")
}
// Test for GenKey
func TestGenKey(t *testing.T) {
priKey, pubKey := GenKey("3.3.3.3", "3456")
if priKey == nil || pubKey == nil {
t.Error("Failed to create keys for BLS sig")
}
pubKeyBytes, _ := hex.DecodeString("ca6247713431a59cbadfe282b36cb13746b6e5c5db6e5972a10a83adffdf23f8aab246229cb3050e061e1024aa9b6518e200dd9663a8c855e596f1007150aa0672e6f40d073947aa027e8ffe8e89d894ca3916f80fdb350f4b8643f6ff99510c")
if bytes.Compare(pubKey.Serialize(), pubKeyBytes) != 0 {
t.Errorf("Unexpected public key: %s", hex.EncodeToString(pubKey.Serialize()))
}
}
// Test for GenKeyP2P, noted the length of private key can be random
// thus we don't test it here.
func TestGenKeyP2P(t *testing.T) {
@ -86,17 +70,6 @@ func TestGenKeyP2PRand(t *testing.T) {
}
}
// Test for GetUniqueIDFromPeer
func TestGetUniqueIDFromPeer(t *testing.T) {
peer := p2p.Peer{IP: "1.1.1.1", Port: "123"}
assert.Equal(t, GetUniqueIDFromPeer(peer), uint32(1111123), "should be equal to 1111123")
}
// Test for GetUniqueIDFromIPPort
func TestGetUniqueIDFromIPPort(t *testing.T) {
assert.Equal(t, GetUniqueIDFromIPPort("1.1.1.1", "123"), uint32(1111123), "should be equal to 1111123")
}
// Test for SavePrivateKey/LoadPrivateKey functions
func TestSaveLoadPrivateKey(t *testing.T) {
pk, _, err := GenKeyP2P("127.0.0.1", "8888")

@ -3,6 +3,8 @@ package node
import (
"testing"
"github.com/harmony-one/harmony/crypto/bls"
"github.com/harmony-one/harmony/consensus"
"github.com/harmony-one/harmony/internal/utils"
"github.com/harmony-one/harmony/p2p"
@ -10,7 +12,7 @@ import (
)
func prepareNode(t *testing.T) *Node {
_, pubKey := utils.GenKey("1", "2")
pubKey := bls.RandPrivateKey().GetPublicKey()
leader := p2p.Peer{IP: "127.0.0.1", Port: "8882", ConsensusPubKey: pubKey}
validator := p2p.Peer{IP: "127.0.0.1", Port: "8885"}
priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902")

@ -3,12 +3,13 @@ package node
import (
"bytes"
"crypto/ecdsa"
"encoding/binary"
"fmt"
"os"
"sync"
"time"
"github.com/harmony-one/bls/ffi/go/bls"
"github.com/harmony-one/harmony/contracts/structs"
"github.com/harmony-one/harmony/contracts"
@ -127,7 +128,7 @@ type Node struct {
downloaderServer *downloader.Server
stateSync *syncing.StateSync
beaconSync *syncing.StateSync
peerRegistrationRecord map[uint32]*syncConfig // record registration time (unixtime) of peers begin in syncing
peerRegistrationRecord map[string]*syncConfig // record registration time (unixtime) of peers begin in syncing
// The p2p host used to send/receive p2p messages
host p2p.Host
@ -275,7 +276,7 @@ func New(host p2p.Host, consensusObj *consensus.Consensus, db ethdb.Database) *N
}
// Setup initial state of syncing.
node.peerRegistrationRecord = make(map[uint32]*syncConfig)
node.peerRegistrationRecord = make(map[string]*syncConfig)
node.OfflinePeers = make(chan p2p.Peer)
go node.RemovePeersHandler()
@ -303,9 +304,6 @@ func (node *Node) AddPeers(peers []*p2p.Peer) int {
node.host.AddPeer(p)
continue
}
if node.SelfPeer.ValidatorID == -1 && p.IP == node.SelfPeer.IP && p.Port == node.SelfPeer.Port {
node.SelfPeer.ValidatorID = p.ValidatorID
}
}
if count > 0 {
@ -370,30 +368,32 @@ func (node *Node) CalculateResponse(request *downloader_pb.DownloaderRequest) (*
node.stateSync.AddNewBlock(request.PeerHash, &blockObj)
case downloader_pb.DownloaderRequest_REGISTER:
peerID := binary.BigEndian.Uint32(request.PeerHash)
if _, ok := node.peerRegistrationRecord[peerID]; ok {
pubKey := bls.PublicKey{}
pubKey.Deserialize(request.PeerHash)
peerAddress := utils.GetAddressFromBlsPubKey(&pubKey).Hex()
if _, ok := node.peerRegistrationRecord[peerAddress]; ok {
response.Type = downloader_pb.DownloaderResponse_FAIL
utils.GetLogInstance().Warn("[SYNC] peerRegistration record already exists", "peerID", peerID)
utils.GetLogInstance().Warn("[SYNC] peerRegistration record already exists", "peerAddress", peerAddress)
return response, nil
} else if len(node.peerRegistrationRecord) >= maxBroadcastNodes {
response.Type = downloader_pb.DownloaderResponse_FAIL
utils.GetLogInstance().Warn("[SYNC] maximum registration limit exceeds", "peerID", peerID)
utils.GetLogInstance().Warn("[SYNC] maximum registration limit exceeds", "peerAddress", peerAddress)
return response, nil
} else {
peer, ok := node.Consensus.GetPeerFromID(peerID)
if !ok {
utils.GetLogInstance().Warn("[SYNC] unable to get peer from peerID", "peerID", peerID)
peer := node.Consensus.GetPeerByAddress(peerAddress)
if peer == nil {
utils.GetLogInstance().Warn("[SYNC] unable to get peer from peerID", "peerAddress", peerAddress)
}
client := downloader.ClientSetup(peer.IP, GetSyncingPort(peer.Port))
if client == nil {
utils.GetLogInstance().Warn("[SYNC] unable to setup client for peerID", "peerID", peerID)
utils.GetLogInstance().Warn("[SYNC] unable to setup client for peerID", "peerAddress", peerAddress)
return response, nil
}
config := &syncConfig{timestamp: time.Now().UnixNano(), client: client}
node.stateMutex.Lock()
node.peerRegistrationRecord[peerID] = config
node.peerRegistrationRecord[peerAddress] = config
node.stateMutex.Unlock()
utils.GetLogInstance().Debug("[SYNC] register peerID success", "peerID", peerID)
utils.GetLogInstance().Debug("[SYNC] register peerID success", "peerAddress", peerAddress)
response.Type = downloader_pb.DownloaderResponse_SUCCESS
}
case downloader_pb.DownloaderRequest_REGISTERTIMEOUT:

@ -365,7 +365,6 @@ func (node *Node) pingMessageHandler(msgPayload []byte, sender string) int {
peer.IP = ping.Node.IP
peer.Port = ping.Node.Port
peer.PeerID = ping.Node.PeerID
peer.ValidatorID = ping.Node.ValidatorID
peer.ConsensusPubKey = nil
if ping.Node.PubKey != nil {
@ -383,13 +382,13 @@ func (node *Node) pingMessageHandler(msgPayload []byte, sender string) int {
node.host.ConnectHostPeer(*peer)
if ping.Node.Role == proto_node.ClientRole {
utils.GetLogInstance().Info("Add Client Peer to Node", "Node", node.Consensus.GetNodeID(), "Client", peer)
utils.GetLogInstance().Info("Add Client Peer to Node", "Address", node.Consensus.GetSelfAddress(), "Client", peer)
node.ClientPeer = peer
return 0
}
// Add to Node's peer list anyway
utils.GetLogInstance().Info("Add Peer to Node", "Node", node.Consensus.GetNodeID(), "Pear", peer)
utils.GetLogInstance().Info("Add Peer to Node", "Address", node.Consensus.GetSelfAddress(), "Pear", peer)
node.AddPeers([]*p2p.Peer{peer})
return 1
@ -473,7 +472,6 @@ func (node *Node) pongMessageHandler(msgPayload []byte) int {
peer := new(p2p.Peer)
peer.IP = p.IP
peer.Port = p.Port
peer.ValidatorID = p.ValidatorID
peer.PeerID = p.PeerID
peer.ConsensusPubKey = &bls.PublicKey{}

@ -3,6 +3,8 @@ package node
import (
"testing"
"github.com/harmony-one/harmony/crypto/bls"
"github.com/harmony-one/harmony/consensus"
"github.com/harmony-one/harmony/internal/utils"
"github.com/harmony-one/harmony/p2p"
@ -10,7 +12,7 @@ import (
)
func TestAddNewBlock(t *testing.T) {
_, pubKey := utils.GenKey("1", "2")
pubKey := bls.RandPrivateKey().GetPublicKey()
leader := p2p.Peer{IP: "127.0.0.1", Port: "9882", ConsensusPubKey: pubKey}
validator := p2p.Peer{IP: "127.0.0.1", Port: "9885"}
priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902")
@ -33,7 +35,7 @@ func TestAddNewBlock(t *testing.T) {
}
func TestVerifyNewBlock(t *testing.T) {
_, pubKey := utils.GenKey("1", "2")
pubKey := bls.RandPrivateKey().GetPublicKey()
leader := p2p.Peer{IP: "127.0.0.1", Port: "8882", ConsensusPubKey: pubKey}
validator := p2p.Peer{IP: "127.0.0.1", Port: "8885"}
priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902")

@ -178,22 +178,22 @@ func (node *Node) SendNewBlockToUnsync() {
}
// really need to have a unique id independent of ip/port
selfPeerID := utils.GetUniqueIDFromIPPort(node.SelfPeer.IP, node.SelfPeer.Port)
utils.GetLogInstance().Debug("[SYNC] peerRegistration Record", "peerID", selfPeerID, "number", len(node.peerRegistrationRecord))
selfPeerAddress := node.Consensus.SelfAddress
utils.GetLogInstance().Debug("[SYNC] peerRegistration Record", "selfPeerAddress", selfPeerAddress, "number", len(node.peerRegistrationRecord))
for peerID, config := range node.peerRegistrationRecord {
elapseTime := time.Now().UnixNano() - config.timestamp
if elapseTime > broadcastTimeout {
utils.GetLogInstance().Warn("[SYNC] SendNewBlockToUnsync to peer timeout", "peerID", peerID)
// send last time and delete
config.client.PushNewBlock(selfPeerID, blockHash, true)
config.client.PushNewBlock(node.Consensus.PubKey.GetAddress(), blockHash, true)
node.stateMutex.Lock()
node.peerRegistrationRecord[peerID].client.Close()
delete(node.peerRegistrationRecord, peerID)
node.stateMutex.Unlock()
continue
}
response := config.client.PushNewBlock(selfPeerID, blockHash, false)
response := config.client.PushNewBlock(node.Consensus.PubKey.GetAddress(), blockHash, false)
if response != nil && response.Type == downloader_pb.DownloaderResponse_INSYNC {
node.stateMutex.Lock()
node.peerRegistrationRecord[peerID].client.Close()

@ -6,6 +6,8 @@ import (
"testing"
"time"
bls2 "github.com/harmony-one/harmony/crypto/bls"
"github.com/harmony-one/bls/ffi/go/bls"
"github.com/harmony-one/harmony/drand"
@ -18,7 +20,7 @@ import (
)
func TestNewNode(t *testing.T) {
_, pubKey := utils.GenKey("1", "2")
pubKey := bls2.RandPrivateKey().GetPublicKey()
leader := p2p.Peer{IP: "127.0.0.1", Port: "8882", ConsensusPubKey: pubKey}
validator := p2p.Peer{IP: "127.0.0.1", Port: "8885"}
priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902")
@ -42,7 +44,7 @@ func TestNewNode(t *testing.T) {
}
func TestGetSyncingPeers(t *testing.T) {
_, pubKey := utils.GenKey("1", "2")
pubKey := bls2.RandPrivateKey().GetPublicKey()
leader := p2p.Peer{IP: "127.0.0.1", Port: "8882", ConsensusPubKey: pubKey}
validator := p2p.Peer{IP: "127.0.0.1", Port: "8885"}
priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902")
@ -76,16 +78,14 @@ func TestAddPeers(t *testing.T) {
IP: "127.0.0.1",
Port: "8888",
ConsensusPubKey: pubKey1,
ValidatorID: 1,
},
&p2p.Peer{
IP: "127.0.0.1",
Port: "9999",
ConsensusPubKey: pubKey2,
ValidatorID: 2,
},
}
_, pubKey := utils.GenKey("1", "2")
pubKey := bls2.RandPrivateKey().GetPublicKey()
leader := p2p.Peer{IP: "127.0.0.1", Port: "8982", ConsensusPubKey: pubKey}
validator := p2p.Peer{IP: "127.0.0.1", Port: "8985"}
priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902")
@ -111,26 +111,24 @@ func TestAddPeers(t *testing.T) {
}
func TestAddBeaconPeer(t *testing.T) {
_, pubKey1 := utils.GenKey("127.0.0.1", "2000")
_, pubKey2 := utils.GenKey("127.0.0.1", "8000")
pubKey1 := bls2.RandPrivateKey().GetPublicKey()
pubKey2 := bls2.RandPrivateKey().GetPublicKey()
peers1 := []*p2p.Peer{
&p2p.Peer{
IP: "127.0.0.1",
Port: "8888",
ConsensusPubKey: pubKey1,
ValidatorID: 1,
PeerID: "1234",
},
&p2p.Peer{
IP: "127.0.0.1",
Port: "9999",
ConsensusPubKey: pubKey2,
ValidatorID: 2,
PeerID: "4567",
},
}
_, pubKey := utils.GenKey("1", "2")
pubKey := bls2.RandPrivateKey().GetPublicKey()
leader := p2p.Peer{IP: "127.0.0.1", Port: "8982", ConsensusPubKey: pubKey}
validator := p2p.Peer{IP: "127.0.0.1", Port: "8985"}
priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902")
@ -201,7 +199,7 @@ func exitServer() {
}
func TestPingPongHandler(t *testing.T) {
_, pubKey := utils.GenKey("127.0.0.1", "8881")
pubKey := bls2.RandPrivateKey().GetPublicKey()
leader := p2p.Peer{IP: "127.0.0.1", Port: "8881", ConsensusPubKey: pubKey}
// validator := p2p.Peer{IP: "127.0.0.1", Port: "9991"}
priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902")

@ -4,6 +4,8 @@ import (
"math/big"
"testing"
"github.com/harmony-one/harmony/crypto/bls"
"github.com/harmony-one/harmony/contracts/structs"
"github.com/ethereum/go-ethereum/common"
@ -22,8 +24,7 @@ var (
)
func TestUpdateStakingList(t *testing.T) {
_, pubKey := utils.GenKey("1", "2")
pubKey := bls.RandPrivateKey().GetPublicKey()
leader := p2p.Peer{IP: "127.0.0.1", Port: "9882", ConsensusPubKey: pubKey}
validator := p2p.Peer{IP: "127.0.0.1", Port: "9885"}
priKey, _, _ := utils.GenKeyP2P("127.0.0.1", "9902")

@ -4,6 +4,8 @@ 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"
@ -17,7 +19,6 @@ type Peer struct {
IP string // IP address of the peer
Port string // Port number of the peer
ConsensusPubKey *bls.PublicKey // Public key of the peer, used for consensus signing
ValidatorID int // -1 is the default value, means not assigned any validator ID in the shard
Addrs []ma.Multiaddr // MultiAddress of the peer
PeerID libp2p_peer.ID // PeerID, the pubkey for communication
}
@ -25,3 +26,11 @@ 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