Skip to content

Commit b6cf1bc

Browse files
authored
Merge pull request #9630 from xinhangzhou/master
refactor: use maps.Copy for cleaner map handling
2 parents eb822a5 + b7e3c20 commit b6cf1bc

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
@@ -480,6 +480,9 @@ the on going rate we'll permit.
480480
inputs spending logic in the sweeper so it can properly handle missing inputs
481481
and recover from restart.
482482

483+
* A code refactor to [use maps.Copy instead of manually copying map
484+
elements](https://github.com/lightningnetwork/lnd/pull/9630).
485+
483486

484487
## Tooling and Documentation
485488

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"
@@ -3338,9 +3339,7 @@ func (r *rpcServer) GetInfo(_ context.Context,
33383339
// Add the features to our map of features, allowing over writing of
33393340
// existing values because features in different sets with the same bit
33403341
// are duplicated across sets.
3341-
for bit, feature := range rpcFeatures {
3342-
features[bit] = feature
3343-
}
3342+
maps.Copy(features, rpcFeatures)
33443343
}
33453344

33463345
// 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)