ensure block template is fresh

This commit is contained in:
Benjamin Wilson
2024-07-04 20:21:53 -04:00
parent e0239d2805
commit c654a706ae
+27 -24
View File
@@ -103,13 +103,16 @@ export class BitcoinRpcService implements OnModuleInit {
public async getBlockTemplate(blockHeight: number): Promise<IBlockTemplate> { public async getBlockTemplate(blockHeight: number): Promise<IBlockTemplate> {
let result: IBlockTemplate; let result: IBlockTemplate;
try { try {
const block = await this.rpcBlockService.getBlock(blockHeight); const block = await this.rpcBlockService.getBlock(blockHeight);
const completeBlock = block?.data != null;
if (block != null && block.data != null) { // If the block has already been loaded, and the same instance is fetching the template again, we just need to refresh it.
if (completeBlock && block.lockedBy == process.env.NODE_APP_INSTANCE) {
result = await this.loadBlockTemplate(blockHeight);
}
else if (completeBlock) {
return Promise.resolve(JSON.parse(block.data)); return Promise.resolve(JSON.parse(block.data));
} else if (block == null) { } else if (!completeBlock) {
if (process.env.NODE_APP_INSTANCE != null) { if (process.env.NODE_APP_INSTANCE != null) {
// There is a unique constraint on the block height so if another process tries to lock, it'll throw // There is a unique constraint on the block height so if another process tries to lock, it'll throw
try { try {
@@ -118,9 +121,24 @@ export class BitcoinRpcService implements OnModuleInit {
result = await this.waitForBlock(blockHeight); result = await this.waitForBlock(blockHeight);
} }
} }
result = await this.loadBlockTemplate(blockHeight);
} else {
//wait for block
result = await this.waitForBlock(blockHeight);
}
} catch (e) {
console.error('Error getblocktemplate:', e.message);
throw new Error('Error getblocktemplate');
}
console.log(`getblocktemplate tx count: ${result.transactions.length}`);
return result;
}
while (result == null) { private async loadBlockTemplate(blockHeight: number) {
result = await this.client.getblocktemplate({
let blockTemplate: IBlockTemplate;
while (blockTemplate == null) {
blockTemplate = await this.client.getblocktemplate({
template_request: { template_request: {
rules: ['segwit'], rules: ['segwit'],
mode: 'template', mode: 'template',
@@ -130,26 +148,11 @@ export class BitcoinRpcService implements OnModuleInit {
} }
let saved = false; let saved = false;
while(!saved){ while (!saved) {
let saveResult = await this.rpcBlockService.saveBlock(blockHeight, JSON.stringify(result)); let saveResult = await this.rpcBlockService.saveBlock(blockHeight, JSON.stringify(blockTemplate));
saved = saveResult.affected > 0; saved = saveResult.affected > 0;
} }
return blockTemplate;
} else {
//wait for block
result = await this.waitForBlock(blockHeight);
}
} catch (e) {
console.error('Error getblocktemplate:', e.message);
throw new Error('Error getblocktemplate');
}
console.log(`getblocktemplate tx count: ${result.transactions.length}`);
return result;
} }
public async getMiningInfo(): Promise<IMiningInfo> { public async getMiningInfo(): Promise<IMiningInfo> {