-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Implement WebSocket to Stream Silent Blocks #46
base: main
Are you sure you want to change the base?
Changes from all commits
50a50f5
335710c
986be7b
39cab24
ccea1b1
81ff0c4
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -17,4 +17,3 @@ bitcoinCore: | |
rpcPass: polarpass | ||
rpcUser: polaruser | ||
rpcPort: 18445 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the lock file is not up to date with diff --git a/package-lock.json b/package-lock.json
index c0b2e37..05d3cd2 100644
--- a/package-lock.json
+++ b/package-lock.json
@@ -17,6 +17,7 @@
"@nestjs/passport": "^10.0.0",
"@nestjs/platform-express": "^10.3.7",
"@nestjs/platform-socket.io": "^10.3.7",
+ "@nestjs/platform-ws": "^10.4.4",
"@nestjs/schedule": "^3.0.0",
"@nestjs/swagger": "^7.3.1",
"@nestjs/typeorm": "^10.0.2",
@@ -32,7 +33,6 @@
},
"devDependencies": {
"@nestjs/cli": "^10.4.5",
- "@nestjs/platform-ws": "^10.4.4",
"@nestjs/schematics": "^9.0.0",
"@nestjs/testing": "^10.3.7",
"@types/express": "^4.17.13",
@@ -2044,7 +2044,6 @@
"version": "10.4.4",
"resolved": "https://registry.npmjs.org/@nestjs/platform-ws/-/platform-ws-10.4.4.tgz",
"integrity": "sha512-6E476YvfO14uQUT6FzWFpGnwp58fpGq2aeGxHFdRMMptOQ5suKqD+3LsZgPiGF1elgVhQcI5uqM15qL3yH+OqQ==",
- "dev": true,
"dependencies": {
"tslib": "2.7.0",
"ws": "8.18.0"
@@ -2062,14 +2061,12 @@
"node_modules/@nestjs/platform-ws/node_modules/tslib": {
"version": "2.7.0",
"resolved": "https://registry.npmjs.org/tslib/-/tslib-2.7.0.tgz",
- "integrity": "sha512-gLXCKdN1/j47AiHiOkJN69hJmcbGTHI0ImLmbYLHykhgeN0jVGola9yVjFgzCUklsZQMW55o+dW7IXv3RCXDzA==",
- "dev": true
+ "integrity": "sha512-gLXCKdN1/j47AiHiOkJN69hJmcbGTHI0ImLmbYLHykhgeN0jVGola9yVjFgzCUklsZQMW55o+dW7IXv3RCXDzA=="
},
"node_modules/@nestjs/platform-ws/node_modules/ws": {
"version": "8.18.0",
"resolved": "https://registry.npmjs.org/ws/-/ws-8.18.0.tgz",
"integrity": "sha512-8VbfWfHLbbwu3+N6OKsOMpBdT4kXPDDB9cJk2bJ6mh9ucxdlnNvH1e+roYkKmN9Nxw2yjz7VzeO9oOz2zJ04Pw==",
- "dev": true,
"engines": {
"node": ">=10.0.0"
}, |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -28,6 +28,8 @@ import { | |||||
import { AxiosRequestConfig } from 'axios'; | ||||||
import * as currency from 'currency.js'; | ||||||
import { AxiosRetryConfig, makeRequest } from '@/common/request'; | ||||||
import { EventEmitter2 } from '@nestjs/event-emitter'; | ||||||
import { INDEXED_BLOCK_EVENT } from '@/common/events'; | ||||||
|
||||||
@Injectable() | ||||||
export class BitcoinCoreProvider | ||||||
|
@@ -44,6 +46,7 @@ export class BitcoinCoreProvider | |||||
private readonly configService: ConfigService, | ||||||
indexerService: IndexerService, | ||||||
operationStateService: OperationStateService, | ||||||
private eventEmitter: EventEmitter2, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
) { | ||||||
super(indexerService, operationStateService); | ||||||
|
||||||
|
@@ -123,6 +126,8 @@ export class BitcoinCoreProvider | |||||
|
||||||
state.indexedBlockHeight = height; | ||||||
await this.setState(state); | ||||||
|
||||||
this.eventEmitter.emit(INDEXED_BLOCK_EVENT, height); | ||||||
} | ||||||
} finally { | ||||||
this.isSyncing = false; | ||||||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need to emit |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
|
@@ -12,6 +12,7 @@ | |||||
} from '@/block-data-providers/esplora/interface'; | ||||||
import { TAPROOT_ACTIVATION_HEIGHT } from '@/common/constants'; | ||||||
import { Cron, CronExpression } from '@nestjs/schedule'; | ||||||
import { EventEmitter2 } from '@nestjs/event-emitter'; | ||||||
|
||||||
@Injectable() | ||||||
export class EsploraProvider | ||||||
|
@@ -29,6 +30,8 @@ | |||||
private readonly configService: ConfigService, | ||||||
indexerService: IndexerService, | ||||||
operationStateService: OperationStateService, | ||||||
private eventEmitter: EventEmitter2, | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
|
||||||
) { | ||||||
super(indexerService, operationStateService); | ||||||
|
||||||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
export const INDEXED_BLOCK_EVENT = 'INDEXED_BLOCK_EVENT'; |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
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}`); | ||
} | ||
|
||
// Method to broadcast silent block to all connected clients | ||
broadcastSilentBlock(silentBlock: Buffer) { | ||
this.server.clients.forEach((client: WebSocket) => { | ||
if (client.readyState === WebSocket.OPEN) { | ||
client.send(silentBlock.toString('hex')); // Send silent block as hex string | ||
} | ||
}); | ||
Comment on lines
+31
to
+35
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,12 +1,28 @@ | ||
import { Injectable } from '@nestjs/common'; | ||
import { Injectable, Logger } from '@nestjs/common'; | ||
import { Transaction } from '@/transactions/transaction.entity'; | ||
import { TransactionsService } from '@/transactions/transactions.service'; | ||
import { SILENT_PAYMENT_BLOCK_TYPE } from '@/common/constants'; | ||
import { encodeVarInt, varIntSize } from '@/common/common'; | ||
import { encodeVarInt, varIntSize, delay } from '@/common/common'; | ||
import { SilentBlocksGateway } from '@/silent-blocks/silent-blocks.gateway'; | ||
import { OnEvent } from '@nestjs/event-emitter'; | ||
import { INDEXED_BLOCK_EVENT } from '@/common/events'; | ||
|
||
@Injectable() | ||
export class SilentBlocksService { | ||
constructor(private readonly transactionsService: TransactionsService) {} | ||
private readonly logger = new Logger(SilentBlocksService.name); | ||
|
||
constructor( | ||
private readonly transactionsService: TransactionsService, | ||
private readonly silentBlocksGateway: SilentBlocksGateway, | ||
) {} | ||
|
||
@OnEvent(INDEXED_BLOCK_EVENT) | ||
async handleBlockIndexedEvent(blockHeight: number) { | ||
this.logger.debug(`New block indexed: ${blockHeight}`); | ||
await delay(1000); | ||
const silentBlock = await this.getSilentBlockByHeight(blockHeight); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are we adding a delay here? |
||
this.silentBlocksGateway.broadcastSilentBlock(silentBlock); | ||
} | ||
|
||
private getSilentBlockLength(transactions: Transaction[]): number { | ||
let length = 1 + varIntSize(transactions.length); // 1 byte for type + varint for transactions count | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
revert please