Skip to content

Commit

Permalink
chore(backend): dont wait 30s when completing/expiring incoming payme…
Browse files Browse the repository at this point in the history
…nt (#3043)

* chore(backend): dont wait 30s when completing/expiring incoming payment

* chore(backend): clean up busy logs

* chore(auth): correct formatting

* test(backend): updating tests
  • Loading branch information
mkurapov authored Oct 24, 2024
1 parent e48c279 commit 75518e8
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 29 deletions.
3 changes: 1 addition & 2 deletions packages/backend/src/open_payments/payment/incoming/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,7 @@ export class IncomingPayment
incomingPayment = await IncomingPayment.query()
.patchAndFetchById(this.id, {
state: IncomingPaymentState.Completed,
// Add 30 seconds to allow a prepared (but not yet fulfilled/rejected) packet to finish before sending webhook event.
processAt: new Date(Date.now() + 30_000)
processAt: new Date()
})
.whereNotIn('state', [
IncomingPaymentState.Expired,
Expand Down
26 changes: 14 additions & 12 deletions packages/backend/src/open_payments/payment/incoming/service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -587,8 +587,7 @@ describe('Incoming Payment Service', (): void => {

test('Sets state of fully paid incoming payment to "completed"', async (): Promise<void> => {
const now = new Date()
jest.useFakeTimers()
jest.setSystemTime(now)
jest.useFakeTimers({ now })
await expect(
incomingPayment.onCredit({
// eslint-disable-next-line @typescript-eslint/no-non-null-assertion
Expand All @@ -597,15 +596,15 @@ describe('Incoming Payment Service', (): void => {
).resolves.toMatchObject({
id: incomingPayment.id,
state: IncomingPaymentState.Completed,
processAt: new Date(now.getTime() + 30_000)
processAt: now
})
await expect(
incomingPaymentService.get({
id: incomingPayment.id
})
).resolves.toMatchObject({
state: IncomingPaymentState.Completed,
processAt: new Date(now.getTime() + 30_000)
processAt: now
})
})
})
Expand Down Expand Up @@ -662,9 +661,8 @@ describe('Incoming Payment Service', (): void => {
})
).resolves.toBeUndefined()

const now = incomingPayment.expiresAt
jest.useFakeTimers()
jest.setSystemTime(now)
jest.setSystemTime(incomingPayment.expiresAt)
await expect(incomingPaymentService.processNext()).resolves.toBe(
incomingPayment.id
)
Expand All @@ -674,7 +672,7 @@ describe('Incoming Payment Service', (): void => {
})
).resolves.toMatchObject({
state: IncomingPaymentState.Expired,
processAt: new Date(now.getTime() + 30_000)
processAt: new Date()
})
})

Expand Down Expand Up @@ -820,21 +818,22 @@ describe('Incoming Payment Service', (): void => {
})
test('updates state of pending incoming payment to complete', async (): Promise<void> => {
const now = new Date()
jest.spyOn(global.Date, 'now').mockImplementation(() => now.valueOf())
jest.useFakeTimers({ now })

await expect(
incomingPaymentService.complete(incomingPayment.id)
).resolves.toMatchObject({
id: incomingPayment.id,
state: IncomingPaymentState.Completed,
processAt: new Date(now.getTime() + 30_000)
processAt: now
})
await expect(
incomingPaymentService.get({
id: incomingPayment.id
})
).resolves.toMatchObject({
state: IncomingPaymentState.Completed,
processAt: new Date(now.getTime() + 30_000)
processAt: now
})
})

Expand All @@ -845,6 +844,9 @@ describe('Incoming Payment Service', (): void => {
})

test('updates state of processing incoming payment to complete', async (): Promise<void> => {
const now = new Date()
jest.useFakeTimers({ now })

await incomingPayment.onCredit({
totalReceived: BigInt(100)
})
Expand All @@ -860,15 +862,15 @@ describe('Incoming Payment Service', (): void => {
).resolves.toMatchObject({
id: incomingPayment.id,
state: IncomingPaymentState.Completed,
processAt: new Date(incomingPayment.expiresAt.getTime())
processAt: now
})
await expect(
incomingPaymentService.get({
id: incomingPayment.id
})
).resolves.toMatchObject({
state: IncomingPaymentState.Completed,
processAt: new Date(incomingPayment.expiresAt.getTime())
processAt: now
})
})

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -268,8 +268,7 @@ async function handleExpired(
)
await incomingPayment.$query(deps.knex).patch({
state: IncomingPaymentState.Expired,
// Add 30 seconds to allow a prepared (but not yet fulfilled/rejected) packet to finish before sending webhook event.
processAt: new Date(Date.now() + 30_000)
processAt: new Date()
})
} else {
deps.logger.debug({ amountReceived }, 'deleting expired incoming payment')
Expand Down Expand Up @@ -437,7 +436,7 @@ async function completeIncomingPayment(
}
await payment.$query(trx).patch({
state: IncomingPaymentState.Completed,
processAt: new Date(Date.now() + 30_000)
processAt: new Date()
})
return await addReceivedAmount(deps, payment)
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -87,10 +87,6 @@ export function createAccountMiddleware(serverAddress: string): ILPMiddleware {
LiquidityAccountType.INCOMING
)
}
ctx.services.logger.debug(
{ incomingPaymentId: incomingPayment.id },
'destination account is incoming payment'
)
return incomingPayment
}
// Open Payments SPSP fallback account
Expand All @@ -104,20 +100,12 @@ export function createAccountMiddleware(serverAddress: string): ILPMiddleware {
LiquidityAccountType.WEB_MONETIZATION
)
}
ctx.services.logger.debug(
{ walletAddressId: walletAddress.id },
'destination account is wallet address'
)
return walletAddress
}
}
const address = ctx.request.prepare.destination
const peer = await peers.getByDestinationAddress(address)
if (peer) {
ctx.services.logger.debug(
{ peerId: peer.id },
'destination account is peer'
)
return peer
}
if (
Expand Down

0 comments on commit 75518e8

Please sign in to comment.