From 21112cfdf8861242c1b67803699f8bb9e5a9a1e8 Mon Sep 17 00:00:00 2001 From: yyforyongyu Date: Mon, 6 Nov 2023 06:34:08 +0800 Subject: [PATCH] htlcswitch: rename `paymentID` to `attemptID` for clarity --- htlcswitch/payment_result.go | 57 ++++++++++++++++++------------------ htlcswitch/switch.go | 16 +++++----- 2 files changed, 36 insertions(+), 37 deletions(-) diff --git a/htlcswitch/payment_result.go b/htlcswitch/payment_result.go index cd982b8bb..db959e2d1 100644 --- a/htlcswitch/payment_result.go +++ b/htlcswitch/payment_result.go @@ -90,32 +90,32 @@ type networkResultStore struct { results map[uint64][]chan *networkResult resultsMtx sync.Mutex - // paymentIDMtx is a multimutex used to make sure the database and - // result subscribers map is consistent for each payment ID in case of + // attemptIDMtx is a multimutex used to make sure the database and + // result subscribers map is consistent for each attempt ID in case of // concurrent callers. - paymentIDMtx *multimutex.Mutex[uint64] + attemptIDMtx *multimutex.Mutex[uint64] } func newNetworkResultStore(db kvdb.Backend) *networkResultStore { return &networkResultStore{ backend: db, results: make(map[uint64][]chan *networkResult), - paymentIDMtx: multimutex.NewMutex[uint64](), + attemptIDMtx: multimutex.NewMutex[uint64](), } } -// storeResult stores the networkResult for the given paymentID, and -// notifies any subscribers. -func (store *networkResultStore) storeResult(paymentID uint64, +// storeResult stores the networkResult for the given attemptID, and notifies +// any subscribers. +func (store *networkResultStore) storeResult(attemptID uint64, result *networkResult) error { - // We get a mutex for this payment ID. This is needed to ensure + // We get a mutex for this attempt ID. This is needed to ensure // consistency between the database state and the subscribers in case // of concurrent calls. - store.paymentIDMtx.Lock(paymentID) - defer store.paymentIDMtx.Unlock(paymentID) + store.attemptIDMtx.Lock(attemptID) + defer store.attemptIDMtx.Unlock(attemptID) - log.Debugf("Storing result for paymentID=%v", paymentID) + log.Debugf("Storing result for attemptID=%v", attemptID) // Serialize the payment result. var b bytes.Buffer @@ -123,8 +123,8 @@ func (store *networkResultStore) storeResult(paymentID uint64, return err } - var paymentIDBytes [8]byte - binary.BigEndian.PutUint64(paymentIDBytes[:], paymentID) + var attemptIDBytes [8]byte + binary.BigEndian.PutUint64(attemptIDBytes[:], attemptID) err := kvdb.Batch(store.backend, func(tx kvdb.RwTx) error { networkResults, err := tx.CreateTopLevelBucket( @@ -134,7 +134,7 @@ func (store *networkResultStore) storeResult(paymentID uint64, return err } - return networkResults.Put(paymentIDBytes[:], b.Bytes()) + return networkResults.Put(attemptIDBytes[:], b.Bytes()) }) if err != nil { return err @@ -143,28 +143,27 @@ func (store *networkResultStore) storeResult(paymentID uint64, // Now that the result is stored in the database, we can notify any // active subscribers. store.resultsMtx.Lock() - for _, res := range store.results[paymentID] { + for _, res := range store.results[attemptID] { res <- result } - delete(store.results, paymentID) + delete(store.results, attemptID) store.resultsMtx.Unlock() return nil } -// subscribeResult is used to get the payment result for the given -// payment ID. It returns a channel on which the result will be delivered when -// ready. -func (store *networkResultStore) subscribeResult(paymentID uint64) ( +// subscribeResult is used to get the HTLC attempt result for the given attempt +// ID. It returns a channel on which the result will be delivered when ready. +func (store *networkResultStore) subscribeResult(attemptID uint64) ( <-chan *networkResult, error) { // We get a mutex for this payment ID. This is needed to ensure // consistency between the database state and the subscribers in case // of concurrent calls. - store.paymentIDMtx.Lock(paymentID) - defer store.paymentIDMtx.Unlock(paymentID) + store.attemptIDMtx.Lock(attemptID) + defer store.attemptIDMtx.Unlock(attemptID) - log.Debugf("Subscribing to result for paymentID=%v", paymentID) + log.Debugf("Subscribing to result for attemptID=%v", attemptID) var ( result *networkResult @@ -173,7 +172,7 @@ func (store *networkResultStore) subscribeResult(paymentID uint64) ( err := kvdb.View(store.backend, func(tx kvdb.RTx) error { var err error - result, err = fetchResult(tx, paymentID) + result, err = fetchResult(tx, attemptID) switch { // Result not yet available, we will notify once a result is @@ -205,8 +204,8 @@ func (store *networkResultStore) subscribeResult(paymentID uint64) ( // Otherwise we store the result channel for when the result is // available. store.resultsMtx.Lock() - store.results[paymentID] = append( - store.results[paymentID], resultChan, + store.results[attemptID] = append( + store.results[attemptID], resultChan, ) store.resultsMtx.Unlock() @@ -234,8 +233,8 @@ func (store *networkResultStore) getResult(pid uint64) ( } func fetchResult(tx kvdb.RTx, pid uint64) (*networkResult, error) { - var paymentIDBytes [8]byte - binary.BigEndian.PutUint64(paymentIDBytes[:], pid) + var attemptIDBytes [8]byte + binary.BigEndian.PutUint64(attemptIDBytes[:], pid) networkResults := tx.ReadBucket(networkResultStoreBucketKey) if networkResults == nil { @@ -243,7 +242,7 @@ func fetchResult(tx kvdb.RTx, pid uint64) (*networkResult, error) { } // Check whether a result is already available. - resultBytes := networkResults.Get(paymentIDBytes[:]) + resultBytes := networkResults.Get(attemptIDBytes[:]) if resultBytes == nil { return nil, ErrPaymentIDNotFound } diff --git a/htlcswitch/switch.go b/htlcswitch/switch.go index 793da57db..d4e9518c8 100644 --- a/htlcswitch/switch.go +++ b/htlcswitch/switch.go @@ -431,11 +431,11 @@ func (s *Switch) ProcessContractResolution(msg contractcourt.ResolutionMsg) erro } } -// GetAttemptResult returns the result of the payment attempt with the given +// GetAttemptResult returns the result of the HTLC attempt with the given // attemptID. The paymentHash should be set to the payment's overall hash, or // in case of AMP payments the payment's unique identifier. // -// The method returns a channel where the payment result will be sent when +// The method returns a channel where the HTLC attempt result will be sent when // available, or an error is encountered during forwarding. When a result is // received on the channel, the HTLC is guaranteed to no longer be in flight. // The switch shutting down is signaled by closing the channel. If the @@ -452,9 +452,9 @@ func (s *Switch) GetAttemptResult(attemptID uint64, paymentHash lntypes.Hash, } ) - // If the payment is not found in the circuit map, check whether a - // result is already available. - // Assumption: no one will add this payment ID other than the caller. + // If the HTLC is not found in the circuit map, check whether a result + // is already available. + // Assumption: no one will add this attempt ID other than the caller. if s.circuits.LookupCircuit(inKey) == nil { res, err := s.networkResults.getResult(attemptID) if err != nil { @@ -464,7 +464,7 @@ func (s *Switch) GetAttemptResult(attemptID uint64, paymentHash lntypes.Hash, c <- res nChan = c } else { - // The payment was committed to the circuits, subscribe for a + // The HTLC was committed to the circuits, subscribe for a // result. nChan, err = s.networkResults.subscribeResult(attemptID) if err != nil { @@ -474,7 +474,7 @@ func (s *Switch) GetAttemptResult(attemptID uint64, paymentHash lntypes.Hash, resultChan := make(chan *PaymentResult, 1) - // Since the payment was known, we can start a goroutine that can + // Since the attempt was known, we can start a goroutine that can // extract the result when it is available, and pass it on to the // caller. s.wg.Add(1) @@ -939,7 +939,7 @@ func (s *Switch) handleLocalResponse(pkt *htlcPacket) { // Store the result to the db. This will also notify subscribers about // the result. if err := s.networkResults.storeResult(attemptID, n); err != nil { - log.Errorf("Unable to complete payment for pid=%v: %v", + log.Errorf("Unable to store attempt result for pid=%v: %v", attemptID, err) return }