Skip to content

Commit

Permalink
Use cursor for invoice subscription
Browse files Browse the repository at this point in the history
  • Loading branch information
ekzyis committed Jan 4, 2024
1 parent 3335ba1 commit 49bac29
Show file tree
Hide file tree
Showing 4 changed files with 14 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- AlterTable
ALTER TABLE "Invoice" ADD COLUMN "confirmedIndex" BIGINT;

-- CreateIndex
CREATE INDEX "Invoice.confirmedIndex_index" ON "Invoice"("confirmedIndex");
2 changes: 2 additions & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -538,6 +538,7 @@ model Invoice {
bolt11 String
expiresAt DateTime
confirmedAt DateTime?
confirmedIndex BigInt?
cancelled Boolean @default(false)
msatsRequested BigInt
msatsReceived BigInt?
Expand All @@ -548,6 +549,7 @@ model Invoice {
@@index([createdAt], map: "Invoice.created_at_index")
@@index([userId], map: "Invoice.userId_index")
@@index([confirmedIndex], map: "Invoice.confirmedIndex_index")
}

model Withdrawl {
Expand Down
4 changes: 3 additions & 1 deletion worker/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,9 @@ async function work () {

await boss.start()

subWrapper(subscribeToInvoices({ lnd }), 'invoice_updated', (inv) => checkInvoice({ data: { hash: inv.id, sub: true }, ...args }))
const [lastConfirmed] = await models.$queryRaw`SELECT "confirmedIndex" FROM "Invoice" ORDER BY "confirmedIndex" DESC NULLS LAST LIMIT 1`
subWrapper(subscribeToInvoices({ lnd, confirmed_after: lastConfirmed?.confirmedIndex }),
'invoice_updated', (inv) => checkInvoice({ data: { hash: inv.id, sub: true }, ...args }))
await boss.work('checkInvoice', jobWrapper(checkInvoice))

await boss.work('checkWithdrawal', jobWrapper(checkWithdrawal))
Expand Down
6 changes: 4 additions & 2 deletions worker/wallet.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,9 @@ export async function checkInvoice ({ data: { hash, isHeldSet, sub }, boss, mode
// never mark hodl invoices as confirmed here because
// we manually confirm them when we settle them
await serialize(models,
models.$executeRaw`SELECT confirm_invoice(${inv.id}, ${Number(inv.received_mtokens)})`)
models.$executeRaw`SELECT confirm_invoice(${inv.id}, ${Number(inv.received_mtokens)}`,
models.invoice.update({ where: { hash }, data: { confirmedIndex: inv.confirmed_index } })
)
if (sub) {
// only send push notifications in the context of a LND subscription.
// else, when this code is run from polling context, we would send another push notification.
Expand Down Expand Up @@ -61,7 +63,7 @@ export async function checkInvoice ({ data: { hash, isHeldSet, sub }, boss, mode
// this is basically confirm_invoice without setting confirmed_at since it's not settled yet
// and without setting the user balance since that's done inside the same tx as the HODL invoice action.
await serialize(models,
models.invoice.update({ where: { hash }, data: { msatsReceived: Number(inv.received_mtokens), isHeld: true } }))
models.invoice.update({ where: { hash }, data: { msatsReceived: Number(inv.received_mtokens), isHeld: true, confirmedIndex: inv.confirmed_index } }))
// remember that we already executed this if clause
// (even though the query above is idempotent but imo, this makes the flow more clear)
isHeldSet = true
Expand Down

0 comments on commit 49bac29

Please sign in to comment.