From ddc7907919139dcae285aca4552b0f0e17a3eb9d Mon Sep 17 00:00:00 2001 From: Ben Wilson Date: Wed, 12 Jul 2023 22:53:26 -0400 Subject: [PATCH] found blocks services and tables --- src/ORM/blocks/blocks.entity.ts | 26 ++++++++++++++++ src/ORM/blocks/blocks.module.ts | 14 +++++++++ src/ORM/blocks/blocks.service.ts | 49 ++++++++++++++++++++++++++++++ src/app.controller.ts | 11 +++++-- src/app.module.ts | 4 ++- src/models/StratumV1Client.ts | 11 ++++++- src/services/stratum-v1.service.ts | 7 +++-- 7 files changed, 115 insertions(+), 7 deletions(-) diff --git a/src/ORM/blocks/blocks.entity.ts b/src/ORM/blocks/blocks.entity.ts index e69de29..ae1fe99 100644 --- a/src/ORM/blocks/blocks.entity.ts +++ b/src/ORM/blocks/blocks.entity.ts @@ -0,0 +1,26 @@ +import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm'; + +import { TrackedEntity } from '../utils/TrackedEntity.entity'; + +@Entity() +export class BlocksEntity extends TrackedEntity { + + @PrimaryGeneratedColumn() + id: number; + + @Column() + height: number; + + @Column({ length: 62, type: 'varchar' }) + minerAddress: string; + + @Column() + worker: string; + + @Column({ length: 8, type: 'varchar' }) + sessionId: string; + + @Column() + blockData: string; + +} \ No newline at end of file diff --git a/src/ORM/blocks/blocks.module.ts b/src/ORM/blocks/blocks.module.ts index e69de29..e090f1d 100644 --- a/src/ORM/blocks/blocks.module.ts +++ b/src/ORM/blocks/blocks.module.ts @@ -0,0 +1,14 @@ +import { Global, Module } from '@nestjs/common'; +import { TypeOrmModule } from '@nestjs/typeorm'; + +import { BlocksEntity } from './blocks.entity'; +import { BlocksService } from './blocks.service'; + + +@Global() +@Module({ + imports: [TypeOrmModule.forFeature([BlocksEntity])], + providers: [BlocksService], + exports: [TypeOrmModule, BlocksService], +}) +export class BlocksModule { } \ No newline at end of file diff --git a/src/ORM/blocks/blocks.service.ts b/src/ORM/blocks/blocks.service.ts index e69de29..bb9964b 100644 --- a/src/ORM/blocks/blocks.service.ts +++ b/src/ORM/blocks/blocks.service.ts @@ -0,0 +1,49 @@ +import { Injectable } from '@nestjs/common'; +import { InjectRepository } from '@nestjs/typeorm'; +import { DataSource, Repository } from 'typeorm'; + +import { BlocksEntity } from './blocks.entity'; + + +@Injectable() +export class BlocksService { + + constructor( + + private dataSource: DataSource, + @InjectRepository(BlocksEntity) + private blocksRepository: Repository, + ) { + + } + + + public async save(block: Partial) { + await this.blocksRepository.save(block); + } + + public async getFoundBlocks() { + return await this.blocksRepository.find({ + select: { + height: true, + minerAddress: true, + worker: true, + sessionId: true + } + }); + } + + public async getFoundBlocksByAddress(address: string) { + return await this.blocksRepository.find({ + select: { + height: true, + minerAddress: true, + worker: true, + sessionId: true + }, + where: { + minerAddress: address + } + }); + } +} \ No newline at end of file diff --git a/src/app.controller.ts b/src/app.controller.ts index e83af13..ead0ce8 100644 --- a/src/app.controller.ts +++ b/src/app.controller.ts @@ -2,6 +2,7 @@ import { CACHE_MANAGER } from '@nestjs/cache-manager'; import { Controller, Get, Inject } from '@nestjs/common'; import { Cache } from 'cache-manager'; +import { BlocksService } from './ORM/blocks/blocks.service'; import { ClientStatisticsService } from './ORM/client-statistics/client-statistics.service'; import { ClientService } from './ORM/client/client.service'; @@ -10,7 +11,8 @@ export class AppController { constructor( @Inject(CACHE_MANAGER) private cacheManager: Cache, private clientService: ClientService, - private clientStatisticsService: ClientStatisticsService + private clientStatisticsService: ClientStatisticsService, + private blocksService: BlocksService ) { } @Get('info') @@ -26,10 +28,13 @@ export class AppController { const chartData = await this.clientStatisticsService.getChartDataForSite(); - await this.cacheManager.set(CACHE_KEY, chartData, 600) + await this.cacheManager.set(CACHE_KEY, chartData, 600); + + const blockData = await this.blocksService.getFoundBlocks(); return { - chartData + chartData, + blockData }; } diff --git a/src/app.module.ts b/src/app.module.ts index 288ecf7..96d57c9 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -8,6 +8,7 @@ import { AppController } from './app.controller'; import { AddressController } from './controllers/address/address.controller'; import { ClientController } from './controllers/client/client.controller'; import { AddressSettingsModule } from './ORM/address-settings/address-settings.module'; +import { BlocksModule } from './ORM/blocks/blocks.module'; import { ClientStatisticsModule } from './ORM/client-statistics/client-statistics.module'; import { ClientModule } from './ORM/client/client.module'; import { TelegramSubscriptionsModule } from './ORM/telegram-subscriptions/telegram-subscriptions.module'; @@ -23,7 +24,8 @@ const ORMModules = [ ClientStatisticsModule, ClientModule, AddressSettingsModule, - TelegramSubscriptionsModule + TelegramSubscriptionsModule, + BlocksModule ] @Module({ diff --git a/src/models/StratumV1Client.ts b/src/models/StratumV1Client.ts index 55458b5..19a38a6 100644 --- a/src/models/StratumV1Client.ts +++ b/src/models/StratumV1Client.ts @@ -8,6 +8,7 @@ import PromiseSocket from 'promise-socket'; import { combineLatest, firstValueFrom, interval, startWith, takeUntil } from 'rxjs'; import { NotificationService } from 'src/services/notification.service'; +import { BlocksService } from '../ORM/blocks/blocks.service'; import { ClientStatisticsService } from '../ORM/client-statistics/client-statistics.service'; import { ClientEntity } from '../ORM/client/client.entity'; import { ClientService } from '../ORM/client/client.service'; @@ -50,7 +51,8 @@ export class StratumV1Client extends EasyUnsubscribe { private readonly bitcoinRpcService: BitcoinRpcService, private readonly clientService: ClientService, private readonly clientStatisticsService: ClientStatisticsService, - private readonly notificationService: NotificationService + private readonly notificationService: NotificationService, + private readonly blocksService: BlocksService ) { super(); @@ -359,6 +361,13 @@ export class StratumV1Client extends EasyUnsubscribe { console.log('!!! BLOCK FOUND !!!'); const blockHex = updatedJobBlock.toHex(false); const result = await this.bitcoinRpcService.SUBMIT_BLOCK(blockHex); + await this.blocksService.save({ + height: job.blockTemplate.height, + minerAddress: this.clientAuthorization.address, + worker: this.clientAuthorization.worker, + sessionId: this.extraNonce, + blockData: blockHex + }) await this.notificationService.notifySubscribersBlockFound(this.clientAuthorization.address, job.blockTemplate.height, updatedJobBlock, result); } try { diff --git a/src/services/stratum-v1.service.ts b/src/services/stratum-v1.service.ts index b2d9cd3..f3fcc24 100644 --- a/src/services/stratum-v1.service.ts +++ b/src/services/stratum-v1.service.ts @@ -3,6 +3,7 @@ import { Server, Socket } from 'net'; import { PromiseSocket } from 'promise-socket'; import { StratumV1Client } from '../models/StratumV1Client'; +import { BlocksService } from '../ORM/blocks/blocks.service'; import { ClientStatisticsService } from '../ORM/client-statistics/client-statistics.service'; import { ClientService } from '../ORM/client/client.service'; import { BitcoinRpcService } from './bitcoin-rpc.service'; @@ -19,7 +20,8 @@ export class StratumV1Service implements OnModuleInit { private readonly blockTemplateService: BlockTemplateService, private readonly clientService: ClientService, private readonly clientStatisticsService: ClientStatisticsService, - private readonly notificationService: NotificationService + private readonly notificationService: NotificationService, + private readonly blocksService: BlocksService ) { } @@ -44,7 +46,8 @@ export class StratumV1Service implements OnModuleInit { this.bitcoinRpcService, this.clientService, this.clientStatisticsService, - this.notificationService + this.notificationService, + this.blocksService );