From b147675aad4e84d22b15a90e7d511502fc1fe7f6 Mon Sep 17 00:00:00 2001 From: libotony Date: Tue, 10 Sep 2024 21:18:54 +0800 Subject: [PATCH] adopt loop var scope in go 1.22 (#843) --- cmd/thor/sync_logdb.go | 6 ++---- comm/comm_test.go | 34 ++++++++++++++++++++++++++++++++++ comm/communicator.go | 14 ++++++-------- comm/sync.go | 14 ++++++-------- comm/txs_loop.go | 7 +++---- 5 files changed, 51 insertions(+), 24 deletions(-) diff --git a/cmd/thor/sync_logdb.go b/cmd/thor/sync_logdb.go index b5062779c..9fccf3127 100644 --- a/cmd/thor/sync_logdb.go +++ b/cmd/thor/sync_logdb.go @@ -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() } } } diff --git a/comm/comm_test.go b/comm/comm_test.go index a5a8d2471..864f29de6 100644 --- a/comm/comm_test.go +++ b/comm/comm_test.go @@ -4,3 +4,37 @@ // file LICENSE or 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) +} diff --git a/comm/communicator.go b/comm/communicator.go index a0ad58c0c..9d0a5a530 100644 --- a/comm/communicator.go +++ b/comm/communicator.go @@ -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) } }) } diff --git a/comm/sync.go b/comm/sync.go index e2897806c..75347098e 100644 --- a/comm/sync.go +++ b/comm/sync.go @@ -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() } } } diff --git a/comm/txs_loop.go b/comm/txs_loop.go index 3040ce587..6b820827b 100644 --- a/comm/txs_loop.go +++ b/comm/txs_loop.go @@ -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) } }) }