Skip to content

Commit

Permalink
[IND-481] Update handlers to use SQL function only removing the Knex …
Browse files Browse the repository at this point in the history
…option

This is towards updating the block processor to be executed as a SQL function.
  • Loading branch information
lcwik committed Nov 28, 2023
1 parent cb7d7a1 commit 77a5255
Show file tree
Hide file tree
Showing 37 changed files with 1,047 additions and 3,993 deletions.
128 changes: 39 additions & 89 deletions indexer/services/ender/__tests__/handlers/asset-handler.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { stats, STATS_FUNCTION_NAME } from '@dydxprotocol-indexer/base';
import { stats } from '@dydxprotocol-indexer/base';
import {
AssetCreateEventV1,
IndexerTendermintBlock,
Expand Down Expand Up @@ -36,7 +36,6 @@ import {
} from '../helpers/constants';
import { updateBlockCache } from '../../src/caches/block-cache';
import { createPostgresFunctions } from '../../src/helpers/postgres/postgres-functions';
import config from '../../src/config';

describe('assetHandler', () => {
beforeAll(async () => {
Expand Down Expand Up @@ -100,99 +99,50 @@ describe('assetHandler', () => {
});
});

it.each([
[
'via knex',
false,
],
[
'via SQL function',
true,
],
])(
'fails when market doesnt exist for asset (%s)',
async (
_name: string,
useSqlFunction: boolean,
) => {
config.USE_ASSET_CREATE_HANDLER_SQL_FUNCTION = useSqlFunction;
const transactionIndex: number = 0;
const kafkaMessage: KafkaMessage = createKafkaMessageFromAssetEvent({
assetEvent: defaultAssetCreateEvent,
transactionIndex,
height: defaultHeight,
time: defaultTime,
txHash: defaultTxHash,
});

await expect(onMessage(kafkaMessage)).rejects.toThrowError(
'Unable to find market with id: 0',
);
it('fails when market doesnt exist for asset', async () => {
const transactionIndex: number = 0;
const kafkaMessage: KafkaMessage = createKafkaMessageFromAssetEvent({
assetEvent: defaultAssetCreateEvent,
transactionIndex,
height: defaultHeight,
time: defaultTime,
txHash: defaultTxHash,
});

it.each([
[
'via knex',
false,
],
[
'via SQL function',
true,
],
])(
'creates new asset (%s)',
async (
_name: string,
useSqlFunction: boolean,
) => {
config.USE_ASSET_CREATE_HANDLER_SQL_FUNCTION = useSqlFunction;
await MarketTable.create(testConstants.defaultMarket);
await marketRefresher.updateMarkets();
const transactionIndex: number = 0;
await expect(onMessage(kafkaMessage)).rejects.toThrowError(
'Unable to find market with id: 0',
);
});

const assetEvent: AssetCreateEventV1 = defaultAssetCreateEvent;
const kafkaMessage: KafkaMessage = createKafkaMessageFromAssetEvent({
assetEvent,
transactionIndex,
height: defaultHeight,
time: defaultTime,
txHash: defaultTxHash,
});
// Confirm there is no existing asset to or from the sender subaccount
await expectNoExistingAssets();

await onMessage(kafkaMessage);

const newAssets: AssetFromDatabase[] = await AssetTable.findAll(
{},
[], {
orderBy: [[AssetColumns.id, Ordering.ASC]],
});
expect(newAssets.length).toEqual(1);
expectAssetMatchesEvent(assetEvent, newAssets[0]);
if (!useSqlFunction) {
expectTimingStats();
}
const asset: AssetFromDatabase = assetRefresher.getAssetFromId('0');
expect(asset).toBeDefined();
it('creates new asset', async () => {
await MarketTable.create(testConstants.defaultMarket);
await marketRefresher.updateMarkets();
const transactionIndex: number = 0;

const assetEvent: AssetCreateEventV1 = defaultAssetCreateEvent;
const kafkaMessage: KafkaMessage = createKafkaMessageFromAssetEvent({
assetEvent,
transactionIndex,
height: defaultHeight,
time: defaultTime,
txHash: defaultTxHash,
});
});
// Confirm there is no existing asset to or from the sender subaccount
await expectNoExistingAssets();

function expectTimingStats() {
expectTimingStat('create_asset');
}
await onMessage(kafkaMessage);

function expectTimingStat(fnName: string) {
expect(stats.timing).toHaveBeenCalledWith(
`ender.${STATS_FUNCTION_NAME}.timing`,
expect.any(Number),
{
className: 'AssetCreationHandler',
eventType: 'AssetCreateEvent',
fnName,
},
);
}
const newAssets: AssetFromDatabase[] = await AssetTable.findAll(
{},
[], {
orderBy: [[AssetColumns.id, Ordering.ASC]],
});
expect(newAssets.length).toEqual(1);
expectAssetMatchesEvent(assetEvent, newAssets[0]);
const asset: AssetFromDatabase = assetRefresher.getAssetFromId('0');
expect(asset).toBeDefined();
});
});

function expectAssetMatchesEvent(
event: AssetCreateEventV1,
Expand Down
Loading

0 comments on commit 77a5255

Please sign in to comment.