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 stale confirmedIndex used on reconnect #764

Merged
merged 1 commit into from
Jan 20, 2024

Conversation

ekzyis
Copy link
Member

@ekzyis ekzyis commented Jan 19, 2024

On reconnects, we used a stale value for confirmedIndex. This means that we would confirm all invoices again since the worker was started.

Found this while debugging #763 but this fixes not #763.

@ekzyis ekzyis added the bug label Jan 19, 2024
@ekzyis ekzyis marked this pull request as draft January 19, 2024 04:58
@ekzyis ekzyis force-pushed the stale-confirmed-index-on-reconnect branch from 38398ab to 0264786 Compare January 19, 2024 05:11
@ekzyis ekzyis marked this pull request as ready for review January 19, 2024 05:11
@ekzyis ekzyis force-pushed the stale-confirmed-index-on-reconnect branch from 0264786 to 1af0bcf Compare January 19, 2024 16:28
@huumn
Copy link
Member

huumn commented Jan 20, 2024

Kind of a nit, but do we need to require subscribeForever receives a promise just because in one case it's convenient to pass one.

If it's easy to make to the promise optional, I think that's what we should do.

@ekzyis
Copy link
Member Author

ekzyis commented Jan 20, 2024

Kind of a nit, but do we need to require subscribeForever receives a promise just because in one case it's convenient to pass one.

If it's easy to make to the promise optional, I think that's what we should do.

Good point, my initial fix tried to do this and it looked like this:

diff --git a/worker/wallet.js b/worker/wallet.js
index 99bd10bd..a91c8edf 100644
--- a/worker/wallet.js
+++ b/worker/wallet.js
@@ -22,11 +22,16 @@ function subscribeForever (subscribe) {
     let sub
     try {
       return await new Promise((resolve, reject) => {
-        sub = subscribe(resolve, bail)
+        const sub = subscribe(resolve, bail)
         if (!sub) {
-          return bail(new Error('function passed to subscribeForever must return a subscription object'))
+          return bail(new Error('function passed to subscribeForever must return a subscription object or promise'))
+        }
+        if (sub.then) {
+          // sub is promise
+          sub.then(sub => sub.on('error', reject))
+        } else {
+          sub.on('error', reject)
         }
-        sub.on('error', reject)
       })
     } catch (error) {
       console.error(error)
@@ -45,13 +50,12 @@ const logEventError = (name, error) => console.error(`error running ${name}`, er
 async function subscribeToDeposits (args) {
   const { models, lnd } = args

-  const [lastConfirmed] = await models.$queryRaw`
+  subscribeForever(async () => {
+    const [lastConfirmed] = await models.$queryRaw`
     SELECT "confirmedIndex"
     FROM "Invoice"
     ORDER BY "confirmedIndex" DESC NULLS LAST
     LIMIT 1`
-
-  subscribeForever(() => {
     const sub = subscribeToInvoices({ lnd, confirmed_after: lastConfirmed?.confirmedIndex })

     sub.on('invoice_updated', async (inv) => {

So if you prefer that, I can use that. I don't have a strong opinion about either approach.

@huumn
Copy link
Member

huumn commented Jan 20, 2024

Seems better to me. I prefer abstractions eat the complexity of using them.

@ekzyis ekzyis force-pushed the stale-confirmed-index-on-reconnect branch from 1af0bcf to 69e6222 Compare January 20, 2024 22:49
@ekzyis
Copy link
Member Author

ekzyis commented Jan 20, 2024

Seems better to me. I prefer abstractions eat the complexity of using them.

Done. Tested by starting worker, paying an invoice, restarting LND and paying another invoice (to make sure reconnect worked).

On master (dc8d35f), the invoice is confirmed again after restarting LND which means another push notification is sent.

@huumn huumn merged commit 30850ac into master Jan 20, 2024
1 check passed
@ekzyis ekzyis deleted the stale-confirmed-index-on-reconnect branch February 14, 2024 00:25
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.

2 participants