From b2e0fdad7b7258e3f1fbc43d4d93466bf944d440 Mon Sep 17 00:00:00 2001 From: islishude Date: Sat, 1 Mar 2025 23:19:48 +0800 Subject: [PATCH] all: replace manual map copying with maps.Copy for improved readability --- cmd/devp2p/crawl.go | 5 ++--- consensus/clique/api.go | 5 ++--- core/blockchain_test.go | 5 ++--- core/state/snapshot/difflayer.go | 4 +--- core/state/snapshot/difflayer_test.go | 9 +++------ core/txpool/txpool.go | 13 ++++--------- rpc/client_opt.go | 5 ++--- rpc/http.go | 5 ++--- rpc/websocket.go | 5 ++--- 9 files changed, 20 insertions(+), 36 deletions(-) diff --git a/cmd/devp2p/crawl.go b/cmd/devp2p/crawl.go index 4288a5feb8..4b3dfb542d 100644 --- a/cmd/devp2p/crawl.go +++ b/cmd/devp2p/crawl.go @@ -24,6 +24,7 @@ import ( "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/p2p/enode" + "maps" ) type crawler struct { @@ -72,9 +73,7 @@ func newCrawler(input nodeSet, bootnodes []*enode.Node, disc resolver, iters ... c.iters = append(c.iters, c.inputIter) // Copy input to output initially. Any nodes that fail validation // will be dropped from output during the run. - for id, n := range input { - c.output[id] = n - } + maps.Copy(c.output, input) return c, nil } diff --git a/consensus/clique/api.go b/consensus/clique/api.go index 374b50692d..090ec7b9ed 100644 --- a/consensus/clique/api.go +++ b/consensus/clique/api.go @@ -26,6 +26,7 @@ import ( "github.com/ethereum/go-ethereum/core/types" "github.com/ethereum/go-ethereum/rlp" "github.com/ethereum/go-ethereum/rpc" + "maps" ) // API is a user facing RPC API to allow controlling the signer and voting @@ -99,9 +100,7 @@ func (api *API) Proposals() map[common.Address]bool { defer api.clique.lock.RUnlock() proposals := make(map[common.Address]bool) - for address, auth := range api.clique.proposals { - proposals[address] = auth - } + maps.Copy(proposals, api.clique.proposals) return proposals } diff --git a/core/blockchain_test.go b/core/blockchain_test.go index 04dfa87b8b..388c19fbc6 100644 --- a/core/blockchain_test.go +++ b/core/blockchain_test.go @@ -45,6 +45,7 @@ import ( "github.com/ethereum/go-ethereum/params" "github.com/ethereum/go-ethereum/trie" "github.com/holiman/uint256" + "maps" ) // So we can deterministically seed different blockchains @@ -3062,9 +3063,7 @@ func testDeleteRecreateSlotsAcrossManyBlocks(t *testing.T, scheme string) { var exp = new(expectation) exp.blocknum = i + 1 exp.values = make(map[int]int) - for k, v := range current.values { - exp.values[k] = v - } + maps.Copy(exp.values, current.values) exp.exist = current.exist b.SetCoinbase(common.Address{1}) diff --git a/core/state/snapshot/difflayer.go b/core/state/snapshot/difflayer.go index 28957051d4..f2f7667e8e 100644 --- a/core/state/snapshot/difflayer.go +++ b/core/state/snapshot/difflayer.go @@ -388,9 +388,7 @@ func (dl *diffLayer) flatten() snapshot { if parent.stale.Swap(true) { panic("parent diff layer is stale") // we've flattened into the same parent from two children, boo } - for hash, data := range dl.accountData { - parent.accountData[hash] = data - } + maps.Copy(parent.accountData, dl.accountData) // Overwrite all the updated storage slots (individually) for accountHash, storage := range dl.storageData { // If storage didn't exist (or was deleted) in the parent, overwrite blindly diff --git a/core/state/snapshot/difflayer_test.go b/core/state/snapshot/difflayer_test.go index b212098e75..369e40262a 100644 --- a/core/state/snapshot/difflayer_test.go +++ b/core/state/snapshot/difflayer_test.go @@ -26,13 +26,12 @@ import ( "github.com/ethereum/go-ethereum/common" "github.com/ethereum/go-ethereum/crypto" "github.com/ethereum/go-ethereum/ethdb/memorydb" + "maps" ) func copyAccounts(accounts map[common.Hash][]byte) map[common.Hash][]byte { copy := make(map[common.Hash][]byte) - for hash, blob := range accounts { - copy[hash] = blob - } + maps.Copy(copy, accounts) return copy } @@ -40,9 +39,7 @@ func copyStorage(storage map[common.Hash]map[common.Hash][]byte) map[common.Hash copy := make(map[common.Hash]map[common.Hash][]byte) for accHash, slots := range storage { copy[accHash] = make(map[common.Hash][]byte) - for slotHash, blob := range slots { - copy[accHash][slotHash] = blob - } + maps.Copy(copy[accHash], slots) } return copy } diff --git a/core/txpool/txpool.go b/core/txpool/txpool.go index 55812415f9..c530603930 100644 --- a/core/txpool/txpool.go +++ b/core/txpool/txpool.go @@ -29,6 +29,7 @@ import ( "github.com/ethereum/go-ethereum/event" "github.com/ethereum/go-ethereum/log" "github.com/ethereum/go-ethereum/metrics" + "maps" ) // TxStatus is the current status of a transaction as seen by the pool. @@ -390,9 +391,7 @@ func (p *TxPool) Add(txs []*types.Transaction, sync bool) []error { func (p *TxPool) Pending(filter PendingFilter) map[common.Address][]*LazyTransaction { txs := make(map[common.Address][]*LazyTransaction) for _, subpool := range p.subpools { - for addr, set := range subpool.Pending(filter) { - txs[addr] = set - } + maps.Copy(txs, subpool.Pending(filter)) } return txs } @@ -445,12 +444,8 @@ func (p *TxPool) Content() (map[common.Address][]*types.Transaction, map[common. for _, subpool := range p.subpools { run, block := subpool.Content() - for addr, txs := range run { - runnable[addr] = txs - } - for addr, txs := range block { - blocked[addr] = txs - } + maps.Copy(runnable, run) + maps.Copy(blocked, block) } return runnable, blocked } diff --git a/rpc/client_opt.go b/rpc/client_opt.go index 3fa045a9b9..582d266e9e 100644 --- a/rpc/client_opt.go +++ b/rpc/client_opt.go @@ -20,6 +20,7 @@ import ( "net/http" "github.com/gorilla/websocket" + "maps" ) // ClientOption is a configuration option for the RPC client. @@ -89,9 +90,7 @@ func WithHeader(key, value string) ClientOption { func WithHeaders(headers http.Header) ClientOption { return optionFunc(func(cfg *clientConfig) { cfg.initHeaders() - for k, vs := range headers { - cfg.httpHeaders[k] = vs - } + maps.Copy(cfg.httpHeaders, headers) }) } diff --git a/rpc/http.go b/rpc/http.go index f4b99429ef..f898d6a913 100644 --- a/rpc/http.go +++ b/rpc/http.go @@ -23,6 +23,7 @@ import ( "errors" "fmt" "io" + "maps" "math" "mime" "net/http" @@ -146,9 +147,7 @@ func newClientTransportHTTP(endpoint string, cfg *clientConfig) reconnectFunc { headers := make(http.Header, 2+len(cfg.httpHeaders)) headers.Set("accept", contentType) headers.Set("content-type", contentType) - for key, values := range cfg.httpHeaders { - headers[key] = values - } + maps.Copy(headers, cfg.httpHeaders) client := cfg.httpClient if client == nil { diff --git a/rpc/websocket.go b/rpc/websocket.go index 9f67caf859..45280516ec 100644 --- a/rpc/websocket.go +++ b/rpc/websocket.go @@ -30,6 +30,7 @@ import ( mapset "github.com/deckarep/golang-set/v2" "github.com/ethereum/go-ethereum/log" "github.com/gorilla/websocket" + "maps" ) const ( @@ -236,9 +237,7 @@ func newClientTransportWS(endpoint string, cfg *clientConfig) (reconnectFunc, er if err != nil { return nil, err } - for key, values := range cfg.httpHeaders { - header[key] = values - } + maps.Copy(header, cfg.httpHeaders) connect := func(ctx context.Context) (ServerCodec, error) { header := header.Clone()