Skip to content

Commit 4abdaf6

Browse files
committedApr 17, 2024
Simplified block indexing walk to always use getblockhash
1 parent 09d4938 commit 4abdaf6

File tree

3 files changed

+12
-133
lines changed

3 files changed

+12
-133
lines changed
 

‎package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@magiceden-oss/runestone-lib",
3-
"version": "0.8.2-alpha",
3+
"version": "0.9.0-alpha",
44
"description": "",
55
"main": "./dist/index.js",
66
"types": "./dist/index.d.ts",

‎src/indexer/index.ts

+6-129
Original file line numberDiff line numberDiff line change
@@ -72,35 +72,29 @@ export class RunestoneIndexer {
7272
return;
7373
}
7474

75-
this._updateInProgress = this._rpc.getblockhashbyheight
76-
? this.updateRuneUtxoBalancesWithBlockHeightImpl(
77-
this._rpc.getblockhashbyheight.bind(this._rpc)
78-
)
79-
: this.updateRuneUtxoBalancesImpl();
75+
this._updateInProgress = this.updateRuneUtxoBalancesImpl();
8076
try {
8177
await this._updateInProgress;
8278
} finally {
8379
this._updateInProgress = null;
8480
}
8581
}
8682

87-
private async updateRuneUtxoBalancesWithBlockHeightImpl(
88-
getblockhashbyheight: (blockheight: number) => Promise<string | null>
89-
) {
83+
private async updateRuneUtxoBalancesImpl() {
9084
const currentStorageBlock = await this._storage.getCurrentBlock();
9185
if (currentStorageBlock) {
9286
// walk down until matching hash is found
9387
const reorgBlockhashesToIndex: string[] = [];
9488
let blockheight = currentStorageBlock.height;
95-
let blockhash = await getblockhashbyheight(blockheight);
89+
let blockhash = (await this._rpc.getblockhash({ height: blockheight })).result;
9690
let storageBlockHash: string | null = currentStorageBlock.hash;
9791
while (storageBlockHash !== blockhash) {
9892
if (blockhash) {
9993
reorgBlockhashesToIndex.push(blockhash);
10094
}
10195

10296
blockheight--;
103-
blockhash = await getblockhashbyheight(blockheight);
97+
blockhash = (await this._rpc.getblockhash({ height: blockheight })).result;
10498
storageBlockHash = await this._storage.getBlockhash(blockheight);
10599
}
106100

@@ -127,7 +121,7 @@ export class RunestoneIndexer {
127121
Network.getFirstRuneHeight(this._network),
128122
currentStorageBlock ? currentStorageBlock.height + 1 : 0
129123
);
130-
let blockhash = await getblockhashbyheight(blockheight);
124+
let blockhash = (await this._rpc.getblockhash({ height: blockheight })).result;
131125
while (blockhash !== null) {
132126
const blockResult = await this._rpc.getblock({ blockhash, verbosity: 2 });
133127
if (blockResult.error !== null) {
@@ -144,124 +138,7 @@ export class RunestoneIndexer {
144138
await this._storage.saveBlockIndex(runeUpdater);
145139

146140
blockheight++;
147-
blockhash = await getblockhashbyheight(blockheight);
148-
}
149-
}
150-
151-
private async updateRuneUtxoBalancesImpl() {
152-
const newBlockhashesToIndex: string[] = [];
153-
154-
const currentStorageBlock = await this._storage.getCurrentBlock();
155-
if (currentStorageBlock !== null) {
156-
// If rpc block indexing is ahead of our storage, let's save up all block hashes
157-
// until we arrive back to the current storage's block tip.
158-
const bestblockhashResult = await this._rpc.getbestblockhash();
159-
if (bestblockhashResult.error !== null) {
160-
throw bestblockhashResult.error;
161-
}
162-
const bestblockhash = bestblockhashResult.result;
163-
164-
let rpcBlockResult = await this._rpc.getblock({
165-
blockhash: bestblockhash,
166-
verbosity: 1,
167-
});
168-
if (rpcBlockResult.error !== null) {
169-
throw rpcBlockResult.error;
170-
}
171-
let rpcBlock = rpcBlockResult.result;
172-
173-
while (rpcBlock.height > currentStorageBlock.height) {
174-
newBlockhashesToIndex.push(rpcBlock.hash);
175-
176-
rpcBlockResult = await this._rpc.getblock({
177-
blockhash: rpcBlock.previousblockhash,
178-
verbosity: 1,
179-
});
180-
if (rpcBlockResult.error !== null) {
181-
throw rpcBlockResult.error;
182-
}
183-
rpcBlock = rpcBlockResult.result;
184-
}
185-
186-
// Handle edge case where storage block height is higher than rpc node block
187-
// (such as pointing to a newly indexing rpc node)
188-
let storageBlockhash =
189-
currentStorageBlock && currentStorageBlock.height === rpcBlock.height
190-
? currentStorageBlock.hash
191-
: await this._storage.getBlockhash(rpcBlock.height);
192-
193-
// Now rpc and storage blocks are at the same height,
194-
// iterate until they are also the same hash
195-
while (rpcBlock.hash !== storageBlockhash) {
196-
newBlockhashesToIndex.push(rpcBlock.hash);
197-
198-
rpcBlockResult = await this._rpc.getblock({
199-
blockhash: rpcBlock.previousblockhash,
200-
verbosity: 1,
201-
});
202-
if (rpcBlockResult.error !== null) {
203-
throw rpcBlockResult.error;
204-
}
205-
rpcBlock = rpcBlockResult.result;
206-
207-
storageBlockhash = await this._storage.getBlockhash(rpcBlock.height);
208-
}
209-
210-
// We can reset our storage state to where rpc node and storage matches
211-
if (currentStorageBlock && currentStorageBlock.hash !== rpcBlock.hash) {
212-
await this._storage.resetCurrentBlock(rpcBlock);
213-
}
214-
} else {
215-
const firstRuneHeight = Network.getFirstRuneHeight(this._network);
216-
217-
// Iterate through the rpc blocks until we reach first rune height
218-
const bestblockhashResult = await this._rpc.getbestblockhash();
219-
if (bestblockhashResult.error !== null) {
220-
throw bestblockhashResult.error;
221-
}
222-
const bestblockhash = bestblockhashResult.result;
223-
224-
let rpcBlockResult = await this._rpc.getblock({
225-
blockhash: bestblockhash,
226-
verbosity: 1,
227-
});
228-
if (rpcBlockResult.error !== null) {
229-
throw rpcBlockResult.error;
230-
}
231-
let rpcBlock = rpcBlockResult.result;
232-
233-
while (rpcBlock.height >= firstRuneHeight) {
234-
newBlockhashesToIndex.push(rpcBlock.hash);
235-
236-
rpcBlockResult = await this._rpc.getblock({
237-
blockhash: rpcBlock.previousblockhash,
238-
verbosity: 1,
239-
});
240-
if (rpcBlockResult.error !== null) {
241-
throw rpcBlockResult.error;
242-
}
243-
rpcBlock = rpcBlockResult.result;
244-
}
245-
}
246-
247-
// Finally start processing balances using newBlockhashesToIndex
248-
let blockhash = newBlockhashesToIndex.pop();
249-
while (blockhash !== undefined) {
250-
const blockResult = await this._rpc.getblock({ blockhash, verbosity: 2 });
251-
if (blockResult.error !== null) {
252-
throw blockResult.error;
253-
}
254-
const block = blockResult.result;
255-
const reorg = currentStorageBlock ? currentStorageBlock.height >= block.height : false;
256-
257-
const runeUpdater = new RuneUpdater(this._network, block, reorg, this._storage, this._rpc);
258-
259-
for (const [txIndex, tx] of block.tx.entries()) {
260-
await runeUpdater.indexRunes(tx, txIndex);
261-
}
262-
263-
await this._storage.saveBlockIndex(runeUpdater);
264-
blockhash = newBlockhashesToIndex.pop();
141+
blockhash = (await this._rpc.getblockhash({ height: blockheight })).result;
265142
}
266143
}
267144
}

‎src/rpcclient.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ export type GetBlockParams = {
6464
verbosity?: 0 | 1 | 2;
6565
};
6666

67+
export type GetBlockhashParams = {
68+
height: number;
69+
};
70+
6771
export type GetRawTransactionParams = {
6872
txid: string;
6973
verbose?: boolean;
@@ -93,7 +97,7 @@ export type RpcResponse<T> =
9397
};
9498

9599
export interface BitcoinRpcClient {
96-
getbestblockhash(): Promise<RpcResponse<string>>;
100+
getblockhash({ height }: GetBlockhashParams): Promise<RpcResponse<string>>;
97101
getblock<T extends GetBlockParams>({
98102
verbosity,
99103
blockhash,
@@ -103,6 +107,4 @@ export interface BitcoinRpcClient {
103107
verbose,
104108
blockhash,
105109
}: T): Promise<RpcResponse<GetRawTransactionReturn<T>>>;
106-
107-
getblockhashbyheight?(blockheight: number): Promise<string | null>;
108110
}

0 commit comments

Comments
 (0)
Please sign in to comment.