diff --git a/.env.example b/.env.example index b64ca4b..0c1ae87 100644 --- a/.env.example +++ b/.env.example @@ -32,13 +32,6 @@ NETWORK=mainnet API_SECURE=false -ENABLE_SOLO=false -ENABLE_PROXY=true - -BRAIINS_ACCESS_TOKEN= - -PROXY_PORT=3333 - #postgresql DB_HOST= DB_PORT= @@ -46,4 +39,4 @@ DB_USERNAME= DB_PASSWORD= DB_DATABASE= -PRODUCTION=false \ No newline at end of file +PRODUCTION=false diff --git a/full-setup/public-pool-mainnet.env b/full-setup/public-pool-mainnet.env index 3a5592c..587dc79 100644 --- a/full-setup/public-pool-mainnet.env +++ b/full-setup/public-pool-mainnet.env @@ -25,10 +25,3 @@ DEV_FEE_ADDRESS= NETWORK=mainnet API_SECURE=false - -ENABLE_SOLO=true -ENABLE_PROXY=false - -BRAIINS_ACCESS_TOKEN= - -PROXY_PORT=3333 diff --git a/full-setup/public-pool-regtest.env b/full-setup/public-pool-regtest.env index d3ff69c..7f21887 100644 --- a/full-setup/public-pool-regtest.env +++ b/full-setup/public-pool-regtest.env @@ -25,10 +25,3 @@ DEV_FEE_ADDRESS= NETWORK=regtest API_SECURE=false - -ENABLE_SOLO=true -ENABLE_PROXY=false - -BRAIINS_ACCESS_TOKEN= - -PROXY_PORT=3333 diff --git a/full-setup/public-pool-testnet.env b/full-setup/public-pool-testnet.env index d548f0b..506d5fb 100644 --- a/full-setup/public-pool-testnet.env +++ b/full-setup/public-pool-testnet.env @@ -24,11 +24,4 @@ DEV_FEE_ADDRESS= # mainnet | testnet NETWORK=testnet -API_SECURE=false - -ENABLE_SOLO=true -ENABLE_PROXY=false - -BRAIINS_ACCESS_TOKEN= - -PROXY_PORT=3333 +API_SECURE=false \ No newline at end of file diff --git a/src/app.module.ts b/src/app.module.ts index c1f812c..e763666 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -32,7 +32,6 @@ import { BraiinsService } from './services/braiins.service'; import { BTCPayService } from './services/btc-pay.service'; import { DiscordService } from './services/discord.service'; import { NotificationService } from './services/notification.service'; -import { ProxyService } from './services/proxy.service'; import { StratumV1JobsService } from './services/stratum-v1-jobs.service'; import { StratumV1Service } from './services/stratum-v1.service'; import { TelegramService } from './services/telegram.service'; @@ -101,7 +100,6 @@ const ORMModules = [ NotificationService, BitcoinAddressValidator, StratumV1JobsService, - ProxyService, BTCPayService, BraiinsService ], diff --git a/src/models/ProxyClient.ts b/src/models/ProxyClient.ts deleted file mode 100644 index 0bae955..0000000 --- a/src/models/ProxyClient.ts +++ /dev/null @@ -1,235 +0,0 @@ -import Big from 'big.js'; -import { Block } from 'bitcoinjs-lib'; -import * as bitcoinjs from 'bitcoinjs-lib'; -import { plainToInstance } from 'class-transformer'; -import { validate, ValidatorOptions } from 'class-validator'; -import { Socket } from 'net'; - -import { eRequestMethod } from './enums/eRequestMethod'; -import { eResponseMethod } from './enums/eResponseMethod'; -import { IMiningNotify } from './stratum-messages/IMiningNotify'; -import { MiningSubmitMessage } from './stratum-messages/MiningSubmitMessage'; - -export class ProxyClient { - - - public braiinsSocket: Socket; - public serverDifficulty: number; - public jobs: { [jobId: string]: IMiningNotify } = {}; - private extraNonce: string; - - constructor(public socket: Socket) { - this.braiinsSocket = new Socket(); - this.braiinsSocket.connect(3333, 'us-east.stratum.braiins.com', () => { - console.log('Connected to braiins'); - }); - - this.braiinsSocket.on('data', (data) => { - data.toString() - .split('\n') - .filter(m => m.length > 0) - .forEach(async (m) => { - try { - await this.toClient(m); - } catch (e) { - await socket.end(); - console.error(e); - } - }) - - }); - - this.braiinsSocket.on('close', () => { - console.log('Braiins closed connection'); - socket.end(); - }); - - - socket.on('error', async (error: Error) => { }); - - socket.on('data', (data: Buffer) => { - data.toString() - .split('\n') - .filter(m => m.length > 0) - .forEach(async (m) => { - try { - await this.toServer(m); - } catch (e) { - await socket.end(); - console.error(e); - } - }) - }); - socket.on('close', () => { - console.log('Client closed connection'); - this.braiinsSocket.end(); - }) - } - - private async toClient(message: string) { - let parsedMessage: IMiningNotify | any; - try { - parsedMessage = JSON.parse(message); - } catch (e) { - //console.log("Invalid JSON"); - await this.socket.end(); - return; - } - - if (parsedMessage.method == null && Array.isArray(parsedMessage.result)) { - this.extraNonce = parsedMessage.result[1]; - } - - switch (parsedMessage.method) { - case eResponseMethod.SET_DIFFICULTY: { - this.serverDifficulty = parsedMessage.params[0]; - parsedMessage.params[0] = 512; - message = JSON.stringify(parsedMessage); - break; - } - case eResponseMethod.MINING_NOTIFY: { - // clear jobs - if ((parsedMessage as IMiningNotify).params[8]) { - this.jobs = {}; - } - this.jobs[parsedMessage.params[0]] = parsedMessage; - break; - } - } - console.log('Server:'); - console.log(message); - this.socket.write(message + `\n`); - } - private async toServer(message: string) { - - let parsedMessage = null; - try { - parsedMessage = JSON.parse(message); - } catch (e) { - //console.log("Invalid JSON"); - await this.socket.end(); - return; - } - - switch (parsedMessage.method) { - - case eRequestMethod.AUTHORIZE: { - parsedMessage.params[0] = 'battlechicken'; - message = JSON.stringify(parsedMessage); - break; - } - case eRequestMethod.SUBMIT: { - parsedMessage.params[0] = 'battlechicken'; - - message = JSON.stringify(parsedMessage); - - const miningSubmitMessage = plainToInstance( - MiningSubmitMessage, - parsedMessage, - ); - - const validatorOptions: ValidatorOptions = { - whitelist: true, - forbidNonWhitelisted: true, - }; - - const errors = await validate(miningSubmitMessage, validatorOptions); - - if (errors.length === 0) { - this.checkSubmission(miningSubmitMessage); - } - else { - console.log('parsing error') - } - break; - } - } - console.log('Client:'); - console.log(message); - this.braiinsSocket.write(message + '\n'); - - } - - private checkSubmission(submission: MiningSubmitMessage) { - console.log('Submission:'); - console.log(submission); - - const job = this.jobs[submission.jobId]; - if (job == null) { - console.log('Job not found'); - return; - } - - const versionMask = parseInt(submission.versionMask, 16); - - const block = new Block(); - - block.version = parseInt(job.params[5], 16); - if (submission.versionMask !== undefined && versionMask != 0) { - block.version = (block.version ^ versionMask); - } - - const prevHash = this.swapEndianWords(job.params[1]); - block.prevHash = Buffer.from(prevHash, 'hex'); - const coinbase = Buffer.from(`${job.params[2]}${this.extraNonce}${submission.extraNonce2}${job.params[3]}`, 'hex'); - const coinbaseHash = bitcoinjs.crypto.hash256(coinbase); - block.merkleRoot = this.calculateMerkleRootHash(coinbaseHash, job.params[4]); - block.timestamp = parseInt(submission.ntime, 16); - block.bits = parseInt(job.params[6], 16); - block.nonce = parseInt(submission.nonce, 16); - - const header = block.toBuffer(true); - - const diff = this.calculateDifficulty(header); - - console.log(`DIFFICULTY: ${diff.submissionDifficulty}`) - } - - - private calculateMerkleRootHash(newRoot: Buffer, merkleBranches: string[]): Buffer { - - const bothMerkles = Buffer.alloc(64); - - bothMerkles.set(newRoot); - - for (let i = 0; i < merkleBranches.length; i++) { - bothMerkles.set(Buffer.from(merkleBranches[i], 'hex'), 32); - newRoot = bitcoinjs.crypto.hash256(bothMerkles); - bothMerkles.set(newRoot); - } - - return bothMerkles.subarray(0, 32) - } - - private calculateDifficulty(header: Buffer): { submissionDifficulty: number, submissionHash: string } { - - const hashResult = bitcoinjs.crypto.hash256(header); - - let s64 = this.le256todouble(hashResult); - - const truediffone = Big('26959535291011309493156476344723991336010898738574164086137773096960'); - const difficulty = truediffone.div(s64.toString()); - return { submissionDifficulty: difficulty.toNumber(), submissionHash: hashResult.toString('hex') }; - } - - private le256todouble(target: Buffer): bigint { - - const number = target.reduceRight((acc, byte) => { - // Shift the number 8 bits to the left and OR with the current byte - return (acc << BigInt(8)) | BigInt(byte); - }, BigInt(0)); - - return number; - } - - private swapEndianWords(str: string) { - const hexGroups = str.match(/.{1,8}/g); - // Reverse each group and concatenate them - const reversedHexString = hexGroups.reduce((pre, cur, indx, arr) => { - const reversed = cur.match(/.{2}/g).reverse(); - return `${pre}${reversed.join('')}`; - }, ''); - return reversedHexString; - } - -} \ No newline at end of file diff --git a/src/services/app.service.ts b/src/services/app.service.ts index 0da6ddb..e055ebb 100644 --- a/src/services/app.service.ts +++ b/src/services/app.service.ts @@ -22,7 +22,7 @@ export class AppService implements OnModuleInit { } async onModuleInit() { - if (process.env.ENABLE_SOLO == 'true' && (process.env.NODE_APP_INSTANCE == null || process.env.NODE_APP_INSTANCE == '0')) { + if (process.env.NODE_APP_INSTANCE == null || process.env.NODE_APP_INSTANCE == '0') { setInterval(async () => { await this.deleteOldStatistics(); diff --git a/src/services/proxy.service.ts b/src/services/proxy.service.ts deleted file mode 100644 index fe93bd2..0000000 --- a/src/services/proxy.service.ts +++ /dev/null @@ -1,29 +0,0 @@ -import { Injectable, OnModuleInit } from '@nestjs/common'; -import { Server, Socket } from 'net'; - -import { ProxyClient } from '../models/ProxyClient'; - -@Injectable() -export class ProxyService implements OnModuleInit { - - constructor() { - - } - - async onModuleInit(): Promise { - if (process.env.ENABLE_PROXY == 'true') { - console.log('Connecting to braiins'); - - - const proxyServer = new Server(async (socket: Socket) => { - const proxyClient = new ProxyClient(socket); - }); - - proxyServer.listen(process.env.PROXY_PORT, () => { - console.log(`Proxy server is listening on port ${process.env.PROXY_PORT}`); - }); - } - } - - -} \ No newline at end of file diff --git a/src/services/stratum-v1.service.ts b/src/services/stratum-v1.service.ts index 2f63d48..a01bb02 100644 --- a/src/services/stratum-v1.service.ts +++ b/src/services/stratum-v1.service.ts @@ -30,15 +30,12 @@ export class StratumV1Service implements OnModuleInit { async onModuleInit(): Promise { - if (process.env.ENABLE_SOLO == 'true') { - //await this.clientStatisticsService.deleteAll(); if (process.env.NODE_APP_INSTANCE == '0') { await this.clientService.deleteAll(); } this.startSocketServer(); - } } private startSocketServer() {