Skip to content

Commit 559197e

Browse files
committed
txpool: modernize test loops (range, WaitGroup.Go)
1 parent 0e34137 commit 559197e

3 files changed

Lines changed: 9 additions & 11 deletions

File tree

txpool/tx_object_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ func mineLegacyTxWithWork(
8585
evalWork := builder.Build().EvaluateWork(from.Address)
8686

8787
const maxAttempts = 20_000_000
88-
for nonce := uint64(0); nonce < maxAttempts; nonce++ {
88+
for nonce := range uint64(maxAttempts) {
8989
if evalWork(nonce).Cmp(minWork) >= 0 {
9090
return tx.MustSign(builder.Nonce(nonce).Build(), from.PrivateKey)
9191
}

txpool/tx_pool_benchmark_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ func newBenchPool() *TxPool {
2626
// Signing is ECDSA and expensive, so it must happen outside the timed region.
2727
func genBenchTxs(pool *TxPool, n, workerIdx int) []*tx.Transaction {
2828
txs := make([]*tx.Transaction, n)
29-
for i := 0; i < n; i++ {
29+
for i := range n {
3030
from := devAccounts[(workerIdx+i)%len(devAccounts)]
3131
txs[i] = newTx(tx.TypeLegacy, pool.repo.ChainTag(), nil, 21000, tx.BlockRef{}, 100, nil, tx.Features(0), from)
3232
}
@@ -38,7 +38,7 @@ func benchAddRemove(b *testing.B, workers, perWorker int, withWash bool) {
3838
defer pool.Close()
3939

4040
sets := make([][]*tx.Transaction, workers)
41-
for w := 0; w < workers; w++ {
41+
for w := range workers {
4242
sets[w] = genBenchTxs(pool, perWorker, w)
4343
}
4444

@@ -62,11 +62,11 @@ func benchAddRemove(b *testing.B, workers, perWorker int, withWash bool) {
6262

6363
var wg sync.WaitGroup
6464
per := b.N / workers
65-
for w := 0; w < workers; w++ {
65+
for w := range workers {
6666
wg.Add(1)
6767
go func(set []*tx.Transaction) {
6868
defer wg.Done()
69-
for i := 0; i < per; i++ {
69+
for i := range per {
7070
trx := set[i%len(set)]
7171
_ = pool.Add(trx) // publishes pricing snapshot (atomic Store) + bookkeeping (map lock)
7272
pool.Remove(trx.Hash(), trx.ID()) // reads Cost()/Payer() (atomic Load) + bookkeeping (map lock)

txpool/tx_pool_wash_race_test.go

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func TestConcurrentWashAddRemove(t *testing.T) {
4848
// tx it successfully adds is also removed by the same goroutine, so by
4949
// the time all workers join, every worker-added tx has had a matching
5050
// Remove call (whether or not wash got to it first).
51-
for w := 0; w < numAddRemoveWorkers; w++ {
51+
for w := range numAddRemoveWorkers {
5252
wg.Add(1)
5353
go func(workerIdx int) {
5454
defer wg.Done()
@@ -70,10 +70,8 @@ func TestConcurrentWashAddRemove(t *testing.T) {
7070

7171
// Wash workers: repeatedly evaluate/evict/promote against the churning
7272
// pool, exercising the lock-free pricing snapshot publish/read path.
73-
for w := 0; w < numWashWorkers; w++ {
74-
wg.Add(1)
75-
go func() {
76-
defer wg.Done()
73+
for range numWashWorkers {
74+
wg.Go(func() {
7775
for {
7876
select {
7977
case <-stop:
@@ -82,7 +80,7 @@ func TestConcurrentWashAddRemove(t *testing.T) {
8280
}
8381
_, _, _, _ = pool.wash(best, true)
8482
}
85-
}()
83+
})
8684
}
8785

8886
time.Sleep(500 * time.Millisecond)

0 commit comments

Comments
 (0)