diff --git a/src/models/MiningJob.ts b/src/models/MiningJob.ts index 4bff540..0a64899 100644 --- a/src/models/MiningJob.ts +++ b/src/models/MiningJob.ts @@ -37,7 +37,7 @@ export class MiningJob { const coinbaseTransaction = this.createCoinbaseTransaction(payoutInformation, this.blockTemplate.coinbasevalue); this.block.transactions.unshift(coinbaseTransaction); - this.block.witnessCommit = bitcoinjs.Block.calculateMerkleRoot(this.block.transactions, true); + // this.block.witnessCommit = bitcoinjs.Block.calculateMerkleRoot(this.block.transactions, true); @@ -51,12 +51,12 @@ export class MiningJob { // 1-byte - OP_RETURN (0x6a) // 1-byte - Push the following 36 bytes (0x24) // 4-byte - Commitment header (0xaa21a9ed) - const segwitMagicBits = Buffer.from('aa21a9ed', 'hex'); + //const segwitMagicBits = Buffer.from('aa21a9ed', 'hex'); // 32-byte - Commitment hash: Double-SHA256(witness root hash|witness reserved value) - const commitmentHash = this.sha256(this.sha256(Buffer.concat([this.block.witnessCommit, coinbaseTransaction.ins[0].witness[0]]))); + // const commitmentHash = this.sha256(this.sha256(Buffer.concat([this.block.witnessCommit, coinbaseTransaction.ins[0].witness[0]]))); // 39th byte onwards: Optional data with no consensus meaning coinbaseTransaction.ins[0].script = Buffer.concat([Buffer.from([littleEndianBlockHeight.byteLength]), littleEndianBlockHeight, Buffer.from('00000000' + '00000000', 'hex')]); - coinbaseTransaction.addOutput(bitcoinjs.script.compile([bitcoinjs.opcodes.OP_RETURN, Buffer.concat([segwitMagicBits, commitmentHash])]), 0); + //coinbaseTransaction.addOutput(bitcoinjs.script.compile([bitcoinjs.opcodes.OP_RETURN, Buffer.concat([segwitMagicBits, commitmentHash])]), 0); // get the non-witness coinbase tx //@ts-ignore @@ -103,13 +103,14 @@ export class MiningJob { //recompute the roots testBlock.merkleRoot = this.calculateMerkleRootHash(testBlock.transactions[0].getHash(false), this.merkle_branch); - testBlock.witnessCommit = bitcoinjs.Block.calculateMerkleRoot(this.block.transactions, true); + testBlock.witnessCommit = bitcoinjs.Block.calculateMerkleRoot(testBlock.transactions, true); const segwitMagicBits = Buffer.from('aa21a9ed', 'hex'); // 32-byte - Commitment hash: Double-SHA256(witness root hash|witness reserved value) const commitmentHash = this.sha256(this.sha256(Buffer.concat([testBlock.witnessCommit, testBlock.transactions[0].ins[0].witness[0]]))); - testBlock.transactions[0].outs[testBlock.transactions[0].outs.length - 1].script = bitcoinjs.script.compile([bitcoinjs.opcodes.OP_RETURN, Buffer.concat([segwitMagicBits, commitmentHash])]) + // testBlock.transactions[0].outs[testBlock.transactions[0].outs.length - 1].script = bitcoinjs.script.compile([bitcoinjs.opcodes.OP_RETURN, Buffer.concat([segwitMagicBits, commitmentHash])]) + testBlock.transactions[0].addOutput(bitcoinjs.script.compile([bitcoinjs.opcodes.OP_RETURN, Buffer.concat([segwitMagicBits, commitmentHash])]), 0) testBlock.timestamp = timestamp; return testBlock; diff --git a/src/models/StratumV1Client.ts b/src/models/StratumV1Client.ts index 9e836da..389592a 100644 --- a/src/models/StratumV1Client.ts +++ b/src/models/StratumV1Client.ts @@ -346,8 +346,8 @@ export class StratumV1Client extends EasyUnsubscribe { if (submissionDifficulty >= job.networkDifficulty) { console.log('!!! BLOCK FOUND !!!'); const blockHex = updatedJobBlock.toHex(false); - this.bitcoinRpcService.SUBMIT_BLOCK(blockHex); - await this.notificationService.notifySubscribersBlockFound(this.clientAuthorization.address); + const result = await this.bitcoinRpcService.SUBMIT_BLOCK(blockHex); + await this.notificationService.notifySubscribersBlockFound(this.clientAuthorization.address, result); } try { await this.statistics.addSubmission(this.entity, submissionHash, this.sessionDifficulty); diff --git a/src/services/bitcoin-rpc.service.ts b/src/services/bitcoin-rpc.service.ts index b551570..3f3a04e 100644 --- a/src/services/bitcoin-rpc.service.ts +++ b/src/services/bitcoin-rpc.service.ts @@ -57,17 +57,20 @@ export class BitcoinRpcService { } - public async SUBMIT_BLOCK(hexdata: string) { + public async SUBMIT_BLOCK(hexdata: string): Promise { + let response: string = ''; try { - const res = await this.client.submitblock({ + response = await this.client.submitblock({ hexdata }); - console.log(`BLOCK SUBMISSION RESPONSE: ${res}`); + console.log(`BLOCK SUBMISSION RESPONSE: ${response}`); console.log(hexdata); - console.log(JSON.stringify(res)); + console.log(JSON.stringify(response)); } catch (e) { + response = e; console.log(`BLOCK SUBMISSION RESPONSE ERROR: ${e}`); } + return response; } } diff --git a/src/services/discord.service.ts b/src/services/discord.service.ts index 341b9b4..e0a36de 100644 --- a/src/services/discord.service.ts +++ b/src/services/discord.service.ts @@ -22,6 +22,7 @@ const commands = [ @Injectable() export class DiscordService implements OnModuleInit { + private token: string; private clientId: string; private guildId: string; @@ -37,6 +38,10 @@ export class DiscordService implements OnModuleInit { this.guildId = this.configService.get('DISCORD_BOT_GUILD_ID'); this.channelId = this.configService.get('DISCORD_BOT_CHANNEL_ID') + if (this.token.length < 1 || this.clientId.length < 1 || this.guildId.length < 1 || this.channelId.length < 1) { + return; + } + console.log('discord init') this.commandCollection = new Collection(); @@ -49,6 +54,10 @@ export class DiscordService implements OnModuleInit { async onModuleInit(): Promise { + if (this.bot == null) { + return; + } + await this.registerCommands(); @@ -93,9 +102,13 @@ export class DiscordService implements OnModuleInit { } } - public async notifySUbscribersBlockFound() { + public async notifySUbscribersBlockFound(message: string) { + if (this.bot == null) { + return; + } + const guild = await this.bot.guilds.fetch(this.guildId); const channel = await guild.channels.fetch(this.channelId) as TextChannel; - channel.send("Block Found!") + channel.send(`Block Found! result ${message}`) } } \ No newline at end of file diff --git a/src/services/notification.service.ts b/src/services/notification.service.ts index 163044e..fa9203e 100644 --- a/src/services/notification.service.ts +++ b/src/services/notification.service.ts @@ -16,8 +16,8 @@ export class NotificationService implements OnModuleInit { } - public async notifySubscribersBlockFound(address: string) { - await this.discordService.notifySUbscribersBlockFound(); - await this.telegramService.notifySubscribersBlockFound(address); + public async notifySubscribersBlockFound(address: string, message: string) { + await this.discordService.notifySUbscribersBlockFound(message); + await this.telegramService.notifySubscribersBlockFound(address, message); } } \ No newline at end of file diff --git a/src/services/telegram.service.ts b/src/services/telegram.service.ts index 29602dc..fe05ecf 100644 --- a/src/services/telegram.service.ts +++ b/src/services/telegram.service.ts @@ -14,8 +14,7 @@ export class TelegramService implements OnModuleInit { private readonly telegramSubscriptionsService: TelegramSubscriptionsService ) { const token: string | null = this.configService.get('TELEGRAM_BOT_TOKEN'); - if (token == null || token.length < 1) { - console.log('No Telegram token found'); + if (token.length < 1) { return; } this.bot = new TelegramBot(token, { polling: true }); @@ -27,7 +26,7 @@ export class TelegramService implements OnModuleInit { async onModuleInit(): Promise { if (this.bot == null) { - return null; + return; } this.bot.onText(/\/subscribe/, async (msg) => { @@ -49,14 +48,14 @@ export class TelegramService implements OnModuleInit { }); } - public async notifySubscribersBlockFound(address: string) { + public async notifySubscribersBlockFound(address: string, message: string) { if (this.bot == null) { - return null; + return; } const subscribers = await this.telegramSubscriptionsService.getSubscriptions(address); subscribers.forEach(subscriber => { - this.bot.sendMessage(subscriber.telegramChatId, 'You found a block!'); + this.bot.sendMessage(subscriber.telegramChatId, `You found a block! ${message}`); }); } } \ No newline at end of file