|
|
@ -13,14 +13,14 @@ import ( |
|
|
|
|
|
|
|
|
|
|
|
// SendMessage is to connect a socket given a port and send the given message.
|
|
|
|
// SendMessage is to connect a socket given a port and send the given message.
|
|
|
|
// TODO(minhdoan, rj): need to check if a peer is reachable or not.
|
|
|
|
// TODO(minhdoan, rj): need to check if a peer is reachable or not.
|
|
|
|
func SendMessage(host Host, p p2p.Peer, message []byte) { |
|
|
|
func SendMessage(host Host, p p2p.Peer, message []byte, lostPeer chan p2p.Peer) { |
|
|
|
// Construct normal p2p message
|
|
|
|
// Construct normal p2p message
|
|
|
|
content := ConstructP2pMessage(byte(0), message) |
|
|
|
content := ConstructP2pMessage(byte(0), message) |
|
|
|
go send(host, p, content) |
|
|
|
go send(host, p, content, lostPeer) |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// BroadcastMessage sends the message to a list of peers
|
|
|
|
// BroadcastMessage sends the message to a list of peers
|
|
|
|
func BroadcastMessage(h Host, peers []p2p.Peer, msg []byte) { |
|
|
|
func BroadcastMessage(h Host, peers []p2p.Peer, msg []byte, lostPeer chan p2p.Peer) { |
|
|
|
if len(peers) == 0 { |
|
|
|
if len(peers) == 0 { |
|
|
|
return |
|
|
|
return |
|
|
|
} |
|
|
|
} |
|
|
@ -32,7 +32,7 @@ func BroadcastMessage(h Host, peers []p2p.Peer, msg []byte) { |
|
|
|
start := time.Now() |
|
|
|
start := time.Now() |
|
|
|
for _, peer := range peers { |
|
|
|
for _, peer := range peers { |
|
|
|
peerCopy := peer |
|
|
|
peerCopy := peer |
|
|
|
go send(h, peerCopy, content) |
|
|
|
go send(h, peerCopy, content, lostPeer) |
|
|
|
} |
|
|
|
} |
|
|
|
log.Info("Broadcasting Done", "time spent(s)", time.Since(start).Seconds()) |
|
|
|
log.Info("Broadcasting Done", "time spent(s)", time.Since(start).Seconds()) |
|
|
|
|
|
|
|
|
|
|
@ -43,6 +43,13 @@ func BroadcastMessage(h Host, peers []p2p.Peer, msg []byte) { |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// BroadcastMessageFromLeader sends the message to a list of peers from a leader.
|
|
|
|
|
|
|
|
func BroadcastMessageFromLeader(h Host, peers []p2p.Peer, msg []byte, lostPeer chan p2p.Peer) { |
|
|
|
|
|
|
|
// TODO(minhdoan): Enable back for multicast.
|
|
|
|
|
|
|
|
peers = SelectMyPeers(peers, 1, MaxBroadCast) |
|
|
|
|
|
|
|
BroadcastMessage(h, peers, msg, lostPeer) |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// ConstructP2pMessage constructs the p2p message as [messageType, contentSize, content]
|
|
|
|
// ConstructP2pMessage constructs the p2p message as [messageType, contentSize, content]
|
|
|
|
func ConstructP2pMessage(msgType byte, content []byte) []byte { |
|
|
|
func ConstructP2pMessage(msgType byte, content []byte) []byte { |
|
|
|
|
|
|
|
|
|
|
@ -58,19 +65,10 @@ func ConstructP2pMessage(msgType byte, content []byte) []byte { |
|
|
|
return byteBuffer.Bytes() |
|
|
|
return byteBuffer.Bytes() |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// BroadcastMessageFromLeader sends the message to a list of peers from a leader.
|
|
|
|
|
|
|
|
func BroadcastMessageFromLeader(h Host, peers []p2p.Peer, msg []byte) { |
|
|
|
|
|
|
|
// TODO(minhdoan): Enable back for multicast.
|
|
|
|
|
|
|
|
peers = SelectMyPeers(peers, 1, MaxBroadCast) |
|
|
|
|
|
|
|
BroadcastMessage(h, peers, msg) |
|
|
|
|
|
|
|
log.Info("Done sending from leader") |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// BroadcastMessageFromValidator 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(h Host, selfPeer p2p.Peer, peers []p2p.Peer, msg []byte) { |
|
|
|
func BroadcastMessageFromValidator(h Host, selfPeer p2p.Peer, peers []p2p.Peer, msg []byte) { |
|
|
|
peers = SelectMyPeers(peers, selfPeer.ValidatorID*MaxBroadCast+1, (selfPeer.ValidatorID+1)*MaxBroadCast) |
|
|
|
peers = SelectMyPeers(peers, selfPeer.ValidatorID*MaxBroadCast+1, (selfPeer.ValidatorID+1)*MaxBroadCast) |
|
|
|
BroadcastMessage(h, peers, msg) |
|
|
|
BroadcastMessage(h, peers, msg, nil) |
|
|
|
log.Info("Done sending from validator") |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// MaxBroadCast is the maximum number of neighbors to broadcast
|
|
|
|
// MaxBroadCast is the maximum number of neighbors to broadcast
|
|
|
@ -89,14 +87,14 @@ func SelectMyPeers(peers []p2p.Peer, min int, max int) []p2p.Peer { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// Send a message to another node with given port.
|
|
|
|
// Send a message to another node with given port.
|
|
|
|
func send(h Host, peer p2p.Peer, message []byte) { |
|
|
|
func send(h Host, peer p2p.Peer, message []byte, lostPeer chan p2p.Peer) { |
|
|
|
// Add attack code here.
|
|
|
|
// Add attack code here.
|
|
|
|
//attack.GetInstance().Run()
|
|
|
|
//attack.GetInstance().Run()
|
|
|
|
backoff := p2p.NewExpBackoff(250*time.Millisecond, 10*time.Second, 2) |
|
|
|
backoff := p2p.NewExpBackoff(150*time.Millisecond, 5*time.Second, 2) |
|
|
|
|
|
|
|
|
|
|
|
for trial := 0; trial < 10; trial++ { |
|
|
|
for trial := 0; trial < 10; trial++ { |
|
|
|
var err error |
|
|
|
var err error |
|
|
|
h.SendMessage(peer, message) |
|
|
|
err = h.SendMessage(peer, message) |
|
|
|
if err == nil { |
|
|
|
if err == nil { |
|
|
|
if trial > 0 { |
|
|
|
if trial > 0 { |
|
|
|
log.Warn("retry send", "rety", trial) |
|
|
|
log.Warn("retry send", "rety", trial) |
|
|
@ -108,6 +106,11 @@ func send(h Host, peer p2p.Peer, message []byte) { |
|
|
|
backoff.Sleep() |
|
|
|
backoff.Sleep() |
|
|
|
} |
|
|
|
} |
|
|
|
log.Error("gave up sending a message", "addr", net.JoinHostPort(peer.IP, peer.Port)) |
|
|
|
log.Error("gave up sending a message", "addr", net.JoinHostPort(peer.IP, peer.Port)) |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if lostPeer != nil { |
|
|
|
|
|
|
|
// Notify lostPeer channel
|
|
|
|
|
|
|
|
lostPeer <- peer |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// DialWithSocketClient joins host port and establishes connection
|
|
|
|
// DialWithSocketClient joins host port and establishes connection
|
|
|
|