-
Notifications
You must be signed in to change notification settings - Fork 2.3k
routing+channeldb: mpp bucket structure #4000
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Roasbeef
merged 16 commits into
lightningnetwork:master
from
joostjager:payment-raw-failure
Mar 10, 2020
Merged
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
967b4b2
channeldb: remove redundant MPPaymentCreationInfo struct
joostjager bee2380
channeldb: rename PaymentAttemptInfo to HTLCAttemptInfo
halseth c29b741
channeldb/test: refactor payment control test
joostjager cc5e18c
channeldb: isolate duplicate payments
joostjager 6aab6c0
routerrpc+lnrpc: move htlc failure messages
joostjager 3f5ba35
routerrpc: move marshall functions out of conditionally compiled file
joostjager e6e9e44
routerrpc: extract wire error marshalling
joostjager fa3a762
channeldb: add error return value to fetchPaymentStatus
joostjager 4c74c08
channeldb/test: extend payment control test with failed attempt
joostjager c357511
channeldb/migtest: remove channeldb dependency
joostjager 8558534
routing: add clock to router config
joostjager 48c0e42
channeldb+routing: store all payment htlcs
joostjager f86e68a
channeldb+routing: store full htlc failure reason
joostjager 4cea2d5
channeldb/migtest: add migration test tools
joostjager 866623e
channeldb/migration13: migrate to mpp structure
joostjager c0cb05d
lnrpc: expose raw htlc failure
joostjager File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,246 @@ | ||
| package channeldb | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "encoding/binary" | ||
| "fmt" | ||
| "io" | ||
| "time" | ||
|
|
||
| "github.com/btcsuite/btcd/btcec" | ||
| "github.com/coreos/bbolt" | ||
| "github.com/lightningnetwork/lnd/lntypes" | ||
| "github.com/lightningnetwork/lnd/lnwire" | ||
| "github.com/lightningnetwork/lnd/routing/route" | ||
| ) | ||
|
|
||
| var ( | ||
| // duplicatePaymentsBucket is the name of a optional sub-bucket within | ||
| // the payment hash bucket, that is used to hold duplicate payments to a | ||
| // payment hash. This is needed to support information from earlier | ||
| // versions of lnd, where it was possible to pay to a payment hash more | ||
| // than once. | ||
| duplicatePaymentsBucket = []byte("payment-duplicate-bucket") | ||
|
|
||
| // duplicatePaymentSettleInfoKey is a key used in the payment's | ||
| // sub-bucket to store the settle info of the payment. | ||
| duplicatePaymentSettleInfoKey = []byte("payment-settle-info") | ||
|
|
||
| // duplicatePaymentAttemptInfoKey is a key used in the payment's | ||
| // sub-bucket to store the info about the latest attempt that was done | ||
| // for the payment in question. | ||
| duplicatePaymentAttemptInfoKey = []byte("payment-attempt-info") | ||
|
|
||
| // duplicatePaymentCreationInfoKey is a key used in the payment's | ||
| // sub-bucket to store the creation info of the payment. | ||
| duplicatePaymentCreationInfoKey = []byte("payment-creation-info") | ||
|
|
||
| // duplicatePaymentFailInfoKey is a key used in the payment's sub-bucket | ||
| // to store information about the reason a payment failed. | ||
| duplicatePaymentFailInfoKey = []byte("payment-fail-info") | ||
|
|
||
| // duplicatePaymentSequenceKey is a key used in the payment's sub-bucket | ||
| // to store the sequence number of the payment. | ||
| duplicatePaymentSequenceKey = []byte("payment-sequence-key") | ||
| ) | ||
|
|
||
| // duplicateHTLCAttemptInfo contains static information about a specific HTLC | ||
| // attempt for a payment. This information is used by the router to handle any | ||
| // errors coming back after an attempt is made, and to query the switch about | ||
| // the status of the attempt. | ||
| type duplicateHTLCAttemptInfo struct { | ||
| // attemptID is the unique ID used for this attempt. | ||
| attemptID uint64 | ||
|
|
||
| // sessionKey is the ephemeral key used for this attempt. | ||
| sessionKey *btcec.PrivateKey | ||
|
|
||
| // route is the route attempted to send the HTLC. | ||
| route route.Route | ||
| } | ||
|
|
||
| // fetchDuplicatePaymentStatus fetches the payment status of the payment. If the | ||
| // payment isn't found, it will default to "StatusUnknown". | ||
| func fetchDuplicatePaymentStatus(bucket *bbolt.Bucket) PaymentStatus { | ||
| if bucket.Get(duplicatePaymentSettleInfoKey) != nil { | ||
| return StatusSucceeded | ||
| } | ||
|
|
||
| if bucket.Get(duplicatePaymentFailInfoKey) != nil { | ||
| return StatusFailed | ||
| } | ||
|
|
||
| if bucket.Get(duplicatePaymentCreationInfoKey) != nil { | ||
| return StatusInFlight | ||
| } | ||
|
|
||
| return StatusUnknown | ||
| } | ||
|
|
||
| func deserializeDuplicateHTLCAttemptInfo(r io.Reader) ( | ||
| *duplicateHTLCAttemptInfo, error) { | ||
|
|
||
| a := &duplicateHTLCAttemptInfo{} | ||
| err := ReadElements(r, &a.attemptID, &a.sessionKey) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| a.route, err = DeserializeRoute(r) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| return a, nil | ||
| } | ||
|
|
||
| func deserializeDuplicatePaymentCreationInfo(r io.Reader) ( | ||
| *PaymentCreationInfo, error) { | ||
|
|
||
| var scratch [8]byte | ||
|
|
||
| c := &PaymentCreationInfo{} | ||
|
|
||
| if _, err := io.ReadFull(r, c.PaymentHash[:]); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| if _, err := io.ReadFull(r, scratch[:]); err != nil { | ||
| return nil, err | ||
| } | ||
| c.Value = lnwire.MilliSatoshi(byteOrder.Uint64(scratch[:])) | ||
|
|
||
| if _, err := io.ReadFull(r, scratch[:]); err != nil { | ||
| return nil, err | ||
| } | ||
| c.CreationTime = time.Unix(int64(byteOrder.Uint64(scratch[:])), 0) | ||
|
|
||
| if _, err := io.ReadFull(r, scratch[:4]); err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| reqLen := byteOrder.Uint32(scratch[:4]) | ||
| payReq := make([]byte, reqLen) | ||
| if reqLen > 0 { | ||
| if _, err := io.ReadFull(r, payReq); err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
| c.PaymentRequest = payReq | ||
|
|
||
| return c, nil | ||
| } | ||
|
|
||
| func fetchDuplicatePayment(bucket *bbolt.Bucket) (*MPPayment, error) { | ||
|
halseth marked this conversation as resolved.
|
||
| seqBytes := bucket.Get(duplicatePaymentSequenceKey) | ||
| if seqBytes == nil { | ||
| return nil, fmt.Errorf("sequence number not found") | ||
| } | ||
|
|
||
| sequenceNum := binary.BigEndian.Uint64(seqBytes) | ||
|
|
||
| // Get the payment status. | ||
| paymentStatus := fetchDuplicatePaymentStatus(bucket) | ||
|
|
||
| // Get the PaymentCreationInfo. | ||
| b := bucket.Get(duplicatePaymentCreationInfoKey) | ||
| if b == nil { | ||
| return nil, fmt.Errorf("creation info not found") | ||
| } | ||
|
|
||
| r := bytes.NewReader(b) | ||
| creationInfo, err := deserializeDuplicatePaymentCreationInfo(r) | ||
| if err != nil { | ||
| return nil, err | ||
|
|
||
| } | ||
|
|
||
| // Get failure reason if available. | ||
| var failureReason *FailureReason | ||
| b = bucket.Get(duplicatePaymentFailInfoKey) | ||
| if b != nil { | ||
| reason := FailureReason(b[0]) | ||
| failureReason = &reason | ||
| } | ||
|
|
||
| payment := &MPPayment{ | ||
| sequenceNum: sequenceNum, | ||
| Info: creationInfo, | ||
| FailureReason: failureReason, | ||
| Status: paymentStatus, | ||
| } | ||
|
|
||
| // Get the HTLCAttemptInfo. It can be absent. | ||
| b = bucket.Get(duplicatePaymentAttemptInfoKey) | ||
| if b != nil { | ||
| r = bytes.NewReader(b) | ||
| attempt, err := deserializeDuplicateHTLCAttemptInfo(r) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| htlc := HTLCAttempt{ | ||
| HTLCAttemptInfo: HTLCAttemptInfo{ | ||
| AttemptID: attempt.attemptID, | ||
| Route: attempt.route, | ||
| SessionKey: attempt.sessionKey, | ||
| }, | ||
| } | ||
|
|
||
| // Get the payment preimage. This is only found for | ||
| // successful payments. | ||
| b = bucket.Get(duplicatePaymentSettleInfoKey) | ||
| if b != nil { | ||
| var preimg lntypes.Preimage | ||
| copy(preimg[:], b) | ||
|
|
||
| htlc.Settle = &HTLCSettleInfo{ | ||
| Preimage: preimg, | ||
| SettleTime: time.Time{}, | ||
| } | ||
| } else { | ||
| // Otherwise the payment must have failed. | ||
| htlc.Failure = &HTLCFailInfo{ | ||
| FailTime: time.Time{}, | ||
| } | ||
| } | ||
|
|
||
| payment.HTLCs = []HTLCAttempt{htlc} | ||
| } | ||
|
|
||
| return payment, nil | ||
| } | ||
|
|
||
| func fetchDuplicatePayments(paymentHashBucket *bbolt.Bucket) ([]*MPPayment, | ||
| error) { | ||
|
|
||
| var payments []*MPPayment | ||
|
|
||
| // For older versions of lnd, duplicate payments to a payment has was | ||
| // possible. These will be found in a sub-bucket indexed by their | ||
| // sequence number if available. | ||
| dup := paymentHashBucket.Bucket(duplicatePaymentsBucket) | ||
| if dup == nil { | ||
| return nil, nil | ||
| } | ||
|
|
||
| err := dup.ForEach(func(k, v []byte) error { | ||
| subBucket := dup.Bucket(k) | ||
| if subBucket == nil { | ||
| // We one bucket for each duplicate to be found. | ||
| return fmt.Errorf("non bucket element" + | ||
| "in duplicate bucket") | ||
| } | ||
|
|
||
| p, err := fetchDuplicatePayment(subBucket) | ||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| payments = append(payments, p) | ||
| return nil | ||
| }) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return payments, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| package migration13 | ||
|
|
||
| import ( | ||
| "github.com/btcsuite/btclog" | ||
| ) | ||
|
|
||
| // log is a logger that is initialized as disabled. This means the package will | ||
| // not perform any logging by default until a logger is set. | ||
| var log = btclog.Disabled | ||
|
|
||
| // UseLogger uses a specified Logger to output package logging info. | ||
| func UseLogger(logger btclog.Logger) { | ||
| log = logger | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
probably doesn't matter, but note that for duplicate payments the
attemptIDandsessionKeywill always be all zeroes (since these payments were created before these fields were introduced).There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, so there isn't even a way to keep the duplicate payments apart at all?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Payment level sequence numbers will be different ofc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh yes, of course. Ok, I think it can be left as is? Previously we also deserialized and returned those zeroes.