-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[Part 3|*] Implement Second Part for SQL Backend functions #10291
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
base: elle-payment-sql-series-new
Are you sure you want to change the base?
Changes from 1 commit
18f03ec
c908b52
52d466f
353160c
fc4ce67
0849296
ed5622c
215490c
3a39bdc
1aae195
f6f2ee3
53b47ea
b3f3627
6145682
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -61,7 +61,7 @@ type SQLQueries interface { | |
| Payment DB write operations. | ||
| */ | ||
| InsertPaymentIntent(ctx context.Context, arg sqlc.InsertPaymentIntentParams) (int64, error) | ||
| InsertPayment(ctx context.Context, arg sqlc.InsertPaymentParams) error | ||
| InsertPayment(ctx context.Context, arg sqlc.InsertPaymentParams) (int64, error) | ||
| InsertPaymentFirstHopCustomRecord(ctx context.Context, arg sqlc.InsertPaymentFirstHopCustomRecordParams) error | ||
|
|
||
| InsertHtlcAttempt(ctx context.Context, arg sqlc.InsertHtlcAttemptParams) (int64, error) | ||
|
|
@@ -629,3 +629,108 @@ func (s *SQLStore) DeleteFailedAttempts(paymentHash lntypes.Hash) error { | |
|
|
||
| return nil | ||
| } | ||
|
|
||
| // InitPayment initializes a payment. | ||
| // | ||
| // This is part of the DB interface. | ||
| func (s *SQLStore) InitPayment(paymentHash lntypes.Hash, | ||
| paymentCreationInfo *PaymentCreationInfo) error { | ||
|
|
||
| ctx := context.TODO() | ||
|
|
||
| // Create the payment in the database. | ||
| err := s.db.ExecTx(ctx, sqldb.WriteTxOpt(), func(db SQLQueries) error { | ||
| existingPayment, err := db.FetchPayment(ctx, paymentHash[:]) | ||
| if err == nil { | ||
| completePayment, err := s.fetchPaymentWithCompleteData( | ||
| ctx, db, existingPayment, | ||
| ) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to fetch payment "+ | ||
| "with complete data: %w", err) | ||
| } | ||
|
|
||
| // Check if the payment is initializable otherwise | ||
| // we'll return early. | ||
| err = completePayment.Status.initializable() | ||
| if err != nil { | ||
| return err | ||
| } | ||
| } else if !errors.Is(err, sql.ErrNoRows) { | ||
| // Some other error occurred | ||
| return fmt.Errorf("failed to check existing "+ | ||
| "payment: %w", err) | ||
| } | ||
|
|
||
| // If payment exists and is failed, delete it first. | ||
| if existingPayment.Payment.ID != 0 { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i find this part quite hard to read. I suggest using a switch with 3 different cases. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this will only be !=0 in the case where There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. so like: |
||
| err := db.DeletePayment(ctx, existingPayment.Payment.ID) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to delete "+ | ||
| "payment: %w", err) | ||
| } | ||
| } | ||
|
|
||
| var intentID *int64 | ||
| if len(paymentCreationInfo.PaymentRequest) > 0 { | ||
| intentIDValue, err := db.InsertPaymentIntent(ctx, | ||
| sqlc.InsertPaymentIntentParams{ | ||
| IntentType: int16( | ||
| PaymentIntentTypeBolt11, | ||
| ), | ||
| IntentPayload: paymentCreationInfo. | ||
| PaymentRequest, | ||
| }) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to initialize "+ | ||
| "payment intent: %w", err) | ||
| } | ||
| intentID = &intentIDValue | ||
| } | ||
|
|
||
| // Only set the intent ID if it's not nil. | ||
| var intentIDParam sql.NullInt64 | ||
| if intentID != nil { | ||
| intentIDParam = sqldb.SQLInt64(*intentID) | ||
| } | ||
|
Comment on lines
+693
to
+697
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. i think rather then dont use the sqldb.SQLInt64 helper |
||
|
|
||
| paymentID, err := db.InsertPayment(ctx, | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ctx on next line |
||
| sqlc.InsertPaymentParams{ | ||
| IntentID: intentIDParam, | ||
| AmountMsat: int64( | ||
| paymentCreationInfo.Value, | ||
| ), | ||
| CreatedAt: paymentCreationInfo. | ||
| CreationTime.UTC(), | ||
| PaymentIdentifier: paymentHash[:], | ||
| }) | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. incorrect wrapping |
||
| if err != nil { | ||
| return fmt.Errorf("failed to insert payment: %w", err) | ||
| } | ||
|
|
||
| firstHopCustomRecords := paymentCreationInfo. | ||
| FirstHopCustomRecords | ||
|
|
||
| for key, value := range firstHopCustomRecords { | ||
| err = db.InsertPaymentFirstHopCustomRecord(ctx, | ||
| sqlc.InsertPaymentFirstHopCustomRecordParams{ | ||
| PaymentID: paymentID, | ||
| Key: int64(key), | ||
| Value: value, | ||
| }) | ||
| if err != nil { | ||
| return fmt.Errorf("failed to insert "+ | ||
| "payment first hop custom "+ | ||
| "record: %w", err) | ||
| } | ||
| } | ||
|
|
||
| return nil | ||
| }, func() { | ||
| }) | ||
|
Comment on lines
+731
to
+732
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. sqldb.NoOpReset |
||
| if err != nil { | ||
| return fmt.Errorf("failed to initialize payment: %w", err) | ||
| } | ||
|
|
||
| return nil | ||
| } | ||
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
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.
again i dont think you need to fetch the whole thing & all its normalised data