From b7e3c20383d9d7a4f90cef94d83c0085f4673fd6 Mon Sep 17 00:00:00 2001 From: xinhangzhou Date: Mon, 24 Mar 2025 17:13:47 +0800 Subject: [PATCH] refactor: use maps.Copy for cleaner map handling Signed-off-by: xinhangzhou --- accessman.go | 5 ++--- channeldb/invoices.go | 13 ++++--------- docs/release-notes/release-notes-0.19.0.md | 3 +++ kvdb/prefetch_test.go | 5 ++--- lnwire/custom_records.go | 10 +++------- rpcserver.go | 5 ++--- watchtower/wtclient/client.go | 5 ++--- 7 files changed, 18 insertions(+), 28 deletions(-) diff --git a/accessman.go b/accessman.go index e1ccc5c34..b11ced72c 100644 --- a/accessman.go +++ b/accessman.go @@ -2,6 +2,7 @@ package lnd import ( "fmt" + "maps" "sync" "github.com/btcsuite/btcd/btcec/v2" @@ -66,9 +67,7 @@ func newAccessMan(cfg *accessManConfig) (*accessMan, error) { // We'll populate the server's peerCounts map with the counts fetched // via initAccessPerms. Also note that we haven't yet connected to the // peers. - for peerPub, count := range counts { - a.peerCounts[peerPub] = count - } + maps.Copy(a.peerCounts, counts) return a, nil } diff --git a/channeldb/invoices.go b/channeldb/invoices.go index 1ef323d9d..13439bf3d 100644 --- a/channeldb/invoices.go +++ b/channeldb/invoices.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "io" + "maps" "time" "github.com/lightningnetwork/lnd/graph/db/models" @@ -815,9 +816,7 @@ func (k *kvInvoiceUpdater) UpdateAmpState(setID [32]byte, cancelledHtlcs := k.invoice.HTLCSet( &setID, invpkg.HtlcStateCanceled, ) - for htlcKey, htlc := range cancelledHtlcs { - k.updatedAmpHtlcs[setID][htlcKey] = htlc - } + maps.Copy(k.updatedAmpHtlcs[setID], cancelledHtlcs) case invpkg.HtlcStateSettled: k.updatedAmpHtlcs[setID] = make( @@ -1438,9 +1437,7 @@ func fetchFilteredAmpInvoices(invoiceBucket kvdb.RBucket, invoiceNum []byte, return nil, err } - for key, htlc := range htlcsBySetID { - htlcs[key] = htlc - } + maps.Copy(htlcs, htlcsBySetID) } return htlcs, nil @@ -1508,9 +1505,7 @@ func fetchAmpSubInvoices(invoiceBucket kvdb.RBucket, invoiceNum []byte, return err } - for key, htlc := range htlcsBySetID { - htlcs[key] = htlc - } + maps.Copy(htlcs, htlcsBySetID) return nil }, diff --git a/docs/release-notes/release-notes-0.19.0.md b/docs/release-notes/release-notes-0.19.0.md index e4ce83116..160b3bd33 100644 --- a/docs/release-notes/release-notes-0.19.0.md +++ b/docs/release-notes/release-notes-0.19.0.md @@ -433,6 +433,9 @@ The underlying functionality between those two options remain the same. inputs spending logic in the sweeper so it can properly handle missing inputs and recover from restart. +* A code refactor to [use maps.Copy instead of manually copying map + elements](https://github.com/lightningnetwork/lnd/pull/9630). + ## Tooling and Documentation diff --git a/kvdb/prefetch_test.go b/kvdb/prefetch_test.go index 4d9a851a1..1b3aaa810 100644 --- a/kvdb/prefetch_test.go +++ b/kvdb/prefetch_test.go @@ -2,6 +2,7 @@ package kvdb import ( "fmt" + "maps" "testing" "github.com/btcsuite/btcwallet/walletdb" @@ -77,9 +78,7 @@ func prefetchTest(t *testing.T, db walletdb.DB, }, func() {}) require.NoError(t, err) - for k, v := range put { - items[k] = v - } + maps.Copy(items, put) for _, k := range remove { delete(items, k) diff --git a/lnwire/custom_records.go b/lnwire/custom_records.go index a63aa5dfb..de5ff4a23 100644 --- a/lnwire/custom_records.go +++ b/lnwire/custom_records.go @@ -4,6 +4,7 @@ import ( "bytes" "fmt" "io" + "maps" "sort" "github.com/lightningnetwork/lnd/fn/v2" @@ -94,13 +95,8 @@ func (c CustomRecords) Copy() CustomRecords { // records will be used. func (c CustomRecords) MergedCopy(other CustomRecords) CustomRecords { copiedRecords := make(CustomRecords, len(c)) - for k, v := range c { - copiedRecords[k] = v - } - - for k, v := range other { - copiedRecords[k] = v - } + maps.Copy(copiedRecords, c) + maps.Copy(copiedRecords, other) return copiedRecords } diff --git a/rpcserver.go b/rpcserver.go index fea7ac30e..cec5823f4 100644 --- a/rpcserver.go +++ b/rpcserver.go @@ -7,6 +7,7 @@ import ( "errors" "fmt" "io" + "maps" "math" "net" "net/http" @@ -3339,9 +3340,7 @@ func (r *rpcServer) GetInfo(_ context.Context, // Add the features to our map of features, allowing over writing of // existing values because features in different sets with the same bit // are duplicated across sets. - for bit, feature := range rpcFeatures { - features[bit] = feature - } + maps.Copy(features, rpcFeatures) } // TODO(roasbeef): add synced height n stuff diff --git a/watchtower/wtclient/client.go b/watchtower/wtclient/client.go index b3e322faa..8d74e9d1f 100644 --- a/watchtower/wtclient/client.go +++ b/watchtower/wtclient/client.go @@ -5,6 +5,7 @@ import ( "crypto/rand" "errors" "fmt" + "maps" "math/big" "net" "sync" @@ -1252,9 +1253,7 @@ func (c *client) handleNewTower(tower *Tower) error { return fmt.Errorf("unable to determine sessions for tower %x: "+ "%v", tower.IdentityKey.SerializeCompressed(), err) } - for id, session := range sessions { - c.candidateSessions[id] = session - } + maps.Copy(c.candidateSessions, sessions) return nil }