Skip to content

Commit

Permalink
feat: expose more functions for working with the indexer
Browse files Browse the repository at this point in the history
  • Loading branch information
lgalabru committed Aug 29, 2023
1 parent d72a862 commit 654fead
Show file tree
Hide file tree
Showing 3 changed files with 185 additions and 13 deletions.
44 changes: 40 additions & 4 deletions components/ordhook-sdk-js/lib/index.ts
Expand Up @@ -2,7 +2,11 @@

const {
ordinalsIndexerNew,
ordinalsIndexerStart,
ordinalsIndexerStreamBlocks,
ordinalsIndexerReplayBlocks,
ordinalsIndexerDropBlocks,
ordinalsIndexerSyncBlocks,
ordinalsIndexerRewriteBlocks,
ordinalsIndexerOnBlockApply,
ordinalsIndexerOnBlockUndo,
} = require("../native/index.node");
Expand Down Expand Up @@ -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);
}

/**
Expand Down
11 changes: 10 additions & 1 deletion components/ordhook-sdk-js/lib/test.ts
Expand Up @@ -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]);
143 changes: 135 additions & 8 deletions components/ordhook-sdk-js/src/lib.rs
Expand Up @@ -39,7 +39,11 @@ struct OrdinalsIndexer {

#[allow(dead_code)]
enum IndexerCommand {
Start,
StreamBlocks,
SyncBlocks,
DropBlocks(Vec<u64>),
RewriteBlocks(Vec<u64>),
ReplayBlocks(Vec<u64>),
Stop,
}

Expand All @@ -64,7 +68,7 @@ impl OrdinalsIndexer {
logger: Some(logger),
tracer: false,
};

// Initialize service
// {
// let _ = initialize_ordhook_db(&ordhook_config.expected_cache_path(), &ctx);
Expand Down Expand Up @@ -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)
Expand All @@ -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;
}
Expand All @@ -177,8 +193,28 @@ impl OrdinalsIndexer {
}
}

fn start(&self) -> Result<bool, String> {
let _ = self.command_tx.send(IndexerCommand::Start);
fn stream_blocks(&self) -> Result<bool, String> {
let _ = self.command_tx.send(IndexerCommand::StreamBlocks);
Ok(true)
}

fn replay_blocks(&self, blocks: Vec<u64>) -> Result<bool, String> {
let _ = self.command_tx.send(IndexerCommand::ReplayBlocks(blocks));
Ok(true)
}

fn drop_blocks(&self, blocks: Vec<u64>) -> Result<bool, String> {
let _ = self.command_tx.send(IndexerCommand::DropBlocks(blocks));
Ok(true)
}

fn rewrite_blocks(&self, blocks: Vec<u64>) -> Result<bool, String> {
let _ = self.command_tx.send(IndexerCommand::RewriteBlocks(blocks));
Ok(true)
}

fn sync_blocks(&self) -> Result<bool, String> {
let _ = self.command_tx.send(IndexerCommand::SyncBlocks);
Ok(true)
}

Expand Down Expand Up @@ -249,10 +285,88 @@ impl OrdinalsIndexer {
Ok(cx.boxed(devnet))
}

fn js_start(mut cx: FunctionContext) -> JsResult<JsUndefined> {
fn js_stream_blocks(mut cx: FunctionContext) -> JsResult<JsUndefined> {
cx.this()
.downcast_or_throw::<JsBox<OrdinalsIndexer>, _>(&mut cx)?
.stream_blocks()
.or_else(|err| cx.throw_error(err.to_string()))?;

Ok(cx.undefined())
}

fn js_replay_blocks(mut cx: FunctionContext) -> JsResult<JsUndefined> {
let blocks = {
let seq = cx
.argument::<JsArray>(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::<JsNumber, _>(&mut cx).unwrap();
blocks.push(block.value(&mut cx) as u64);
}
blocks
};

cx.this()
.downcast_or_throw::<JsBox<OrdinalsIndexer>, _>(&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<JsUndefined> {
let blocks = {
let seq = cx
.argument::<JsArray>(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::<JsNumber, _>(&mut cx).unwrap();
blocks.push(block.value(&mut cx) as u64);
}
blocks
};

cx.this()
.downcast_or_throw::<JsBox<OrdinalsIndexer>, _>(&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<JsUndefined> {
cx.this()
.downcast_or_throw::<JsBox<OrdinalsIndexer>, _>(&mut cx)?
.sync_blocks()
.or_else(|err| cx.throw_error(err.to_string()))?;

Ok(cx.undefined())
}

fn js_rewrite_blocks(mut cx: FunctionContext) -> JsResult<JsUndefined> {
let blocks = {
let seq = cx
.argument::<JsArray>(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::<JsNumber, _>(&mut cx).unwrap();
blocks.push(block.value(&mut cx) as u64);
}
blocks
};

cx.this()
.downcast_or_throw::<JsBox<OrdinalsIndexer>, _>(&mut cx)?
.start()
.rewrite_blocks(blocks)
.or_else(|err| cx.throw_error(err.to_string()))?;

Ok(cx.undefined())
Expand Down Expand Up @@ -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",
Expand Down

0 comments on commit 654fead

Please sign in to comment.