mirror of
https://github.com/benjamin-wilson/public-pool.git
synced 2026-09-29 17:15:03 -07:00
cleanup and support for all address types
This commit is contained in:
@@ -1,4 +1,4 @@
|
||||
import { Column, Entity, PrimaryGeneratedColumn } from 'typeorm';
|
||||
import { Column, Entity, Index, PrimaryGeneratedColumn } from 'typeorm';
|
||||
|
||||
import { DateTimeTransformer } from '../utils/DateTimeTransformer';
|
||||
import { TrackedEntity } from '../utils/TrackedEntity.entity';
|
||||
@@ -15,9 +15,11 @@ export class ClientStatisticsEntity extends TrackedEntity {
|
||||
@Column()
|
||||
clientName: string;
|
||||
|
||||
@Index()
|
||||
@Column({ length: 8, type: 'varchar' })
|
||||
sessionId: string;
|
||||
|
||||
@Index()
|
||||
@Column({
|
||||
type: 'datetime',
|
||||
transformer: new DateTimeTransformer()
|
||||
|
||||
@@ -19,6 +19,17 @@ export class ClientStatisticsService {
|
||||
return await this.clientStatisticsRepository.save(clientStatistic);
|
||||
}
|
||||
|
||||
public async deleteOldStatistics() {
|
||||
const oneDayAgo = new Date(Date.now() - 24 * 60 * 60 * 1000);
|
||||
|
||||
return await this.clientStatisticsRepository
|
||||
.createQueryBuilder()
|
||||
.delete()
|
||||
.from(ClientStatisticsEntity)
|
||||
.where('time < :time', { time: oneDayAgo })
|
||||
.execute();
|
||||
}
|
||||
|
||||
public async getHashRateForAddress(address: string) {
|
||||
|
||||
const query = `
|
||||
|
||||
@@ -25,6 +25,19 @@ export class ClientService {
|
||||
return await this.clientRepository.softDelete({ sessionId });
|
||||
}
|
||||
|
||||
public async deleteOldClients() {
|
||||
|
||||
const oneDayAgo = new Date(Date.now() - 24 * 60 * 60 * 1000);
|
||||
|
||||
return await this.clientRepository
|
||||
.createQueryBuilder()
|
||||
.delete()
|
||||
.from(ClientEntity)
|
||||
.where('deletedAt < :time', { time: oneDayAgo })
|
||||
.execute();
|
||||
|
||||
}
|
||||
|
||||
public async updateBestDifficulty(sessionId: string, bestDifficulty: number) {
|
||||
return await this.clientRepository.update({ sessionId }, { bestDifficulty });
|
||||
}
|
||||
|
||||
@@ -1,15 +1,13 @@
|
||||
import { Controller, Get, NotFoundException, Param } from '@nestjs/common';
|
||||
|
||||
import { AppService } from './app.service';
|
||||
import { ClientStatisticsService } from './ORM/client-statistics/client-statistics.service';
|
||||
import { ClientService } from './ORM/client/client.service';
|
||||
import { StratumV1Service } from './stratum-v1.service';
|
||||
|
||||
|
||||
|
||||
@Controller()
|
||||
export class AppController {
|
||||
constructor(
|
||||
private readonly appService: AppService,
|
||||
private readonly stratumV1Service: StratumV1Service,
|
||||
private readonly clientService: ClientService,
|
||||
private readonly clientStatisticsService: ClientStatisticsService
|
||||
) { }
|
||||
|
||||
+10
-5
@@ -1,14 +1,16 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { ConfigModule } from '@nestjs/config';
|
||||
import { ScheduleModule } from '@nestjs/schedule';
|
||||
import { TypeOrmModule } from '@nestjs/typeorm';
|
||||
|
||||
import { AppController } from './app.controller';
|
||||
import { AppService } from './app.service';
|
||||
import { BitcoinRpcService } from './bitcoin-rpc.service';
|
||||
import { BlockTemplateService } from './BlockTemplateService';
|
||||
import { ClientStatisticsModule } from './ORM/client-statistics/client-statistics.module';
|
||||
import { ClientModule } from './ORM/client/client.module';
|
||||
import { StratumV1Service } from './stratum-v1.service';
|
||||
import { BitcoinRpcService } from './services/bitcoin-rpc.service';
|
||||
import { BlockTemplateService } from './services/block-template.service';
|
||||
import { CleanupService } from './services/cleanup.service';
|
||||
import { StratumV1Service } from './services/stratum-v1.service';
|
||||
|
||||
|
||||
const ORMModules = [
|
||||
ClientStatisticsModule,
|
||||
@@ -23,12 +25,15 @@ const ORMModules = [
|
||||
database: './DB/public-pool.sqlite',
|
||||
synchronize: true,
|
||||
autoLoadEntities: true,
|
||||
logging: false
|
||||
}),
|
||||
ScheduleModule.forRoot(),
|
||||
...ORMModules
|
||||
],
|
||||
controllers: [AppController],
|
||||
providers: [
|
||||
AppService,
|
||||
|
||||
CleanupService,
|
||||
StratumV1Service,
|
||||
BitcoinRpcService,
|
||||
BlockTemplateService
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
|
||||
@Injectable()
|
||||
export class AppService {
|
||||
getHello(): string {
|
||||
return 'Hello World!';
|
||||
}
|
||||
}
|
||||
+26
-2
@@ -1,3 +1,4 @@
|
||||
import { AddressType, getAddressInfo } from 'bitcoin-address-validation';
|
||||
import * as bitcoinjs from 'bitcoinjs-lib';
|
||||
import * as crypto from 'crypto';
|
||||
import { MerkleTree } from 'merkletreejs';
|
||||
@@ -134,10 +135,9 @@ export class MiningJob {
|
||||
let rewardBalance = reward;
|
||||
|
||||
addresses.forEach(recipientAddress => {
|
||||
const scriptPubKey = bitcoinjs.payments.p2wpkh({ address: recipientAddress.address, network: bitcoinjs.networks.testnet });
|
||||
const amount = Math.floor((recipientAddress.percent / 100) * reward);
|
||||
rewardBalance -= amount;
|
||||
coinbaseTransaction.addOutput(scriptPubKey.output, amount);
|
||||
coinbaseTransaction.addOutput(this.getPaymentScript(recipientAddress.address), amount);
|
||||
})
|
||||
|
||||
//Add any remaining sats from the Math.floor
|
||||
@@ -151,6 +151,30 @@ export class MiningJob {
|
||||
return coinbaseTransaction;
|
||||
}
|
||||
|
||||
private getPaymentScript(address: string): Buffer {
|
||||
const addressInfo = getAddressInfo(address);
|
||||
switch (addressInfo.type) {
|
||||
case AddressType.p2wpkh: {
|
||||
return bitcoinjs.payments.p2wpkh({ address, network: bitcoinjs.networks.testnet }).output;
|
||||
}
|
||||
case AddressType.p2pkh: {
|
||||
return bitcoinjs.payments.p2pkh({ address }).output;;
|
||||
}
|
||||
case AddressType.p2sh: {
|
||||
return bitcoinjs.payments.p2sh({ address }).output;;
|
||||
}
|
||||
case AddressType.p2tr: {
|
||||
return bitcoinjs.payments.p2tr({ address }).output;;
|
||||
}
|
||||
case AddressType.p2wsh: {
|
||||
return bitcoinjs.payments.p2wsh({ address }).output;
|
||||
}
|
||||
default: {
|
||||
return Buffer.alloc(0);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private sha256(data) {
|
||||
return crypto.createHash('sha256').update(data).digest()
|
||||
}
|
||||
|
||||
@@ -5,12 +5,12 @@ import * as crypto from 'crypto';
|
||||
import { Socket } from 'net';
|
||||
import { combineLatest, interval, startWith } from 'rxjs';
|
||||
|
||||
import { BitcoinRpcService } from '../bitcoin-rpc.service';
|
||||
import { BlockTemplateService } from '../BlockTemplateService';
|
||||
import { ClientStatisticsService } from '../ORM/client-statistics/client-statistics.service';
|
||||
import { ClientEntity } from '../ORM/client/client.entity';
|
||||
import { ClientService } from '../ORM/client/client.service';
|
||||
import { StratumV1JobsService } from '../stratum-v1-jobs.service';
|
||||
import { BitcoinRpcService } from '../services/bitcoin-rpc.service';
|
||||
import { BlockTemplateService } from '../services/block-template.service';
|
||||
import { StratumV1JobsService } from '../services/stratum-v1-jobs.service';
|
||||
import { EasyUnsubscribe } from '../utils/AutoUnsubscribe';
|
||||
import { eRequestMethod } from './enums/eRequestMethod';
|
||||
import { MiningJob } from './MiningJob';
|
||||
@@ -22,6 +22,7 @@ import { SuggestDifficulty } from './stratum-messages/SuggestDifficultyMessage';
|
||||
import { StratumV1ClientStatistics } from './StratumV1ClientStatistics';
|
||||
|
||||
|
||||
|
||||
export class StratumV1Client extends EasyUnsubscribe {
|
||||
|
||||
private clientSubscription: SubscriptionMessage;
|
||||
|
||||
@@ -3,8 +3,9 @@ import { ConfigService } from '@nestjs/config';
|
||||
import { RPCClient } from 'rpc-bitcoin';
|
||||
import { BehaviorSubject, filter } from 'rxjs';
|
||||
|
||||
import { IBlockTemplate } from './models/bitcoin-rpc/IBlockTemplate';
|
||||
import { IMiningInfo } from './models/bitcoin-rpc/IMiningInfo';
|
||||
import { IBlockTemplate } from '../models/bitcoin-rpc/IBlockTemplate';
|
||||
import { IMiningInfo } from '../models/bitcoin-rpc/IMiningInfo';
|
||||
|
||||
|
||||
|
||||
@Injectable()
|
||||
@@ -1,8 +1,8 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { from, map, Observable, shareReplay, switchMap, tap } from 'rxjs';
|
||||
|
||||
import { IBlockTemplate } from '../models/bitcoin-rpc/IBlockTemplate';
|
||||
import { BitcoinRpcService } from './bitcoin-rpc.service';
|
||||
import { IBlockTemplate } from './models/bitcoin-rpc/IBlockTemplate';
|
||||
|
||||
@Injectable()
|
||||
export class BlockTemplateService {
|
||||
@@ -0,0 +1,27 @@
|
||||
import { Injectable, OnModuleInit } from '@nestjs/common';
|
||||
import { Cron, CronExpression } from '@nestjs/schedule';
|
||||
|
||||
import { ClientStatisticsService } from '../ORM/client-statistics/client-statistics.service';
|
||||
import { ClientService } from '../ORM/client/client.service';
|
||||
|
||||
@Injectable()
|
||||
export class CleanupService implements OnModuleInit {
|
||||
|
||||
constructor(
|
||||
private clientStatisticsService: ClientStatisticsService,
|
||||
private clientService: ClientService
|
||||
) {
|
||||
|
||||
}
|
||||
onModuleInit() {
|
||||
console.log('Cleanup service running.')
|
||||
}
|
||||
|
||||
@Cron(CronExpression.EVERY_HOUR)
|
||||
private async deleteOldStatistics() {
|
||||
const deletedStatistics = await this.clientStatisticsService.deleteOldStatistics();
|
||||
console.log(`Deleted ${deletedStatistics.affected} old statistics`);
|
||||
const deletedClients = await this.clientService.deleteOldClients();
|
||||
console.log(`Deleted ${deletedClients.affected} old clients`);
|
||||
}
|
||||
}
|
||||
@@ -1,4 +1,4 @@
|
||||
import { MiningJob } from './models/MiningJob';
|
||||
import { MiningJob } from '../models/MiningJob';
|
||||
|
||||
|
||||
export class StratumV1JobsService {
|
||||
@@ -1,11 +1,11 @@
|
||||
import { Injectable, OnModuleInit } from '@nestjs/common';
|
||||
import { Server, Socket } from 'net';
|
||||
|
||||
import { StratumV1Client } from '../models/StratumV1Client';
|
||||
import { ClientStatisticsService } from '../ORM/client-statistics/client-statistics.service';
|
||||
import { ClientService } from '../ORM/client/client.service';
|
||||
import { BitcoinRpcService } from './bitcoin-rpc.service';
|
||||
import { BlockTemplateService } from './BlockTemplateService';
|
||||
import { StratumV1Client } from './models/StratumV1Client';
|
||||
import { ClientStatisticsService } from './ORM/client-statistics/client-statistics.service';
|
||||
import { ClientService } from './ORM/client/client.service';
|
||||
import { BlockTemplateService } from './block-template.service';
|
||||
import { StratumV1JobsService } from './stratum-v1-jobs.service';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user