add self peer to node

pull/69/merge
Minh Doan 6 years ago
parent 037f2477fc
commit 4328d72652
  1. 3
      benchmark.go
  2. 1
      node/node.go
  3. 31
      p2p/peer.go
  4. 11
      utils/distribution_config.go

@ -84,6 +84,7 @@ func main() {
shardID := distributionConfig.GetShardID(*ip, *port)
peers := distributionConfig.GetPeers(*ip, *port, shardID)
leader := distributionConfig.GetLeader(shardID)
selfPeer := distributionConfig.GetSelfPeer(*ip, *port, shardID)
var role string
if leader.Ip == *ip && leader.Port == *port {
@ -123,6 +124,8 @@ func main() {
attack.GetInstance().SetLogger(consensus.Log)
// Current node.
currentNode := node.New(consensus, ldb)
// Add self peer.
currentNode.SelfPeer = selfPeer
// Add sync node configuration.
currentNode.SyncNode = *syncNode
// Create client peer.

@ -37,6 +37,7 @@ type Node struct {
ClientPeer *p2p.Peer // The peer for the benchmark tx generator client, used for leaders to return proof-of-accept
Client *client.Client // The presence of a client object means this node will also act as a client
IsWaiting bool
SelfPeer p2p.Peer // TODO(minhdoan): it could be duplicated with Self below whose is Alok work.
Self p2p.Peer
IDCPeer p2p.Peer
SyncNode bool // TODO(minhdoan): Remove it later.

@ -56,11 +56,42 @@ func BroadcastMessage(peers []Peer, msg []byte) {
log.Info("Broadcasting Down", "time spent", time.Now().Sub(start).Seconds())
}
func SelectMyPeers(peers []Peer, min int, max int) []Peer {
res := []Peer{}
for _, peer := range peers {
if peer.ValidatorID >= min && peer.ValidatorID <= max {
res = append(res, peer)
}
}
return res
}
// BroadcastMessage sends the message to a list of peers from a leader.
func BroadcastMessageFromLeader(peers []Peer, msg []byte) {
// Construct broadcast p2p message
content := ConstructP2pMessage(byte(17), msg)
peers = SelectMyPeers(peers, 0, MAX_BROADCAST-1)
var wg sync.WaitGroup
wg.Add(len(peers))
for _, peer := range peers {
peerCopy := peer
go func() {
defer wg.Done()
send(peerCopy.Ip, peerCopy.Port, content)
}()
}
wg.Wait()
}
// BroadcastMessage sends the message to a list of peers from a leader.
func BroadcastMessageFromValidator(Self Peer, peers []Peer, msg []byte) {
// Construct broadcast p2p message
content := ConstructP2pMessage(byte(17), msg)
peers = SelectMyPeers(peers, 0, MAX_BROADCAST-1)
var wg sync.WaitGroup
wg.Add(len(peers))

@ -123,6 +123,17 @@ func (config *DistributionConfig) GetPeers(ip, port, shardID string) []p2p.Peer
return peerList
}
// GetPeers Gets the validator list
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}
return peer
}
}
return p2p.Peer{}
}
// GetLeader Gets the leader of this shard id
func (config *DistributionConfig) GetLeader(shardID string) p2p.Peer {
var leaderPeer p2p.Peer

Loading…
Cancel
Save