block copy

This commit is contained in:
Ben Wilson
2023-07-30 17:59:51 -04:00
parent 89e785182c
commit e38f4e1edc
4 changed files with 13 additions and 14 deletions
+6 -4
View File
@@ -58,9 +58,11 @@ export class MiningJob {
} }
public copyAndUpdateBlock(jobTemplate: { data: IJobTemplate, blockBuffer: Buffer }, versionMask: number, nonce: number, extraNonce: string, extraNonce2: string, timestamp: number): bitcoinjs.Block { public copyAndUpdateBlock(jobTemplate: IJobTemplate, versionMask: number, nonce: number, extraNonce: string, extraNonce2: string, timestamp: number): bitcoinjs.Block {
const testBlock = Object.assign(new bitcoinjs.Block(), jobTemplate.block);
const testBlock = bitcoinjs.Block.fromBuffer(jobTemplate.blockBuffer);
testBlock.transactions[0] = this.coinbaseTransaction; testBlock.transactions[0] = this.coinbaseTransaction;
@@ -77,12 +79,12 @@ export class MiningJob {
testBlock.transactions[0].ins[0].script = Buffer.from(`${nonceScript.substring(0, nonceScript.length - 16)}${extraNonce}${extraNonce2}`, 'hex'); testBlock.transactions[0].ins[0].script = Buffer.from(`${nonceScript.substring(0, nonceScript.length - 16)}${extraNonce}${extraNonce2}`, 'hex');
//recompute the root since we updated the coinbase script with the nonces //recompute the root since we updated the coinbase script with the nonces
testBlock.merkleRoot = this.calculateMerkleRootHash(testBlock.transactions[0].getHash(false), jobTemplate.data.merkle_branch); testBlock.merkleRoot = this.calculateMerkleRootHash(testBlock.transactions[0].getHash(false), jobTemplate.merkle_branch);
testBlock.timestamp = timestamp; testBlock.timestamp = timestamp;
return testBlock; return testBlock as any;
} }
+1 -1
View File
@@ -255,7 +255,7 @@ describe('StratumV1Client', () => {
socketEmitter(Buffer.from(MockRecording1.MINING_SUBMIT)); socketEmitter(Buffer.from(MockRecording1.MINING_SUBMIT));
jest.useRealTimers(); jest.useRealTimers();
await new Promise((r) => setTimeout(r, 100)); await new Promise((r) => setTimeout(r, 1000));
expect(promiseSocket.write).lastCalledWith(`{\"id\":5,\"error\":null,\"result\":true}\n`); expect(promiseSocket.write).lastCalledWith(`{\"id\":5,\"error\":null,\"result\":true}\n`);
+3 -3
View File
@@ -382,19 +382,19 @@ export class StratumV1Client extends EasyUnsubscribe {
if (submissionDifficulty >= this.sessionDifficulty) { if (submissionDifficulty >= this.sessionDifficulty) {
if (submissionDifficulty >= jobTemplate.data.blockData.networkDifficulty) { if (submissionDifficulty >= jobTemplate.blockData.networkDifficulty) {
console.log('!!! BLOCK FOUND !!!'); console.log('!!! BLOCK FOUND !!!');
const blockHex = updatedJobBlock.toHex(false); const blockHex = updatedJobBlock.toHex(false);
const result = await this.bitcoinRpcService.SUBMIT_BLOCK(blockHex); const result = await this.bitcoinRpcService.SUBMIT_BLOCK(blockHex);
await this.blocksService.save({ await this.blocksService.save({
height: jobTemplate.data.blockData.height, height: jobTemplate.blockData.height,
minerAddress: this.clientAuthorization.address, minerAddress: this.clientAuthorization.address,
worker: this.clientAuthorization.worker, worker: this.clientAuthorization.worker,
sessionId: this.extraNonceAndSessionId, sessionId: this.extraNonceAndSessionId,
blockData: blockHex blockData: blockHex
}); });
await this.notificationService.notifySubscribersBlockFound(this.clientAuthorization.address, jobTemplate.data.blockData.height, updatedJobBlock, result); await this.notificationService.notifySubscribersBlockFound(this.clientAuthorization.address, jobTemplate.blockData.height, updatedJobBlock, result);
//success //success
if (result == null) { if (result == null) {
await this.addressSettingsService.resetBestDifficultyAndShares(); await this.addressSettingsService.resetBestDifficultyAndShares();
+3 -6
View File
@@ -34,7 +34,7 @@ export class StratumV1JobsService {
public jobs: MiningJob[] = []; public jobs: MiningJob[] = [];
public blocks: { [id: number]: { data: IJobTemplate, blockBuffer: Buffer } } = {}; public blocks: { [id: number]: IJobTemplate } = {};
private currentBlockTemplate$: Observable<{ blockTemplate: IBlockTemplate }>; private currentBlockTemplate$: Observable<{ blockTemplate: IBlockTemplate }>;
@@ -131,10 +131,7 @@ export class StratumV1JobsService {
this.blocks = {}; this.blocks = {};
this.jobs = []; this.jobs = [];
} }
this.blocks[data.blockData.id] = { this.blocks[data.blockData.id] = data;
data,
blockBuffer: data.block.toBuffer()
};
}), }),
shareReplay({ refCount: true, bufferSize: 1 }) shareReplay({ refCount: true, bufferSize: 1 })
) )
@@ -157,7 +154,7 @@ export class StratumV1JobsService {
return bytes; return bytes;
} }
public getJobTemplateById(jobTemplateId: string): { data: IJobTemplate, blockBuffer: Buffer } { public getJobTemplateById(jobTemplateId: string): IJobTemplate {
return this.blocks[jobTemplateId]; return this.blocks[jobTemplateId];
} }