From edbc2076949f3b4af7a6b9e3fc7a2a20793856d9 Mon Sep 17 00:00:00 2001 From: joemicky Date: Thu, 14 Aug 2025 11:33:55 +0800 Subject: [PATCH] refactor: use a more modern writing style to simplify code Signed-off-by: joemicky --- accounts/checkers.go | 21 +++++++-------------- accounts/checkers_test.go | 1 - accounts/interceptor.go | 4 ++-- accounts/rpcserver.go | 1 - accounts/service.go | 3 --- firewalldb/privacy_mapper.go | 5 ++--- rules/channel_restrictions_test.go | 1 - rules/peer_restrictions_test.go | 1 - session/tlv_test.go | 1 - 9 files changed, 11 insertions(+), 27 deletions(-) diff --git a/accounts/checkers.go b/accounts/checkers.go index 0b99bd68a..f755cb70e 100644 --- a/accounts/checkers.go +++ b/accounts/checkers.go @@ -494,7 +494,6 @@ func filterInvoices(ctx context.Context, // after filtering. var filteredInvoices []*lnrpc.Invoice for _, invoice := range t.Invoices { - invoice := invoice hash, err := lntypes.MakeHash(invoice.RHash) if err != nil { @@ -523,7 +522,6 @@ func filterPayments(ctx context.Context, // after filtering. var filteredPayments []*lnrpc.Payment for _, payment := range t.Payments { - payment := payment hash, err := lntypes.MakeHashFromStr(payment.PaymentHash) if err != nil { @@ -549,10 +547,7 @@ func checkSend(ctx context.Context, chainParams *chaincfg.Params, return err } - sendAmt := lnwire.NewMSatFromSatoshis(btcutil.Amount(amt)) - if lnwire.MilliSatoshi(amtMsat) > sendAmt { - sendAmt = lnwire.MilliSatoshi(amtMsat) - } + sendAmt := max(lnwire.MilliSatoshi(amtMsat), lnwire.NewMSatFromSatoshis(btcutil.Amount(amt))) // We require that a payment hash is set, which we either read from the // payment hash from the invoice or from the request. @@ -697,10 +692,9 @@ func checkSendToRoute(ctx context.Context, service Service, paymentHash []byte, return fmt.Errorf("invalid route") } - sendAmt := lnwire.NewMSatFromSatoshis(btcutil.Amount(route.TotalAmt)) // nolint - if lnwire.MilliSatoshi(route.TotalAmtMsat) > sendAmt { - sendAmt = lnwire.MilliSatoshi(route.TotalAmtMsat) - } + sendAmt := max( + // nolint + lnwire.MilliSatoshi(route.TotalAmtMsat), lnwire.NewMSatFromSatoshis(btcutil.Amount(route.TotalAmt))) log.Tracef("Handling send request for payment with hash: %s and "+ "amount: %d", hash, sendAmt) @@ -709,10 +703,9 @@ func checkSendToRoute(ctx context.Context, service Service, paymentHash []byte, // not every single satoshi of an account can be used up. But it // prevents an account from going into a negative balance if we only // check for the amount to send but then later debit the full amount. - fee := lnwire.NewMSatFromSatoshis(btcutil.Amount(route.TotalFees)) // nolint - if lnwire.MilliSatoshi(route.TotalFeesMsat) > fee { - fee = lnwire.MilliSatoshi(route.TotalFeesMsat) - } + fee := max( + // nolint + lnwire.MilliSatoshi(route.TotalFeesMsat), lnwire.NewMSatFromSatoshis(btcutil.Amount(route.TotalFees))) sendAmt += fee err = service.CheckBalance(ctx, acct.ID, sendAmt) diff --git a/accounts/checkers_test.go b/accounts/checkers_test.go index 0e6ef37ab..f4ad75712 100644 --- a/accounts/checkers_test.go +++ b/accounts/checkers_test.go @@ -450,7 +450,6 @@ func TestAccountCheckers(t *testing.T) { }} for _, tc := range testCases { - tc := tc t.Run(tc.name, func(tt *testing.T) { tt.Parallel() diff --git a/accounts/interceptor.go b/accounts/interceptor.go index 5ca331b24..39f635c39 100644 --- a/accounts/interceptor.go +++ b/accounts/interceptor.go @@ -30,9 +30,9 @@ var ( // caveatPrefix is the prefix that is used for custom caveats that are // used by the account system. This prefix is used to identify the // custom caveat and extract the condition (the AccountID) from it. - caveatPrefix = []byte(fmt.Sprintf( + caveatPrefix = fmt.Appendf(nil, "%s %s ", macaroons.CondLndCustom, CondAccount, - )) + ) ) // Name returns the name of the interceptor. diff --git a/accounts/rpcserver.go b/accounts/rpcserver.go index ce1291c17..1c6f705b1 100644 --- a/accounts/rpcserver.go +++ b/accounts/rpcserver.go @@ -223,7 +223,6 @@ func (s *RPCServer) ListAccounts(ctx context.Context, // Map the response into the proper response type and return it. rpcAccounts := make([]*litrpc.Account, len(accts)) for i, acct := range accts { - acct := acct rpcAccounts[i] = marshalAccount(acct) } diff --git a/accounts/service.go b/accounts/service.go index 102b9ea84..b812c9f73 100644 --- a/accounts/service.go +++ b/accounts/service.go @@ -116,16 +116,13 @@ func (s *InterceptorService) Start(ctx context.Context, "accounts: %w", err) } for _, acct := range existingAccounts { - acct := acct for invoice := range acct.Invoices { - invoice := invoice s.invoiceToAccount[invoice] = acct.ID } // Let's also resume tracking payments that have a last recorded // state of being in-flight. for hash, entry := range acct.Payments { - entry := entry if !successState(entry.Status) { err := s.TrackPayment( ctx, acct.ID, hash, entry.FullAmount, diff --git a/firewalldb/privacy_mapper.go b/firewalldb/privacy_mapper.go index cde91bfe3..0fd8764ef 100644 --- a/firewalldb/privacy_mapper.go +++ b/firewalldb/privacy_mapper.go @@ -7,6 +7,7 @@ import ( "encoding/hex" "errors" "fmt" + "maps" "math/big" "strconv" "strings" @@ -340,9 +341,7 @@ func (p *PrivacyMapPairs) Add(pairs map[string]string) error { } // In our second pass, we can add the new pairs to our set. - for realStr, pseudoStr := range pairs { - p.pairs[realStr] = pseudoStr - } + maps.Copy(p.pairs, pairs) return nil } diff --git a/rules/channel_restrictions_test.go b/rules/channel_restrictions_test.go index 6102851c4..caa145804 100644 --- a/rules/channel_restrictions_test.go +++ b/rules/channel_restrictions_test.go @@ -227,7 +227,6 @@ func TestChannelRestrictRealToPseudo(t *testing.T) { } for _, test := range tests { - test := test t.Run(test.name, func(t *testing.T) { t.Parallel() diff --git a/rules/peer_restrictions_test.go b/rules/peer_restrictions_test.go index abfa30540..a9cef0938 100644 --- a/rules/peer_restrictions_test.go +++ b/rules/peer_restrictions_test.go @@ -259,7 +259,6 @@ func TestPeerRestrictRealToPseudo(t *testing.T) { } for _, test := range tests { - test := test t.Run(test.name, func(t *testing.T) { t.Parallel() diff --git a/session/tlv_test.go b/session/tlv_test.go index 9258aa7be..e27cbf095 100644 --- a/session/tlv_test.go +++ b/session/tlv_test.go @@ -122,7 +122,6 @@ func TestSerializeDeserializeSession(t *testing.T) { } for _, test := range tests { - test := test t.Run(test.name, func(t *testing.T) { t.Parallel()