Merge pull request #111 from harmony-one/HAR-84_golint_fix_for_proto_node

HAR-84: golint fix of proto/node files
pull/113/head
Leo Chen 6 years ago committed by GitHub
commit f4ee9aa786
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 2
      benchmark.go
  2. 6
      client/config/config.go
  3. 4
      client/wallet/main.go
  4. 4
      client/wallet_v2/main.go
  5. 12
      consensus/consensus.go
  6. 8
      consensus/consensus_leader_msg_test.go
  7. 4
      consensus/consensus_test.go
  8. 8
      consensus/consensus_validator_msg_test.go
  9. 2
      node/node.go
  10. 8
      node/node_handler.go
  11. 26
      node/node_test.go
  12. 14
      p2p/peer.go
  13. 20
      proto/node/node.go
  14. 24
      proto/node/pingpong.go
  15. 6
      proto/node/pingpong_test.go
  16. 2
      syncing/syncing.go
  17. 2
      syncing/syncing_test.go
  18. 10
      utils/distribution_config.go
  19. 19
      utils/singleton.go
  20. 6
      utils/singleton_test.go
  21. 6
      utils/utils.go

@ -146,7 +146,7 @@ func main() {
selfPeer.PubKey = pubKey
var role string
if leader.Ip == *ip && leader.Port == *port {
if leader.IP == *ip && leader.Port == *port {
role = "leader"
} else {
role = "validator"

@ -36,7 +36,7 @@ func (config *Config) GetValidators() []p2p.Peer {
if entry.Role != "validator" {
continue
}
peer := p2p.Peer{Port: entry.Port, Ip: entry.IP}
peer := p2p.Peer{Port: entry.Port, IP: entry.IP}
peerList = append(peerList, peer)
}
return peerList
@ -49,7 +49,7 @@ func (config *Config) GetShardIDToLeaderMap() map[uint32]p2p.Peer {
if entry.Role == "leader" {
val, err := strconv.Atoi(entry.ShardID)
if err == nil {
shardIDLeaderMap[uint32(val)] = p2p.Peer{Ip: entry.IP, Port: entry.Port}
shardIDLeaderMap[uint32(val)] = p2p.Peer{IP: entry.IP, Port: entry.Port}
} else {
log.Print("[Generator] Error parsing the shard Id ", entry.ShardID)
}
@ -64,7 +64,7 @@ func (config *Config) GetClientPeer() *p2p.Peer {
if entry.Role != "client" {
continue
}
peer := p2p.Peer{Port: entry.Port, Ip: entry.IP}
peer := p2p.Peer{Port: entry.Port, IP: entry.IP}
return &peer
}
return nil

@ -236,7 +236,7 @@ func getShardIDToLeaderMap() map[uint32]p2p.Peer {
shardID := parts[3]
id, err := strconv.Atoi(shardID)
if err == nil {
shardIDLeaderMap[uint32(id)] = p2p.Peer{Ip: parts[0], Port: parts[1]}
shardIDLeaderMap[uint32(id)] = p2p.Peer{IP: parts[0], Port: parts[1]}
} else {
log.Print("[Generator] Error parsing the shard Id ", shardID)
}
@ -254,7 +254,7 @@ func CreateWalletServerNode() *node.Node {
clientPeer = configr.GetClientPeer()
} else {
shardIDLeaderMap = getShardIDToLeaderMap()
clientPeer = &p2p.Peer{Port: "127.0.0.1", Ip: "1234"}
clientPeer = &p2p.Peer{Port: "127.0.0.1", IP: "1234"}
}
walletNode := node.New(nil, nil)
walletNode.Client = client.NewClient(&shardIDLeaderMap)

@ -236,7 +236,7 @@ func getShardIDToLeaderMap() map[uint32]p2p.Peer {
shardID := parts[3]
id, err := strconv.Atoi(shardID)
if err == nil {
shardIDLeaderMap[uint32(id)] = p2p.Peer{Ip: parts[0], Port: parts[1]}
shardIDLeaderMap[uint32(id)] = p2p.Peer{IP: parts[0], Port: parts[1]}
} else {
log.Print("[Generator] Error parsing the shard Id ", shardID)
}
@ -254,7 +254,7 @@ func CreateWalletServerNode() *node.Node {
clientPeer = configr.GetClientPeer()
} else {
shardIDLeaderMap = getShardIDToLeaderMap()
clientPeer = &p2p.Peer{Port: "127.0.0.1", Ip: "1234"}
clientPeer = &p2p.Peer{Port: "127.0.0.1", IP: "1234"}
}
walletNode := node.New(nil, nil)
walletNode.Client = client.NewClient(&shardIDLeaderMap)

@ -88,7 +88,7 @@ type Consensus struct {
Log log.Logger
uniqueIDInstance *utils.UniqueValidatorId
uniqueIDInstance *utils.UniqueValidatorID
}
// BlockConsensusStatus used to keep track of the consensus status of multiple blocks received so far
@ -108,7 +108,7 @@ type BlockConsensusStatus struct {
func NewConsensus(ip, port, ShardID string, peers []p2p.Peer, leader p2p.Peer) *Consensus {
consensus := Consensus{}
if leader.Port == port && leader.Ip == ip {
if leader.Port == port && leader.IP == ip {
consensus.IsLeader = true
} else {
consensus.IsLeader = false
@ -146,7 +146,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(p2p.Peer{IP: ip, Port: port})
// Set private key for myself so that I can sign messages.
consensus.priKey = crypto.Ed25519Curve.Scalar().SetInt64(int64(consensus.nodeID))
@ -173,7 +173,7 @@ func NewConsensus(ip, port, ShardID string, peers []p2p.Peer, leader p2p.Peer) *
}
consensus.Log = log.New()
consensus.uniqueIDInstance = utils.GetUniqueValidatorIdInstance()
consensus.uniqueIDInstance = utils.GetUniqueValidatorIDInstance()
return &consensus
}
@ -242,7 +242,7 @@ func (consensus *Consensus) AddPeers(peers []p2p.Peer) int {
_, ok := consensus.validators.Load(utils.GetUniqueIdFromPeer(peer))
if !ok {
if peer.ValidatorID == -1 {
peer.ValidatorID = int(consensus.uniqueIDInstance.GetUniqueId())
peer.ValidatorID = int(consensus.uniqueIDInstance.GetUniqueID())
}
consensus.validators.Store(utils.GetUniqueIdFromPeer(peer), peer)
consensus.PublicKeys = append(consensus.PublicKeys, peer.PubKey)
@ -275,7 +275,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.PubKey)
consensus.Log.Debug("validator:", "IP", p.Ip, "Port", p.Port, "VID", p.ValidatorID, "Key", str2)
consensus.Log.Debug("validator:", "IP", p.IP, "Port", p.Port, "VID", p.ValidatorID, "Key", str2)
count++
return true
}

@ -10,8 +10,8 @@ import (
)
func TestConstructAnnounceMessage(test *testing.T) {
leader := p2p.Peer{Ip: "1", Port: "2"}
validator := p2p.Peer{Ip: "3", Port: "5"}
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.blockHash = [32]byte{}
header := consensus.blockHeader
@ -27,13 +27,13 @@ func TestConstructChallengeMessage(test *testing.T) {
priKeyInBytes := crypto.HashSha256("12")
leaderPriKey.UnmarshalBinary(priKeyInBytes[:])
leaderPubKey := pki.GetPublicKeyFromScalar(leaderPriKey)
leader := p2p.Peer{Ip: "1", Port: "2", PubKey: leaderPubKey}
leader := p2p.Peer{IP: "1", Port: "2", PubKey: leaderPubKey}
validatorPriKey := crypto.Ed25519Curve.Scalar()
priKeyInBytes = crypto.HashSha256("12")
validatorPriKey.UnmarshalBinary(priKeyInBytes[:])
validatorPubKey := pki.GetPublicKeyFromScalar(leaderPriKey)
validator := p2p.Peer{Ip: "3", Port: "5", PubKey: validatorPubKey}
validator := p2p.Peer{IP: "3", Port: "5", PubKey: validatorPubKey}
consensus := NewConsensus("1", "2", "0", []p2p.Peer{leader, validator}, leader)
consensus.blockHash = [32]byte{}

@ -7,8 +7,8 @@ import (
)
func TestNewConsensus(test *testing.T) {
leader := p2p.Peer{Ip: "1", Port: "2"}
validator := p2p.Peer{Ip: "3", Port: "5"}
leader := p2p.Peer{IP: "1", Port: "2"}
validator := p2p.Peer{IP: "3", Port: "5"}
consensus := NewConsensus("1", "2", "0", []p2p.Peer{leader, validator}, leader)
if consensus.consensusID != 0 {
test.Errorf("Consensus Id is initialized to the wrong value: %d", consensus.consensusID)

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

@ -358,7 +358,7 @@ func (node *Node) InitSyncingServer() {
// StartSyncingServer starts syncing server.
func (node *Node) StartSyncingServer() {
if port, err := strconv.Atoi(node.SelfPeer.Port); err == nil {
node.downloaderServer.Start(node.SelfPeer.Ip, fmt.Sprintf("%d", port-1000))
node.downloaderServer.Start(node.SelfPeer.IP, fmt.Sprintf("%d", port-1000))
} else {
node.log.Error("Wrong port format provided")
os.Exit(1)

@ -103,7 +103,7 @@ func (node *Node) NodeHandler(conn net.Conn) {
}
}
case proto.Node:
actionType := proto_node.NodeMessageType(msgType)
actionType := proto_node.MessageType(msgType)
switch actionType {
case proto_node.Transaction:
node.log.Info("NET: received message: Node/Transaction")
@ -242,7 +242,7 @@ FOR_LOOP:
}
msgType, err := proto.GetMessageType(content)
actionType := proto_node.NodeMessageType(msgType)
actionType := proto_node.MessageType(msgType)
if err != nil || actionType != proto_node.BlockchainSync {
node.log.Error("Failed in reading message type from syncing node", err)
return
@ -574,7 +574,7 @@ func (node *Node) pingMessageHandler(msgPayload []byte) int {
// node.log.Info("Ping", "Msg", ping)
peer := new(p2p.Peer)
peer.Ip = ping.Node.IP
peer.IP = ping.Node.IP
peer.Port = ping.Node.Port
peer.ValidatorID = ping.Node.ValidatorID
@ -614,7 +614,7 @@ func (node *Node) pongMessageHandler(msgPayload []byte) int {
for _, p := range pong.Peers {
peer := new(p2p.Peer)
peer.Ip = p.IP
peer.IP = p.IP
peer.Port = p.Port
peer.ValidatorID = p.ValidatorID

@ -16,8 +16,8 @@ import (
)
func TestNewNewNode(test *testing.T) {
leader := p2p.Peer{Ip: "1", Port: "2"}
validator := p2p.Peer{Ip: "3", Port: "5"}
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)
node := New(consensus, nil)
@ -43,8 +43,8 @@ func TestNewNewNode(test *testing.T) {
}
func TestCountNumTransactionsInBlockchain(test *testing.T) {
leader := p2p.Peer{Ip: "1", Port: "2"}
validator := p2p.Peer{Ip: "3", Port: "5"}
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)
node := New(consensus, nil)
@ -63,22 +63,22 @@ func TestAddPeers(test *testing.T) {
peers1 := []p2p.Peer{
{
Ip: "127.0.0.1",
IP: "127.0.0.1",
Port: "8888",
PubKey: pubKey1,
Ready: true,
ValidatorID: 1,
},
{
Ip: "127.0.0.1",
IP: "127.0.0.1",
Port: "9999",
PubKey: pubKey2,
Ready: false,
ValidatorID: 2,
},
}
leader := p2p.Peer{Ip: "1", Port: "2"}
validator := p2p.Peer{Ip: "3", Port: "5"}
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)
node := New(consensus, nil)
@ -99,7 +99,7 @@ func sendPingMessage(leader p2p.Peer) {
pubKey1 := pki.GetPublicKeyFromScalar(priKey1)
p1 := p2p.Peer{
Ip: "127.0.0.1",
IP: "127.0.0.1",
Port: "9999",
PubKey: pubKey1,
}
@ -118,14 +118,14 @@ func sendPongMessage(leader p2p.Peer) {
priKey1 := crypto.Ed25519Curve.Scalar().SetInt64(int64(333))
pubKey1 := pki.GetPublicKeyFromScalar(priKey1)
p1 := p2p.Peer{
Ip: "127.0.0.1",
IP: "127.0.0.1",
Port: "9998",
PubKey: pubKey1,
}
priKey2 := crypto.Ed25519Curve.Scalar().SetInt64(int64(999))
pubKey2 := pki.GetPublicKeyFromScalar(priKey2)
p2 := p2p.Peer{
Ip: "127.0.0.1",
IP: "127.0.0.1",
Port: "9999",
PubKey: pubKey2,
}
@ -148,8 +148,8 @@ 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"}
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, validator}, leader)
node := New(consensus, nil)

@ -15,7 +15,7 @@ import (
// Peer is the object for a p2p peer (node)
type Peer struct {
Ip string // Ip address of the peer
IP string // IP address of the peer
Port string // Port number of the peer
PubKey kyber.Point // Public key of the peer
Ready bool // Ready is true if the peer is ready to join consensus.
@ -23,13 +23,14 @@ type Peer struct {
// TODO(minhdoan, rj): use this Ready to not send/broadcast to this peer if it wasn't available.
}
// MaxBroadCast is the maximum number of neighbors to broadcast
const MaxBroadCast = 20
// SendMessage sends the message to the peer
func SendMessage(peer Peer, msg []byte) {
// Construct normal p2p message
content := ConstructP2pMessage(byte(0), msg)
go send(peer.Ip, peer.Port, content)
go send(peer.IP, peer.Port, content)
}
// BroadcastMessage sends the message to a list of peers
@ -45,7 +46,7 @@ func BroadcastMessage(peers []Peer, msg []byte) {
start := time.Now()
for _, peer := range peers {
peerCopy := peer
go send(peerCopy.Ip, peerCopy.Port, content)
go send(peerCopy.IP, peerCopy.Port, content)
}
log.Info("Broadcasting Done", "time spent(s)", time.Since(start).Seconds())
@ -56,6 +57,8 @@ func BroadcastMessage(peers []Peer, msg []byte) {
}
}
// SelectMyPeers chooses a list of peers based on the range of ValidatorID
// This is a quick hack of the current p2p networking model
func SelectMyPeers(peers []Peer, min int, max int) []Peer {
res := []Peer{}
for _, peer := range peers {
@ -66,7 +69,7 @@ func SelectMyPeers(peers []Peer, min int, max int) []Peer {
return res
}
// BroadcastMessage sends the message to a list of peers from a leader.
// BroadcastMessageFromLeader sends the message to a list of peers from a leader.
func BroadcastMessageFromLeader(peers []Peer, msg []byte) {
// TODO(minhdoan): Enable back for multicast.
peers = SelectMyPeers(peers, 1, MaxBroadCast)
@ -74,7 +77,7 @@ func BroadcastMessageFromLeader(peers []Peer, msg []byte) {
log.Info("Done sending from leader")
}
// BroadcastMessage sends the message to a list of peers from a validator.
// BroadcastMessageFromValidator sends the message to a list of peers from a validator.
func BroadcastMessageFromValidator(selfPeer Peer, peers []Peer, msg []byte) {
peers = SelectMyPeers(peers, selfPeer.ValidatorID*MaxBroadCast+1, (selfPeer.ValidatorID+1)*MaxBroadCast)
BroadcastMessage(peers, msg)
@ -147,6 +150,7 @@ func send(ip, port string, message []byte) {
log.Error("gave up sending a message", "addr", net.JoinHostPort(ip, port))
}
// DialWithSocketClient joins host port and establishes connection
func DialWithSocketClient(ip, port string) (conn net.Conn, err error) {
//log.Printf("Sending message to ip %s and port %s\n", ip, port)
addr := net.JoinHostPort(ip, port)

@ -14,15 +14,17 @@ import (
"github.com/harmony-one/harmony/proto"
)
// NodeMessageType is to indicate the specific type of message under Node category
type NodeMessageType byte
// MessageType is to indicate the specific type of message under Node category
type MessageType byte
// ProtocolVersion is a constant defined as the version of the Harmony protocol
const (
PROTOCOL_VERSION = 1
ProtocolVersion = 1
)
// Constant of the top level Message Type exchanged among nodes
const (
Transaction NodeMessageType = iota
Transaction MessageType = iota
Block
Client
Control
@ -41,6 +43,7 @@ type BlockchainSyncMessage struct {
// BlockchainSyncMessageType represents BlockchainSyncMessageType type.
type BlockchainSyncMessageType int
// Constant of blockchain sync-up message subtype
const (
Done BlockchainSyncMessageType = iota
GetLastBlockHashes
@ -50,27 +53,30 @@ const (
// TransactionMessageType representa the types of messages used for Node/Transaction
type TransactionMessageType int
// Constant of transaction message subtype
const (
Send TransactionMessageType = iota
Request
Unlock
)
// BlockMessageType represents the types of messages used for Node/Block
// BlockMessageType represents the type of messages used for Node/Block
type BlockMessageType int
// Block sync message subtype
const (
Sync BlockMessageType = iota
)
// The types of messages used for Node/Block
// ClientMessageType defines the type of messages used for Node/Block
type ClientMessageType int
// Constant of the client message subtype
const (
LookupUtxo ClientMessageType = iota
)
// The types of messages used for Node/Control
// ControlMessageType is the type of messages used for Node/Control
type ControlMessageType int
// ControlMessageType

@ -31,11 +31,13 @@ type nodeInfo struct {
ValidatorID int
}
// PingMessageType defines the data structure of the Ping message
type PingMessageType struct {
Version uint16 // version of the protocol
Node nodeInfo
}
// PongMessageType defines the data structure of the Pong message
type PongMessageType struct {
Version uint16 // version of the protocol
Peers []nodeInfo
@ -51,12 +53,13 @@ func (p PongMessageType) String() string {
return str
}
// NewPingMessage creates a new Ping message based on the p2p.Peer input
func NewPingMessage(peer p2p.Peer) *PingMessageType {
ping := new(PingMessageType)
var err error
ping.Version = PROTOCOL_VERSION
ping.Node.IP = peer.Ip
ping.Version = ProtocolVersion
ping.Node.IP = peer.IP
ping.Node.Port = peer.Port
ping.Node.ValidatorID = peer.ValidatorID
ping.Node.PubKey, err = peer.PubKey.MarshalBinary()
@ -69,17 +72,18 @@ func NewPingMessage(peer p2p.Peer) *PingMessageType {
return ping
}
// NewPongMessage creates a new Pong message based on a list of p2p.Peer and a list of publicKeys
func NewPongMessage(peers []p2p.Peer, pubKeys []kyber.Point) *PongMessageType {
pong := new(PongMessageType)
pong.PubKeys = make([][]byte, 0)
pong.Version = PROTOCOL_VERSION
pong.Version = ProtocolVersion
pong.Peers = make([]nodeInfo, 0)
var err error
for _, p := range peers {
n := nodeInfo{}
n.IP = p.Ip
n.IP = p.IP
n.Port = p.Port
n.ValidatorID = p.ValidatorID
n.PubKey, err = p.PubKey.MarshalBinary()
@ -103,7 +107,7 @@ func NewPongMessage(peers []p2p.Peer, pubKeys []kyber.Point) *PongMessageType {
return pong
}
// Deserialize Ping Message
// GetPingMessage deserializes the Ping Message from a list of byte
func GetPingMessage(payload []byte) (*PingMessageType, error) {
ping := new(PingMessageType)
@ -118,7 +122,7 @@ func GetPingMessage(payload []byte) (*PingMessageType, error) {
return ping, nil
}
// Deserialize Pong Message
// GetPongMessage deserializes the Pong Message from a list of byte
func GetPongMessage(payload []byte) (*PongMessageType, error) {
pong := new(PongMessageType)
pong.Peers = make([]nodeInfo, 0)
@ -136,12 +140,12 @@ func GetPongMessage(payload []byte) (*PongMessageType, error) {
}
// ConstructPingMessage contructs ping message from node to leader
func (ping PingMessageType) ConstructPingMessage() []byte {
func (p PingMessageType) ConstructPingMessage() []byte {
byteBuffer := bytes.NewBuffer([]byte{byte(proto.Node)})
byteBuffer.WriteByte(byte(PING))
encoder := gob.NewEncoder(byteBuffer)
err := encoder.Encode(ping)
err := encoder.Encode(p)
if err != nil {
log.Panic("Can't serialize Ping message", "error:", err)
return nil
@ -150,12 +154,12 @@ func (ping PingMessageType) ConstructPingMessage() []byte {
}
// ConstructPongMessage contructs pong message from leader to node
func (pong PongMessageType) ConstructPongMessage() []byte {
func (p PongMessageType) ConstructPongMessage() []byte {
byteBuffer := bytes.NewBuffer([]byte{byte(proto.Node)})
byteBuffer.WriteByte(byte(PONG))
encoder := gob.NewEncoder(byteBuffer)
err := encoder.Encode(pong)
err := encoder.Encode(p)
if err != nil {
log.Panic("Can't serialize Pong message", "error:", err)
return nil

@ -16,7 +16,7 @@ var (
priKey1 = crypto.Ed25519Curve.Scalar().SetInt64(int64(333))
pubKey1 = pki.GetPublicKeyFromScalar(priKey1)
p1 = p2p.Peer{
Ip: "127.0.0.1",
IP: "127.0.0.1",
Port: "9999",
ValidatorID: -1,
PubKey: pubKey1,
@ -28,14 +28,14 @@ var (
p2 = []p2p.Peer{
{
Ip: "127.0.0.1",
IP: "127.0.0.1",
Port: "8888",
PubKey: pubKey1,
Ready: true,
ValidatorID: -1,
},
{
Ip: "127.0.0.1",
IP: "127.0.0.1",
Port: "9999",
PubKey: pubKey2,
Ready: false,

@ -86,7 +86,7 @@ func (ss *StateSync) CreateSyncConfig(peers []p2p.Peer) {
}
for id := range ss.syncConfig.peers {
ss.syncConfig.peers[id] = SyncPeerConfig{
ip: peers[id].Ip,
ip: peers[id].IP,
port: peers[id].Port,
}
}

@ -113,7 +113,7 @@ func TestSyncing(t *testing.T) {
bc := &bc.Blockchain{}
peers := make([]p2p.Peer, len(fakeNodes))
for i := range peers {
peers[i].Ip = fakeNodes[i].ip
peers[i].IP = fakeNodes[i].ip
peers[i].Port = fakeNodes[i].port
}

@ -36,7 +36,7 @@ func (config *DistributionConfig) GetLeadersAndShardIDs() ([]p2p.Peer, []uint32)
var shardIDs []uint32
for _, entry := range config.config {
if entry.Role == "leader" {
peerList = append(peerList, p2p.Peer{Ip: entry.IP, Port: entry.Port})
peerList = append(peerList, p2p.Peer{IP: entry.IP, Port: entry.Port})
val, err := strconv.Atoi(entry.ShardID)
if err == nil {
shardIDs = append(shardIDs, uint32(val))
@ -53,7 +53,7 @@ func (config *DistributionConfig) GetClientPeer() *p2p.Peer {
if entry.Role != "client" {
continue
}
peer := p2p.Peer{Port: entry.Port, Ip: entry.IP}
peer := p2p.Peer{Port: entry.Port, IP: entry.IP}
return &peer
}
return nil
@ -116,7 +116,7 @@ func (config *DistributionConfig) GetPeers(ip, port, shardID string) []p2p.Peer
continue
}
// Get public key deterministically based on ip and port
peer := p2p.Peer{Port: entry.Port, Ip: entry.IP, ValidatorID: entry.ValidatorID}
peer := p2p.Peer{Port: entry.Port, IP: entry.IP, ValidatorID: entry.ValidatorID}
setKey(&peer)
peerList = append(peerList, peer)
}
@ -127,7 +127,7 @@ func (config *DistributionConfig) GetPeers(ip, port, shardID string) []p2p.Peer
func (config *DistributionConfig) GetSelfPeer(ip, port, shardID string) p2p.Peer {
for _, entry := range config.config {
if entry.IP == ip && entry.Port == port && entry.ShardID == shardID {
peer := p2p.Peer{Port: entry.Port, Ip: entry.IP, ValidatorID: entry.ValidatorID}
peer := p2p.Peer{Port: entry.Port, IP: entry.IP, ValidatorID: entry.ValidatorID}
return peer
}
}
@ -139,7 +139,7 @@ func (config *DistributionConfig) GetLeader(shardID string) p2p.Peer {
var leaderPeer p2p.Peer
for _, entry := range config.config {
if entry.Role == "leader" && entry.ShardID == shardID {
leaderPeer.Ip = entry.IP
leaderPeer.IP = entry.IP
leaderPeer.Port = entry.Port
setKey(&leaderPeer)
}

@ -7,22 +7,25 @@ import (
"sync/atomic"
)
type UniqueValidatorId struct {
uniqueId uint32
// UniqueValidatorID defines the structure of unique validator ID
type UniqueValidatorID struct {
uniqueID uint32
}
var instance *UniqueValidatorId
var instance *UniqueValidatorID
var once sync.Once
func GetUniqueValidatorIdInstance() *UniqueValidatorId {
// GetUniqueValidatorIDInstance returns a singleton instance
func GetUniqueValidatorIDInstance() *UniqueValidatorID {
once.Do(func() {
instance = &UniqueValidatorId{
uniqueId: 0,
instance = &UniqueValidatorID{
uniqueID: 0,
}
})
return instance
}
func (s *UniqueValidatorId) GetUniqueId() uint32 {
return atomic.AddUint32(&s.uniqueId, 1)
// GetUniqueID returns a unique ID and increment the internal variable
func (s *UniqueValidatorID) GetUniqueID() uint32 {
return atomic.AddUint32(&s.uniqueID, 1)
}

@ -6,10 +6,10 @@ import (
"time"
)
var NumThreads int = 20
var NumThreads = 20
func TestSingleton(t *testing.T) {
si := GetUniqueValidatorIdInstance()
si := GetUniqueValidatorIDInstance()
var wg sync.WaitGroup
t.Log("unique ID provided by singleton instance")
@ -18,7 +18,7 @@ func TestSingleton(t *testing.T) {
wg.Add(1)
go func() {
defer wg.Done()
t.Logf("id:%v\n", si.GetUniqueId())
t.Logf("id:%v\n", si.GetUniqueID())
time.Sleep(time.Millisecond)
}()

@ -30,12 +30,12 @@ func GetUniqueIdFromPeer(peer p2p.Peer) uint16 {
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
socketId := reg.ReplaceAllString(peer.IP+peer.Port, "") // A integer Id formed by unique IP/PORT pair
value, _ := strconv.Atoi(socketId)
return uint16(value)
}
func GetUniqueIdFromIpPort(ip, port string) uint16 {
func GetUniqueIdFromIPPort(ip, port string) uint16 {
reg, err := regexp.Compile("[^0-9]+")
if err != nil {
log.Panic("Regex Compilation Failed", "err", err)
@ -65,7 +65,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

Loading…
Cancel
Save