Skip to content

Commit 2f9cb0c

Browse files
authored
fix: add bnsImportUpdate to event emitter to fix BNS import test (#1491)
* fix: add bnsImportUpdate to event emitter to fix BNS import test
1 parent 0586de6 commit 2f9cb0c

File tree

5 files changed

+32
-14
lines changed

5 files changed

+32
-14
lines changed

src/datastore/pg-notifier.ts

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import * as postgres from 'postgres';
22
import { logError, logger } from '../helpers';
3+
import { DbConfigState } from './common';
34
import { connectPostgres, PgServer, PgSqlClient } from './connection';
45

56
type PgNotificationType =
@@ -10,7 +11,8 @@ type PgNotificationType =
1011
| 'addressUpdate'
1112
| 'nameUpdate'
1213
| 'tokenMetadataUpdateQueued'
13-
| 'tokensUpdate';
14+
| 'tokensUpdate'
15+
| 'configStateUpdate';
1416

1517
export type PgTxNotificationPayload = {
1618
txId: string;
@@ -34,6 +36,8 @@ export type PgAddressNotificationPayload = {
3436
blockHeight: number;
3537
};
3638

39+
export type PgConfigStateNotificationPayload = DbConfigState;
40+
3741
export type PgTokenMetadataNotificationPayload = {
3842
queueId: number;
3943
};
@@ -54,7 +58,8 @@ type PgNotificationPayload =
5458
| PgNftEventNotificationPayload
5559
| PgTokenMetadataNotificationPayload
5660
| PgTokensNotificationPayload
57-
| PgTxNotificationPayload;
61+
| PgTxNotificationPayload
62+
| PgConfigStateNotificationPayload;
5863

5964
type PgNotification = {
6065
type: PgNotificationType;
@@ -127,6 +132,10 @@ export class PgNotifier {
127132
await this.notify({ type: 'tokensUpdate', payload: payload });
128133
}
129134

135+
public async sendConfigState(payload: PgConfigStateNotificationPayload) {
136+
await this.notify({ type: 'configStateUpdate', payload });
137+
}
138+
130139
public async close() {
131140
await this.listener
132141
?.unlisten()

src/datastore/pg-store-event-emitter.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import { EventEmitter } from 'events';
22
import StrictEventEmitter from 'strict-event-emitter-types';
3-
import { DbMempoolStats } from './common';
3+
import { DbConfigState, DbMempoolStats } from './common';
44

55
type DataStoreEventEmitter = StrictEventEmitter<
66
EventEmitter,
@@ -14,6 +14,7 @@ type DataStoreEventEmitter = StrictEventEmitter<
1414
tokensUpdate: (contractID: string) => void;
1515
tokenMetadataUpdateQueued: (queueId: number) => void;
1616
mempoolStatsUpdate: (mempoolStats: DbMempoolStats) => void;
17+
configStateUpdate: (configState: DbConfigState) => void;
1718
}
1819
>;
1920

src/datastore/pg-store.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import {
2828
DbBnsZoneFile,
2929
DbBurnchainReward,
3030
DbChainTip,
31+
DbConfigState,
3132
DbEvent,
3233
DbEventTypeId,
3334
DbFtBalance,
@@ -91,6 +92,7 @@ import {
9192
import {
9293
PgAddressNotificationPayload,
9394
PgBlockNotificationPayload,
95+
PgConfigStateNotificationPayload,
9496
PgMicroblockNotificationPayload,
9597
PgNameNotificationPayload,
9698
PgNftEventNotificationPayload,
@@ -239,6 +241,12 @@ export class PgStore {
239241
const nftEvent = notification.payload as PgNftEventNotificationPayload;
240242
this.eventEmitter.emit('nftEventUpdate', nftEvent.txId, nftEvent.eventIndex);
241243
break;
244+
case 'configStateUpdate':
245+
this.eventEmitter.emit(
246+
'configStateUpdate',
247+
notification.payload as PgConfigStateNotificationPayload
248+
);
249+
break;
242250
}
243251
});
244252
}

src/datastore/pg-write-store.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1683,6 +1683,7 @@ export class PgWriteStore extends PgStore {
16831683
bns_subdomains_imported = ${configState.bns_subdomains_imported},
16841684
token_offering_imported = ${configState.token_offering_imported}
16851685
`;
1686+
await this.notifier?.sendConfigState(configState);
16861687
if (queryResult.count !== 1) {
16871688
throw new Error(`Unexpected config update row count: ${queryResult.count}`);
16881689
}

src/tests-bns/event-server-tests.ts

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ describe('BNS event server tests', () => {
1717
beforeEach(async () => {
1818
process.env.PG_DATABASE = 'postgres';
1919
await cycleMigrations();
20-
db = await PgWriteStore.connect({ usageName: 'tests', withNotifier: false });
20+
db = await PgWriteStore.connect({ usageName: 'tests', withNotifier: true });
2121
client = db.sql;
2222
eventServer = await startEventServer({
2323
datastore: db,
@@ -1052,7 +1052,6 @@ describe('BNS event server tests', () => {
10521052
})
10531053

10541054
test('BNS middleware is async. /new_block posts return before importing BNS finishes', async () => {
1055-
jest.useRealTimers();
10561055
process.env.BNS_IMPORT_DIR = 'src/tests-bns/import-test-files';
10571056
const genesisBlock = await getGenesisBlockData('src/tests-event-replay/tsv/mainnet.tsv');
10581057

@@ -1069,15 +1068,15 @@ describe('BNS event server tests', () => {
10691068
expect(configState.bns_names_onchain_imported).toBe(false)
10701069
expect(configState.bns_subdomains_imported).toBe(false)
10711070

1072-
const timeoutId: NodeJS.Timeout = await new Promise(resolve => {
1073-
const timeoutId = setTimeout(async() => {
1074-
const configState = await db.getConfigState();
1075-
expect(configState.bns_names_onchain_imported).toBe(true)
1076-
expect(configState.bns_subdomains_imported).toBe(true)
1077-
resolve(timeoutId)
1078-
}, 2000)
1071+
await new Promise(resolve => {
1072+
db.eventEmitter.on('configStateUpdate', (configState) => {
1073+
if (configState.bns_names_onchain_imported && configState.bns_subdomains_imported) {
1074+
expect(configState.bns_names_onchain_imported).toBe(true)
1075+
expect(configState.bns_subdomains_imported).toBe(true);
1076+
resolve(undefined);
1077+
}
1078+
})
10791079
})
1080-
1081-
clearTimeout(timeoutId);
1080+
db.eventEmitter.removeAllListeners('configStateUpdate');
10821081
})
10831082
})

0 commit comments

Comments
 (0)