Skip to content

Commit fc11afb

Browse files
authored
lint: run modernize mapsloop (#6434)
1 parent ab66259 commit fc11afb

File tree

16 files changed

+43
-92
lines changed

16 files changed

+43
-92
lines changed

agreement/fuzzer/ledger_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package fuzzer
1919
import (
2020
"context"
2121
"fmt"
22+
"maps"
2223
"math/rand"
2324

2425
"github.com/algorand/go-algorand/agreement"
@@ -152,11 +153,8 @@ func makeTestLedger(state map[basics.Address]basics.AccountData, sync testLedger
152153
l.certs = make(map[basics.Round]agreement.Certificate)
153154
l.nextRound = 1
154155

155-
// deep copy of state
156156
l.state = make(map[basics.Address]basics.AccountData)
157-
for k, v := range state {
158-
l.state[k] = v
159-
}
157+
maps.Copy(l.state, state)
160158

161159
l.notifications = make(map[basics.Round]signal)
162160
l.EnsuringDigestStartCh = make(chan struct{})

config/consensus.go

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
package config
1818

1919
import (
20+
"maps"
2021
"time"
2122

2223
"github.com/algorand/go-algorand/config/bounds"
@@ -768,13 +769,7 @@ func (cp ConsensusProtocols) DeepCopy() ConsensusProtocols {
768769
staticConsensus := make(ConsensusProtocols)
769770
for consensusVersion, consensusParams := range cp {
770771
// recreate the ApprovedUpgrades map since we don't want to modify the original one.
771-
if consensusParams.ApprovedUpgrades != nil {
772-
newApprovedUpgrades := make(map[protocol.ConsensusVersion]uint64)
773-
for ver, when := range consensusParams.ApprovedUpgrades {
774-
newApprovedUpgrades[ver] = when
775-
}
776-
consensusParams.ApprovedUpgrades = newApprovedUpgrades
777-
}
772+
consensusParams.ApprovedUpgrades = maps.Clone(consensusParams.ApprovedUpgrades)
778773
staticConsensus[consensusVersion] = consensusParams
779774
}
780775
return staticConsensus

crypto/merkletrie/cache.go

Lines changed: 2 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -256,9 +256,7 @@ func (mtc *merkleTrieCache) loadPage(page uint64) (err error) {
256256
mtc.cachedNodeCount += len(mtc.pageToNIDsPtr[page])
257257
} else {
258258
mtc.cachedNodeCount -= len(mtc.pageToNIDsPtr[page])
259-
for nodeID, pnode := range decodedNodes {
260-
mtc.pageToNIDsPtr[page][nodeID] = pnode
261-
}
259+
maps.Copy(mtc.pageToNIDsPtr[page], decodedNodes)
262260
mtc.cachedNodeCount += len(mtc.pageToNIDsPtr[page])
263261
}
264262

@@ -485,9 +483,7 @@ func (mtc *merkleTrieCache) reallocatePendingPages(stats *CommitStats) (pagesToC
485483
delete(createdPages, page)
486484
}
487485

488-
for pageID, page := range mtc.reallocatedPages {
489-
createdPages[pageID] = page
490-
}
486+
maps.Copy(createdPages, mtc.reallocatedPages)
491487

492488
for _, nodeIDs := range createdPages {
493489
for _, node := range nodeIDs {

data/account/registeryDbOps.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"database/sql"
2222
"errors"
2323
"fmt"
24+
"maps"
2425
"strings"
2526

2627
"github.com/algorand/go-algorand/data/basics"
@@ -257,9 +258,7 @@ func (f *flushOp) apply(db *participationDB) error {
257258
if err != nil {
258259
// put back what we didn't finish with
259260
db.mutex.Lock()
260-
for id, v := range dirty {
261-
db.dirty[id] = v
262-
}
261+
maps.Copy(db.dirty, dirty)
263262
db.mutex.Unlock()
264263
}
265264

data/basics/serr.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package basics
1818

1919
import (
2020
"errors"
21+
"maps"
2122
"strings"
2223

2324
"golang.org/x/exp/slog"
@@ -108,9 +109,7 @@ func Wrap(err error, msg string, field string, pairs ...any) error {
108109
var inner *SError
109110
if ok := errors.As(err, &inner); ok {
110111
attributes := make(map[string]any, len(inner.Attrs))
111-
for key, val := range inner.Attrs {
112-
attributes[key] = val
113-
}
112+
maps.Copy(attributes, inner.Attrs)
114113
serr.Attrs[field+"-attrs"] = attributes
115114
}
116115

data/pools/transactionPool.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package pools
1919
import (
2020
"errors"
2121
"fmt"
22+
"maps"
2223
"sync"
2324
"sync/atomic"
2425
"time"
@@ -268,9 +269,7 @@ func (pool *TransactionPool) rememberCommit(flush bool) {
268269
} else {
269270
pool.pendingTxGroups = append(pool.pendingTxGroups, pool.rememberedTxGroups...)
270271

271-
for txid, txn := range pool.rememberedTxids {
272-
pool.pendingTxids[txid] = txn
273-
}
272+
maps.Copy(pool.pendingTxids, pool.rememberedTxids)
274273
}
275274

276275
pool.rememberedTxGroups = nil

data/transactions/logic/mocktracer/scenarios.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package mocktracer
1919
import (
2020
"encoding/hex"
2121
"fmt"
22+
"maps"
2223
"math"
2324

2425
"github.com/algorand/go-algorand/crypto"
@@ -836,9 +837,7 @@ func MergeStateDeltas(deltas ...ledgercore.StateDelta) ledgercore.StateDelta {
836837
includedTx.Intra += txidBase
837838
result.Txids[txid] = includedTx
838839
}
839-
for lease, round := range delta.Txleases {
840-
result.Txleases[lease] = round
841-
}
840+
maps.Copy(result.Txleases, delta.Txleases)
842841
}
843842
return result
844843
}

ledger/acctdeltas_test.go

Lines changed: 6 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import (
2323
"encoding/binary"
2424
"errors"
2525
"fmt"
26+
"maps"
2627
"math"
2728
"math/rand"
2829
"os"
@@ -188,9 +189,7 @@ func creatablesFromUpdates(base map[basics.Address]basics.AccountData, updates l
188189

189190
func applyPartialDeltas(base map[basics.Address]basics.AccountData, deltas ledgercore.AccountDeltas) map[basics.Address]basics.AccountData {
190191
result := make(map[basics.Address]basics.AccountData, len(base)+deltas.Len())
191-
for addr, ad := range base {
192-
result[addr] = ad
193-
}
192+
maps.Copy(result, base)
194193

195194
for i := 0; i < deltas.Len(); i++ {
196195
addr, _ := deltas.GetByIdx(i)
@@ -1478,18 +1477,10 @@ func (m mockAccountWriter) clone() (m2 mockAccountWriter) {
14781477
m2.resources = make(map[mockResourcesKey]ledgercore.AccountResource, len(m.resources))
14791478
m2.addresses = make(map[basics.Address]trackerdb.AccountRef, len(m.resources))
14801479
m2.rowids = make(map[trackerdb.AccountRef]basics.Address, len(m.rowids))
1481-
for k, v := range m.accounts {
1482-
m2.accounts[k] = v
1483-
}
1484-
for k, v := range m.resources {
1485-
m2.resources[k] = v
1486-
}
1487-
for k, v := range m.addresses {
1488-
m2.addresses[k] = v
1489-
}
1490-
for k, v := range m.rowids {
1491-
m2.rowids[k] = v
1492-
}
1480+
maps.Copy(m2.accounts, m.accounts)
1481+
maps.Copy(m2.resources, m.resources)
1482+
maps.Copy(m2.addresses, m.addresses)
1483+
maps.Copy(m2.rowids, m.rowids)
14931484
m2.lastAcctRef = m.lastAcctRef
14941485
m2.availAcctRefs = m.availAcctRefs
14951486
return m2

ledger/acctonline_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ package ledger
1919
import (
2020
"context"
2121
"fmt"
22+
"maps"
2223
"sort"
2324
"strconv"
2425
"testing"
@@ -871,9 +872,7 @@ func TestAcctOnlineCacheDBSync(t *testing.T) {
871872
copyGenesisAccts := func() []map[basics.Address]basics.AccountData {
872873
accounts := []map[basics.Address]basics.AccountData{{}}
873874
accounts[0] = make(map[basics.Address]basics.AccountData, numAccts)
874-
for addr, ad := range genesisAccts[0] {
875-
accounts[0][addr] = ad
876-
}
875+
maps.Copy(accounts[0], genesisAccts[0])
877876
return accounts
878877
}
879878

ledger/acctupdates_test.go

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import (
2121
"context"
2222
"errors"
2323
"fmt"
24+
"maps"
2425
"os"
2526
"runtime"
2627
"strings"
@@ -167,15 +168,12 @@ func (ml *mockLedgerForTracker) fork(t testing.TB) *mockLedgerForTracker {
167168
log: dblogger,
168169
blocks: make([]blockEntry, len(ml.blocks)),
169170
deltas: make([]ledgercore.StateDelta, len(ml.deltas)),
170-
accts: make(map[basics.Address]basics.AccountData),
171+
accts: maps.Clone(ml.accts),
171172
filename: fn,
172173
consensusParams: ml.consensusParams,
173174
consensusVersion: ml.consensusVersion,
174175
trackers: trackerRegistry{log: dblogger},
175176
}
176-
for k, v := range ml.accts {
177-
newLedgerTracker.accts[k] = v
178-
}
179177
copy(newLedgerTracker.blocks, ml.blocks)
180178
copy(newLedgerTracker.deltas, ml.deltas)
181179

0 commit comments

Comments
 (0)