Skip to content

Commit

Permalink
adopt loop var scope in go 1.22 (#843)
Browse files Browse the repository at this point in the history
  • Loading branch information
libotony authored Sep 10, 2024
1 parent a9357cc commit b147675
Show file tree
Hide file tree
Showing 5 changed files with 51 additions and 24 deletions.
6 changes: 2 additions & 4 deletions cmd/thor/sync_logdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -373,14 +373,12 @@ func pumpBlockAndReceipts(ctx context.Context, repo *chain.Repository, headID th
select {
case <-co.Parallel(func(queue chan<- func()) {
for _, b := range buf {
h := b.Header()
queue <- func() {
h.ID()
b.Header().ID()
}
for _, tx := range b.Transactions() {
tmpTx := tx
queue <- func() {
tmpTx.ID()
tx.ID()
}
}
}
Expand Down
34 changes: 34 additions & 0 deletions comm/comm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,37 @@
// file LICENSE or <https://www.gnu.org/licenses/lgpl-3.0.html>

package comm_test

import (
"sort"
"testing"

"github.com/stretchr/testify/assert"
)

func TestLoopVar(t *testing.T) {
cases := []uint{1, 2, 3, 4}

ch := make(chan uint)

go func() {
for _, c := range cases {
go func() {
ch <- c
}()
}
}()

ret := make([]uint, 0, len(cases))
for i := 0; i < len(cases); i++ {
v := <-ch
ret = append(ret, v)
}
sort.Slice(ret, func(i, j int) bool {
return ret[i] < ret[j]
})
// before go1.22 the result is [4, 4, 4, 4]
// after go1.22 the result is [1, 2, 3, 4]

assert.Equal(t, cases, ret)
}
14 changes: 6 additions & 8 deletions comm/communicator.go
Original file line number Diff line number Diff line change
Expand Up @@ -238,21 +238,19 @@ func (c *Communicator) BroadcastBlock(blk *block.Block) {
toAnnounce := peers[p:]

for _, peer := range toPropagate {
tmpPeer := peer
tmpPeer.MarkBlock(blk.Header().ID())
peer.MarkBlock(blk.Header().ID())
c.goes.Go(func() {
if err := proto.NotifyNewBlock(c.ctx, tmpPeer, blk); err != nil {
tmpPeer.logger.Debug("failed to broadcast new block", "err", err)
if err := proto.NotifyNewBlock(c.ctx, peer, blk); err != nil {
peer.logger.Debug("failed to broadcast new block", "err", err)
}
})
}

for _, peer := range toAnnounce {
tmpPeer := peer
tmpPeer.MarkBlock(blk.Header().ID())
peer.MarkBlock(blk.Header().ID())
c.goes.Go(func() {
if err := proto.NotifyNewBlockID(c.ctx, tmpPeer, blk.Header().ID()); err != nil {
tmpPeer.logger.Debug("failed to broadcast new block id", "err", err)
if err := proto.NotifyNewBlockID(c.ctx, peer, blk.Header().ID()); err != nil {
peer.logger.Debug("failed to broadcast new block id", "err", err)
}
})
}
Expand Down
14 changes: 6 additions & 8 deletions comm/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,18 +81,16 @@ func warmupBlocks(ctx context.Context, fetched <-chan []*block.Block, warmedUp c
<-co.Parallel(func(queue chan<- func()) {
for blocks := range fetched {
for _, blk := range blocks {
h := blk.Header()
queue <- func() {
h.ID()
h.Beta()
blk.Header().ID()
blk.Header().Beta()
}
for _, tx := range blk.Transactions() {
tmpTx := tx
queue <- func() {
tmpTx.ID()
tmpTx.UnprovedWork()
_, _ = tmpTx.IntrinsicGas()
_, _ = tmpTx.Delegator()
tx.ID()
tx.UnprovedWork()
_, _ = tx.IntrinsicGas()
_, _ = tx.Delegator()
}
}
}
Expand Down
7 changes: 3 additions & 4 deletions comm/txs_loop.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,10 @@ func (c *Communicator) txsLoop() {
})

for _, peer := range peers {
tmpPeer := peer
tmpPeer.MarkTransaction(tx.Hash())
peer.MarkTransaction(tx.Hash())
c.goes.Go(func() {
if err := proto.NotifyNewTx(c.ctx, tmpPeer, tx); err != nil {
tmpPeer.logger.Debug("failed to broadcast tx", "err", err)
if err := proto.NotifyNewTx(c.ctx, peer, tx); err != nil {
peer.logger.Debug("failed to broadcast tx", "err", err)
}
})
}
Expand Down

0 comments on commit b147675

Please sign in to comment.