Skip to content

Commit 654fead

Browse files
author
Ludo Galabru
committed
feat: expose more functions for working with the indexer
1 parent d72a862 commit 654fead

File tree

3 files changed

+185
-13
lines changed

3 files changed

+185
-13
lines changed

components/ordhook-sdk-js/lib/index.ts

+40-4
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,11 @@
22

33
const {
44
ordinalsIndexerNew,
5-
ordinalsIndexerStart,
5+
ordinalsIndexerStreamBlocks,
6+
ordinalsIndexerReplayBlocks,
7+
ordinalsIndexerDropBlocks,
8+
ordinalsIndexerSyncBlocks,
9+
ordinalsIndexerRewriteBlocks,
610
ordinalsIndexerOnBlockApply,
711
ordinalsIndexerOnBlockUndo,
812
} = require("../native/index.node");
@@ -38,11 +42,43 @@ export class OrdinalsIndexer {
3842
}
3943

4044
/**
41-
* @summary Start indexing ordinals
45+
* @summary Start streaming blocks
4246
* @memberof OrdinalsIndexer
4347
*/
44-
start() {
45-
return ordinalsIndexerStart.call(this.handle);
48+
streamBlocks() {
49+
return ordinalsIndexerStreamBlocks.call(this.handle);
50+
}
51+
52+
/**
53+
* @summary Drop a set of blocks
54+
* @memberof OrdinalsIndexer
55+
*/
56+
dropBlocks(blocks: number[]) {
57+
return ordinalsIndexerDropBlocks.call(this.handle, blocks);
58+
}
59+
60+
/**
61+
* @summary Drop, downloard and re-index a set of blocks
62+
* @memberof OrdinalsIndexer
63+
*/
64+
rewriteBlocks(blocks: number[]) {
65+
return ordinalsIndexerRewriteBlocks.call(this.handle, blocks);
66+
}
67+
68+
/**
69+
* @summary Replay a set of blocks
70+
* @memberof OrdinalsIndexer
71+
*/
72+
replayBlocks(blocks: number[]) {
73+
return ordinalsIndexerReplayBlocks.call(this.handle, blocks);
74+
}
75+
76+
/**
77+
* @summary Download and index blocks
78+
* @memberof OrdinalsIndexer
79+
*/
80+
syncBlocks() {
81+
return ordinalsIndexerSyncBlocks.call(this.handle);
4682
}
4783

4884
/**

components/ordhook-sdk-js/lib/test.ts

+10-1
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,13 @@ indexer.undoBlock(block => {
1616
console.log(`Hello from JS ${JSON.stringify(block)}`);
1717
});
1818

19-
indexer.start();
19+
20+
// indexer.streamBlocks();
21+
22+
indexer.dropBlocks([32103, 32104]);
23+
24+
indexer.rewriteBlocks([32103, 32104]);
25+
26+
indexer.syncBlocks();
27+
28+
indexer.replayBlocks([32103, 32104]);

components/ordhook-sdk-js/src/lib.rs

+135-8
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,11 @@ struct OrdinalsIndexer {
3939

4040
#[allow(dead_code)]
4141
enum IndexerCommand {
42-
Start,
42+
StreamBlocks,
43+
SyncBlocks,
44+
DropBlocks(Vec<u64>),
45+
RewriteBlocks(Vec<u64>),
46+
ReplayBlocks(Vec<u64>),
4347
Stop,
4448
}
4549

@@ -64,7 +68,7 @@ impl OrdinalsIndexer {
6468
logger: Some(logger),
6569
tracer: false,
6670
};
67-
71+
6872
// Initialize service
6973
// {
7074
// let _ = initialize_ordhook_db(&ordhook_config.expected_cache_path(), &ctx);
@@ -151,7 +155,7 @@ impl OrdinalsIndexer {
151155
};
152156

153157
match cmd {
154-
IndexerCommand::Start => {
158+
IndexerCommand::StreamBlocks => {
155159
// We start the service as soon as the start() method is being called.
156160
let future = service.catch_up_with_chain_tip(false, &observer_config);
157161
let _ = hiro_system_kit::nestable_block_on(future)
@@ -163,6 +167,18 @@ impl OrdinalsIndexer {
163167
let _ = service.start_main_runloop(&command_tx, event_rx, None);
164168
break;
165169
}
170+
IndexerCommand::ReplayBlocks(blocks) => {
171+
println!("Will replay blocks {:?}", blocks);
172+
}
173+
IndexerCommand::DropBlocks(blocks) => {
174+
println!("Will drop blocks {:?}", blocks);
175+
}
176+
IndexerCommand::RewriteBlocks(blocks) => {
177+
println!("Will rewrite blocks {:?}", blocks);
178+
}
179+
IndexerCommand::SyncBlocks => {
180+
println!("Will sync blocks");
181+
}
166182
IndexerCommand::Stop => {
167183
break;
168184
}
@@ -177,8 +193,28 @@ impl OrdinalsIndexer {
177193
}
178194
}
179195

180-
fn start(&self) -> Result<bool, String> {
181-
let _ = self.command_tx.send(IndexerCommand::Start);
196+
fn stream_blocks(&self) -> Result<bool, String> {
197+
let _ = self.command_tx.send(IndexerCommand::StreamBlocks);
198+
Ok(true)
199+
}
200+
201+
fn replay_blocks(&self, blocks: Vec<u64>) -> Result<bool, String> {
202+
let _ = self.command_tx.send(IndexerCommand::ReplayBlocks(blocks));
203+
Ok(true)
204+
}
205+
206+
fn drop_blocks(&self, blocks: Vec<u64>) -> Result<bool, String> {
207+
let _ = self.command_tx.send(IndexerCommand::DropBlocks(blocks));
208+
Ok(true)
209+
}
210+
211+
fn rewrite_blocks(&self, blocks: Vec<u64>) -> Result<bool, String> {
212+
let _ = self.command_tx.send(IndexerCommand::RewriteBlocks(blocks));
213+
Ok(true)
214+
}
215+
216+
fn sync_blocks(&self) -> Result<bool, String> {
217+
let _ = self.command_tx.send(IndexerCommand::SyncBlocks);
182218
Ok(true)
183219
}
184220

@@ -249,10 +285,88 @@ impl OrdinalsIndexer {
249285
Ok(cx.boxed(devnet))
250286
}
251287

252-
fn js_start(mut cx: FunctionContext) -> JsResult<JsUndefined> {
288+
fn js_stream_blocks(mut cx: FunctionContext) -> JsResult<JsUndefined> {
289+
cx.this()
290+
.downcast_or_throw::<JsBox<OrdinalsIndexer>, _>(&mut cx)?
291+
.stream_blocks()
292+
.or_else(|err| cx.throw_error(err.to_string()))?;
293+
294+
Ok(cx.undefined())
295+
}
296+
297+
fn js_replay_blocks(mut cx: FunctionContext) -> JsResult<JsUndefined> {
298+
let blocks = {
299+
let seq = cx
300+
.argument::<JsArray>(0)?
301+
.root(&mut cx)
302+
.into_inner(&mut cx)
303+
.to_vec(&mut cx)?;
304+
let mut blocks = vec![];
305+
for item in seq.iter() {
306+
let block = item.downcast::<JsNumber, _>(&mut cx).unwrap();
307+
blocks.push(block.value(&mut cx) as u64);
308+
}
309+
blocks
310+
};
311+
312+
cx.this()
313+
.downcast_or_throw::<JsBox<OrdinalsIndexer>, _>(&mut cx)?
314+
.replay_blocks(blocks)
315+
.or_else(|err| cx.throw_error(err.to_string()))?;
316+
317+
Ok(cx.undefined())
318+
}
319+
320+
fn js_drop_blocks(mut cx: FunctionContext) -> JsResult<JsUndefined> {
321+
let blocks = {
322+
let seq = cx
323+
.argument::<JsArray>(0)?
324+
.root(&mut cx)
325+
.into_inner(&mut cx)
326+
.to_vec(&mut cx)?;
327+
let mut blocks = vec![];
328+
for item in seq.iter() {
329+
let block = item.downcast::<JsNumber, _>(&mut cx).unwrap();
330+
blocks.push(block.value(&mut cx) as u64);
331+
}
332+
blocks
333+
};
334+
335+
cx.this()
336+
.downcast_or_throw::<JsBox<OrdinalsIndexer>, _>(&mut cx)?
337+
.drop_blocks(blocks)
338+
.or_else(|err| cx.throw_error(err.to_string()))?;
339+
340+
Ok(cx.undefined())
341+
}
342+
343+
fn js_sync_blocks(mut cx: FunctionContext) -> JsResult<JsUndefined> {
344+
cx.this()
345+
.downcast_or_throw::<JsBox<OrdinalsIndexer>, _>(&mut cx)?
346+
.sync_blocks()
347+
.or_else(|err| cx.throw_error(err.to_string()))?;
348+
349+
Ok(cx.undefined())
350+
}
351+
352+
fn js_rewrite_blocks(mut cx: FunctionContext) -> JsResult<JsUndefined> {
353+
let blocks = {
354+
let seq = cx
355+
.argument::<JsArray>(0)?
356+
.root(&mut cx)
357+
.into_inner(&mut cx)
358+
.to_vec(&mut cx)?;
359+
let mut blocks = vec![];
360+
for item in seq.iter() {
361+
let block = item.downcast::<JsNumber, _>(&mut cx).unwrap();
362+
blocks.push(block.value(&mut cx) as u64);
363+
}
364+
blocks
365+
};
366+
253367
cx.this()
254368
.downcast_or_throw::<JsBox<OrdinalsIndexer>, _>(&mut cx)?
255-
.start()
369+
.rewrite_blocks(blocks)
256370
.or_else(|err| cx.throw_error(err.to_string()))?;
257371

258372
Ok(cx.undefined())
@@ -288,7 +402,20 @@ impl OrdinalsIndexer {
288402
#[neon::main]
289403
fn main(mut cx: ModuleContext) -> NeonResult<()> {
290404
cx.export_function("ordinalsIndexerNew", OrdinalsIndexer::js_new)?;
291-
cx.export_function("ordinalsIndexerStart", OrdinalsIndexer::js_start)?;
405+
cx.export_function(
406+
"ordinalsIndexerStreamBlocks",
407+
OrdinalsIndexer::js_stream_blocks,
408+
)?;
409+
cx.export_function(
410+
"ordinalsIndexerReplayBlocks",
411+
OrdinalsIndexer::js_replay_blocks,
412+
)?;
413+
cx.export_function("ordinalsIndexerDropBlocks", OrdinalsIndexer::js_drop_blocks)?;
414+
cx.export_function("ordinalsIndexerSyncBlocks", OrdinalsIndexer::js_sync_blocks)?;
415+
cx.export_function(
416+
"ordinalsIndexerRewriteBlocks",
417+
OrdinalsIndexer::js_rewrite_blocks,
418+
)?;
292419
cx.export_function("ordinalsIndexerTerminate", OrdinalsIndexer::js_terminate)?;
293420
cx.export_function(
294421
"ordinalsIndexerOnBlockApply",

0 commit comments

Comments
 (0)