-
Couldn't load subscription status.
- Fork 2.2k
[Part 2|*] Implement First Part for SQL Backend functions #10287
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
Open
ziggie1984
wants to merge
6
commits into
lightningnetwork:elle-payment-sql-series-new
Choose a base branch
from
ziggie1984:introduce-sql-schema-payments-part-2
base: elle-payment-sql-series-new
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
18f03ec
sqldb: add index and comment to payment tables
ziggie1984 c908b52
multi: add relevant queries for QueryPayments implemenation
ziggie1984 52d466f
paymentsdb: add new internal error
ziggie1984 353160c
paymentsdb: implement QueryPayments for sql backend
ziggie1984 fc4ce67
paymentsdb: implement FetchPayment for sql backend
ziggie1984 0849296
docs: add release-notes
ziggie1984 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
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,274 @@ | ||
| package paymentsdb | ||
|
|
||
| import ( | ||
| "bytes" | ||
| "fmt" | ||
| "sort" | ||
| "strconv" | ||
| "time" | ||
|
|
||
| "github.com/btcsuite/btcd/btcec/v2" | ||
| "github.com/lightningnetwork/lnd/lntypes" | ||
| "github.com/lightningnetwork/lnd/lnwire" | ||
| "github.com/lightningnetwork/lnd/record" | ||
| "github.com/lightningnetwork/lnd/routing/route" | ||
| "github.com/lightningnetwork/lnd/sqldb/sqlc" | ||
| "github.com/lightningnetwork/lnd/tlv" | ||
| ) | ||
|
|
||
| // dbPaymentToCreationInfo converts database payment data to | ||
| // PaymentCreationInfo. | ||
| func dbPaymentToCreationInfo(paymentIdentifier []byte, amountMsat int64, | ||
| createdAt time.Time, intentPayload []byte, | ||
| firstHopCustomRecords lnwire.CustomRecords) *PaymentCreationInfo { | ||
|
|
||
| // This is the payment hash for non-AMP payments and the SetID for AMP | ||
| // payments. | ||
| var identifier lntypes.Hash | ||
| copy(identifier[:], paymentIdentifier) | ||
|
|
||
| return &PaymentCreationInfo{ | ||
| PaymentIdentifier: identifier, | ||
| Value: lnwire.MilliSatoshi(amountMsat), | ||
| CreationTime: createdAt.Local(), | ||
| PaymentRequest: intentPayload, | ||
| FirstHopCustomRecords: firstHopCustomRecords, | ||
| } | ||
| } | ||
|
|
||
| // dbAttemptToHTLCAttempt converts a database HTLC attempt to an HTLCAttempt. | ||
| func dbAttemptToHTLCAttempt( | ||
| dbAttempt sqlc.FetchHtlcAttemptsForPaymentRow, | ||
| hops []sqlc.FetchHopsForAttemptsRow, | ||
| hopCustomRecords map[int64][]sqlc.PaymentHopCustomRecord, | ||
| routeCustomRecords []sqlc.PaymentAttemptFirstHopCustomRecord) ( | ||
| *HTLCAttempt, error) { | ||
|
|
||
| // Convert route-level first hop custom records to CustomRecords map. | ||
| var firstHopWireCustomRecords lnwire.CustomRecords | ||
| if len(routeCustomRecords) > 0 { | ||
| firstHopWireCustomRecords = make(lnwire.CustomRecords) | ||
| for _, record := range routeCustomRecords { | ||
| firstHopWireCustomRecords[uint64(record.Key)] = | ||
| record.Value | ||
| } | ||
| } | ||
|
|
||
| // Build the route from the database data. | ||
| route, err := dbDataToRoute( | ||
| hops, hopCustomRecords, dbAttempt.FirstHopAmountMsat, | ||
| dbAttempt.RouteTotalTimeLock, dbAttempt.RouteTotalAmount, | ||
| dbAttempt.RouteSourceKey, firstHopWireCustomRecords, | ||
| ) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to convert to route: %w", | ||
| err) | ||
| } | ||
|
|
||
| hash, err := lntypes.MakeHash(dbAttempt.PaymentHash) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to parse payment "+ | ||
| "hash: %w", err) | ||
| } | ||
|
|
||
| // Create the attempt info. | ||
| var sessionKey [32]byte | ||
| copy(sessionKey[:], dbAttempt.SessionKey) | ||
|
|
||
| info := HTLCAttemptInfo{ | ||
| AttemptID: uint64(dbAttempt.AttemptIndex), | ||
| sessionKey: sessionKey, | ||
| Route: *route, | ||
| AttemptTime: dbAttempt.AttemptTime, | ||
| Hash: &hash, | ||
| } | ||
|
|
||
| attempt := &HTLCAttempt{ | ||
| HTLCAttemptInfo: info, | ||
| } | ||
|
|
||
| // Add settlement info if present. | ||
| if dbAttempt.ResolutionType.Valid && | ||
| HTLCAttemptResolutionType(dbAttempt.ResolutionType.Int32) == | ||
| HTLCAttemptResolutionSettled { | ||
|
|
||
| var preimage lntypes.Preimage | ||
| copy(preimage[:], dbAttempt.SettlePreimage) | ||
|
|
||
| attempt.Settle = &HTLCSettleInfo{ | ||
| Preimage: preimage, | ||
| SettleTime: dbAttempt.ResolutionTime.Time, | ||
| } | ||
| } | ||
|
|
||
| // Add failure info if present. | ||
| if dbAttempt.ResolutionType.Valid && | ||
| HTLCAttemptResolutionType(dbAttempt.ResolutionType.Int32) == | ||
| HTLCAttemptResolutionFailed { | ||
|
|
||
| failure := &HTLCFailInfo{ | ||
| FailTime: dbAttempt.ResolutionTime.Time, | ||
| } | ||
|
|
||
| if dbAttempt.HtlcFailReason.Valid { | ||
| failure.Reason = HTLCFailReason( | ||
| dbAttempt.HtlcFailReason.Int32, | ||
| ) | ||
| } | ||
|
|
||
| if dbAttempt.FailureSourceIndex.Valid { | ||
| failure.FailureSourceIndex = uint32( | ||
| dbAttempt.FailureSourceIndex.Int32, | ||
| ) | ||
| } | ||
|
|
||
| // Decode the failure message if present. | ||
| if len(dbAttempt.FailureMsg) > 0 { | ||
| msg, err := lnwire.DecodeFailureMessage( | ||
| bytes.NewReader(dbAttempt.FailureMsg), 0, | ||
| ) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to decode "+ | ||
| "failure message: %w", err) | ||
| } | ||
| failure.Message = msg | ||
| } | ||
|
|
||
| attempt.Failure = failure | ||
| } | ||
|
|
||
| return attempt, nil | ||
| } | ||
|
|
||
| // dbDataToRoute converts database route data to a route.Route. | ||
| func dbDataToRoute(hops []sqlc.FetchHopsForAttemptsRow, | ||
| hopCustomRecords map[int64][]sqlc.PaymentHopCustomRecord, | ||
| firstHopAmountMsat int64, totalTimeLock int32, totalAmount int64, | ||
| sourceKey []byte, firstHopWireCustomRecords lnwire.CustomRecords) ( | ||
| *route.Route, error) { | ||
|
|
||
| if len(hops) == 0 { | ||
| return nil, fmt.Errorf("no hops provided") | ||
| } | ||
|
|
||
| // Sort hops by hop index. | ||
| sort.Slice(hops, func(i, j int) bool { | ||
| return hops[i].HopIndex < hops[j].HopIndex | ||
| }) | ||
|
|
||
| routeHops := make([]*route.Hop, len(hops)) | ||
|
|
||
| for i, hop := range hops { | ||
| pubKey, err := route.NewVertexFromBytes(hop.PubKey) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to parse pub key: %w", | ||
| err) | ||
| } | ||
|
|
||
| var channelID uint64 | ||
| if hop.Scid != "" { | ||
| // The SCID is stored as a string representation | ||
| // of the uint64. | ||
| var err error | ||
| channelID, err = strconv.ParseUint(hop.Scid, 10, 64) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to parse "+ | ||
| "scid: %w", err) | ||
| } | ||
| } | ||
|
|
||
| routeHop := &route.Hop{ | ||
| PubKeyBytes: pubKey, | ||
| ChannelID: channelID, | ||
| OutgoingTimeLock: uint32(hop.OutgoingTimeLock), | ||
| AmtToForward: lnwire.MilliSatoshi(hop.AmtToForward), | ||
| } | ||
|
|
||
| // Add MPP record if present. | ||
| if len(hop.MppPaymentAddr) > 0 { | ||
| var paymentAddr [32]byte | ||
| copy(paymentAddr[:], hop.MppPaymentAddr) | ||
| routeHop.MPP = record.NewMPP( | ||
| lnwire.MilliSatoshi(hop.MppTotalMsat.Int64), | ||
| paymentAddr, | ||
| ) | ||
| } | ||
|
|
||
| // Add AMP record if present. | ||
| if len(hop.AmpRootShare) > 0 { | ||
| var rootShare [32]byte | ||
| copy(rootShare[:], hop.AmpRootShare) | ||
| var setID [32]byte | ||
| copy(setID[:], hop.AmpSetID) | ||
|
|
||
| routeHop.AMP = record.NewAMP( | ||
| rootShare, setID, | ||
| uint32(hop.AmpChildIndex.Int32), | ||
| ) | ||
| } | ||
|
|
||
| // Add blinding point if present (only for introduction node). | ||
| if len(hop.BlindingPoint) > 0 { | ||
| pubKey, err := btcec.ParsePubKey(hop.BlindingPoint) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("failed to parse "+ | ||
| "blinding point: %w", err) | ||
| } | ||
| routeHop.BlindingPoint = pubKey | ||
| } | ||
|
|
||
| // Add encrypted data if present (for all blinded hops). | ||
| if len(hop.EncryptedData) > 0 { | ||
| routeHop.EncryptedData = hop.EncryptedData | ||
| } | ||
|
|
||
| // Add total amount if present (only for final hop in blinded | ||
| // route). | ||
| if hop.BlindedPathTotalAmt.Valid { | ||
| routeHop.TotalAmtMsat = lnwire.MilliSatoshi( | ||
| hop.BlindedPathTotalAmt.Int64, | ||
| ) | ||
| } | ||
|
|
||
| // Add hop-level custom records. | ||
| if records, ok := hopCustomRecords[hop.ID]; ok { | ||
| routeHop.CustomRecords = make( | ||
| record.CustomSet, | ||
| ) | ||
| for _, rec := range records { | ||
| routeHop.CustomRecords[uint64(rec.Key)] = | ||
| rec.Value | ||
| } | ||
| } | ||
|
|
||
| // Add metadata if present. | ||
| if len(hop.MetaData) > 0 { | ||
| routeHop.Metadata = hop.MetaData | ||
| } | ||
|
|
||
| routeHops[i] = routeHop | ||
| } | ||
|
|
||
| // Parse the source node public key. | ||
| var sourceNode route.Vertex | ||
| copy(sourceNode[:], sourceKey) | ||
|
|
||
| route := &route.Route{ | ||
| TotalTimeLock: uint32(totalTimeLock), | ||
| TotalAmount: lnwire.MilliSatoshi(totalAmount), | ||
| SourcePubKey: sourceNode, | ||
| Hops: routeHops, | ||
| FirstHopWireCustomRecords: firstHopWireCustomRecords, | ||
| } | ||
|
|
||
| // Set the first hop amount if it is set. | ||
| if firstHopAmountMsat != 0 { | ||
| route.FirstHopAmount = tlv.NewRecordT[tlv.TlvType0]( | ||
| tlv.NewBigSizeT(lnwire.MilliSatoshi( | ||
| firstHopAmountMsat, | ||
| )), | ||
| ) | ||
| } | ||
|
|
||
| return route, nil | ||
| } | ||
Oops, something went wrong.
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.
nit: can exit early when
if !dbAttempt.ResolutionType.Valid