-
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
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
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,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); | ||
} | ||
} | ||
} | ||
} |
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); | ||
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?
Comment on lines
+19
to
+22
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. @seekersoftec , kindly research if we could use a superior way to handle the latency, we could use a while loop with a cut-off time, the cut-off which i believe may be quite unnecessary due to the operational model that requires such SiientBlock to be eventually available . |
||
const silentBlock = await this.getSilentBlockByHeight(blockHeight); | ||
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.
We need to emit
INDEXED_BLOCK_EVENT
event in this provider as well