Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add lifecycle hooks for indexing #71

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/indexer/AbstractIndexerLifeCycle.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Tx, GetBlockReturn } from '../rpcclient';

export type BlockLifeCycleParams = {
block: GetBlockReturn<{ verbosity: 2 }>;
};

export type TxLifeCycleParams = {
txIndex: number;
tx: Tx;
} & BlockLifeCycleParams;

export default abstract class AbstractIndexerLifeCycle {
async beforeTxIndex({ txIndex, tx, block }: TxLifeCycleParams): Promise<void> {}
async afterTxIndex({ txIndex, tx, block }: TxLifeCycleParams): Promise<void> {}
async beforeBlockIndex({ block }: BlockLifeCycleParams): Promise<void> {}
async afterBlockIndex({ block }: BlockLifeCycleParams): Promise<void> {}
}
25 changes: 25 additions & 0 deletions src/indexer/DefaultIndexerLifeCycle.ts
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we just drop the default entirely and make it do nothing?

Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import AbstractIndexerLifeCycle, {
TxLifeCycleParams,
BlockLifeCycleParams,
} from './AbstractIndexerLifeCycle';

class DefaultIndexerLifeCycle implements AbstractIndexerLifeCycle {
async beforeTxIndex({ txIndex, block, tx }: TxLifeCycleParams): Promise<void> {
console.log(
`Indexing tx ${txIndex + 1}/${block.tx.length} in block ${block.height} ${block.hash}`
);
}
async afterTxIndex({ txIndex, block, tx }: TxLifeCycleParams): Promise<void> {
console.log(
`Indexed tx ${txIndex + 1}/${block.tx.length} in block ${block.height} ${block.hash}`
);
}
async beforeBlockIndex({ block }: BlockLifeCycleParams): Promise<void> {
console.log(`Indexing block ${block.height} ${block.hash}`);
}
async afterBlockIndex({ block }: BlockLifeCycleParams): Promise<void> {
console.log(`Indexed block ${block.height} ${block.hash}`);
}
}

export default DefaultIndexerLifeCycle;
59 changes: 33 additions & 26 deletions src/indexer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Network } from '../network';
import { BitcoinRpcClient } from '../rpcclient';
import { RuneUpdater } from './updater';
import { u128 } from '../integer';
import DefaultIndexerLifeCycle from './DefaultIndexerLifeCycle';

export * from './types';
export { RuneUpdater } from './updater';
Expand All @@ -11,6 +12,7 @@ export class RunestoneIndexer {
private readonly _storage: RunestoneStorage;
private readonly _rpc: BitcoinRpcClient;
private readonly _network: Network;
private readonly _lifecyle: DefaultIndexerLifeCycle;

private _started: boolean = false;
private _updateInProgress: boolean = false;
Expand All @@ -19,6 +21,7 @@ export class RunestoneIndexer {
this._rpc = options.bitcoinRpcClient;
this._storage = options.storage;
this._network = options.network;
this._lifecyle = options.lifecycle || new DefaultIndexerLifeCycle();
}

async start(): Promise<void> {
Expand Down Expand Up @@ -71,6 +74,34 @@ export class RunestoneIndexer {
}
}

private async asyncProcessBlockHash({
blockhash,
reorg = false,
}: {
blockhash: string;
reorg: boolean;
}): Promise<void> {
const blockResult = await this._rpc.getblock({ blockhash, verbosity: 2 });
if (blockResult.error !== null) {
throw blockResult.error;
}
const block = blockResult.result;

const runeUpdater = new RuneUpdater(this._network, block, reorg, this._storage, this._rpc);

await this._lifecyle.beforeBlockIndex({ block });

for (const [txIndex, tx] of block.tx.entries()) {
await this._lifecyle.beforeTxIndex({ tx, txIndex, block });
await runeUpdater.indexRunes(tx, txIndex);
await this._lifecyle.afterTxIndex({ tx, txIndex, block });
}

await this._lifecyle.afterBlockIndex({ block });

await this._storage.saveBlockIndex(runeUpdater);
}

private async updateRuneUtxoBalancesImpl() {
const currentStorageBlock = await this._storage.getCurrentBlock();
if (currentStorageBlock) {
Expand All @@ -92,19 +123,7 @@ export class RunestoneIndexer {

// process blocks that are reorgs
for (const blockhash of reorgBlockhashesToIndex) {
const blockResult = await this._rpc.getblock({ blockhash, verbosity: 2 });
if (blockResult.error !== null) {
throw blockResult.error;
}
const block = blockResult.result;

const runeUpdater = new RuneUpdater(this._network, block, true, this._storage, this._rpc);

for (const [txIndex, tx] of block.tx.entries()) {
await runeUpdater.indexRunes(tx, txIndex);
}

await this._storage.saveBlockIndex(runeUpdater);
await this.asyncProcessBlockHash({ blockhash, reorg: true });
}
}

Expand All @@ -115,19 +134,7 @@ export class RunestoneIndexer {
);
let blockhash = (await this._rpc.getblockhash({ height: blockheight })).result;
while (blockhash !== null) {
const blockResult = await this._rpc.getblock({ blockhash, verbosity: 2 });
if (blockResult.error !== null) {
throw blockResult.error;
}
const block = blockResult.result;

const runeUpdater = new RuneUpdater(this._network, block, false, this._storage, this._rpc);

for (const [txIndex, tx] of block.tx.entries()) {
await runeUpdater.indexRunes(tx, txIndex);
}

await this._storage.saveBlockIndex(runeUpdater);
await this.asyncProcessBlockHash({ blockhash, reorg: false });

blockheight++;
blockhash = (await this._rpc.getblockhash({ height: blockheight })).result;
Expand Down
2 changes: 2 additions & 0 deletions src/indexer/types.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Network } from '../network';
import { BitcoinRpcClient } from '../rpcclient';
import AbstractIndexerLifeCycle from './AbstractIndexerLifeCycle';

export interface RunestoneStorage {
/**
Expand Down Expand Up @@ -73,6 +74,7 @@ export type RunestoneIndexerOptions = {
bitcoinRpcClient: BitcoinRpcClient;
network: Network;
storage: RunestoneStorage;
lifecycle?: AbstractIndexerLifeCycle;
};

export type BlockIdentifier = {
Expand Down