htlcswitch: rename paymentID to attemptID for clarity

This commit is contained in:
yyforyongyu authored and yyforyongyu committed 2024-08-07 22:17:58 +08:00
1 parent 941716d60e
commit 21112cfdf8
2 files changed
+36 -37

No files matched your search

+28 -29
View File
@@ -90,32 +90,32 @@ type networkResultStore struct {
results map[uint64][]chan *networkResult results map[uint64][]chan *networkResult
resultsMtx sync.Mutex resultsMtx sync.Mutex
// paymentIDMtx is a multimutex used to make sure the database and // attemptIDMtx is a multimutex used to make sure the database and
// result subscribers map is consistent for each payment ID in case of // result subscribers map is consistent for each attempt ID in case of
// concurrent callers. // concurrent callers.
paymentIDMtx *multimutex.Mutex[uint64] attemptIDMtx *multimutex.Mutex[uint64]
} }
func newNetworkResultStore(db kvdb.Backend) *networkResultStore { func newNetworkResultStore(db kvdb.Backend) *networkResultStore {
return &networkResultStore{ return &networkResultStore{
backend: db, backend: db,
results: make(map[uint64][]chan *networkResult), results: make(map[uint64][]chan *networkResult),
paymentIDMtx: multimutex.NewMutex[uint64](), attemptIDMtx: multimutex.NewMutex[uint64](),
} }
} }
// storeResult stores the networkResult for the given paymentID, and // storeResult stores the networkResult for the given attemptID, and notifies
// notifies any subscribers. // any subscribers.
func (store *networkResultStore) storeResult(paymentID uint64, func (store *networkResultStore) storeResult(attemptID uint64,
result *networkResult) error { 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 // consistency between the database state and the subscribers in case
// of concurrent calls. // of concurrent calls.
store.paymentIDMtx.Lock(paymentID) store.attemptIDMtx.Lock(attemptID)
defer store.paymentIDMtx.Unlock(paymentID) 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. // Serialize the payment result.
var b bytes.Buffer var b bytes.Buffer
@@ -123,8 +123,8 @@ func (store *networkResultStore) storeResult(paymentID uint64,
return err return err
} }
var paymentIDBytes [8]byte var attemptIDBytes [8]byte
binary.BigEndian.PutUint64(paymentIDBytes[:], paymentID) binary.BigEndian.PutUint64(attemptIDBytes[:], attemptID)
err := kvdb.Batch(store.backend, func(tx kvdb.RwTx) error { err := kvdb.Batch(store.backend, func(tx kvdb.RwTx) error {
networkResults, err := tx.CreateTopLevelBucket( networkResults, err := tx.CreateTopLevelBucket(
@@ -134,7 +134,7 @@ func (store *networkResultStore) storeResult(paymentID uint64,
return err return err
} }
return networkResults.Put(paymentIDBytes[:], b.Bytes()) return networkResults.Put(attemptIDBytes[:], b.Bytes())
}) })
if err != nil { if err != nil {
return err 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 // Now that the result is stored in the database, we can notify any
// active subscribers. // active subscribers.
store.resultsMtx.Lock() store.resultsMtx.Lock()
for _, res := range store.results[paymentID] { for _, res := range store.results[attemptID] {
res <- result res <- result
} }
delete(store.results, paymentID) delete(store.results, attemptID)
store.resultsMtx.Unlock() store.resultsMtx.Unlock()
return nil return nil
} }
// subscribeResult is used to get the payment result for the given // subscribeResult is used to get the HTLC attempt result for the given attempt
// payment ID. It returns a channel on which the result will be delivered when // ID. It returns a channel on which the result will be delivered when ready.
// ready. func (store *networkResultStore) subscribeResult(attemptID uint64) (
func (store *networkResultStore) subscribeResult(paymentID uint64) (
<-chan *networkResult, error) { <-chan *networkResult, error) {
// We get a mutex for this payment ID. This is needed to ensure // We get a mutex for this payment ID. This is needed to ensure
// consistency between the database state and the subscribers in case // consistency between the database state and the subscribers in case
// of concurrent calls. // of concurrent calls.
store.paymentIDMtx.Lock(paymentID) store.attemptIDMtx.Lock(attemptID)
defer store.paymentIDMtx.Unlock(paymentID) defer store.attemptIDMtx.Unlock(attemptID)
log.Debugf("Subscribing to result for paymentID=%v", paymentID) log.Debugf("Subscribing to result for attemptID=%v", attemptID)
var ( var (
result *networkResult result *networkResult
@@ -173,7 +172,7 @@ func (store *networkResultStore) subscribeResult(paymentID uint64) (
err := kvdb.View(store.backend, func(tx kvdb.RTx) error { err := kvdb.View(store.backend, func(tx kvdb.RTx) error {
var err error var err error
result, err = fetchResult(tx, paymentID) result, err = fetchResult(tx, attemptID)
switch { switch {
// Result not yet available, we will notify once a result is // 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 // Otherwise we store the result channel for when the result is
// available. // available.
store.resultsMtx.Lock() store.resultsMtx.Lock()
store.results[paymentID] = append( store.results[attemptID] = append(
store.results[paymentID], resultChan, store.results[attemptID], resultChan,
) )
store.resultsMtx.Unlock() store.resultsMtx.Unlock()
@@ -234,8 +233,8 @@ func (store *networkResultStore) getResult(pid uint64) (
} }
func fetchResult(tx kvdb.RTx, pid uint64) (*networkResult, error) { func fetchResult(tx kvdb.RTx, pid uint64) (*networkResult, error) {
var paymentIDBytes [8]byte var attemptIDBytes [8]byte
binary.BigEndian.PutUint64(paymentIDBytes[:], pid) binary.BigEndian.PutUint64(attemptIDBytes[:], pid)
networkResults := tx.ReadBucket(networkResultStoreBucketKey) networkResults := tx.ReadBucket(networkResultStoreBucketKey)
if networkResults == nil { if networkResults == nil {
@@ -243,7 +242,7 @@ func fetchResult(tx kvdb.RTx, pid uint64) (*networkResult, error) {
} }
// Check whether a result is already available. // Check whether a result is already available.
resultBytes := networkResults.Get(paymentIDBytes[:]) resultBytes := networkResults.Get(attemptIDBytes[:])
if resultBytes == nil { if resultBytes == nil {
return nil, ErrPaymentIDNotFound return nil, ErrPaymentIDNotFound
} }
+8 -8
View File
@@ -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 // attemptID. The paymentHash should be set to the payment's overall hash, or
// in case of AMP payments the payment's unique identifier. // 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 // 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. // 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 // 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 // If the HTLC is not found in the circuit map, check whether a result
// result is already available. // is already available.
// Assumption: no one will add this payment ID other than the caller. // Assumption: no one will add this attempt ID other than the caller.
if s.circuits.LookupCircuit(inKey) == nil { if s.circuits.LookupCircuit(inKey) == nil {
res, err := s.networkResults.getResult(attemptID) res, err := s.networkResults.getResult(attemptID)
if err != nil { if err != nil {
@@ -464,7 +464,7 @@ func (s *Switch) GetAttemptResult(attemptID uint64, paymentHash lntypes.Hash,
c <- res c <- res
nChan = c nChan = c
} else { } else {
// The payment was committed to the circuits, subscribe for a // The HTLC was committed to the circuits, subscribe for a
// result. // result.
nChan, err = s.networkResults.subscribeResult(attemptID) nChan, err = s.networkResults.subscribeResult(attemptID)
if err != nil { if err != nil {
@@ -474,7 +474,7 @@ func (s *Switch) GetAttemptResult(attemptID uint64, paymentHash lntypes.Hash,
resultChan := make(chan *PaymentResult, 1) 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 // extract the result when it is available, and pass it on to the
// caller. // caller.
s.wg.Add(1) 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 // Store the result to the db. This will also notify subscribers about
// the result. // the result.
if err := s.networkResults.storeResult(attemptID, n); err != nil { 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) attemptID, err)
return return
} }