Skip to content

Commit

Permalink
feat: Stabilize TAP DB tables
Browse files Browse the repository at this point in the history
  • Loading branch information
carlosvdr committed May 9, 2024
1 parent 5ac1377 commit bc0b473
Show file tree
Hide file tree
Showing 9 changed files with 401 additions and 155 deletions.
9 changes: 8 additions & 1 deletion packages/indexer-agent/src/commands/start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -586,7 +586,14 @@ export async function run(
const networks: Network[] = await pMap(
networkSpecifications,
async (spec: NetworkSpecification) =>
Network.create(logger, spec, queryFeeModels, graphNode, metrics),
Network.create(
logger,
spec,
queryFeeModels,
graphNode,
metrics,
sequelize.getQueryInterface(),
),
)

// --------------------------------------------------------------------------------
Expand Down
61 changes: 44 additions & 17 deletions packages/indexer-agent/src/db/migrations/12-add-scalar-tap-table.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,6 @@ export async function up({ context }: Context): Promise<void> {
}

logger.debug('Create function and trigger using raw SQL')

const functionSQL = `
CREATE FUNCTION scalar_tap_receipt_notify()
RETURNS trigger AS
Expand All @@ -70,25 +69,53 @@ export async function up({ context }: Context): Promise<void> {
ON scalar_tap_receipts
FOR EACH ROW EXECUTE PROCEDURE scalar_tap_receipt_notify();
`
const indexSQL = `
CREATE INDEX IF NOT EXISTS scalar_tap_receipts_allocation_id_idx ON scalar_tap_receipts (allocation_id);
CREATE INDEX IF NOT EXISTS scalar_tap_receipts_timestamp_ns_idx ON scalar_tap_receipts (timestamp_ns);
`
queryInterface.addIndex('scalar_tap_receipts', ['allocation_id'], {
name: 'scalar_tap_receipts_allocation_id_idx',
})
queryInterface.addIndex('scalar_tap_receipts', ['timestamp_ns'], {
name: 'scalar_tap_receipts_timestamp_ns_idx',
})

const functionLogsInvalidRavsSQL = `
CREATE TABLE IF NOT EXISTS scalar_tap_receipts_invalid (
id BIGSERIAL PRIMARY KEY,
allocation_id CHAR(40) NOT NULL,
signer_address CHAR(40) NOT NULL,
timestamp_ns NUMERIC(20) NOT NULL,
value NUMERIC(39) NOT NULL,
received_receipt JSON NOT NULL
);
`
if (tables.includes('scalar_tap_receipts_invalid')) {
logger.info(
`scalar_tap_receipts_invalid already exist, migration not necessary`,
)
return
}
// Create the scalar_tap_ravs table if it doesn't exist
await queryInterface.createTable('scalar_tap_receipts_invalid', {
id: {
type: DataTypes.BIGINT,
primaryKey: true,
autoIncrement: true,
},
allocation_id: {
type: DataTypes.CHAR(40),
allowNull: false,
},
signer_address: {
type: DataTypes.CHAR(40),
allowNull: false,
},
signature: {
type: DataTypes.BLOB,
allowNull: false,
},
timestamp_ns: {
type: DataTypes.DECIMAL(20),
allowNull: false,
},
nonce: {
type: DataTypes.DECIMAL,
allowNull: false,
},
value: {
type: DataTypes.DECIMAL(39),
allowNull: false,
},
})
await queryInterface.sequelize.query(functionSQL)
await queryInterface.sequelize.query(triggerSQL)
await queryInterface.sequelize.query(indexSQL)
await queryInterface.sequelize.query(functionLogsInvalidRavsSQL)

if (tables.includes('scalar_tap_ravs')) {
logger.info(`scalar_tap_ravs already exist, migration not necessary`)
Expand Down
1 change: 0 additions & 1 deletion packages/indexer-common/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@
"parsimmon": "^1.18.1",
"sequelize": "6.33.0",
"ts-custom-error": "^3.3.1",
"tap-contracts": "semiotic-ai/timeline-aggregation-protocol-contracts#ca1cfa653d0d1d209aee48ab021b0e562d2b5688",
"zod": "^3.21.4"
},
"devDependencies": {
Expand Down
54 changes: 10 additions & 44 deletions packages/indexer-common/src/allocations/__tests__/tap.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import {
} from '@graphprotocol/common-ts'
import { testNetworkSpecification } from '../../indexer-management/__tests__/util'
import { Sequelize } from 'sequelize'
import { utils } from 'ethers'

// Make global Jest variables available
// eslint-disable-next-line @typescript-eslint/no-explicit-any
Expand All @@ -32,7 +31,6 @@ const startRAVProcessing = jest.spyOn(
AllocationReceiptCollector.prototype,
'startRAVProcessing',
)

const setup = async () => {
logger = createLogger({
name: 'Indexer API Client',
Expand Down Expand Up @@ -60,6 +58,7 @@ const setup = async () => {
queryFeeModels,
graphNode,
metrics,
sequelize.getQueryInterface(),
)
receiptCollector = network.receiptCollector
}
Expand All @@ -82,7 +81,6 @@ const rav = {

const setupEach = async () => {
sequelize = await sequelize.sync({ force: true })

await queryFeeModels.receiptAggregateVouchers.create(rav)
}
const teardownEach = async () => {
Expand All @@ -95,67 +93,35 @@ const teardownAll = async () => {
}

describe('TAP', () => {
beforeAll(setup)
beforeEach(setupEach)
afterEach(teardownEach)
afterAll(teardownAll)

beforeAll(setup, timeout)
beforeEach(setupEach, timeout)
afterEach(teardownEach, timeout)
afterAll(teardownAll, timeout)
test(
'test if startRAVProcessing is called',
async () => {
expect(startRAVProcessing).toHaveBeenCalled()
},
timeout,
)

test(
'test getPendingRAVsEventual',
'test getPendingRAVs',
async () => {
const ravs = await receiptCollector['pendingRAVs']()

expect(ravs).toEqual([
expect.objectContaining({
allocationId: rav.allocationId,
final: rav.final,
last: rav.last,
senderAddress: rav.senderAddress,
signature: rav.signature,
timestampNs: BigInt(rav.timestampNs).toString(),
valueAggregate: BigInt(rav.valueAggregate).toString(),
timestampNs: rav.timestampNs,
valueAggregate: rav.valueAggregate,
}),
])
},
timeout,
)

test(
'check signature rav',
async () => {
const domain = {
name: 'TAP',
version: '1',
chainId: 1337,
verifyingContract: toAddress('0x5aeef48fe943f91c39a7609049f8968f5b84414e'),
}
const [first] = await queryFeeModels.receiptAggregateVouchers.findAll()
const signedRav = first.getSignedRAV()

const signerAddress = utils.verifyTypedData(
domain,
{
ReceiptAggregateVoucher: [
{ name: 'allocationId', type: 'address' },
{ name: 'timestampNs', type: 'uint64' },
{ name: 'valueAggregate', type: 'uint128' },
],
},
signedRav.rav,
signedRav.signature,
)

expect(signerAddress).toEqual('0x886574712d0ca20C36FD090A594Df7eCa17cd38e')
},
timeout,
),
test('test submitRAVs', async () => {})

test('test RAV Processing eventual', async () => {})
})
Loading

0 comments on commit bc0b473

Please sign in to comment.