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

Fix autowithdrawal logs #1073

Merged
merged 5 commits into from Apr 16, 2024
Merged

Fix autowithdrawal logs #1073

merged 5 commits into from Apr 16, 2024

Conversation

ekzyis
Copy link
Member

@ekzyis ekzyis commented Apr 15, 2024

Description

Fix #1069 Based on #1072

  • only log autowithdrawal success after withdrawal itself via lightning was successful (before it was logged as success immediately after withdrawal creation)
  • also log withdrawal errors during routing (before only logged if withdrawal creation failed)

Screenshots

Additional Context

  • made sure changes are backwards compatible by not dropping autoWithdrawal column even though it's obsolete with new walletId column
  • using ON DELETE SET NULL to not delete withdrawals if wallet was deleted

Checklist

  • Are your changes backwards compatible?
  • Did you QA this? Could we deploy this straight to production?
  • For frontend changes: Tested on mobile?
  • Did you introduce any new environment variables? If so, call them out explicitly in the PR description.

Summary by CodeRabbit

  • New Features
    • Added wallet identification features to the withdrawal process, enhancing transaction security and traceability.
  • Enhancements
    • Streamlined auto withdrawal processing and improved error handling in the system.
  • Database Updates
    • Introduced a new database column and foreign key relationship to better manage withdrawal records linked to specific wallets.
  • Bug Fixes
    • Adjusted withdrawal creation logic to ensure accurate processing and logging.

@ekzyis ekzyis added the bug label Apr 15, 2024
Copy link
Contributor

coderabbitai bot commented Apr 15, 2024

Walkthrough

Walkthrough

The recent updates involve enhancements to the wallet management system, including the introduction of a new walletId parameter in various functions to better track and manage withdrawals. A SQL migration adds a corresponding column to the database, and adjustments in the auto withdrawal process aim to improve log accuracy and error handling.

Changes

File Path Change Summary
api/resolvers/wallet.js Updated createWithdrawal function to include a new walletId parameter.
prisma/migrations/20240415113539_withdrawal_wallet_id/migration.sql Added walletId column and a new PL/pgSQL function in the "Withdrawl" table.
prisma/schema.prisma Updated Wallet and Withdrawl models to include new relational fields.
worker/autowithdraw.js Enhanced auto withdrawal logic and error handling, adjusted function parameters.
worker/wallet.js Improved withdrawal checks and logging through added imports and function modifications.
components/logger.js Refactored renameWallet function for improved readability using a switch statement.

Assessment against linked issues

Objective Addressed Explanation
Add user-exposed logs for attached wallets (#823) It's unclear if the changes directly affect user-exposed logs as described.
Record connection errors/successes for server-side wallets (#823) No changes related to connection error/success logging were noted.
Ensure logs work uniformly for server-side and client-side wallets (#823) The changes do not mention uniformity in log handling across wallet types.
Ensure all errors related to wallet operations are recorded (#823)
Failed autowithdrawals show up in wallet logs correctly (#1069)

Possibly related issues


Recent Review Details

Configuration used: CodeRabbit UI

Commits Files that changed from the base of the PR and between 00ca354 and b715005.
Files selected for processing (6)
  • api/resolvers/wallet.js (11 hunks)
  • components/logger.js (1 hunks)
  • prisma/migrations/20240415113539_withdrawal_wallet_id/migration.sql (1 hunks)
  • prisma/schema.prisma (2 hunks)
  • worker/autowithdraw.js (6 hunks)
  • worker/wallet.js (3 hunks)
Files skipped from review as they are similar to previous changes (6)
  • api/resolvers/wallet.js
  • components/logger.js
  • prisma/migrations/20240415113539_withdrawal_wallet_id/migration.sql
  • prisma/schema.prisma
  • worker/autowithdraw.js
  • worker/wallet.js

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

Share
Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (invoked as PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger a review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 3

@@ -185,5 +169,5 @@ async function autowithdrawCLN ({ amount, maxFee }, { me, models, lnd }) {
expiry: 360
})

return await createWithdrawal(null, { invoice: inv.bolt11, maxFee }, { me, models, lnd, autoWithdraw: true })
return await createWithdrawal(null, { invoice: inv.bolt11, maxFee }, { me, models, lnd, walletId: wallet.id })
Copy link
Contributor

@coderabbitai coderabbitai bot Apr 15, 2024

Choose a reason for hiding this comment

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

Tip

Codebase Verification

The verification of the createWithdrawal function's integration with the walletId parameter across the codebase shows that in most cases, the walletId is correctly passed as part of the function's arguments. Specifically, in the worker/autowithdraw.js file, both occurrences (lines 139 and 172) include walletId within an object as the third parameter. This aligns with the expected integration pattern.

However, the calls in pages/api/lnwith.js (lines 69-71) do not include the walletId parameter. This might be intentional, depending on the function's design and the requirements of the API endpoint, but it does indicate that walletId is not universally integrated in all calls to createWithdrawal.

Given this information, it is recommended to review whether the absence of walletId in the API endpoint calls is appropriate or if it represents an oversight that needs correction.

Conclusion:
The createWithdrawal function is generally integrated with the walletId parameter correctly in the worker/autowithdraw.js file. However, its absence in some API calls may need further review to ensure consistency and correctness across the application.

Analysis chain

Confirm that the createWithdrawal function is properly integrated with the walletId parameter in all relevant calls.

Scripts Executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Confirm integration of `walletId` in `createWithdrawal` function calls.
ast-grep --lang javascript --pattern $'createWithdrawal($_, $_, $_, $_)'

Length of output: 73



Script:

#!/bin/bash
# Search for all occurrences of `createWithdrawal` to understand its usage patterns.
ast-grep --lang javascript --pattern $'createWithdrawal($$$)'

Length of output: 720

Copy link
Member

Choose a reason for hiding this comment

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

@ekzyis is this omission intentional?

Copy link
Contributor

Choose a reason for hiding this comment

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

Seems like the humans are having a chat. I'll hop back into my burrow for now. If you need me again, just tag @coderabbitai in a new comment, and I'll come hopping out!

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes since the createWithdrawal call in pages/api/lnwith.js is not about an autowithdrawal

Copy link
Member

Choose a reason for hiding this comment

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

I see. So once we ship this we'll remove the old procedure and this code will use the new one.

Copy link
Member Author

@ekzyis ekzyis Apr 16, 2024

Choose a reason for hiding this comment

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

Mhh, I see. So it's about when we remove create_withdrawal(TEXT, TEXT, BIGINT, BIGINT, TEXT, BOOLEAN) and old code is still calling it during the migration. This will fail since that function no longer exists. It wouldn't fail when there would be a default.

Therefore, I see following options:

a) Remove create_withdrawal(TEXT, TEXT, BIGINT, BIGINT, TEXT, BOOLEAN) and replace create_withdrawal(TEXT, TEXT, BIGINT, BIGINT, TEXT, BOOLEAN, INTEGER) with create_withdrawal(TEXT, TEXT, BIGINT, BIGINT, TEXT, BOOLEAN, INTEGER DEFAULT NULL) in the same future migration
b) Simply delete create_withdrawal(TEXT, TEXT, BIGINT, BIGINT, TEXT, BOOLEAN) since this will only break withdrawals while we're migrating which shouldn't take too long. This isn't backwards incompatible in a way that it will take down the site afaict. It's still an interesting problem to think about though.
c) Think more about versioned schemas like create_withdrawal_vX etc.
d) What are the reasons we're using postgres functions instead of inline JS code? Mostly historical since support for interactive transactions in Prisma was only made generally available in 4.7.0? I think there is a case to be made for interactive transactions instead of postgres functions.

I prefer option b)

Copy link
Member

Choose a reason for hiding this comment

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

b sounds fine. Another option

e) have pages/api/lnwith use the new version by supplying a null wallet_id

Copy link
Member Author

Choose a reason for hiding this comment

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

I think I am super confused now or I was super confused. I don't understand my own comment anymore:

So it's about when we remove create_withdrawal(TEXT, TEXT, BIGINT, BIGINT, TEXT, BOOLEAN) and old code is still calling it during the migration

When we deploy the changes in this PR and the currently deployed code (1d154e) stops running, there will no longer be code that calls create_withdrawal(,,,,, BOOLEAN). There will only be code from this PR that calls create_withdrawal(,,,,, BOOLEAN, INTEGER) including the code in pages/api/lnwith. So we can safely remove create_withdrawal(,,,,, BOOLEAN) then.

But we actually will want to migrate from create_withdrawal(..., BOOLEAN, INTEGER) to just create_withdrawal(..., INTEGER). Is this the problem you are talking about? Even though I don't see how a INTEGER DEFAULT NULL fits in here.

Copy link
Member

Choose a reason for hiding this comment

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

I just realized that page/api/lnwith doesn't have an independent call to create_with.. lol. Let's stop me from confusing you more.

Is this good to merge?

Copy link
Member Author

Choose a reason for hiding this comment

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

This is as good as it gets

-- AddForeignKey
ALTER TABLE "Withdrawl" ADD CONSTRAINT "Withdrawl_walletId_fkey" FOREIGN KEY ("walletId") REFERENCES "Wallet"("id") ON DELETE SET NULL ON UPDATE CASCADE;

CREATE OR REPLACE FUNCTION create_withdrawl(lnd_id TEXT, invoice TEXT, msats_amount BIGINT, msats_max_fee BIGINT, username TEXT, auto_withdraw BOOLEAN, wallet_id INTEGER)
Copy link
Member Author

Choose a reason for hiding this comment

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

I didn't replace the old function signature TEXT, TEXT, BIGINT, BIGINT, TEXT, BOOLEAN with TEXT, TEXT, BIGINT, BIGINT, TEXT, INTEGER since then the old code might be confused which function it should use if two signatures exist with the same amount of parameters during migration even though it should be obvious when looking at the JS types (boolean is not an integer).

But I neither trust JS nor Prisma to not fuck this up so to be safe, I added a new function signature.

worker/wallet.js Outdated
let walletName
if (dbWdrwl.wallet) {
const { wallet } = dbWdrwl.wallet
walletName = wallet.address ? 'walletLNAddr' : wallet.macaroon ? 'walletLND' : 'walletCLN'
Copy link
Member

Choose a reason for hiding this comment

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

This works, but dbWdrwl.wallet.type is probably be a better way to determine wallet types.

Copy link
Member Author

@ekzyis ekzyis Apr 15, 2024

Choose a reason for hiding this comment

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

Oh, yes. I should actually just use WalletType in the WalletLog table, too.

update: But that would be backwards incompatible 👀

I'll first use strings that would be compatible with WalletType. Then we can later migrate the column to actually use WalletType.

Copy link
Member Author

@ekzyis ekzyis Apr 15, 2024

Choose a reason for hiding this comment

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

Done in 742b1d3

Copy link
Member

Choose a reason for hiding this comment

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

🙊 I had a code review draft that mentioned that change in the wallet log PR but figured it was better to let you find it. I figured so long as it's not going to break, I can take this approach. :)

Copy link
Member Author

@ekzyis ekzyis Apr 16, 2024

Choose a reason for hiding this comment

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

This is actually a fun game to play 👀:

What did @huumn not tell me about this PR?

Copy link
Member

Choose a reason for hiding this comment

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

This is why shipping MVPs is good because you begin thinking about what people might notice. Same goes for open source code. You begin simulating accountability.

Copy link
Member Author

Choose a reason for hiding this comment

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

This reminds me of this:

I believe our brains create a literal simulation of our partner's mind that overtime converges towards their actual thought processes. In that way, we are feeling and thinking what they're feeling and thinking, and vice versa.

-- https://stacker.news/items/413755

Your analogy with startup hires being like marriages becomes more and more true over time

worker/wallet.js Outdated Show resolved Hide resolved
@ekzyis ekzyis force-pushed the 1069-fix-autowithdrawal-logs branch from f573814 to 0a8b615 Compare April 15, 2024 18:41
@ekzyis ekzyis force-pushed the 1069-fix-autowithdrawal-logs branch from 0a8b615 to b715005 Compare April 16, 2024 14:11
@huumn huumn merged commit e30dfba into master Apr 16, 2024
5 checks passed
@ekzyis ekzyis deleted the 1069-fix-autowithdrawal-logs branch April 16, 2024 19:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Failed autowithdrawals show up in wallet logs as 'OK'
2 participants