Skip to content

Commit 514bc91

Browse files
authored
refactor: modernize code (#1105)
- apply go fix modernizers from Go 1.26 - require go1.25 or later - go1.26 is required to run sharness tests
1 parent 6aa643a commit 514bc91

File tree

95 files changed

+325
-463
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

95 files changed

+325
-463
lines changed

.github/workflows/gateway-sharness.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ jobs:
1818
- name: Setup Go
1919
uses: actions/setup-go@v5
2020
with:
21-
go-version: 1.24.x
21+
go-version: 1.26.x
2222
- name: Checkout boxo
2323
uses: actions/checkout@v3
2424
with:

bitswap/benchmarks_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func BenchmarkFetchFromOldBitswap(b *testing.B) {
161161
instances = append(instances, inst)
162162
}
163163
// Create old nodes (just seeds)
164-
for i := 0; i < oldSeedCount; i++ {
164+
for range oldSeedCount {
165165
inst := oldNodeGenerator.Next()
166166
instances = append(instances, inst)
167167
}
@@ -631,7 +631,7 @@ func unixfsFileFetchLarge(b *testing.B, bs *bitswap.Bitswap, ks []cid.Cid) {
631631
func printResults(rs []runStats) {
632632
nameOrder := make([]string, 0)
633633
names := make(map[string]struct{})
634-
for i := 0; i < len(rs); i++ {
634+
for i := range rs {
635635
if _, ok := names[rs[i].Name]; !ok {
636636
nameOrder = append(nameOrder, rs[i].Name)
637637
names[rs[i].Name] = struct{}{}
@@ -646,7 +646,7 @@ func printResults(rs []runStats) {
646646
dups := 0.0
647647
blks := 0.0
648648
elpd := 0.0
649-
for i := 0; i < len(rs); i++ {
649+
for i := range rs {
650650
if rs[i].Name == name {
651651
count++
652652
sent += float64(rs[i].MsgSent)

bitswap/bitswap_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -521,8 +521,7 @@ func TestDoubleGet(t *testing.T) {
521521
t.Fatal(err)
522522
}
523523

524-
ctx2, cancel2 := context.WithCancel(context.Background())
525-
defer cancel2()
524+
ctx2 := t.Context()
526525

527526
blkch2, err := instances[1].Exchange.GetBlocks(ctx2, []cid.Cid{blocks[0].Cid()})
528527
if err != nil {

bitswap/client/bitswap_with_sessions_test.go

Lines changed: 13 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,7 @@ func addBlock(t *testing.T, ctx context.Context, inst testinstance.Instance, blk
4848
}
4949

5050
func TestBasicSessions(t *testing.T) {
51-
ctx, cancel := context.WithCancel(context.Background())
52-
defer cancel()
51+
ctx := t.Context()
5352

5453
vnet := getVirtualNetwork()
5554
router := mockrouting.NewServer()
@@ -133,8 +132,7 @@ func TestCustomProviderQueryManager(t *testing.T) {
133132
}
134133
defer pqm.Close()
135134

136-
ctx, cancel := context.WithCancel(context.Background())
137-
defer cancel()
135+
ctx := t.Context()
138136

139137
bs := bitswap.New(ctx, a.Adapter, pqm, a.Blockstore,
140138
bitswap.WithClientOption(client.WithDefaultProviderQueryManager(false)),
@@ -173,8 +171,7 @@ func TestCustomProviderQueryManager(t *testing.T) {
173171
}
174172

175173
func TestSessionBetweenPeers(t *testing.T) {
176-
ctx, cancel := context.WithCancel(context.Background())
177-
defer cancel()
174+
ctx := t.Context()
178175

179176
vnet := tn.VirtualNetwork(delay.Fixed(time.Millisecond))
180177
router := mockrouting.NewServer()
@@ -212,7 +209,7 @@ func TestSessionBetweenPeers(t *testing.T) {
212209
cids = cids[1:]
213210

214211
// Fetch blocks with the session, 10 at a time
215-
for i := 0; i < 10; i++ {
212+
for i := range 10 {
216213
ch, err := ses.GetBlocks(ctx, cids[i*10:(i+1)*10])
217214
if err != nil {
218215
t.Fatal(err)
@@ -242,8 +239,7 @@ func TestSessionBetweenPeers(t *testing.T) {
242239
}
243240

244241
func TestSessionSplitFetch(t *testing.T) {
245-
ctx, cancel := context.WithCancel(context.Background())
246-
defer cancel()
242+
ctx := t.Context()
247243

248244
vnet := getVirtualNetwork()
249245
router := mockrouting.NewServer()
@@ -254,7 +250,7 @@ func TestSessionSplitFetch(t *testing.T) {
254250

255251
// Add 10 distinct blocks to each of 10 peers
256252
blks := random.BlocksOfSize(100, blockSize)
257-
for i := 0; i < 10; i++ {
253+
for i := range 10 {
258254
if err := inst[i].Blockstore.PutMany(ctx, blks[i*10:(i+1)*10]); err != nil {
259255
t.Fatal(err)
260256
}
@@ -269,7 +265,7 @@ func TestSessionSplitFetch(t *testing.T) {
269265
ses := inst[10].Exchange.NewSession(ctx).(*session.Session)
270266
ses.SetBaseTickDelay(time.Millisecond * 10)
271267

272-
for i := 0; i < 10; i++ {
268+
for i := range 10 {
273269
ch, err := ses.GetBlocks(ctx, cids[i*10:(i+1)*10])
274270
if err != nil {
275271
t.Fatal(err)
@@ -371,7 +367,7 @@ func TestFetchAfterDisconnect(t *testing.T) {
371367

372368
// Should get first 5 blocks
373369
var got []blocks.Block
374-
for i := 0; i < 5; i++ {
370+
for range 5 {
375371
b := <-ch
376372
got = append(got, b)
377373
}
@@ -397,7 +393,7 @@ func TestFetchAfterDisconnect(t *testing.T) {
397393
// Peer B should call FindProviders() and find Peer A
398394

399395
// Should get last 5 blocks
400-
for i := 0; i < 5; i++ {
396+
for range 5 {
401397
select {
402398
case b := <-ch:
403399
got = append(got, b)
@@ -411,8 +407,7 @@ func TestFetchAfterDisconnect(t *testing.T) {
411407
}
412408

413409
func TestInterestCacheOverflow(t *testing.T) {
414-
ctx, cancel := context.WithCancel(context.Background())
415-
defer cancel()
410+
ctx := t.Context()
416411

417412
vnet := getVirtualNetwork()
418413
router := mockrouting.NewServer()
@@ -461,8 +456,7 @@ func TestInterestCacheOverflow(t *testing.T) {
461456
}
462457

463458
func TestPutAfterSessionCacheEvict(t *testing.T) {
464-
ctx, cancel := context.WithCancel(context.Background())
465-
defer cancel()
459+
ctx := t.Context()
466460

467461
vnet := getVirtualNetwork()
468462
router := mockrouting.NewServer()
@@ -499,8 +493,7 @@ func TestPutAfterSessionCacheEvict(t *testing.T) {
499493
}
500494

501495
func TestMultipleSessions(t *testing.T) {
502-
ctx, cancel := context.WithCancel(context.Background())
503-
defer cancel()
496+
ctx := t.Context()
504497

505498
vnet := getVirtualNetwork()
506499
router := mockrouting.NewServer()
@@ -597,8 +590,7 @@ func TestDontHaveTimeoutConfig(t *testing.T) {
597590
}
598591
defer pqm.Close()
599592

600-
ctx, cancel := context.WithCancel(context.Background())
601-
defer cancel()
593+
ctx := t.Context()
602594

603595
bs := bitswap.New(ctx, a.Adapter, pqm, a.Blockstore,
604596
bitswap.WithClientOption(client.WithDontHaveTimeoutConfig(cfg)))

bitswap/client/internal/messagequeue/messagequeue_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -857,7 +857,7 @@ func BenchmarkMessageQueue(b *testing.B) {
857857

858858
// Create a handful of message queues to start with
859859
var qs []*MessageQueue
860-
for i := 0; i < 5; i++ {
860+
for range 5 {
861861
qs = append(qs, createQueue())
862862
}
863863

bitswap/client/internal/peermanager/peermanager_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -354,7 +354,7 @@ func BenchmarkPeerManager(b *testing.B) {
354354

355355
// Create a bunch of connections
356356
connected := 0
357-
for i := 0; i < len(peers); i++ {
357+
for i := range peers {
358358
peerManager.Connected(peers[i])
359359
connected++
360360
}

bitswap/client/internal/session/cidqueue.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ func (cq *cidQueue) len() int {
5050
func (cq *cidQueue) gc() {
5151
if cq.elems.Len() > cq.eset.Len() {
5252
n := cq.elems.Len()
53-
for i := 0; i < n; i++ {
53+
for range n {
5454
c := cq.elems.PopFront()
5555
if cq.eset.Has(c) {
5656
cq.elems.PushBack(c)

bitswap/client/internal/session/peerresponsetracker_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ func TestPeerResponseTrackerProbabilityUnknownPeers(t *testing.T) {
3030

3131
choices := []int{0, 0, 0, 0}
3232
count := 1000
33-
for i := 0; i < count; i++ {
33+
for range count {
3434
p := prt.choose(peers)
3535
if p == peers[0] {
3636
choices[0]++
@@ -61,7 +61,7 @@ func TestPeerResponseTrackerProbabilityOneKnownOneUnknownPeer(t *testing.T) {
6161

6262
chooseFirst := 0
6363
chooseSecond := 0
64-
for i := 0; i < 1000; i++ {
64+
for range 1000 {
6565
p := prt.choose(peers)
6666
if p == peers[0] {
6767
chooseFirst++
@@ -95,7 +95,7 @@ func TestPeerResponseTrackerProbabilityProportional(t *testing.T) {
9595
choices = append(choices, 0)
9696
}
9797

98-
for i := 0; i < count; i++ {
98+
for range count {
9999
p := prt.choose(peers)
100100
if p == peers[0] {
101101
choices[0]++

bitswap/client/internal/session/sessionwants_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ func TestPrepareBroadcast(t *testing.T) {
125125
sw.GetNextWants()
126126

127127
// Broadcast should contain wants in order
128-
for i := 0; i < 10; i++ {
128+
for range 10 {
129129
ws := sw.PrepareBroadcast()
130130
if len(ws) != 3 {
131131
t.Fatal("should broadcast all live wants")
@@ -155,7 +155,7 @@ func TestPrepareBroadcast(t *testing.T) {
155155

156156
// Broadcast should contain wants in order
157157
cids = cids[1:]
158-
for i := 0; i < 10; i++ {
158+
for range 10 {
159159
ws := sw.PrepareBroadcast()
160160
if len(ws) != 3 {
161161
t.Fatal("should broadcast live wants up to limit", len(ws), len(cids))

bitswap/client/internal/session/sessionwantsender_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package session
22

33
import (
4+
"maps"
45
"sync"
56
"testing"
67
"time"
@@ -102,9 +103,7 @@ func (pm *mockPeerManager) waitNextWants() map[peer.ID]*sentWants {
102103
defer pm.lk.Unlock()
103104

104105
nw := make(map[peer.ID]*sentWants, len(pm.peerSends))
105-
for p, sentWants := range pm.peerSends {
106-
nw[p] = sentWants
107-
}
106+
maps.Copy(nw, pm.peerSends)
108107
return nw
109108
}
110109

0 commit comments

Comments
 (0)