Skip to content

Commit

Permalink
test: add mark as non last
Browse files Browse the repository at this point in the history
Signed-off-by: Gustavo Inacio <gustavo@semiotic.ai>
  • Loading branch information
gusinacio committed Aug 28, 2024
1 parent 2a8ef12 commit 3a7c871
Showing 1 changed file with 138 additions and 32 deletions.
170 changes: 138 additions & 32 deletions packages/indexer-common/src/allocations/__tests__/tap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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<QueryResult<unknown>> => {
return {
data: {
transactions: [],
_meta: {
block: {
timestamp: Date.now(),
},
},
},
}
})
}
const teardownEach = async () => {
// Clear out query fee model tables
Expand Down Expand Up @@ -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']()
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -253,31 +351,39 @@ describe('TAP', () => {
return {
data: {
transactions: [
{ allocationID: '0xdead47df40c29949a75a6693c77834c00b8ad624' },
{
allocationID: '0xdead47df40c29949a75a6693c77834c00b8ad624',
sender: {
id: '0xdead47df40c29949a75a6693c77834c00b8ad624',
},
},
],
_meta: {
block: {
timestamp: Date.now(),
},
},
},
}
})

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 },
Expand Down

0 comments on commit 3a7c871

Please sign in to comment.