Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle null block templates due to RPC errors #72

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 11 additions & 6 deletions src/services/bitcoin-rpc.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,10 +41,13 @@ export class BitcoinRpcService implements OnModuleInit {

this.client = new RPCClient({ url, port, timeout, user, pass });

this.client.getrpcinfo().then((res) => {
this.client.getrpcinfo().then(() => {
console.log('Bitcoin RPC connected');
}, () => {
console.error('Could not reach RPC host');
})
.catch((err) => {
console.error('Error connecting to RPC host. Error: ', err);
});

if (this.configService.get('BITCOIN_ZMQ_HOST')) {
Expand All @@ -59,6 +62,9 @@ export class BitcoinRpcService implements OnModuleInit {
sock.events.on('connect:retry', () => {
console.log('ZMQ Unable to connect, Retrying');
});
sock.events.on('close:error', () => {
console.log('ZMQ Connection Closed with error');
});

sock.connect(this.configService.get('BITCOIN_ZMQ_HOST'));
sock.subscribe('rawblock');
Expand Down Expand Up @@ -100,8 +106,9 @@ export class BitcoinRpcService implements OnModuleInit {
}
}

public async getBlockTemplate(blockHeight: number): Promise<IBlockTemplate> {
let result: IBlockTemplate;
public async getBlockTemplate(blockHeight: number): Promise<IBlockTemplate | null> {
let result: IBlockTemplate | null = null;

try {
const block = await this.rpcBlockService.getBlock(blockHeight);
const completeBlock = block?.data != null;
Expand All @@ -128,9 +135,8 @@ export class BitcoinRpcService implements OnModuleInit {
}
} catch (e) {
console.error('Error getblocktemplate:', e.message);
throw new Error('Error getblocktemplate');
}
console.log(`getblocktemplate tx count: ${result.transactions.length}`);
console.log(`getblocktemplate tx count: ${result?.transactions?.length ?? 0}`);
return result;
}

Expand All @@ -147,7 +153,6 @@ export class BitcoinRpcService implements OnModuleInit {
});
}


await this.rpcBlockService.saveBlock(blockHeight, JSON.stringify(blockTemplate));

return blockTemplate;
Expand Down
3 changes: 3 additions & 0 deletions src/services/stratum-v1-jobs.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,9 @@ export class StratumV1JobsService {
}))
}),
map(({ blockTemplate, interval }) => {
if (blockTemplate == null) {
return null;
}

let clearJobs = false;
if (this.lastIntervalCount === interval) {
Expand Down