Skip to content
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

refactor(backend): handle grant lookup more gracefully in remote incoming payment service #2888

Merged
merged 22 commits into from
Aug 23, 2024

Conversation

mkurapov
Copy link
Contributor

@mkurapov mkurapov commented Aug 22, 2024

Changes proposed in this pull request

A new grantService.getOrCreate function was created in #2887, that, besides storing grant information, does all of the actual Open Payments grant requests with the OP client (as well as doing token rotation for those grant tokens as necessary).

Previously, the logic for requesting grants and doing token rotation was repeated in the remoteIncomingPaymentService as well as the receiverService. Both services were doing the same thing: using the OP client to do grant requests and do token rotation. Now, since this logic is consolidated in grantService.getOrCreate, we are able to clean up those respective services:

  • remoteIncomingPaymentService calls grantService.getOrCreate in both of its create and get methods, which allows us to clean it up considerably
  • receiverService just has to call remoteIncomingPaymentService.get/create to get & create remote incoming payments (which under the hood handles the grant management), which allows us to to clean up its logic

Additionally, in the remoteIncomingPaymentService, there is additional logic that is a bit more graceful in its incoming payment requests: if we fail to get or create an incoming payment with an existing grant (by checking for 401 or 403 errors), we delete the old grant, and try a request once more.

Finally, we remove old external grantService methods which are no longer used after these changes.

Context

Fixes #2852

Checklist

  • Related issues linked using fixes #number
  • Tests added/updated
  • Documentation added
  • Make sure that all checks pass
  • Bruno collection updated

@github-actions github-actions bot added pkg: backend Changes in the backend package. type: source Changes business logic labels Aug 22, 2024
@github-actions github-actions bot added the type: tests Testing related label Aug 23, 2024
@mkurapov mkurapov force-pushed the 2852/mk/get-or-create-ip-grants branch from 5a69214 to 4bff426 Compare August 23, 2024 13:32
@mkurapov mkurapov force-pushed the 2852/mk/improve-ip-grant-lookups branch from 2e014e5 to 1b85b85 Compare August 23, 2024 14:29
Comment on lines +101 to +102
const resourceServerUrl =
walletAddress.resourceServer ?? new URL(walletAddress.id).origin
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We don't really need new URL(walletAddress.id).origin at this point, but being defensive

return createIncomingPayment(deps, {
createArgs,
walletAddress,
retryOnTokenError: false
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we could make it such that we retry multiple times, but for our use case, we just want to retry with a new grant once

@mkurapov mkurapov marked this pull request as ready for review August 23, 2024 15:08
Base automatically changed from 2852/mk/get-or-create-ip-grants to main August 23, 2024 15:09
# Conflicts:
#	packages/backend/src/open_payments/grant/service.test.ts
#	packages/backend/src/open_payments/grant/service.ts
@mkurapov mkurapov requested a review from BlairCurrey August 23, 2024 15:10
Copy link

netlify bot commented Aug 23, 2024

Deploy Preview for brilliant-pasca-3e80ec canceled.

Name Link
🔨 Latest commit bbf09a5
🔍 Latest deploy log https://app.netlify.com/sites/brilliant-pasca-3e80ec/deploys/66c8b595b2d28a0008842c14

@mkurapov mkurapov requested review from njlie and golobitch August 23, 2024 15:11
@mkurapov mkurapov merged commit 750f414 into main Aug 23, 2024
42 checks passed
@mkurapov mkurapov deleted the 2852/mk/improve-ip-grant-lookups branch August 23, 2024 16:41
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pkg: backend Changes in the backend package. type: source Changes business logic type: tests Testing related
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Try to re-request a grant if token rotation fails during remote incoming payment creation
2 participants