From 654feadbdfb3b5e108d3ca32e865167e49ed6d02 Mon Sep 17 00:00:00 2001 From: Ludo Galabru Date: Tue, 29 Aug 2023 16:44:24 -0400 Subject: [PATCH] feat: expose more functions for working with the indexer --- components/ordhook-sdk-js/lib/index.ts | 44 +++++++- components/ordhook-sdk-js/lib/test.ts | 11 +- components/ordhook-sdk-js/src/lib.rs | 143 +++++++++++++++++++++++-- 3 files changed, 185 insertions(+), 13 deletions(-) diff --git a/components/ordhook-sdk-js/lib/index.ts b/components/ordhook-sdk-js/lib/index.ts index fe5e55d3..657786ba 100644 --- a/components/ordhook-sdk-js/lib/index.ts +++ b/components/ordhook-sdk-js/lib/index.ts @@ -2,7 +2,11 @@ const { ordinalsIndexerNew, - ordinalsIndexerStart, + ordinalsIndexerStreamBlocks, + ordinalsIndexerReplayBlocks, + ordinalsIndexerDropBlocks, + ordinalsIndexerSyncBlocks, + ordinalsIndexerRewriteBlocks, ordinalsIndexerOnBlockApply, ordinalsIndexerOnBlockUndo, } = require("../native/index.node"); @@ -38,11 +42,43 @@ export class OrdinalsIndexer { } /** - * @summary Start indexing ordinals + * @summary Start streaming blocks * @memberof OrdinalsIndexer */ - start() { - return ordinalsIndexerStart.call(this.handle); + streamBlocks() { + return ordinalsIndexerStreamBlocks.call(this.handle); + } + + /** + * @summary Drop a set of blocks + * @memberof OrdinalsIndexer + */ + dropBlocks(blocks: number[]) { + return ordinalsIndexerDropBlocks.call(this.handle, blocks); + } + + /** + * @summary Drop, downloard and re-index a set of blocks + * @memberof OrdinalsIndexer + */ + rewriteBlocks(blocks: number[]) { + return ordinalsIndexerRewriteBlocks.call(this.handle, blocks); + } + + /** + * @summary Replay a set of blocks + * @memberof OrdinalsIndexer + */ + replayBlocks(blocks: number[]) { + return ordinalsIndexerReplayBlocks.call(this.handle, blocks); + } + + /** + * @summary Download and index blocks + * @memberof OrdinalsIndexer + */ + syncBlocks() { + return ordinalsIndexerSyncBlocks.call(this.handle); } /** diff --git a/components/ordhook-sdk-js/lib/test.ts b/components/ordhook-sdk-js/lib/test.ts index 8116eab5..bc74a93d 100644 --- a/components/ordhook-sdk-js/lib/test.ts +++ b/components/ordhook-sdk-js/lib/test.ts @@ -16,4 +16,13 @@ indexer.undoBlock(block => { console.log(`Hello from JS ${JSON.stringify(block)}`); }); -indexer.start(); + +// indexer.streamBlocks(); + +indexer.dropBlocks([32103, 32104]); + +indexer.rewriteBlocks([32103, 32104]); + +indexer.syncBlocks(); + +indexer.replayBlocks([32103, 32104]); diff --git a/components/ordhook-sdk-js/src/lib.rs b/components/ordhook-sdk-js/src/lib.rs index ad81cd2b..5460fc76 100644 --- a/components/ordhook-sdk-js/src/lib.rs +++ b/components/ordhook-sdk-js/src/lib.rs @@ -39,7 +39,11 @@ struct OrdinalsIndexer { #[allow(dead_code)] enum IndexerCommand { - Start, + StreamBlocks, + SyncBlocks, + DropBlocks(Vec), + RewriteBlocks(Vec), + ReplayBlocks(Vec), Stop, } @@ -64,7 +68,7 @@ impl OrdinalsIndexer { logger: Some(logger), tracer: false, }; - + // Initialize service // { // let _ = initialize_ordhook_db(&ordhook_config.expected_cache_path(), &ctx); @@ -151,7 +155,7 @@ impl OrdinalsIndexer { }; match cmd { - IndexerCommand::Start => { + IndexerCommand::StreamBlocks => { // We start the service as soon as the start() method is being called. let future = service.catch_up_with_chain_tip(false, &observer_config); let _ = hiro_system_kit::nestable_block_on(future) @@ -163,6 +167,18 @@ impl OrdinalsIndexer { let _ = service.start_main_runloop(&command_tx, event_rx, None); break; } + IndexerCommand::ReplayBlocks(blocks) => { + println!("Will replay blocks {:?}", blocks); + } + IndexerCommand::DropBlocks(blocks) => { + println!("Will drop blocks {:?}", blocks); + } + IndexerCommand::RewriteBlocks(blocks) => { + println!("Will rewrite blocks {:?}", blocks); + } + IndexerCommand::SyncBlocks => { + println!("Will sync blocks"); + } IndexerCommand::Stop => { break; } @@ -177,8 +193,28 @@ impl OrdinalsIndexer { } } - fn start(&self) -> Result { - let _ = self.command_tx.send(IndexerCommand::Start); + fn stream_blocks(&self) -> Result { + let _ = self.command_tx.send(IndexerCommand::StreamBlocks); + Ok(true) + } + + fn replay_blocks(&self, blocks: Vec) -> Result { + let _ = self.command_tx.send(IndexerCommand::ReplayBlocks(blocks)); + Ok(true) + } + + fn drop_blocks(&self, blocks: Vec) -> Result { + let _ = self.command_tx.send(IndexerCommand::DropBlocks(blocks)); + Ok(true) + } + + fn rewrite_blocks(&self, blocks: Vec) -> Result { + let _ = self.command_tx.send(IndexerCommand::RewriteBlocks(blocks)); + Ok(true) + } + + fn sync_blocks(&self) -> Result { + let _ = self.command_tx.send(IndexerCommand::SyncBlocks); Ok(true) } @@ -249,10 +285,88 @@ impl OrdinalsIndexer { Ok(cx.boxed(devnet)) } - fn js_start(mut cx: FunctionContext) -> JsResult { + fn js_stream_blocks(mut cx: FunctionContext) -> JsResult { + cx.this() + .downcast_or_throw::, _>(&mut cx)? + .stream_blocks() + .or_else(|err| cx.throw_error(err.to_string()))?; + + Ok(cx.undefined()) + } + + fn js_replay_blocks(mut cx: FunctionContext) -> JsResult { + let blocks = { + let seq = cx + .argument::(0)? + .root(&mut cx) + .into_inner(&mut cx) + .to_vec(&mut cx)?; + let mut blocks = vec![]; + for item in seq.iter() { + let block = item.downcast::(&mut cx).unwrap(); + blocks.push(block.value(&mut cx) as u64); + } + blocks + }; + + cx.this() + .downcast_or_throw::, _>(&mut cx)? + .replay_blocks(blocks) + .or_else(|err| cx.throw_error(err.to_string()))?; + + Ok(cx.undefined()) + } + + fn js_drop_blocks(mut cx: FunctionContext) -> JsResult { + let blocks = { + let seq = cx + .argument::(0)? + .root(&mut cx) + .into_inner(&mut cx) + .to_vec(&mut cx)?; + let mut blocks = vec![]; + for item in seq.iter() { + let block = item.downcast::(&mut cx).unwrap(); + blocks.push(block.value(&mut cx) as u64); + } + blocks + }; + + cx.this() + .downcast_or_throw::, _>(&mut cx)? + .drop_blocks(blocks) + .or_else(|err| cx.throw_error(err.to_string()))?; + + Ok(cx.undefined()) + } + + fn js_sync_blocks(mut cx: FunctionContext) -> JsResult { + cx.this() + .downcast_or_throw::, _>(&mut cx)? + .sync_blocks() + .or_else(|err| cx.throw_error(err.to_string()))?; + + Ok(cx.undefined()) + } + + fn js_rewrite_blocks(mut cx: FunctionContext) -> JsResult { + let blocks = { + let seq = cx + .argument::(0)? + .root(&mut cx) + .into_inner(&mut cx) + .to_vec(&mut cx)?; + let mut blocks = vec![]; + for item in seq.iter() { + let block = item.downcast::(&mut cx).unwrap(); + blocks.push(block.value(&mut cx) as u64); + } + blocks + }; + cx.this() .downcast_or_throw::, _>(&mut cx)? - .start() + .rewrite_blocks(blocks) .or_else(|err| cx.throw_error(err.to_string()))?; Ok(cx.undefined()) @@ -288,7 +402,20 @@ impl OrdinalsIndexer { #[neon::main] fn main(mut cx: ModuleContext) -> NeonResult<()> { cx.export_function("ordinalsIndexerNew", OrdinalsIndexer::js_new)?; - cx.export_function("ordinalsIndexerStart", OrdinalsIndexer::js_start)?; + cx.export_function( + "ordinalsIndexerStreamBlocks", + OrdinalsIndexer::js_stream_blocks, + )?; + cx.export_function( + "ordinalsIndexerReplayBlocks", + OrdinalsIndexer::js_replay_blocks, + )?; + cx.export_function("ordinalsIndexerDropBlocks", OrdinalsIndexer::js_drop_blocks)?; + cx.export_function("ordinalsIndexerSyncBlocks", OrdinalsIndexer::js_sync_blocks)?; + cx.export_function( + "ordinalsIndexerRewriteBlocks", + OrdinalsIndexer::js_rewrite_blocks, + )?; cx.export_function("ordinalsIndexerTerminate", OrdinalsIndexer::js_terminate)?; cx.export_function( "ordinalsIndexerOnBlockApply",