Skip to content

Commit 7162e91

Browse files
committed
refactor: use maps.Copy to simplify the code
Signed-off-by: rustversion <[email protected]>
1 parent 98d6277 commit 7162e91

File tree

7 files changed

+15
-24
lines changed

7 files changed

+15
-24
lines changed

commit/merkleroot/rmn/controller_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"crypto/sha256"
77
"errors"
88
"fmt"
9+
"maps"
910
"sort"
1011
"strconv"
1112
"strings"
@@ -1053,9 +1054,7 @@ func (m *mockPeerClient) getReceivedRequests() map[rmntypes.NodeID][]*rmnpb.Requ
10531054
cp := make(map[rmntypes.NodeID][]*rmnpb.Request)
10541055
m.mu.RLock()
10551056
defer m.mu.RUnlock()
1056-
for k, v := range m.receivedRequests {
1057-
cp[k] = v
1058-
}
1057+
maps.Copy(cp, m.receivedRequests)
10591058
return cp
10601059
}
10611060

commit/merkleroot/transmission_checks_test.go

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

33
import (
4+
"maps"
45
"testing"
56

67
"github.com/stretchr/testify/mock"
@@ -28,9 +29,7 @@ func TestValidateRootBlessings(t *testing.T) {
2829
copyConfigWithOverride := func(chain cciptypes.ChainSelector,
2930
override reader2.StaticSourceChainConfig) map[cciptypes.ChainSelector]reader2.StaticSourceChainConfig {
3031
config := make(map[cciptypes.ChainSelector]reader2.StaticSourceChainConfig)
31-
for k, v := range sourceChainConfig {
32-
config[k] = v
33-
}
32+
maps.Copy(config, sourceChainConfig)
3433
config[chain] = override
3534
return config
3635
}

pkg/addressbook/book.go

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

33
import (
44
"errors"
5+
"maps"
56
"sync"
67

78
"github.com/smartcontractkit/chainlink-common/pkg/types/ccipocr3"
@@ -73,9 +74,7 @@ func (b *Book) InsertOrUpdate(addresses ContractAddresses) error {
7374
}
7475

7576
// if contract exists, set or replace any existing address for each chain
76-
for chain, addr := range chains {
77-
b.mem[name][chain] = addr
78-
}
77+
maps.Copy(b.mem[name], chains)
7978
}
8079

8180
return nil

pkg/ocrtypecodec/v1/translate.go

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

33
import (
4+
"maps"
45
"math/big"
56

67
"google.golang.org/protobuf/types/known/timestamppb"
@@ -854,9 +855,7 @@ func (t *protoTranslator) nonceObservationsToProto(
854855

855856
for chainSel, nonceMap := range observations {
856857
addrToNonce := make(map[string]uint64, len(nonceMap))
857-
for addr, nonce := range nonceMap {
858-
addrToNonce[addr] = nonce
859-
}
858+
maps.Copy(addrToNonce, nonceMap)
860859
nonceObservations[uint64(chainSel)] = &ocrtypecodecpb.StringAddrToNonce{Nonces: addrToNonce}
861860
}
862861

@@ -873,9 +872,7 @@ func (t *protoTranslator) nonceObservationsFromProto(
873872

874873
for chainSel, nonceMap := range pbObservations {
875874
innerMap := make(map[string]uint64, len(nonceMap.Nonces))
876-
for addr, nonce := range nonceMap.Nonces {
877-
innerMap[addr] = nonce
878-
}
875+
maps.Copy(innerMap, nonceMap.Nonces)
879876
nonces[cciptypes.ChainSelector(chainSel)] = innerMap
880877
}
881878

pkg/reader/config_poller.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package reader
33
import (
44
"context"
55
"fmt"
6+
"maps"
67
"sync"
78
"sync/atomic"
89
"time"
@@ -591,9 +592,7 @@ func (c *configPoller) GetOfframpSourceChainConfigs(
591592
}
592593

593594
// Merge the new configs with existing cached results
594-
for chain, config := range newCachedConfigs {
595-
cachedSourceConfigs[chain] = config
596-
}
595+
maps.Copy(cachedSourceConfigs, newCachedConfigs)
597596

598597
return cachedSourceConfigs, nil
599598
}

pkg/reader/config_poller_v2_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package reader
33
import (
44
"context"
55
"errors"
6+
"maps"
67
"sync"
78
"sync/atomic"
89
"testing"
@@ -314,9 +315,7 @@ func TestConfigPollerV2_BatchRefreshChainAndSourceConfigs_Error(t *testing.T) {
314315
cache.sourceChainMu.RLock()
315316
initialSourceRefreshTime := cache.sourceChainRefresh
316317
initialSourceConfigData := make(map[cciptypes.ChainSelector]StaticSourceChainConfig)
317-
for k, v := range cache.staticSourceChainConfigs {
318-
initialSourceConfigData[k] = v
319-
}
318+
maps.Copy(initialSourceConfigData, cache.staticSourceChainConfigs)
320319
cache.sourceChainMu.RUnlock()
321320

322321
// Verify cache was populated

pkg/reader/usdc_reader.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"encoding/binary"
66
"fmt"
7+
"maps"
78

89
sel "github.com/smartcontractkit/chain-selectors"
910

@@ -231,9 +232,7 @@ func AllAvailableDomains() map[uint64]uint32 {
231232
}
232233

233234
destDomains := make(map[uint64]uint32)
234-
for k, v := range CCTPDestDomains {
235-
destDomains[k] = v
236-
}
235+
maps.Copy(destDomains, CCTPDestDomains)
237236

238237
for i, chainID := range chainIDs {
239238
chainSelector, _ := sel.SelectorFromChainId(chainID)

0 commit comments

Comments
 (0)