Skip to content

Commit

Permalink
feat: implement silent block broadcasting
Browse files Browse the repository at this point in the history
fixed event emitter issue

fix: Add a delay function for silent block events

fix: silent block broadcasting

Fix: Use a more explicit event name for indexing blocks

feat: implement silent block broadcasting

fix: Make event emitter protected

feat: implement silent block broadcasting

fix: Add event emitter and gateway dependencies to tests

chore: added makerfile to make local e2e test a breeze

added makerfile to make local e2e test a breeze:

working

improved maker file structure with output logs for debug purpose

chore: update vulnerable dependancies

Signed-off-by: Anmol Sharma <anmolsharma0234@gmail.com>

feat: implement silent block broadcasting

fixed event emitter issue

fix: Add a delay function for silent block events

fix: silent block broadcasting

Fix: Use a more explicit event name for indexing blocks

feat: implement silent block broadcasting

fix: Make event emitter protected

feat: implement silent block broadcasting

fix: Add event emitter and gateway dependencies to tests

fix: clean block provider module

Fixes duplicate pm2 dependency and updates packages and Removes unused delay function from common.ts.

fix: Remove delay in block processing
  • Loading branch information
seekersoftec committed Nov 29, 2024
1 parent 5d30c84 commit 7a1aa8e
Show file tree
Hide file tree
Showing 14 changed files with 169 additions and 3 deletions.
78 changes: 78 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
"@nestjs/common": "^10.4.1",
"@nestjs/config": "^3.2.3",
"@nestjs/core": "^10.4.1",
"@nestjs/event-emitter": "^2.0.4",
"@nestjs/microservices": "^10.4.1",
"@nestjs/passport": "^10.0.0",
"@nestjs/platform-express": "^10.3.7",
Expand All @@ -39,6 +40,7 @@
"@nestjs/swagger": "^7.3.1",
"@nestjs/typeorm": "^10.0.2",
"@nestjs/websockets": "^10.3.7",
"@nestjs/platform-ws": "^10.4.4",
"axios": "^1.7.2",
"currency.js": "^2.0.4",
"js-yaml": "^4.1.0",
Expand All @@ -58,6 +60,7 @@
"@types/node": "18.15.11",
"@types/secp256k1": "^4.0.6",
"@types/supertest": "^2.0.11",
"@types/ws": "^8.5.12",
"@typescript-eslint/eslint-plugin": "^5.0.0",
"@typescript-eslint/parser": "^5.0.0",
"bip32": "^2.0.0",
Expand Down
2 changes: 2 additions & 0 deletions src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,12 @@ import { SilentBlocksModule } from '@/silent-blocks/silent-blocks.module';
import { OperationStateModule } from '@/operation-state/operation-state.module';
import { ScheduleModule } from '@nestjs/schedule';
import { BlockProviderModule } from '@/block-data-providers/block-provider.module';
import { EventEmitterModule } from '@nestjs/event-emitter';

@Module({
imports: [
ScheduleModule.forRoot(),
EventEmitterModule.forRoot(),
ConfigModule.forRoot({
ignoreEnvFile: true,
load: [configuration],
Expand Down
2 changes: 2 additions & 0 deletions src/block-data-providers/base-block-data-provider.abstract.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { OperationStateService } from '@/operation-state/operation-state.service';
import { Logger } from '@nestjs/common';
import { EventEmitter2 } from '@nestjs/event-emitter';
import {
IndexerService,
TransactionInput,
Expand All @@ -12,6 +13,7 @@ import { EntityManager } from 'typeorm';
import { OperationState } from '@/operation-state/operation-state.entity';

export abstract class BaseBlockDataProvider<OperationState> {
protected readonly eventEmitter: EventEmitter2 = new EventEmitter2();
protected abstract readonly logger: Logger;
protected abstract readonly operationStateKey: string;

Expand Down
5 changes: 5 additions & 0 deletions src/block-data-providers/bitcoin-core/provider.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import {
import { Test, TestingModule } from '@nestjs/testing';
import { BlockStateService } from '@/block-state/block-state.service';
import { DbTransactionService } from '@/db-transaction/db-transaction.service';
import { EventEmitter2 } from '@nestjs/event-emitter';

describe('Bitcoin Core Provider', () => {
let provider: BitcoinCoreProvider;
Expand Down Expand Up @@ -58,6 +59,10 @@ describe('Bitcoin Core Provider', () => {
execute: jest.fn(),
},
},
{
provide: EventEmitter2,
useValue: jest.fn(),
},
],
}).compile();

Expand Down
5 changes: 5 additions & 0 deletions src/block-data-providers/bitcoin-core/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ import * as currency from 'currency.js';
import { AxiosRetryConfig, makeRequest } from '@/common/request';
import { BlockStateService } from '@/block-state/block-state.service';
import { DbTransactionService } from '@/db-transaction/db-transaction.service';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { INDEXED_BLOCK_EVENT } from '@/common/events';

@Injectable()
export class BitcoinCoreProvider
Expand All @@ -48,6 +50,7 @@ export class BitcoinCoreProvider
operationStateService: OperationStateService,
blockStateService: BlockStateService,
private readonly dbTransactionService: DbTransactionService,
protected readonly eventEmitter: EventEmitter2,
) {
super(
configService,
Expand Down Expand Up @@ -156,6 +159,8 @@ export class BitcoinCoreProvider
manager,
);
});

this.eventEmitter.emit(INDEXED_BLOCK_EVENT, height);
}
} finally {
this.isSyncing = false;
Expand Down
6 changes: 6 additions & 0 deletions src/block-data-providers/block-provider.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { BlockStateService } from '@/block-state/block-state.service';
import { BlockStateModule } from '@/block-state/block-state.module';
import { DbTransactionModule } from '@/db-transaction/db-transaction.module';
import { DbTransactionService } from '@/db-transaction/db-transaction.service';
import { EventEmitter2, EventEmitterModule } from '@nestjs/event-emitter';

@Module({
imports: [
Expand All @@ -19,6 +20,7 @@ import { DbTransactionService } from '@/db-transaction/db-transaction.service';
ConfigModule,
BlockStateModule,
DbTransactionModule,
EventEmitterModule,
],
controllers: [],
providers: [
Expand All @@ -30,13 +32,15 @@ import { DbTransactionService } from '@/db-transaction/db-transaction.service';
OperationStateService,
BlockStateService,
DbTransactionService,
EventEmitter2,
],
useFactory: (
configService: ConfigService,
indexerService: IndexerService,
operationStateService: OperationStateService,
blockStateService: BlockStateService,
dbTransactionService: DbTransactionService,
eventEmitter: EventEmitter2,
) => {
switch (configService.get<ProviderType>('providerType')) {
case ProviderType.ESPLORA:
Expand All @@ -46,6 +50,7 @@ import { DbTransactionService } from '@/db-transaction/db-transaction.service';
operationStateService,
blockStateService,
dbTransactionService,
eventEmitter,
);
case ProviderType.BITCOIN_CORE_RPC:
return new BitcoinCoreProvider(
Expand All @@ -54,6 +59,7 @@ import { DbTransactionService } from '@/db-transaction/db-transaction.service';
operationStateService,
blockStateService,
dbTransactionService,
eventEmitter,
);
default:
throw Error('unrecognised provider type in config');
Expand Down
5 changes: 5 additions & 0 deletions src/block-data-providers/esplora/provider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ import { TAPROOT_ACTIVATION_HEIGHT } from '@/common/constants';
import { BlockStateService } from '@/block-state/block-state.service';
import { Cron, CronExpression } from '@nestjs/schedule';
import { DbTransactionService } from '@/db-transaction/db-transaction.service';
import { EventEmitter2 } from '@nestjs/event-emitter';
import { INDEXED_BLOCK_EVENT } from '@/common/events';

@Injectable()
export class EsploraProvider
Expand All @@ -33,6 +35,7 @@ export class EsploraProvider
operationStateService: OperationStateService,
blockStateService: BlockStateService,
private readonly dbTransactionService: DbTransactionService,
protected readonly eventEmitter: EventEmitter2,
) {
super(
configService,
Expand Down Expand Up @@ -189,6 +192,8 @@ export class EsploraProvider
manager,
);
});

this.eventEmitter.emit(INDEXED_BLOCK_EVENT, height);
} catch (error) {
this.logger.error(
`Error processing transactions in block at height ${height}, hash ${hash}: ${error.message}`,
Expand Down
1 change: 1 addition & 0 deletions src/common/events.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export const INDEXED_BLOCK_EVENT = 'INDEXED_BLOCK_EVENT';
2 changes: 2 additions & 0 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,13 @@ import { NestFactory } from '@nestjs/core';
import { AppModule } from '@/app.module';
import { ConfigService } from '@nestjs/config';
import { LogLevel } from '@nestjs/common';
import { WsAdapter } from '@nestjs/platform-ws';

declare const module: any;

async function bootstrap() {
const app = await NestFactory.create(AppModule);
app.useWebSocketAdapter(new WsAdapter(app));

const configService = app.get<ConfigService>(ConfigService);
const port = configService.get<number>('app.port');
Expand Down
36 changes: 36 additions & 0 deletions src/silent-blocks/silent-blocks.gateway.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
import { Injectable, Logger } from '@nestjs/common';
import {
WebSocketGateway,
WebSocketServer,
OnGatewayConnection,
OnGatewayDisconnect,
} from '@nestjs/websockets';
import { Server, WebSocket } from 'ws';

@Injectable()
@WebSocketGateway()
export class SilentBlocksGateway
implements OnGatewayConnection, OnGatewayDisconnect
{
private readonly logger = new Logger(SilentBlocksGateway.name);

@WebSocketServer() server: Server;

handleConnection(client: WebSocket) {
const remoteAddress = (client as any)._socket.remoteAddress;
this.logger.debug(`Client connected: ${remoteAddress}`);
}

handleDisconnect(client: WebSocket) {
const remoteAddress = (client as any)._socket.remoteAddress;
this.logger.debug(`Client disconnected: ${remoteAddress}`);
}

broadcastSilentBlock(silentBlock: Buffer) {
for (const client of this.server.clients) {
if (client.readyState === WebSocket.OPEN) {
client.send(silentBlock);
}
}
}
}
3 changes: 2 additions & 1 deletion src/silent-blocks/silent-blocks.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ import { Transaction } from '@/transactions/transaction.entity';
import { TransactionsService } from '@/transactions/transactions.service';
import { SilentBlocksController } from '@/silent-blocks/silent-blocks.controller';
import { SilentBlocksService } from '@/silent-blocks/silent-blocks.service';
import { SilentBlocksGateway } from '@/silent-blocks/silent-blocks.gateway';

@Module({
imports: [TypeOrmModule.forFeature([Transaction])],
providers: [TransactionsService, SilentBlocksService],
providers: [TransactionsService, SilentBlocksService, SilentBlocksGateway],
controllers: [SilentBlocksController],
exports: [SilentBlocksService],
})
Expand Down
5 changes: 5 additions & 0 deletions src/silent-blocks/silent-blocks.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Test, TestingModule } from '@nestjs/testing';
import { TransactionsService } from '@/transactions/transactions.service';
import { SilentBlocksService } from '@/silent-blocks/silent-blocks.service';
import { silentBlockEncodingFixture } from '@/silent-blocks/silent-blocks.service.fixtures';
import { SilentBlocksGateway } from '@/silent-blocks/silent-blocks.gateway';
import { DataSource, Repository } from 'typeorm';
import { Transaction } from '@/transactions/transaction.entity';
import { getRepositoryToken } from '@nestjs/typeorm';
Expand Down Expand Up @@ -31,6 +32,10 @@ describe('SilentBlocksService', () => {
provide: getRepositoryToken(Transaction),
useValue: transactionRepository,
},
{
provide: SilentBlocksGateway,
useValue: jest.fn(),
},
],
}).compile();

Expand Down
Loading

0 comments on commit 7a1aa8e

Please sign in to comment.