mirror of
https://github.com/benjamin-wilson/public-pool.git
synced 2026-09-29 09:05:06 -07:00
ensure block template is fresh
This commit is contained in:
@@ -95,13 +95,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 {
|
||||||
@@ -110,32 +113,11 @@ export class BitcoinRpcService implements OnModuleInit {
|
|||||||
result = await this.waitForBlock(blockHeight);
|
result = await this.waitForBlock(blockHeight);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
result = await this.loadBlockTemplate(blockHeight);
|
||||||
while (result == null) {
|
|
||||||
result = await this.client.getblocktemplate({
|
|
||||||
template_request: {
|
|
||||||
rules: ['segwit'],
|
|
||||||
mode: 'template',
|
|
||||||
capabilities: ['serverlist', 'proposal']
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
let saved = false;
|
|
||||||
while(!saved){
|
|
||||||
let saveResult = await this.rpcBlockService.saveBlock(blockHeight, JSON.stringify(result));
|
|
||||||
saved = saveResult.affected > 0;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
//wait for block
|
//wait for block
|
||||||
result = await this.waitForBlock(blockHeight);
|
result = await this.waitForBlock(blockHeight);
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
console.error('Error getblocktemplate:', e.message);
|
console.error('Error getblocktemplate:', e.message);
|
||||||
throw new Error('Error getblocktemplate');
|
throw new Error('Error getblocktemplate');
|
||||||
@@ -144,6 +126,27 @@ export class BitcoinRpcService implements OnModuleInit {
|
|||||||
return result;
|
return result;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private async loadBlockTemplate(blockHeight: number) {
|
||||||
|
|
||||||
|
let blockTemplate: IBlockTemplate;
|
||||||
|
while (blockTemplate == null) {
|
||||||
|
blockTemplate = await this.client.getblocktemplate({
|
||||||
|
template_request: {
|
||||||
|
rules: ['segwit'],
|
||||||
|
mode: 'template',
|
||||||
|
capabilities: ['serverlist', 'proposal']
|
||||||
|
}
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
let saved = false;
|
||||||
|
while (!saved) {
|
||||||
|
let saveResult = await this.rpcBlockService.saveBlock(blockHeight, JSON.stringify(blockTemplate));
|
||||||
|
saved = saveResult.affected > 0;
|
||||||
|
}
|
||||||
|
return blockTemplate;
|
||||||
|
}
|
||||||
|
|
||||||
public async getMiningInfo(): Promise<IMiningInfo> {
|
public async getMiningInfo(): Promise<IMiningInfo> {
|
||||||
try {
|
try {
|
||||||
return await this.client.getmininginfo();
|
return await this.client.getmininginfo();
|
||||||
|
|||||||
Reference in New Issue
Block a user