Skip to content

Commit b7e3c20

Browse files
committed
refactor: use maps.Copy for cleaner map handling
Signed-off-by: xinhangzhou <shuangcui@aliyun.com>
1 parent 5235f3b commit b7e3c20

File tree

7 files changed

+18
-28
lines changed

7 files changed

+18
-28
lines changed

accessman.go

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

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

78
"github.com/btcsuite/btcd/btcec/v2"
@@ -66,9 +67,7 @@ func newAccessMan(cfg *accessManConfig) (*accessMan, error) {
6667
// We'll populate the server's peerCounts map with the counts fetched
6768
// via initAccessPerms. Also note that we haven't yet connected to the
6869
// peers.
69-
for peerPub, count := range counts {
70-
a.peerCounts[peerPub] = count
71-
}
70+
maps.Copy(a.peerCounts, counts)
7271

7372
return a, nil
7473
}

channeldb/invoices.go

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"errors"
88
"fmt"
99
"io"
10+
"maps"
1011
"time"
1112

1213
"github.com/lightningnetwork/lnd/graph/db/models"
@@ -815,9 +816,7 @@ func (k *kvInvoiceUpdater) UpdateAmpState(setID [32]byte,
815816
cancelledHtlcs := k.invoice.HTLCSet(
816817
&setID, invpkg.HtlcStateCanceled,
817818
)
818-
for htlcKey, htlc := range cancelledHtlcs {
819-
k.updatedAmpHtlcs[setID][htlcKey] = htlc
820-
}
819+
maps.Copy(k.updatedAmpHtlcs[setID], cancelledHtlcs)
821820

822821
case invpkg.HtlcStateSettled:
823822
k.updatedAmpHtlcs[setID] = make(
@@ -1438,9 +1437,7 @@ func fetchFilteredAmpInvoices(invoiceBucket kvdb.RBucket, invoiceNum []byte,
14381437
return nil, err
14391438
}
14401439

1441-
for key, htlc := range htlcsBySetID {
1442-
htlcs[key] = htlc
1443-
}
1440+
maps.Copy(htlcs, htlcsBySetID)
14441441
}
14451442

14461443
return htlcs, nil
@@ -1508,9 +1505,7 @@ func fetchAmpSubInvoices(invoiceBucket kvdb.RBucket, invoiceNum []byte,
15081505
return err
15091506
}
15101507

1511-
for key, htlc := range htlcsBySetID {
1512-
htlcs[key] = htlc
1513-
}
1508+
maps.Copy(htlcs, htlcsBySetID)
15141509

15151510
return nil
15161511
},

docs/release-notes/release-notes-0.19.0.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -433,6 +433,9 @@ The underlying functionality between those two options remain the same.
433433
inputs spending logic in the sweeper so it can properly handle missing inputs
434434
and recover from restart.
435435

436+
* A code refactor to [use maps.Copy instead of manually copying map
437+
elements](https://github.com/lightningnetwork/lnd/pull/9630).
438+
436439

437440
## Tooling and Documentation
438441

kvdb/prefetch_test.go

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

33
import (
44
"fmt"
5+
"maps"
56
"testing"
67

78
"github.com/btcsuite/btcwallet/walletdb"
@@ -77,9 +78,7 @@ func prefetchTest(t *testing.T, db walletdb.DB,
7778
}, func() {})
7879
require.NoError(t, err)
7980

80-
for k, v := range put {
81-
items[k] = v
82-
}
81+
maps.Copy(items, put)
8382

8483
for _, k := range remove {
8584
delete(items, k)

lnwire/custom_records.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"fmt"
66
"io"
7+
"maps"
78
"sort"
89

910
"github.com/lightningnetwork/lnd/fn/v2"
@@ -94,13 +95,8 @@ func (c CustomRecords) Copy() CustomRecords {
9495
// records will be used.
9596
func (c CustomRecords) MergedCopy(other CustomRecords) CustomRecords {
9697
copiedRecords := make(CustomRecords, len(c))
97-
for k, v := range c {
98-
copiedRecords[k] = v
99-
}
100-
101-
for k, v := range other {
102-
copiedRecords[k] = v
103-
}
98+
maps.Copy(copiedRecords, c)
99+
maps.Copy(copiedRecords, other)
104100

105101
return copiedRecords
106102
}

rpcserver.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import (
77
"errors"
88
"fmt"
99
"io"
10+
"maps"
1011
"math"
1112
"net"
1213
"net/http"
@@ -3339,9 +3340,7 @@ func (r *rpcServer) GetInfo(_ context.Context,
33393340
// Add the features to our map of features, allowing over writing of
33403341
// existing values because features in different sets with the same bit
33413342
// are duplicated across sets.
3342-
for bit, feature := range rpcFeatures {
3343-
features[bit] = feature
3344-
}
3343+
maps.Copy(features, rpcFeatures)
33453344
}
33463345

33473346
// TODO(roasbeef): add synced height n stuff

watchtower/wtclient/client.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"crypto/rand"
66
"errors"
77
"fmt"
8+
"maps"
89
"math/big"
910
"net"
1011
"sync"
@@ -1252,9 +1253,7 @@ func (c *client) handleNewTower(tower *Tower) error {
12521253
return fmt.Errorf("unable to determine sessions for tower %x: "+
12531254
"%v", tower.IdentityKey.SerializeCompressed(), err)
12541255
}
1255-
for id, session := range sessions {
1256-
c.candidateSessions[id] = session
1257-
}
1256+
maps.Copy(c.candidateSessions, sessions)
12581257

12591258
return nil
12601259
}

0 commit comments

Comments
 (0)