Skip to content

refactor: dry up the body of the indexing #69

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
50 changes: 24 additions & 26 deletions src/indexer/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,28 @@ 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);

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

await this._storage.saveBlockIndex(runeUpdater);
}

private async updateRuneUtxoBalancesImpl() {
const currentStorageBlock = await this._storage.getCurrentBlock();
if (currentStorageBlock) {
Expand All @@ -92,19 +114,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);
Copy link
Contributor Author

@tansanDOTeth tansanDOTeth Jun 18, 2024

Choose a reason for hiding this comment

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

@summraznboi gm

I noticed this reorg parameter isn't being used in the RuneUpdater. Do you have plans for it? If not, I can greatly simplify this function here.

Currently, it will process all the reorg hashes first in the loop here:

for (const blockhash of reorgBlockhashesToIndex) {
await this.asyncProcessBlockHash({ blockhash, reorg: true });
}

However, I don't think the code needs to do this here. Instead, we can just set the block height further back for the latter loop, so it will start processing from the last known synced block height.

Ex, here

let blockheight = Math.max(
Network.getFirstRuneHeight(this._network),
currentStorageBlock ? currentStorageBlock.height + 1 : 0
);
let blockhash = (await this._rpc.getblockhash({ height: blockheight })).result;
while (blockhash !== null) {
await this.asyncProcessBlockHash({ blockhash, reorg: false });
blockheight++;
blockhash = (await this._rpc.getblockhash({ height: blockheight })).result;
}

What do you think?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Can you provide some unit tests to ensure no regressions are occurring with this refactor?


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 +125,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