remove dead code

This commit is contained in:
Benjamin Wilson 2024-07-03 14:28:28 -04:00
parent 76e2426c30
commit 917d312211
9 changed files with 2 additions and 299 deletions

View File

@ -34,10 +34,3 @@ DEV_FEE_ADDRESS=
NETWORK=mainnet
API_SECURE=false
ENABLE_SOLO=false
ENABLE_PROXY=true
BRAIINS_ACCESS_TOKEN=
PROXY_PORT=3333

View File

@ -25,10 +25,3 @@ DEV_FEE_ADDRESS=
NETWORK=mainnet
API_SECURE=false
ENABLE_SOLO=true
ENABLE_PROXY=false
BRAIINS_ACCESS_TOKEN=
PROXY_PORT=3333

View File

@ -25,10 +25,3 @@ DEV_FEE_ADDRESS=
NETWORK=regtest
API_SECURE=false
ENABLE_SOLO=true
ENABLE_PROXY=false
BRAIINS_ACCESS_TOKEN=
PROXY_PORT=3333

View File

@ -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

View File

@ -21,7 +21,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';
@ -68,7 +67,6 @@ const ORMModules = [
NotificationService,
BitcoinAddressValidator,
StratumV1JobsService,
ProxyService,
BTCPayService,
BraiinsService
],

View File

@ -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;
}
}

View File

@ -30,7 +30,7 @@ export class AppService implements OnModuleInit {
// //6Gb
// await this.dataSource.query(`PRAGMA mmap_size = 6000000000;`);
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();

View File

@ -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<void> {
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}`);
});
}
}
}

View File

@ -30,8 +30,6 @@ export class StratumV1Service implements OnModuleInit {
async onModuleInit(): Promise<void> {
if (process.env.ENABLE_SOLO == 'true') {
//await this.clientStatisticsService.deleteAll();
if (process.env.NODE_APP_INSTANCE == '0') {
await this.clientService.deleteAll();
}
@ -39,7 +37,6 @@ export class StratumV1Service implements OnModuleInit {
this.startSocketServer();
}, 1000 * 10)
}
}
private startSocketServer() {