mirror of
https://github.com/benjamin-wilson/public-pool.git
synced 2026-09-29 09:05:06 -07:00
RPC load reduction
This commit is contained in:
Vendored
+2
-1
@@ -15,5 +15,6 @@
|
|||||||
"Satoshis",
|
"Satoshis",
|
||||||
"submitblock",
|
"submitblock",
|
||||||
"Tempalte"
|
"Tempalte"
|
||||||
]
|
],
|
||||||
|
"idf.portWin": "COM30"
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import { Column, Entity, PrimaryColumn } from 'typeorm';
|
||||||
|
|
||||||
|
@Entity()
|
||||||
|
export class RpcBlockEntity {
|
||||||
|
|
||||||
|
@PrimaryColumn()
|
||||||
|
blockHeight: number;
|
||||||
|
|
||||||
|
@Column({ nullable: true })
|
||||||
|
lockedBy?: string;
|
||||||
|
|
||||||
|
@Column({ nullable: true })
|
||||||
|
data?: string;
|
||||||
|
}
|
||||||
@@ -0,0 +1,14 @@
|
|||||||
|
import { Global, Module } from '@nestjs/common';
|
||||||
|
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||||
|
|
||||||
|
import { RpcBlockEntity } from './rpc-block.entity';
|
||||||
|
import { RpcBlockService } from './rpc-block.service';
|
||||||
|
|
||||||
|
|
||||||
|
@Global()
|
||||||
|
@Module({
|
||||||
|
imports: [TypeOrmModule.forFeature([RpcBlockEntity])],
|
||||||
|
providers: [RpcBlockService],
|
||||||
|
exports: [TypeOrmModule, RpcBlockService],
|
||||||
|
})
|
||||||
|
export class RpcBlocksModule { }
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
import { Injectable } from '@nestjs/common';
|
||||||
|
import { InjectRepository } from '@nestjs/typeorm';
|
||||||
|
import { Repository } from 'typeorm';
|
||||||
|
|
||||||
|
import { RpcBlockEntity } from './rpc-block.entity';
|
||||||
|
|
||||||
|
@Injectable()
|
||||||
|
export class RpcBlockService {
|
||||||
|
constructor(
|
||||||
|
@InjectRepository(RpcBlockEntity)
|
||||||
|
private rpcBlockRepository: Repository<RpcBlockEntity>
|
||||||
|
) {
|
||||||
|
|
||||||
|
}
|
||||||
|
public getBlock(blockHeight: number) {
|
||||||
|
return this.rpcBlockRepository.findOne({
|
||||||
|
where: { blockHeight }
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
public lockBlock(blockHeight: number, process: string) {
|
||||||
|
return this.rpcBlockRepository.save({ blockHeight, data: null, lockedBy: process });
|
||||||
|
}
|
||||||
|
|
||||||
|
public saveBlock(blockHeight: number, data: string) {
|
||||||
|
return this.rpcBlockRepository.update(blockHeight, { data })
|
||||||
|
}
|
||||||
|
}
|
||||||
+4
-1
@@ -13,6 +13,7 @@ import { AddressSettingsModule } from './ORM/address-settings/address-settings.m
|
|||||||
import { BlocksModule } from './ORM/blocks/blocks.module';
|
import { BlocksModule } from './ORM/blocks/blocks.module';
|
||||||
import { ClientStatisticsModule } from './ORM/client-statistics/client-statistics.module';
|
import { ClientStatisticsModule } from './ORM/client-statistics/client-statistics.module';
|
||||||
import { ClientModule } from './ORM/client/client.module';
|
import { ClientModule } from './ORM/client/client.module';
|
||||||
|
import { RpcBlocksModule } from './ORM/rpc-block/rpc-block.module';
|
||||||
import { TelegramSubscriptionsModule } from './ORM/telegram-subscriptions/telegram-subscriptions.module';
|
import { TelegramSubscriptionsModule } from './ORM/telegram-subscriptions/telegram-subscriptions.module';
|
||||||
import { AppService } from './services/app.service';
|
import { AppService } from './services/app.service';
|
||||||
import { BitcoinRpcService } from './services/bitcoin-rpc.service';
|
import { BitcoinRpcService } from './services/bitcoin-rpc.service';
|
||||||
@@ -25,12 +26,14 @@ import { StratumV1JobsService } from './services/stratum-v1-jobs.service';
|
|||||||
import { StratumV1Service } from './services/stratum-v1.service';
|
import { StratumV1Service } from './services/stratum-v1.service';
|
||||||
import { TelegramService } from './services/telegram.service';
|
import { TelegramService } from './services/telegram.service';
|
||||||
|
|
||||||
|
|
||||||
const ORMModules = [
|
const ORMModules = [
|
||||||
ClientStatisticsModule,
|
ClientStatisticsModule,
|
||||||
ClientModule,
|
ClientModule,
|
||||||
AddressSettingsModule,
|
AddressSettingsModule,
|
||||||
TelegramSubscriptionsModule,
|
TelegramSubscriptionsModule,
|
||||||
BlocksModule
|
BlocksModule,
|
||||||
|
RpcBlocksModule
|
||||||
]
|
]
|
||||||
|
|
||||||
@Module({
|
@Module({
|
||||||
|
|||||||
@@ -352,8 +352,7 @@ export class StratumV1Client {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
this.stratumSubscription = this.stratumV1JobsService.newMiningJob$.pipe(
|
this.stratumSubscription = this.stratumV1JobsService.newMiningJob$.subscribe(async (jobTemplate) => {
|
||||||
).subscribe(async (jobTemplate) => {
|
|
||||||
try {
|
try {
|
||||||
await this.sendNewMiningJob(jobTemplate);
|
await this.sendNewMiningJob(jobTemplate);
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
|||||||
@@ -2,10 +2,12 @@ import { Injectable } from '@nestjs/common';
|
|||||||
import { ConfigService } from '@nestjs/config';
|
import { ConfigService } from '@nestjs/config';
|
||||||
import { RPCClient } from 'rpc-bitcoin';
|
import { RPCClient } from 'rpc-bitcoin';
|
||||||
import { BehaviorSubject, filter, shareReplay } from 'rxjs';
|
import { BehaviorSubject, filter, shareReplay } from 'rxjs';
|
||||||
|
import { RpcBlockService } from 'src/ORM/rpc-block/rpc-block.service';
|
||||||
|
|
||||||
import { IBlockTemplate } from '../models/bitcoin-rpc/IBlockTemplate';
|
import { IBlockTemplate } from '../models/bitcoin-rpc/IBlockTemplate';
|
||||||
import { IMiningInfo } from '../models/bitcoin-rpc/IMiningInfo';
|
import { IMiningInfo } from '../models/bitcoin-rpc/IMiningInfo';
|
||||||
import * as zmq from 'zeromq';
|
|
||||||
|
// import * as zmq from 'zeromq';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class BitcoinRpcService {
|
export class BitcoinRpcService {
|
||||||
@@ -15,7 +17,11 @@ export class BitcoinRpcService {
|
|||||||
private _newBlock$: BehaviorSubject<IMiningInfo> = new BehaviorSubject(undefined);
|
private _newBlock$: BehaviorSubject<IMiningInfo> = new BehaviorSubject(undefined);
|
||||||
public newBlock$ = this._newBlock$.pipe(filter(block => block != null), shareReplay({ refCount: true, bufferSize: 1 }));
|
public newBlock$ = this._newBlock$.pipe(filter(block => block != null), shareReplay({ refCount: true, bufferSize: 1 }));
|
||||||
|
|
||||||
constructor(private readonly configService: ConfigService) {
|
constructor(
|
||||||
|
private readonly configService: ConfigService,
|
||||||
|
private rpcBlockService: RpcBlockService
|
||||||
|
) {
|
||||||
|
|
||||||
const url = this.configService.get('BITCOIN_RPC_URL');
|
const url = this.configService.get('BITCOIN_RPC_URL');
|
||||||
const user = this.configService.get('BITCOIN_RPC_USER');
|
const user = this.configService.get('BITCOIN_RPC_USER');
|
||||||
const pass = this.configService.get('BITCOIN_RPC_PASSWORD');
|
const pass = this.configService.get('BITCOIN_RPC_PASSWORD');
|
||||||
@@ -27,13 +33,13 @@ export class BitcoinRpcService {
|
|||||||
console.log('Bitcoin RPC connected');
|
console.log('Bitcoin RPC connected');
|
||||||
|
|
||||||
if (this.configService.get('BITCOIN_ZMQ_HOST')) {
|
if (this.configService.get('BITCOIN_ZMQ_HOST')) {
|
||||||
const sock = zmq.socket("sub");
|
// const sock = zmq.socket("sub");
|
||||||
sock.connect(this.configService.get('BITCOIN_ZMQ_HOST'));
|
// sock.connect(this.configService.get('BITCOIN_ZMQ_HOST'));
|
||||||
sock.subscribe("rawblock");
|
// sock.subscribe("rawblock");
|
||||||
sock.on("message", async (topic: Buffer, message: Buffer) => {
|
// sock.on("message", async (topic: Buffer, message: Buffer) => {
|
||||||
console.log("new block zmq");
|
// console.log("new block zmq");
|
||||||
this.pollMiningInfo();
|
// this.pollMiningInfo();
|
||||||
});
|
// });
|
||||||
this.pollMiningInfo();
|
this.pollMiningInfo();
|
||||||
} else {
|
} else {
|
||||||
setInterval(this.pollMiningInfo.bind(this), 500);
|
setInterval(this.pollMiningInfo.bind(this), 500);
|
||||||
@@ -49,18 +55,53 @@ export class BitcoinRpcService {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public async getBlockTemplate(): Promise<IBlockTemplate> {
|
private async waitForBlock(blockHeight: number) {
|
||||||
|
while (true) {
|
||||||
|
await new Promise(r => setTimeout(r, 100));
|
||||||
|
|
||||||
|
const block = await this.rpcBlockService.getBlock(blockHeight);
|
||||||
|
if (block != null && block.data != null) {
|
||||||
|
console.log('promise loop resolved');
|
||||||
|
return Promise.resolve(JSON.parse(block.data));
|
||||||
|
}
|
||||||
|
console.log('promise loop');
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public async getBlockTemplate(blockHeight: number): Promise<IBlockTemplate> {
|
||||||
let result: IBlockTemplate;
|
let result: IBlockTemplate;
|
||||||
try {
|
try {
|
||||||
result = await this.client.getblocktemplate({
|
|
||||||
template_request: {
|
const block = await this.rpcBlockService.getBlock(blockHeight);
|
||||||
rules: ['segwit'],
|
|
||||||
mode: 'template',
|
if (block != null && block.data != null) {
|
||||||
capabilities: ['serverlist', 'proposal']
|
return Promise.resolve(JSON.parse(block.data));
|
||||||
|
} else if (block == null) {
|
||||||
|
const { lockedBy } = await this.rpcBlockService.lockBlock(blockHeight, process.env.NODE_APP_INSTANCE);
|
||||||
|
|
||||||
|
if (lockedBy != process.env.NODE_APP_INSTANCE) {
|
||||||
|
await this.waitForBlock(blockHeight);
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
});
|
|
||||||
|
result = await this.client.getblocktemplate({
|
||||||
|
template_request: {
|
||||||
|
rules: ['segwit'],
|
||||||
|
mode: 'template',
|
||||||
|
capabilities: ['serverlist', 'proposal']
|
||||||
|
}
|
||||||
|
});
|
||||||
|
await this.rpcBlockService.saveBlock(blockHeight, JSON.stringify(result));
|
||||||
|
} else {
|
||||||
|
//wait for block
|
||||||
|
await this.waitForBlock(blockHeight);
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Error getblocktemplate:' ,e.message);
|
console.error('Error getblocktemplate:', e.message);
|
||||||
throw new Error('Error getblocktemplate');
|
throw new Error('Error getblocktemplate');
|
||||||
}
|
}
|
||||||
console.log(`getblocktemplate tx count: ${result.transactions.length}`);
|
console.log(`getblocktemplate tx count: ${result.transactions.length}`);
|
||||||
|
|||||||
@@ -44,7 +44,7 @@ export class StratumV1JobsService {
|
|||||||
|
|
||||||
this.newMiningJob$ = combineLatest([this.bitcoinRpcService.newBlock$, interval(60000).pipe(delay(this.delay), startWith(-1))]).pipe(
|
this.newMiningJob$ = combineLatest([this.bitcoinRpcService.newBlock$, interval(60000).pipe(delay(this.delay), startWith(-1))]).pipe(
|
||||||
switchMap(([miningInfo, interval]) => {
|
switchMap(([miningInfo, interval]) => {
|
||||||
return from(this.bitcoinRpcService.getBlockTemplate()).pipe(map((blockTemplate) => {
|
return from(this.bitcoinRpcService.getBlockTemplate(miningInfo.blocks)).pipe(map((blockTemplate) => {
|
||||||
return {
|
return {
|
||||||
blockTemplate,
|
blockTemplate,
|
||||||
interval
|
interval
|
||||||
|
|||||||
Reference in New Issue
Block a user