From deb3aa30d41945781919f42f84a21fcda0c1fbcd Mon Sep 17 00:00:00 2001 From: Ben Wilson Date: Sat, 10 Jun 2023 12:25:51 -0400 Subject: [PATCH] block template --- .vscode/settings.json | 3 ++- src/app.module.ts | 6 +++-- src/bitcoin-rpc.service.ts | 19 ++++++++++--- src/bitcoin-stratum.provider.ts | 4 ++- src/coinbase-constructor.service.ts | 14 ++++++++++ src/models/IBlockTempalte.ts | 41 +++++++++++++++++++++++++++++ src/models/MiningJob.ts | 10 +++---- 7 files changed, 85 insertions(+), 12 deletions(-) create mode 100644 src/coinbase-constructor.service.ts create mode 100644 src/models/IBlockTempalte.ts diff --git a/.vscode/settings.json b/.vscode/settings.json index 25b6851..11c43bf 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -6,6 +6,7 @@ "merkle", "nbits", "ntime", - "prevhash" + "prevhash", + "Tempalte" ] } \ No newline at end of file diff --git a/src/app.module.ts b/src/app.module.ts index cb060d4..348f715 100644 --- a/src/app.module.ts +++ b/src/app.module.ts @@ -5,18 +5,20 @@ import { AppController } from './app.controller'; import { AppService } from './app.service'; import { BitcoinRpcService } from './bitcoin-rpc.service'; import { BitcoinStratumProvider } from './bitcoin-stratum.provider'; +import { CoinbaseConstructorService } from './coinbase-constructor.service'; @Module({ imports: [ - ConfigModule + ConfigModule.forRoot() ], controllers: [AppController], providers: [ AppService, BitcoinStratumProvider, - BitcoinRpcService + BitcoinRpcService, + CoinbaseConstructorService ], }) export class AppModule { diff --git a/src/bitcoin-rpc.service.ts b/src/bitcoin-rpc.service.ts index 310b5cc..f08d90f 100644 --- a/src/bitcoin-rpc.service.ts +++ b/src/bitcoin-rpc.service.ts @@ -1,10 +1,12 @@ -import { Injectable } from '@nestjs/common'; +import { Injectable, OnModuleInit } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { RPCClient } from 'rpc-bitcoin'; +import { IBlockTempalte } from './models/IBlockTempalte'; + @Injectable() -export class BitcoinRpcService { +export class BitcoinRpcService implements OnModuleInit { private client: RPCClient; @@ -14,20 +16,31 @@ export class BitcoinRpcService { const pass = configService.get('BITCOIN_RPC_PASSWORD'); const port = parseInt(configService.get('BITCOIN_RPC_PORT')); const timeout = parseInt(configService.get('BITCOIN_RPC_TIMEOUT')); + this.client = new RPCClient({ url, port, timeout, user, pass }); console.log('Bitcoin RPC connected'); } + async onModuleInit(): Promise { + console.log('onModuleInit'); + // Asynchronous initialization logic + //await this.getBlockTemplate(); + + return; + } + public async getBlockTemplate() { - const result = await this.client.getblocktemplate({ + const result: IBlockTempalte = await this.client.getblocktemplate({ template_request: { rules: ['segwit'], mode: 'template', capabilities: ['serverlist', 'proposal'] } }); + + console.log('Block Template: ', JSON.stringify(result)); } } diff --git a/src/bitcoin-stratum.provider.ts b/src/bitcoin-stratum.provider.ts index 4ae1249..ac3e12f 100644 --- a/src/bitcoin-stratum.provider.ts +++ b/src/bitcoin-stratum.provider.ts @@ -1,6 +1,7 @@ import { Injectable } from '@nestjs/common'; import { Server, Socket } from 'net'; +import { CoinbaseConstructorService } from './coinbase-constructor.service'; import { StratumV1Client } from './models/StratumV1Client'; @Injectable() @@ -10,7 +11,8 @@ export class BitcoinStratumProvider { private server: Server; - constructor() { + constructor(private coinbaseConstructorService: CoinbaseConstructorService) { + this.server = new Server((socket: Socket) => { console.log('New client connected:', socket.remoteAddress); diff --git a/src/coinbase-constructor.service.ts b/src/coinbase-constructor.service.ts new file mode 100644 index 0000000..dead721 --- /dev/null +++ b/src/coinbase-constructor.service.ts @@ -0,0 +1,14 @@ +import { Injectable } from '@nestjs/common'; + +import { BitcoinRpcService } from './bitcoin-rpc.service'; + + +@Injectable() +export class CoinbaseConstructorService { + + constructor(private bitcoinRPCService: BitcoinRpcService) { + + } + + +} \ No newline at end of file diff --git a/src/models/IBlockTempalte.ts b/src/models/IBlockTempalte.ts new file mode 100644 index 0000000..831ca52 --- /dev/null +++ b/src/models/IBlockTempalte.ts @@ -0,0 +1,41 @@ +export interface IBlockTempalte { + // (json object) + version: number; // (numeric) The preferred block version + rules: string[]; // (json array) specific block rules that are to be enforced // (string) name of a rule the client must understand to some extent; see BIP 9 for format + + vbavailable: { // (json object) set of pending, supported versionbit (BIP 9) softfork deployments + rulename: number, // (numeric) identifies the bit number as indicating acceptance and readiness for the named softfork rule + }, + vbrequired: number, // (numeric) bit mask of versionbits the server requires set in submissions + previousblockhash: string, // (string) The hash of current highest block + transactions: [ // (json array) contents of non-coinbase transactions that should be included in the next block + { // (json object) + data: string; //'hex', // (string) transaction data encoded in hexadecimal (byte-for-byte) + txid: string; //'hex', // (string) transaction id encoded in little-endian hexadecimal + hash: string; //'hex', // (string) hash encoded in little-endian hexadecimal (including witness data) + depends: number[]; // (json array) array of numbers // (numeric) transactions before this one (by 1-based index in 'transactions' list) that must be present in the final block if this one is + fee: number, // (numeric) difference in value between transaction inputs and outputs (in satoshis); for coinbase transactions, this is a negative Number of the total collected block fees (ie, not including the block subsidy); if key is not present, fee is unknown and clients MUST NOT assume there isn't one + sigops: number, // (numeric) total SigOps cost, as counted for purposes of block limits; if key is not present, sigop cost is unknown and clients MUST NOT assume it is zero + weight: number // (numeric) total transaction weight, as counted for purposes of block limits + } + ], + coinbaseaux: { // (json object) data that should be included in the coinbase's scriptSig content + key: string; //'hex', // (string) values must be in the coinbase (keys may be ignored) + }, + coinbasevalue: number, // (numeric) maximum allowable input to coinbase transaction, including the generation award and transaction fees (in satoshis) + longpollid: string, // (string) an id to include with a request to longpoll on an update to this template + target: string, // (string) The hash target + mintime: number, // (numeric) The minimum timestamp appropriate for the next block time, expressed in UNIX epoch time + mutable: string[]; // (json array) list of ways the block template may be changed // (string) A way the block template may be changed, e.g. 'time', 'transactions', 'prevblock' + + noncerange: string; // 'hex', // (string) A range of valid nonces + sigoplimit: number, // (numeric) limit of sigops in blocks + sizelimit: number, // (numeric) limit of block size + weightlimit: number, // (numeric) limit of block weight + curtime: number, // (numeric) current timestamp in UNIX epoch time + bits: string, // (string) compressed target of next block + height: number, // (numeric) The height of the next block + default_witness_commitment: string // (string, optional) a valid witness commitment for the unmodified block template + + +} \ No newline at end of file diff --git a/src/models/MiningJob.ts b/src/models/MiningJob.ts index c19adf3..e1b5eac 100644 --- a/src/models/MiningJob.ts +++ b/src/models/MiningJob.ts @@ -9,12 +9,12 @@ export class MiningJob { public job_id: string; // ID of the job. Use this ID while submitting share generated from this job. - public prevhash: string; // Hash of previous block. - public coinb1: string; // Initial part of coinbase transaction. - public coinb2: string; // Final part of coinbase transaction. + public prevhash: string; // The hex-encoded previous block hash. + public coinb1: string; // The hex-encoded prefix of the coinbase transaction (to precede extra nonce 2). + public coinb2: string; //The hex-encoded suffix of the coinbase transaction (to follow extra nonce 2). public merkle_branch: string[]; // List of hashes, will be used for calculation of merkle root. This is not a list of all transactions, it only contains prepared hashes of steps of merkle tree algorithm. - public version: string; // Bitcoin block version. - public nbits: string; // Encoded current network difficulty + public version: string; // The hex-encoded block version. + public nbits: string; // The hex-encoded network difficulty required for the block. public ntime: string; // Current ntime/ public clean_jobs: boolean; // When true, server indicates that submitting shares from previous jobs don't have a sense and such shares will be rejected. When this flag is set, miner should also drop all previous jobs too.