Fix the broadcast bug introduced by for loop deferencing to the last element (for loop variable is just a temp pointer to the element)

pull/20/head
Rongjian Lan 7 years ago
parent 35d69bfb2c
commit da45cdeef5
  1. 2
      consensus/consensus.go
  2. BIN
      harmony-benchmark
  3. 5
      p2p/peer.go

@ -146,8 +146,8 @@ func NewConsensus(ip, port, ShardID string, peers []p2p.Peer, leader p2p.Peer) C
if consensus.IsLeader { if consensus.IsLeader {
consensus.ReadySignal = make(chan int) consensus.ReadySignal = make(chan int)
// TODO: why do we need to do go rountine here.
// send a signal to indicate it's ready to run consensus // send a signal to indicate it's ready to run consensus
// this is a goroutine because go channel without buffer will block
go func() { go func() {
consensus.ReadySignal <- 1 consensus.ReadySignal <- 1
}() }()

Binary file not shown.

@ -32,11 +32,12 @@ func BroadcastMessage(peers []Peer, msg []byte) {
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(len(peers)) wg.Add(len(peers))
for _, peer := range peers { for _, peer := range peers {
// send(peer.Ip, peer.Port, content) peerCopy := peer
go func() { go func() {
defer wg.Done() defer wg.Done()
send(peer.Ip, peer.Port, content) send(peerCopy.Ip, peerCopy.Port, content)
}() }()
} }
wg.Wait() wg.Wait()

Loading…
Cancel
Save