From 3a7c871a17d764e10360bd07614c1c5e676ec13b Mon Sep 17 00:00:00 2001 From: Gustavo Inacio Date: Wed, 28 Aug 2024 20:44:24 +0200 Subject: [PATCH] test: add mark as non last Signed-off-by: Gustavo Inacio --- .../src/allocations/__tests__/tap.test.ts | 170 ++++++++++++++---- 1 file changed, 138 insertions(+), 32 deletions(-) diff --git a/packages/indexer-common/src/allocations/__tests__/tap.test.ts b/packages/indexer-common/src/allocations/__tests__/tap.test.ts index 4dd3ea4c9..a653c91f7 100644 --- a/packages/indexer-common/src/allocations/__tests__/tap.test.ts +++ b/packages/indexer-common/src/allocations/__tests__/tap.test.ts @@ -63,8 +63,18 @@ const setup = async () => { receiptCollector = network.receiptCollector } +const ALLOCATION_ID_1 = toAddress('edde47df40c29949a75a6693c77834c00b8ad626') +const ALLOCATION_ID_2 = toAddress('dead47df40c29949a75a6693c77834c00b8ad624') +const ALLOCATION_ID_3 = toAddress('6aea8894b5ab5a36cdc2d8be9290046801dd5fed') + +const SENDER_ADDRESS_1 = toAddress('ffcf8fdee72ac11b5c542428b35eef5769c409f0') +const SENDER_ADDRESS_2 = toAddress('dead47df40c29949a75a6693c77834c00b8ad624') + +const currentTimestamp = new Date() + +// last rav not redeemed const rav = { - allocationId: toAddress('edde47df40c29949a75a6693c77834c00b8ad626'), + allocationId: ALLOCATION_ID_1, last: true, final: false, timestampNs: 1709067401177959664n, @@ -73,15 +83,78 @@ const rav = { 'ede3f7ca5ace3629009f190bb51271f30c1aeaf565f82c25c447c7c9501f3ff31b628efcaf69138bf12960dd663924a692ee91f401785901848d8d7a639003ad1b', 'hex', ), - senderAddress: toAddress('ffcf8fdee72ac11b5c542428b35eef5769c409f0'), + senderAddress: SENDER_ADDRESS_1, redeemedAt: null, - createdAt: new Date(), - updatedAt: new Date(), + createdAt: currentTimestamp, + updatedAt: currentTimestamp, } +const SIGNATURE = Buffer.from( + 'ede3f7ca5ace3629009f190bb51271f30c1aeaf565f82c25c447c7c9501f3ff31b628efcaf69138bf12960dd663924a692ee91f401785901848d8d7a639003ad1b', + 'hex', +) + +const rav_list = [ + rav, + // redeemed rav but non-final + { + allocationId: ALLOCATION_ID_2, + last: true, + final: false, + timestampNs: 1709067401177959664n, + valueAggregate: 20000000000000n, + signature: SIGNATURE, + senderAddress: SENDER_ADDRESS_1, + redeemedAt: new Date(currentTimestamp.getTime() - 1), + createdAt: currentTimestamp, + updatedAt: currentTimestamp, + }, + + { + allocationId: ALLOCATION_ID_2, + last: true, + final: false, + timestampNs: 1709067401177959664n, + valueAggregate: 20000000000000n, + signature: SIGNATURE, + senderAddress: SENDER_ADDRESS_2, + redeemedAt: currentTimestamp, + createdAt: currentTimestamp, + updatedAt: currentTimestamp, + }, + + { + allocationId: ALLOCATION_ID_3, + last: true, + final: false, + timestampNs: 1709067401177959664n, + valueAggregate: 20000000000000n, + signature: SIGNATURE, + senderAddress: SENDER_ADDRESS_1, + redeemedAt: new Date(currentTimestamp.getTime() + 1), + createdAt: currentTimestamp, + updatedAt: currentTimestamp, + }, +] + const setupEach = async () => { sequelize = await sequelize.sync({ force: true }) - await queryFeeModels.receiptAggregateVouchers.create(rav) + await queryFeeModels.receiptAggregateVouchers.bulkCreate(rav_list) + + jest + .spyOn(receiptCollector.tapSubgraph!, 'query') + .mockImplementation(async (): Promise> => { + return { + data: { + transactions: [], + _meta: { + block: { + timestamp: Date.now(), + }, + }, + }, + } + }) } const teardownEach = async () => { // Clear out query fee model tables @@ -125,25 +198,50 @@ describe('TAP', () => { timeout, ) + test('should revert the rav request', async () => { + // we have a redeemed non-final rav in our database + // it's not showing on the subgraph on a specific point in time + // the timestamp of the subgraph is greater than the receipt id + // should revert the rav + // const finalRavs = await queryFeeModels.receiptAggregateVouchers.findAll({ + // where: { last: true, final: true }, + // }) + // + // const allocationIds = [ALLOCATION_ID_1.toString()] + // const blockTimestamp = 1000 + // await receiptCollector['revertRavsRedeemed'](allocationIds, blockTimestamp) + }) + + test('should not revert the rav request, allocation_id not in the list ', async () => { + // we have a redeemed non-final rav in our database + // it's showing on the subgraph on a specific point in time + // the timestamp of the subgraph is greater than the receipt id + // should not revert the rav + }) + + test('should not revert the rav request, timestamp not greater than the subgraph timestamp', async () => { + // we have a redeemed non-final rav in our database + // it's not showing on the subgraph on a specific point in time + // the timestamp of the subgraph is lower than the receipt id + // should not revert the rav + }) + test( 'test ignore final rav', async () => { const date = new Date() const redeemDate = date.setHours(date.getHours() - 2) const rav2 = { - allocationId: toAddress('dead47df40c29949a75a6693c77834c00b8ad624'), + allocationId: ALLOCATION_ID_2, last: true, final: true, timestampNs: 1709067401177959664n, valueAggregate: 20000000000000n, - signature: Buffer.from( - 'ede3f7ca5ace3629009f190bb51271f30c1aeaf565f82c25c447c7c9501f3ff31b628efcaf69138bf12960dd663924a692ee91f401785901848d8d7a639003ad1b', - 'hex', - ), - senderAddress: toAddress('deadbeefcafedeadceefcafedeadbeefcafedead'), + signature: SIGNATURE, + senderAddress: SENDER_ADDRESS_2, redeemedAt: new Date(redeemDate), - createdAt: new Date(), - updatedAt: new Date(), + createdAt: currentTimestamp, + updatedAt: currentTimestamp, } await queryFeeModels.receiptAggregateVouchers.create(rav2) const ravs = await receiptCollector['pendingRAVs']() @@ -171,23 +269,23 @@ describe('TAP', () => { const date = new Date() const redeemDate = date.setHours(date.getHours() - 2) const rav2 = { - allocationId: toAddress('dead47df40c29949a75a6693c77834c00b8ad624'), + allocationId: ALLOCATION_ID_2, last: true, final: false, timestampNs: 1709067401177959664n, valueAggregate: 20000000000000n, - signature: Buffer.from( - 'ede3f7ca5ace3629009f190bb51271f30c1aeaf565f82c25c447c7c9501f3ff31b628efcaf69138bf12960dd663924a692ee91f401785901848d8d7a639003ad1b', - 'hex', - ), - senderAddress: toAddress('deadbeefcafedeadceefcafedeadbeefcafedead'), + signature: SIGNATURE, + senderAddress: SENDER_ADDRESS_2, redeemedAt: new Date(redeemDate), - createdAt: new Date(), - updatedAt: new Date(), + createdAt: currentTimestamp, + updatedAt: currentTimestamp, } await queryFeeModels.receiptAggregateVouchers.create(rav2) - const ravs = await receiptCollector['pendingRAVs']() + + let ravs = await receiptCollector['pendingRAVs']() + ravs = await receiptCollector['filterAndUpdateRavs'](ravs) // The point is it will only return the rav that is not final + expect(ravs).toEqual([ expect.objectContaining({ allocationId: rav.allocationId, @@ -253,8 +351,18 @@ describe('TAP', () => { return { data: { transactions: [ - { allocationID: '0xdead47df40c29949a75a6693c77834c00b8ad624' }, + { + allocationID: '0xdead47df40c29949a75a6693c77834c00b8ad624', + sender: { + id: '0xdead47df40c29949a75a6693c77834c00b8ad624', + }, + }, ], + _meta: { + block: { + timestamp: Date.now(), + }, + }, }, } }) @@ -262,22 +370,20 @@ describe('TAP', () => { const date = new Date() const redeemDate = date.setHours(date.getHours() - 2) const rav2 = { - allocationId: toAddress('dead47df40c29949a75a6693c77834c00b8ad624'), + allocationId: ALLOCATION_ID_2, last: true, final: false, timestampNs: 1709067401177959664n, valueAggregate: 20000000000000n, - signature: Buffer.from( - 'ede3f7ca5ace3629009f190bb51271f30c1aeaf565f82c25c447c7c9501f3ff31b628efcaf69138bf12960dd663924a692ee91f401785901848d8d7a639003ad1b', - 'hex', - ), - senderAddress: toAddress('deadbeefcafedeadceefcafedeadbeefcafedead'), + signature: SIGNATURE, + senderAddress: SENDER_ADDRESS_2, redeemedAt: new Date(redeemDate), - createdAt: new Date(), - updatedAt: new Date(), + createdAt: currentTimestamp, + updatedAt: currentTimestamp, } await queryFeeModels.receiptAggregateVouchers.create(rav2) - const ravs = await receiptCollector['pendingRAVs']() + let ravs = await receiptCollector['pendingRAVs']() + ravs = await receiptCollector['filterAndUpdateRavs'](ravs) expect(anotherFuncSpy).toBeCalled() const finalRavs = await queryFeeModels.receiptAggregateVouchers.findAll({ where: { last: true, final: true },