Skip to content

Commit

Permalink
feat(ref-imp): emitted a couple of events in bitcoin processor
Browse files Browse the repository at this point in the history
  • Loading branch information
thehenrytsai committed Mar 9, 2021
1 parent e7211f2 commit 189cfcd
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 3 deletions.
22 changes: 22 additions & 0 deletions docs/bitcoin.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,25 @@ The fund allocated for transaction fees for subsequent re-locking of the initial

> Developer's note:
This allotted amount is locked together with value time lock for simplicity of re-lock implementation. If this allotted amount is depleted due to subsequent re-locks, the remaining locked amount will be released back to wallet, and a new lock will be created with this allotted amount added to it again.

## Events

### `bitcoin_processor_databases_revert`
Occurs every time the databases are reverted due to a bitcoin reorg.

Event data:
```json
{
"blockHeight": "The block height that the databases are reverted to.",
}
```

### `bitcoin_processor_observing_loop_failed`
Occurs every time the bitcoin processor fails an observing loop.

Event data: none

### `bitcoin_processor_observing_loop_success`
Occurs every time the bitcoin processor successfully completes a processing loop.

Event data: none
16 changes: 13 additions & 3 deletions lib/bitcoin/BitcoinProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import * as timeSpan from 'time-span';
import { ISidetreeEventEmitter, ISidetreeLogger } from '..';
import BitcoinBlockDataIterator from './BitcoinBlockDataIterator';
import BitcoinBlockModel from './models/BitcoinBlockModel';
import BitcoinClient from './BitcoinClient';
Expand All @@ -8,8 +9,9 @@ import BitcoinVersionModel from './models/BitcoinVersionModel';
import BlockMetadata from './models/BlockMetadata';
import BlockMetadataWithoutNormalizedFee from './models/BlockMetadataWithoutNormalizedFee';
import ErrorCode from './ErrorCode';
import EventCode from './EventCode';
import EventEmitter from '../common/EventEmitter';
import IBitcoinConfig from './IBitcoinConfig';
import { ISidetreeLogger } from '..';
import LockMonitor from './lock/LockMonitor';
import LockResolver from './lock/LockResolver';
import LogColor from '../common/LogColor';
Expand Down Expand Up @@ -153,8 +155,9 @@ export default class BitcoinProcessor {
/**
* Initializes the Bitcoin processor
*/
public async initialize (versionModels: BitcoinVersionModel[], customLogger?: ISidetreeLogger) {
public async initialize (versionModels: BitcoinVersionModel[], customLogger?: ISidetreeLogger, customEventEmitter?: ISidetreeEventEmitter) {
Logger.initialize(customLogger);
EventEmitter.initialize(customEventEmitter);

await this.versionManager.initialize(versionModels, this.config, this.blockMetadataStore);
await this.serviceStateStore.initialize();
Expand Down Expand Up @@ -671,7 +674,10 @@ export default class BitcoinProcessor {
} else {
await this.processTransactions(startingBlock);
}

EventEmitter.emit(EventCode.BitcoinProcessorObservingLoopSuccessful);
} catch (error) {
EventEmitter.emit(EventCode.BitcoinProcessorObservingLoopFailed);
Logger.error(error);
} finally {
this.pollTimeoutId = setTimeout(this.periodicPoll.bind(this), 1000 * interval, interval);
Expand Down Expand Up @@ -752,11 +758,15 @@ export default class BitcoinProcessor {
* @returns A known valid block before the fork. `undefined` if no known valid block can be found.
*/
private async revertDatabases (): Promise<IBlockInfo | undefined> {
Logger.info(`Reverting databases...`);
const exponentiallySpacedBlocks = await this.blockMetadataStore.lookBackExponentially();
const lastKnownValidBlock = await this.firstValidBlock(exponentiallySpacedBlocks);
const lastKnownValidBlockHeight = lastKnownValidBlock ? lastKnownValidBlock.height : undefined;

await this.trimDatabasesToBlock(lastKnownValidBlock ? lastKnownValidBlock.height : undefined);
Logger.info(LogColor.lightBlue(`Reverting database to ${LogColor.green(lastKnownValidBlockHeight || 'genesis')} block...`));
await this.trimDatabasesToBlock(lastKnownValidBlockHeight);

EventEmitter.emit(EventCode.BitcoinProcessorDatabasesRevert, { blockHeight: lastKnownValidBlockHeight });
return lastKnownValidBlock;
}

Expand Down
8 changes: 8 additions & 0 deletions lib/bitcoin/EventCode.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
/**
* Event codes used by Bitcoin Processor.
*/
export default {
BitcoinProcessorDatabasesRevert: 'bitcoin_processor_databases_revert',
BitcoinProcessorObservingLoopFailed: 'bitcoin_processor_observing_loop_failed',
BitcoinProcessorObservingLoopSuccessful: `bitcoin_processor_observing_loop_successful`
};

0 comments on commit 189cfcd

Please sign in to comment.