Merge pull request #9630 from xinhangzhou/master

refactor: use maps.Copy for cleaner map handling
This commit is contained in:
András Bánki-Horváth authored and GitHub committed 2025-03-28 07:54:34 +01:00
commit b6cf1bcaa0
7 files changed
+18 -28

No files matched your search

+2 -3
View File
@@ -2,6 +2,7 @@ package lnd
import ( import (
"fmt" "fmt"
"maps"
"sync" "sync"
"github.com/btcsuite/btcd/btcec/v2" "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 // We'll populate the server's peerCounts map with the counts fetched
// via initAccessPerms. Also note that we haven't yet connected to the // via initAccessPerms. Also note that we haven't yet connected to the
// peers. // peers.
for peerPub, count := range counts { maps.Copy(a.peerCounts, counts)
a.peerCounts[peerPub] = count
}
return a, nil return a, nil
} }
+4 -9
View File
@@ -7,6 +7,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"maps"
"time" "time"
"github.com/lightningnetwork/lnd/graph/db/models" "github.com/lightningnetwork/lnd/graph/db/models"
@@ -815,9 +816,7 @@ func (k *kvInvoiceUpdater) UpdateAmpState(setID [32]byte,
cancelledHtlcs := k.invoice.HTLCSet( cancelledHtlcs := k.invoice.HTLCSet(
&setID, invpkg.HtlcStateCanceled, &setID, invpkg.HtlcStateCanceled,
) )
for htlcKey, htlc := range cancelledHtlcs { maps.Copy(k.updatedAmpHtlcs[setID], cancelledHtlcs)
k.updatedAmpHtlcs[setID][htlcKey] = htlc
}
case invpkg.HtlcStateSettled: case invpkg.HtlcStateSettled:
k.updatedAmpHtlcs[setID] = make( k.updatedAmpHtlcs[setID] = make(
@@ -1438,9 +1437,7 @@ func fetchFilteredAmpInvoices(invoiceBucket kvdb.RBucket, invoiceNum []byte,
return nil, err return nil, err
} }
for key, htlc := range htlcsBySetID { maps.Copy(htlcs, htlcsBySetID)
htlcs[key] = htlc
}
} }
return htlcs, nil return htlcs, nil
@@ -1508,9 +1505,7 @@ func fetchAmpSubInvoices(invoiceBucket kvdb.RBucket, invoiceNum []byte,
return err return err
} }
for key, htlc := range htlcsBySetID { maps.Copy(htlcs, htlcsBySetID)
htlcs[key] = htlc
}
return nil return nil
}, },
@@ -480,6 +480,9 @@ the on going rate we'll permit.
inputs spending logic in the sweeper so it can properly handle missing inputs inputs spending logic in the sweeper so it can properly handle missing inputs
and recover from restart. 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 ## Tooling and Documentation
+2 -3
View File
@@ -2,6 +2,7 @@ package kvdb
import ( import (
"fmt" "fmt"
"maps"
"testing" "testing"
"github.com/btcsuite/btcwallet/walletdb" "github.com/btcsuite/btcwallet/walletdb"
@@ -77,9 +78,7 @@ func prefetchTest(t *testing.T, db walletdb.DB,
}, func() {}) }, func() {})
require.NoError(t, err) require.NoError(t, err)
for k, v := range put { maps.Copy(items, put)
items[k] = v
}
for _, k := range remove { for _, k := range remove {
delete(items, k) delete(items, k)
+3 -7
View File
@@ -4,6 +4,7 @@ import (
"bytes" "bytes"
"fmt" "fmt"
"io" "io"
"maps"
"sort" "sort"
"github.com/lightningnetwork/lnd/fn/v2" "github.com/lightningnetwork/lnd/fn/v2"
@@ -94,13 +95,8 @@ func (c CustomRecords) Copy() CustomRecords {
// records will be used. // records will be used.
func (c CustomRecords) MergedCopy(other CustomRecords) CustomRecords { func (c CustomRecords) MergedCopy(other CustomRecords) CustomRecords {
copiedRecords := make(CustomRecords, len(c)) copiedRecords := make(CustomRecords, len(c))
for k, v := range c { maps.Copy(copiedRecords, c)
copiedRecords[k] = v maps.Copy(copiedRecords, other)
}
for k, v := range other {
copiedRecords[k] = v
}
return copiedRecords return copiedRecords
} }
+2 -3
View File
@@ -7,6 +7,7 @@ import (
"errors" "errors"
"fmt" "fmt"
"io" "io"
"maps"
"math" "math"
"net" "net"
"net/http" "net/http"
@@ -3338,9 +3339,7 @@ func (r *rpcServer) GetInfo(_ context.Context,
// Add the features to our map of features, allowing over writing of // Add the features to our map of features, allowing over writing of
// existing values because features in different sets with the same bit // existing values because features in different sets with the same bit
// are duplicated across sets. // are duplicated across sets.
for bit, feature := range rpcFeatures { maps.Copy(features, rpcFeatures)
features[bit] = feature
}
} }
// TODO(roasbeef): add synced height n stuff // TODO(roasbeef): add synced height n stuff
+2 -3
View File
@@ -5,6 +5,7 @@ import (
"crypto/rand" "crypto/rand"
"errors" "errors"
"fmt" "fmt"
"maps"
"math/big" "math/big"
"net" "net"
"sync" "sync"
@@ -1252,9 +1253,7 @@ func (c *client) handleNewTower(tower *Tower) error {
return fmt.Errorf("unable to determine sessions for tower %x: "+ return fmt.Errorf("unable to determine sessions for tower %x: "+
"%v", tower.IdentityKey.SerializeCompressed(), err) "%v", tower.IdentityKey.SerializeCompressed(), err)
} }
for id, session := range sessions { maps.Copy(c.candidateSessions, sessions)
c.candidateSessions[id] = session
}
return nil return nil
} }