Skip to content

Commit

Permalink
fix(ref-imp): fixed initialization failure when restart bitcoin proce… (
Browse files Browse the repository at this point in the history
  • Loading branch information
isaacJChen committed Sep 3, 2020
1 parent b6945a9 commit 289ffce
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 22 deletions.
26 changes: 13 additions & 13 deletions lib/bitcoin/BitcoinProcessor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,19 +177,17 @@ export default class BitcoinProcessor {

const startingBlock = await this.getStartingBlockForPeriodicPoll();

// Throw if bitcoin client is not synced up to the bitcoin service's known height.
// NOTE: Implementation for issue #692 can simplify this method and remove this check.
if (startingBlock === undefined) {
throw new SidetreeError(ErrorCode.BitcoinProcessorBitcoinClientCurrentHeightNotUpToDate);
}

console.debug('Synchronizing blocks for sidetree transactions...');
console.info(`Starting block: ${startingBlock.height} (${startingBlock.hash})`);
if (this.bitcoinDataDirectory) {
// This reads into the raw block files and parse to speed up the initial startup instead of rpc
await this.fastProcessTransactions(startingBlock);
console.info('Bitcoin processor state is ahead of bitcoind: skipping initialization');
} else {
await this.processTransactions(startingBlock);
console.debug('Synchronizing blocks for sidetree transactions...');
console.info(`Starting block: ${startingBlock.height} (${startingBlock.hash})`);
if (this.bitcoinDataDirectory) {
// This reads into the raw block files and parse to speed up the initial startup instead of rpc
await this.fastProcessTransactions(startingBlock);
} else {
await this.processTransactions(startingBlock);
}
}

// NOTE: important to this initialization after we have processed all the blocks
Expand Down Expand Up @@ -605,7 +603,9 @@ export default class BitcoinProcessor {

const startingBlock = await this.getStartingBlockForPeriodicPoll();

if (startingBlock) {
if (startingBlock === undefined) {
console.info('Bitcoin processor state is ahead of bitcoind: skipping periodic poll');
} else {
await this.processTransactions(startingBlock);
}
} catch (error) {
Expand Down Expand Up @@ -698,7 +698,7 @@ export default class BitcoinProcessor {
const exponentiallySpacedBlocks = await this.blockMetadataStore.lookBackExponentially();
const lastKnownValidBlock = await this.firstValidBlock(exponentiallySpacedBlocks);

await this.trimDatabasesToBlock(lastKnownValidBlock?.height);
await this.trimDatabasesToBlock(lastKnownValidBlock ? lastKnownValidBlock.height : undefined);

return lastKnownValidBlock;
}
Expand Down
1 change: 0 additions & 1 deletion lib/bitcoin/ErrorCode.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ export default {
BitcoinFileReaderBlockCannotReadDirectory: 'bitcoin_file_reader_block_cannot_read_directory',
BitcoinFileReaderBlockCannotReadFile: 'bitcoin_file_reader_block_cannot_read_file',
BitcoinWalletIncorrectImportString: 'bitcoin_wallet_incorrect_import_string',
BitcoinProcessorBitcoinClientCurrentHeightNotUpToDate: 'bitcion_processor_bitcoin_client_current_height_not_up_to_date',
BitcoinProcessorCannotProcessBlocksBeforeGenesis: 'bitcoin_processor_cannot_process_blocks_before_genesis',
BitcoinProcessInvalidPreviousBlockHash: 'bitcoin_processor_invalid_previous_block_hash',
BitcoinRawDataParserInvalidBlockData: 'bitcoin_raw_data_parser_invalid_block_data',
Expand Down
4 changes: 2 additions & 2 deletions lib/core/versions/latest/Multihash.ts
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export default class Multihash {
const intermediateHashBuffer = Multihash.hashAsNonMultihashBuffer(content, hashAlgorithmCode);
const actualMultihashBuffer = Multihash.hash(intermediateHashBuffer, hashAlgorithmCode);

return Buffer.compare(actualMultihashBuffer, expectedMultihashBuffer) == 0;
return Buffer.compare(actualMultihashBuffer, expectedMultihashBuffer) === 0;
} catch (error) {
console.log(error);
return false;
Expand All @@ -190,7 +190,7 @@ export default class Multihash {

const actualMultihashBuffer = Multihash.hash(content, hashAlgorithmCode);

return Buffer.compare(actualMultihashBuffer, expectedMultihashBuffer) == 0;
return Buffer.compare(actualMultihashBuffer, expectedMultihashBuffer) === 0;
} catch (error) {
console.log(error);
return false;
Expand Down
9 changes: 4 additions & 5 deletions tests/bitcoin/BitcoinProcessor.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,13 +195,12 @@ describe('BitcoinProcessor', () => {
done();
});

it('should throw error if unable to find a starting block.', async (done) => {
it('should skip initialization if unable to find a starting block.', async (done) => {
getStartingBlockForPeriodicPollSpy.and.returnValue(Promise.resolve(undefined));

await JasmineSidetreeErrorValidator.expectSidetreeErrorToBeThrownAsync(
() => bitcoinProcessor.initialize(),
ErrorCode.BitcoinProcessorBitcoinClientCurrentHeightNotUpToDate
);
await bitcoinProcessor.initialize();
expect(processTransactionsSpy).not.toHaveBeenCalled();
expect(fastProcessTransactionsSpy).not.toHaveBeenCalled();

done();
});
Expand Down
2 changes: 1 addition & 1 deletion tests/generators/VegetaLoadGenerator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export default class VegetaLoadGenerator {
* One targets file containing all Create requests
* One targets file containing all Update requests
* One targets file containing all recovery requests
* One targets file containing all deactivate requests
* One targets file containing all deactivate requests
* @param uniqueDidCount The number of unique DID to be generated.
* @param endpointUrl The URL that the requests will be sent to.
* @param absoluteFolderPath The folder that all the generated files will be saved to.
Expand Down

0 comments on commit 289ffce

Please sign in to comment.