Skip to content

Commit a74befd

Browse files
authored
chore: modernize codebase for go 1.25 (#5236)
1 parent 1a4f351 commit a74befd

File tree

133 files changed

+498
-570
lines changed

Some content is hidden

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

133 files changed

+498
-570
lines changed

cmd/bee/cmd/db_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ func TestDBExportImport(t *testing.T) {
3939

4040
chunks := make(map[string]int)
4141
nChunks := 10
42-
for i := 0; i < nChunks; i++ {
42+
for range nChunks {
4343
ch := storagetest.GenerateTestRandomChunk()
4444
err := db1.ReservePutter().Put(ctx, ch)
4545
if err != nil {
@@ -101,13 +101,13 @@ func TestDBExportImportPinning(t *testing.T) {
101101
pins := make(map[string]any)
102102
nChunks := 10
103103

104-
for i := 0; i < 2; i++ {
104+
for range 2 {
105105
rootAddr := swarm.RandAddress(t)
106106
collection, err := db1.NewCollection(ctx)
107107
if err != nil {
108108
t.Fatal(err)
109109
}
110-
for j := 0; j < nChunks; j++ {
110+
for range nChunks {
111111
ch := storagetest.GenerateTestRandomChunk()
112112
err = collection.Put(ctx, ch)
113113
if err != nil {
@@ -186,7 +186,7 @@ func TestDBNuke_FLAKY(t *testing.T) {
186186
}, dataDir)
187187

188188
nChunks := 10
189-
for i := 0; i < nChunks; i++ {
189+
for range nChunks {
190190
ch := storagetest.GenerateTestRandomChunk()
191191
err := db.ReservePutter().Put(ctx, ch)
192192
if err != nil {
@@ -241,7 +241,7 @@ func TestDBInfo(t *testing.T) {
241241
}, dir1)
242242

243243
nChunks := 10
244-
for i := 0; i < nChunks; i++ {
244+
for range nChunks {
245245
ch := storagetest.GenerateTestRandomChunk()
246246
err := db1.ReservePutter().Put(ctx, ch)
247247
if err != nil {

pkg/accounting/accounting.go

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -312,10 +312,7 @@ func (a *Accounting) PrepareCredit(ctx context.Context, peer swarm.Address, pric
312312
}
313313
}
314314

315-
timeElapsedInSeconds := (a.timeNow().UnixMilli() - accountingPeer.refreshTimestampMilliseconds) / 1000
316-
if timeElapsedInSeconds > 1 {
317-
timeElapsedInSeconds = 1
318-
}
315+
timeElapsedInSeconds := min((a.timeNow().UnixMilli()-accountingPeer.refreshTimestampMilliseconds)/1000, 1)
319316

320317
refreshDue := new(big.Int).Mul(big.NewInt(timeElapsedInSeconds), a.refreshRate)
321318
overdraftLimit := new(big.Int).Add(accountingPeer.paymentThreshold, refreshDue)
@@ -745,10 +742,7 @@ func (a *Accounting) PeerAccounting() (map[string]PeerInfo, error) {
745742

746743
t := a.timeNow()
747744

748-
timeElapsedInSeconds := t.Unix() - accountingPeer.refreshReceivedTimestamp
749-
if timeElapsedInSeconds > 1 {
750-
timeElapsedInSeconds = 1
751-
}
745+
timeElapsedInSeconds := min(t.Unix()-accountingPeer.refreshReceivedTimestamp, 1)
752746

753747
// get appropriate refresh rate
754748
refreshRate := new(big.Int).Set(a.refreshRate)
@@ -759,10 +753,7 @@ func (a *Accounting) PeerAccounting() (map[string]PeerInfo, error) {
759753
refreshDue := new(big.Int).Mul(big.NewInt(timeElapsedInSeconds), refreshRate)
760754
currentThresholdGiven := new(big.Int).Add(accountingPeer.disconnectLimit, refreshDue)
761755

762-
timeElapsedInSeconds = (t.UnixMilli() - accountingPeer.refreshTimestampMilliseconds) / 1000
763-
if timeElapsedInSeconds > 1 {
764-
timeElapsedInSeconds = 1
765-
}
756+
timeElapsedInSeconds = min((t.UnixMilli()-accountingPeer.refreshTimestampMilliseconds)/1000, 1)
766757

767758
// get appropriate refresh rate
768759
refreshDue = new(big.Int).Mul(big.NewInt(timeElapsedInSeconds), a.refreshRate)
@@ -1352,10 +1343,7 @@ func (d *debitAction) Apply() error {
13521343
a.metrics.TotalDebitedAmount.Add(tot)
13531344
a.metrics.DebitEventsCount.Inc()
13541345

1355-
timeElapsedInSeconds := a.timeNow().Unix() - d.accountingPeer.refreshReceivedTimestamp
1356-
if timeElapsedInSeconds > 1 {
1357-
timeElapsedInSeconds = 1
1358-
}
1346+
timeElapsedInSeconds := min(a.timeNow().Unix()-d.accountingPeer.refreshReceivedTimestamp, 1)
13591347

13601348
// get appropriate refresh rate
13611349
refreshRate := new(big.Int).Set(a.refreshRate)

pkg/accounting/accounting_test.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1503,7 +1503,7 @@ func TestAccountingCallPaymentErrorRetries(t *testing.T) {
15031503
acc.NotifyPaymentSent(peer1Addr, sentAmount, errors.New("error"))
15041504

15051505
// try another n requests 1 per second
1506-
for i := 0; i < 10; i++ {
1506+
for range 10 {
15071507
ts++
15081508
acc.SetTime(ts)
15091509

@@ -1857,8 +1857,8 @@ func testAccountingSettlementGrowingThresholds(t *testing.T, settleFunc func(t *
18571857
checkPaymentThreshold := new(big.Int).Set(testPayThreshold)
18581858

18591859
// Simulate first 18 threshold upgrades
1860-
for j := 0; j < 18; j++ {
1861-
for i := 0; i < 100; i++ {
1860+
for range 18 {
1861+
for range 100 {
18621862

18631863
// expect no change in threshold while less than 100 seconds worth of refreshment rate was settled
18641864
settleFunc(t, acc, peer1Addr, testGrowth-1)
@@ -1891,7 +1891,7 @@ func testAccountingSettlementGrowingThresholds(t *testing.T, settleFunc func(t *
18911891

18921892
// Expect no increase for the next 179 seconds of refreshment
18931893

1894-
for k := 0; k < 1799; k++ {
1894+
for range 1799 {
18951895

18961896
settleFunc(t, acc, peer1Addr, testGrowth)
18971897

@@ -1917,7 +1917,7 @@ func testAccountingSettlementGrowingThresholds(t *testing.T, settleFunc func(t *
19171917

19181918
// Expect no increase for another 3599 seconds of refreshments
19191919

1920-
for k := 0; k < 3599; k++ {
1920+
for range 3599 {
19211921

19221922
settleFunc(t, acc, peer1Addr, testGrowth)
19231923

pkg/api/api.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -624,7 +624,7 @@ func (s *Service) checkOrigin(r *http.Request) bool {
624624
// validationError is a custom error type for validation errors.
625625
type validationError struct {
626626
Entry string
627-
Value interface{}
627+
Value any
628628
Cause error
629629
}
630630

@@ -636,7 +636,7 @@ func (e *validationError) Error() string {
636636
// mapStructure maps the input into output struct and validates the output.
637637
// It's a helper method for the handlers, which reduces the chattiness
638638
// of the code.
639-
func (s *Service) mapStructure(input, output interface{}) func(string, log.Logger, http.ResponseWriter) {
639+
func (s *Service) mapStructure(input, output any) func(string, log.Logger, http.ResponseWriter) {
640640
// response unifies the response format for parsing and validation errors.
641641
response := func(err error) func(string, log.Logger, http.ResponseWriter) {
642642
return func(msg string, logger log.Logger, w http.ResponseWriter) {

pkg/api/bzz_test.go

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -104,10 +104,7 @@ func TestBzzUploadDownloadWithRedundancy_FLAKY(t *testing.T) {
104104
store.Record()
105105
defer store.Unrecord()
106106
// we intend to forget as many chunks as possible for the given redundancy level
107-
forget := parityCnt
108-
if parityCnt > shardCnt {
109-
forget = shardCnt
110-
}
107+
forget := min(parityCnt, shardCnt)
111108
if levels == 1 {
112109
forget = 2
113110
}
@@ -141,7 +138,7 @@ func TestBzzUploadDownloadWithRedundancy_FLAKY(t *testing.T) {
141138
if len(got) != len(want) {
142139
t.Fatalf("got %v parts, want %v parts", len(got), len(want))
143140
}
144-
for i := 0; i < len(want); i++ {
141+
for i := range want {
145142
if !bytes.Equal(got[i], want[i]) {
146143
t.Errorf("part %v: got %q, want %q", i, string(got[i]), string(want[i]))
147144
}
@@ -670,7 +667,7 @@ func TestBzzFilesRangeRequests(t *testing.T) {
670667
if len(got) != len(want) {
671668
t.Fatalf("got %v parts, want %v parts", len(got), len(want))
672669
}
673-
for i := 0; i < len(want); i++ {
670+
for i := range want {
674671
if !bytes.Equal(got[i], want[i]) {
675672
t.Errorf("part %v: got %q, want %q", i, string(got[i]), string(want[i]))
676673
}
@@ -681,7 +678,7 @@ func TestBzzFilesRangeRequests(t *testing.T) {
681678
}
682679
}
683680

684-
func createRangeHeader(data interface{}, ranges [][2]int) (header string, parts [][]byte) {
681+
func createRangeHeader(data any, ranges [][2]int) (header string, parts [][]byte) {
685682
getLen := func() int {
686683
switch data := data.(type) {
687684
case []byte:

pkg/api/chunk_stream_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ func TestChunkUploadStream(t *testing.T) {
3838

3939
t.Run("upload and verify", func(t *testing.T) {
4040
chsToGet := []swarm.Chunk{}
41-
for i := 0; i < 5; i++ {
41+
for range 5 {
4242
ch := testingc.GenerateTestRandomChunk()
4343

4444
err := wsConn.SetWriteDeadline(time.Now().Add(time.Second))

pkg/api/export_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ var ErrHexLength = errHexLength
133133

134134
type HexInvalidByteError = hexInvalidByteError
135135

136-
func MapStructure(input, output interface{}, hooks map[string]func(v string) (string, error)) error {
136+
func MapStructure(input, output any, hooks map[string]func(v string) (string, error)) error {
137137
return mapStructure(input, output, hooks)
138138
}
139139

pkg/api/logger_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,8 @@ func TestGetLoggers(t *testing.T) {
6565
}
6666
api.ReplaceLogRegistryIterateFn(fn)
6767

68-
have := make(map[string]interface{})
69-
want := make(map[string]interface{})
68+
have := make(map[string]any)
69+
want := make(map[string]any)
7070
data := `{"loggers":[{"id":"b25lWzBdW10-PjgyNDYzNDg2MDM2MA==","logger":"one","subsystem":"one[0][]\u003e\u003e824634860360","verbosity":"all"},{"id":"b25lL25hbWVbMF1bXT4-ODI0NjM0ODYwMzYw","logger":"one/name","subsystem":"one/name[0][]\u003e\u003e824634860360","verbosity":"warning"},{"id":"b25lL25hbWVbMF1bXCJ2YWxcIj0xXT4-ODI0NjM0ODYwMzYw","logger":"one/name","subsystem":"one/name[0][\\\"val\\\"=1]\u003e\u003e824634860360","verbosity":"warning"},{"id":"b25lL25hbWVbMV1bXT4-ODI0NjM0ODYwMzYw","logger":"one/name","subsystem":"one/name[1][]\u003e\u003e824634860360","verbosity":"info"},{"id":"b25lL25hbWVbMl1bXT4-ODI0NjM0ODYwMzYw","logger":"one/name","subsystem":"one/name[2][]\u003e\u003e824634860360","verbosity":"info"}],"tree":{"one":{"+":["all|one[0][]\u003e\u003e824634860360"],"/":{"name":{"+":["warning|one/name[0][]\u003e\u003e824634860360","warning|one/name[0][\\\"val\\\"=1]\u003e\u003e824634860360","info|one/name[1][]\u003e\u003e824634860360","info|one/name[2][]\u003e\u003e824634860360"]}}}}}`
7171
if err := json.Unmarshal([]byte(data), &want); err != nil {
7272
t.Fatalf("unexpected error: %v", err)

pkg/api/postage_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -702,7 +702,7 @@ func TestPostageAccessHandler(t *testing.T) {
702702
method string
703703
url string
704704
respCode int
705-
resp interface{}
705+
resp any
706706
}
707707

708708
success := []operation{

pkg/api/tag.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import (
1919
)
2020

2121
type tagRequest struct {
22-
Address swarm.Address `json:"address,omitempty"`
22+
Address swarm.Address `json:"address"`
2323
}
2424

2525
type tagResponse struct {

0 commit comments

Comments
 (0)