11import { ChainID } from '@stacks/transactions' ;
22import { bnsNameCV , httpPostRequest } from '../helpers' ;
3- import { EventStreamServer , startEventServer } from '../event-stream/event-server' ;
3+ import { bnsImportMiddleware , EventStreamServer , startEventServer } from '../event-stream/event-server' ;
44import { TestBlockBuilder , TestMicroblockStreamBuilder } from '../test-utils/test-builders' ;
55import { DbAssetEventTypeId , DbBnsZoneFile } from '../datastore/common' ;
66import { PgWriteStore } from '../datastore/pg-write-store' ;
77import { cycleMigrations , runMigrations } from '../datastore/migrations' ;
88import { PgSqlClient } from '../datastore/connection' ;
9+ import { getGenesisBlockData } from '../event-replay/helpers' ;
10+ import { NextFunction } from 'express' ;
911
1012describe ( 'BNS event server tests' , ( ) => {
1113 let db : PgWriteStore ;
@@ -15,7 +17,7 @@ describe('BNS event server tests', () => {
1517 beforeEach ( async ( ) => {
1618 process . env . PG_DATABASE = 'postgres' ;
1719 await cycleMigrations ( ) ;
18- db = await PgWriteStore . connect ( { usageName : 'tests' , withNotifier : false } ) ;
20+ db = await PgWriteStore . connect ( { usageName : 'tests' , withNotifier : true } ) ;
1921 client = db . sql ;
2022 eventServer = await startEventServer ( {
2123 datastore : db ,
@@ -26,6 +28,12 @@ describe('BNS event server tests', () => {
2628 } ) ;
2729 } ) ;
2830
31+ afterEach ( async ( ) => {
32+ await eventServer . closeAsync ( ) ;
33+ await db ?. close ( ) ;
34+ await runMigrations ( undefined , 'down' ) ;
35+ } ) ;
36+
2937 test ( 'namespace-ready called by a contract other than BNS' , async ( ) => {
3038 const block = new TestBlockBuilder ( {
3139 block_height : 1 ,
@@ -1027,9 +1035,48 @@ describe('BNS event server tests', () => {
10271035 expect ( namespaceList . results ) . toStrictEqual ( [ 'ape.mega' ] ) ;
10281036 } ) ;
10291037
1030- afterEach ( async ( ) => {
1031- await eventServer . closeAsync ( ) ;
1032- await db ?. close ( ) ;
1033- await runMigrations ( undefined , 'down' ) ;
1034- } ) ;
1035- } ) ;
1038+ test ( 'BNS middleware imports bns when ir receives the genesis block' , async ( ) => {
1039+ process . env . BNS_IMPORT_DIR = 'src/tests-bns/import-test-files' ;
1040+ const genesisBlock = await getGenesisBlockData ( 'src/tests-event-replay/tsv/mainnet.tsv' ) ;
1041+ const bnsImportMiddlewareInitialized = bnsImportMiddleware ( db ) ;
1042+ let mockRequest = {
1043+ body : genesisBlock
1044+ } as unknown as Partial < Request > ;
1045+ let mockResponse : Partial < Response > = { } ;
1046+ let nextFunction : NextFunction = jest . fn ( ) ;
1047+ await bnsImportMiddlewareInitialized ( mockRequest as any , mockResponse as any , nextFunction )
1048+
1049+ const configState = await db . getConfigState ( ) ;
1050+ expect ( configState . bns_names_onchain_imported ) . toBe ( true )
1051+ expect ( configState . bns_subdomains_imported ) . toBe ( true )
1052+ } )
1053+
1054+ test ( 'BNS middleware is async. /new_block posts return before importing BNS finishes' , async ( ) => {
1055+ process . env . BNS_IMPORT_DIR = 'src/tests-bns/import-test-files' ;
1056+ const genesisBlock = await getGenesisBlockData ( 'src/tests-event-replay/tsv/mainnet.tsv' ) ;
1057+
1058+ await httpPostRequest ( {
1059+ host : '127.0.0.1' ,
1060+ port : eventServer . serverAddress . port ,
1061+ path : '/new_block' ,
1062+ headers : { 'Content-Type' : 'application/json' } ,
1063+ body : Buffer . from ( JSON . stringify ( genesisBlock ) , 'utf8' ) ,
1064+ throwOnNotOK : true ,
1065+ } ) ;
1066+
1067+ const configState = await db . getConfigState ( ) ;
1068+ expect ( configState . bns_names_onchain_imported ) . toBe ( false )
1069+ expect ( configState . bns_subdomains_imported ) . toBe ( false )
1070+
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+ } )
1079+ } )
1080+ db . eventEmitter . removeAllListeners ( 'configStateUpdate' ) ;
1081+ } )
1082+ } )
0 commit comments