client unit tests

This commit is contained in:
Ben Wilson
2023-07-16 11:34:37 -04:00
parent e001383484
commit c81b894b4c
8 changed files with 268 additions and 35 deletions
+5 -5
View File
@@ -15,11 +15,11 @@ export class BitcoinRpcService {
public newBlock$ = this._newBlock$.pipe(filter(block => block != null));
constructor(private readonly configService: ConfigService) {
const url = configService.get('BITCOIN_RPC_URL');
const user = configService.get('BITCOIN_RPC_USER');
const pass = configService.get('BITCOIN_RPC_PASSWORD');
const port = parseInt(configService.get('BITCOIN_RPC_PORT'));
const timeout = parseInt(configService.get('BITCOIN_RPC_TIMEOUT'));
const url = this.configService.get('BITCOIN_RPC_URL');
const user = this.configService.get('BITCOIN_RPC_USER');
const pass = this.configService.get('BITCOIN_RPC_PASSWORD');
const port = parseInt(this.configService.get('BITCOIN_RPC_PORT'));
const timeout = parseInt(this.configService.get('BITCOIN_RPC_TIMEOUT'));
this.client = new RPCClient({ url, port, timeout, user, pass });
+1 -3
View File
@@ -1,5 +1,5 @@
import { Injectable } from '@nestjs/common';
import { from, map, Observable, shareReplay, switchMap, tap } from 'rxjs';
import { from, map, Observable, shareReplay, switchMap } from 'rxjs';
import { IBlockTemplate } from '../models/bitcoin-rpc/IBlockTemplate';
import { BitcoinRpcService } from './bitcoin-rpc.service';
@@ -7,14 +7,12 @@ import { BitcoinRpcService } from './bitcoin-rpc.service';
@Injectable()
export class BlockTemplateService {
public currentBlockTemplate: IBlockTemplate;
public currentBlockTemplate$: Observable<{ blockTemplate: IBlockTemplate }>;
constructor(private readonly bitcoinRpcService: BitcoinRpcService) {
this.currentBlockTemplate$ = this.bitcoinRpcService.newBlock$.pipe(
switchMap((miningInfo) => from(this.bitcoinRpcService.getBlockTemplate()).pipe(map(blockTemplate => { return { miningInfo, blockTemplate } }))),
tap(({ blockTemplate }) => this.currentBlockTemplate = blockTemplate),
shareReplay({ refCount: true, bufferSize: 1 })
);
}
+6 -2
View File
@@ -62,16 +62,20 @@ export class StratumV1Service implements OnModuleInit {
promiseSocket.socket.on('end', async (error: Error) => {
// Handle socket disconnection
client.destroy();
await this.clientService.delete(client.extraNonce);
promiseSocket.destroy();
await this.clientService.delete(client.extraNonceAndSessionId);
const clientCount = await this.clientService.connectedClientCount();
console.log(`Client disconnected: ${promiseSocket.socket.remoteAddress}, ${clientCount} total clients`);
});
promiseSocket.socket.on('error', async (error: Error) => {
client.destroy();
await this.clientService.delete(client.extraNonce);
promiseSocket.destroy();
await this.clientService.delete(client.extraNonceAndSessionId);
const clientCount = await this.clientService.connectedClientCount();
console.error(`Socket error:`, error);
console.log(`Client disconnected: ${promiseSocket.socket.remoteAddress}, ${clientCount} total clients`);