Skip to content

Commit

Permalink
feat: ordinal inscription_transfer code complete
Browse files Browse the repository at this point in the history
  • Loading branch information
lgalabru committed Mar 21, 2023
1 parent 5a00135 commit f55a5ee
Show file tree
Hide file tree
Showing 34 changed files with 1,207 additions and 868 deletions.
8 changes: 5 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 0 additions & 3 deletions components/chainhook-cli/Cargo.toml
Expand Up @@ -14,7 +14,6 @@ serde_derive = "1"
redis = "0.21.5"
serde-redis = "0.12.0"
hex = "0.4.3"
ciborium = "0.2.0"
rand = "0.8.5"
# tikv-client = { git = "https://github.com/tikv/client-rust.git", rev = "8f54e6114227718e256027df2577bbacdf425f86" }
# raft-proto = { git = "https://github.com/tikv/raft-rs", rev="f73766712a538c2f6eb135b455297ad6c03fc58d", version = "0.7.0"}
Expand All @@ -37,8 +36,6 @@ flume = "0.10.14"
ansi_term = "0.12.1"
atty = "0.2.14"
crossbeam-channel = "0.5.6"
rusqlite = { version = "0.27.0", features = ["bundled"] }
threadpool = "1.8.1"

[dev-dependencies]
criterion = "0.3"
Expand Down
50 changes: 43 additions & 7 deletions components/chainhook-cli/src/cli/mod.rs
@@ -1,13 +1,18 @@
use crate::block::DigestingCommand;
use crate::config::Config;
use crate::node::Node;
use crate::scan::bitcoin::{
build_bitcoin_traversal_local_storage, scan_bitcoin_chain_with_predicate,
};
use crate::scan::bitcoin::scan_bitcoin_chain_with_predicate;
use crate::scan::stacks::scan_stacks_chain_with_predicate;

use chainhook_event_observer::chainhooks::types::ChainhookFullSpecification;
use chainhook_event_observer::indexer::ordinals::db::{
build_bitcoin_traversal_local_storage, open_readonly_ordinals_db_conn,
retrieve_satoshi_point_using_local_storage,
};
use chainhook_event_observer::indexer::ordinals::ord::height::Height;
use chainhook_event_observer::observer::BitcoinConfig;
use chainhook_event_observer::utils::Context;
use chainhook_types::{BlockIdentifier, TransactionIdentifier};
use clap::{Parser, Subcommand};
use ctrlc;
use hiro_system_kit;
Expand Down Expand Up @@ -142,6 +147,8 @@ struct BuildOrdinalsTraversalsCommand {
pub start_block: u64,
/// Starting block
pub end_block: u64,
/// # of Networking thread
pub network_threads: usize,
/// Target Devnet network
#[clap(
long = "devnet",
Expand Down Expand Up @@ -175,6 +182,8 @@ struct BuildOrdinalsTraversalsCommand {

#[derive(Parser, PartialEq, Clone, Debug)]
struct GetSatoshiCommand {
/// Block height
pub block_height: u64,
/// Txid
pub txid: String,
/// Output index
Expand Down Expand Up @@ -276,19 +285,46 @@ async fn handle_command(opts: Opts, ctx: Context) -> Result<(), String> {
},
Command::Protocols(ProtocolsCommand::Ordinals(subcmd)) => match subcmd {
OrdinalsCommand::Satoshi(cmd) => {
let _config =
let config =
Config::default(cmd.devnet, cmd.testnet, cmd.mainnet, &cmd.config_path)?;
let transaction_identifier = TransactionIdentifier {
hash: cmd.txid.clone(),
};
let block_identifier = BlockIdentifier {
index: cmd.block_height,
hash: "".into(),
};
let storage_conn =
open_readonly_ordinals_db_conn(&config.expected_cache_path()).unwrap();
let (block_height, offset) = retrieve_satoshi_point_using_local_storage(
&storage_conn,
&block_identifier,
&transaction_identifier,
&ctx,
)?;
let satoshi_id = Height(block_height).starting_sat().0 + offset;
info!(
ctx.expect_logger(),
"Block: {block_height}, Offset {offset}:, Satoshi ID: {satoshi_id}",
);
}
OrdinalsCommand::Traversals(cmd) => {
let config =
Config::default(cmd.devnet, cmd.testnet, cmd.mainnet, &cmd.config_path)?;

build_bitcoin_traversal_local_storage(
config,
let bitcoin_config = BitcoinConfig {
username: config.network.bitcoin_node_rpc_username.clone(),
password: config.network.bitcoin_node_rpc_password.clone(),
rpc_url: config.network.bitcoin_node_rpc_url.clone(),
};

let _ = build_bitcoin_traversal_local_storage(
&bitcoin_config,
&config.expected_cache_path(),
cmd.start_block,
cmd.end_block,
&ctx,
6,
cmd.network_threads,
)
.await;
}
Expand Down
9 changes: 2 additions & 7 deletions components/chainhook-cli/src/config/mod.rs
Expand Up @@ -10,6 +10,8 @@ use std::path::PathBuf;

const DEFAULT_MAINNET_TSV_ARCHIVE: &str = "https://storage.googleapis.com/hirosystems-archive/mainnet/api/mainnet-blockchain-api-latest.tar.gz";
const DEFAULT_TESTNET_TSV_ARCHIVE: &str = "https://storage.googleapis.com/hirosystems-archive/testnet/api/testnet-blockchain-api-latest.tar.gz";
// const DEFAULT_MAINNET_TSV_ARCHIVE: &str = "https://archive.hiro.so/mainnet/stacks-blockchain-api/mainnet-stacks-blockchain-api-latest.gz";
// const DEFAULT_TESTNET_TSV_ARCHIVE: &str = "https://archive.hiro.so/testnet/stacks-blockchain-api/testnet-stacks-blockchain-api-latest.gz";

#[derive(Clone, Debug)]
pub struct Config {
Expand Down Expand Up @@ -181,13 +183,6 @@ impl Config {
destination_path
}

pub fn get_bitcoin_block_traversal_db_path(&self) -> PathBuf {
let mut destination_path = PathBuf::new();
destination_path.push(&self.storage.cache_path);
destination_path.push("bitcoin_block_traversal.sqlite");
destination_path
}

pub fn expected_stacks_node_event_source(&self) -> &String {
for source in self.event_sources.iter() {
if let EventSourceConfig::StacksNode(config) = source {
Expand Down
124 changes: 69 additions & 55 deletions components/chainhook-cli/src/node/mod.rs
@@ -1,23 +1,16 @@
use crate::config::Config;
use crate::node::ordinals::inscription_id::InscriptionId;
use chainhook_event_observer::bitcoincore_rpc::bitcoin::BlockHash;
use chainhook_event_observer::bitcoincore_rpc::jsonrpc;
use chainhook_event_observer::chainhooks::bitcoin::{
handle_bitcoin_hook_action, BitcoinChainhookOccurrence, BitcoinTriggerChainhook,
};
use chainhook_event_observer::chainhooks::types::{
BitcoinPredicateType, ChainhookConfig, OrdinalOperations, Protocols,
BitcoinPredicateType, ChainhookConfig, ChainhookFullSpecification, OrdinalOperations, Protocols,
};
use chainhook_event_observer::indexer::ordinals::indexing::entry::Entry;
use chainhook_event_observer::indexer::ordinals::indexing::{
HEIGHT_TO_BLOCK_HASH, INSCRIPTION_NUMBER_TO_INSCRIPTION_ID,
};
use chainhook_event_observer::indexer::ordinals::{self, initialize_ordinal_index};
use chainhook_event_observer::indexer::ordinals::{self, ord::initialize_ordinal_index};
use chainhook_event_observer::indexer::{self, BitcoinChainContext};
use chainhook_event_observer::observer::{
start_event_observer, EventObserverConfig, ObserverEvent,
start_event_observer, ApiKey, EventObserverConfig, ObserverEvent,
};
use chainhook_event_observer::redb::ReadableTable;
use chainhook_event_observer::utils::{file_append, send_request, Context};
use chainhook_event_observer::{
chainhooks::stacks::{
Expand Down Expand Up @@ -71,7 +64,7 @@ impl Node {
for key in chainhooks_to_load.iter() {
let chainhook = match redis_con.hget::<_, _, String>(key, "specification") {
Ok(spec) => {
ChainhookSpecification::deserialize_specification(&spec, key).unwrap()
ChainhookFullSpecification::deserialize_specification(&spec, key).unwrap()
// todo
}
Err(e) => {
Expand All @@ -84,8 +77,30 @@ impl Node {
continue;
}
};
// TODO
// chainhook_config.register_hook(chainhook);

match chainhook_config.register_hook(
(
&self.config.network.bitcoin_network,
&self.config.network.stacks_network,
),
chainhook,
&ApiKey(None),
) {
Ok(spec) => {
info!(
self.ctx.expect_logger(),
"Predicate {} retrieved from storage and loaded",
spec.uuid(),
);
}
Err(e) => {
error!(
self.ctx.expect_logger(),
"Failed loading predicate from storage: {}",
e.to_string()
);
}
}
}
}

Expand Down Expand Up @@ -128,7 +143,6 @@ impl Node {
panic!()
}
};
let mut bitcoin_context = BitcoinChainContext::new(Some(ordinal_index));

let context_cloned = self.ctx.clone();
let _ = std::thread::spawn(move || {
Expand Down Expand Up @@ -311,50 +325,51 @@ impl Node {
}
ChainhookSpecification::Bitcoin(predicate_spec) => {
let mut inscriptions_hints = BTreeMap::new();
let mut use_hinting = false;
let use_hinting = false;
if let BitcoinPredicateType::Protocol(Protocols::Ordinal(
OrdinalOperations::InscriptionRevealed,
)) = &predicate_spec.predicate
{
if let Some(ref ordinal_index) = bitcoin_context.ordinal_index {
for (inscription_number, inscription_id) in ordinal_index
.database
.begin_read()
.unwrap()
.open_table(INSCRIPTION_NUMBER_TO_INSCRIPTION_ID)
.unwrap()
.iter()
.unwrap()
{
let inscription =
InscriptionId::load(*inscription_id.value());
println!(
"{} -> {}",
inscription_number.value(),
inscription
);

let entry = ordinal_index
.get_inscription_entry(inscription)
.unwrap()
.unwrap();
println!("{:?}", entry);

let blockhash = ordinal_index
.database
.begin_read()
.unwrap()
.open_table(HEIGHT_TO_BLOCK_HASH)
.unwrap()
.get(&entry.height)
.unwrap()
.map(|k| BlockHash::load(*k.value()))
.unwrap();

inscriptions_hints.insert(entry.height, blockhash);
use_hinting = true;
}
}
inscriptions_hints.insert(1, 1);
// if let Some(ref ordinal_index) = bitcoin_context.ordinal_index {
// for (inscription_number, inscription_id) in ordinal_index
// .database
// .begin_read()
// .unwrap()
// .open_table(INSCRIPTION_NUMBER_TO_INSCRIPTION_ID)
// .unwrap()
// .iter()
// .unwrap()
// {
// let inscription =
// InscriptionId::load(*inscription_id.value());
// println!(
// "{} -> {}",
// inscription_number.value(),
// inscription
// );

// let entry = ordinal_index
// .get_inscription_entry(inscription)
// .unwrap()
// .unwrap();
// println!("{:?}", entry);

// let blockhash = ordinal_index
// .database
// .begin_read()
// .unwrap()
// .open_table(HEIGHT_TO_BLOCK_HASH)
// .unwrap()
// .get(&entry.height)
// .unwrap()
// .map(|k| BlockHash::load(*k.value()))
// .unwrap();

// inscriptions_hints.insert(entry.height, blockhash);
// use_hinting = true;
// }
// }
}

let start_block = match predicate_spec.start_block {
Expand Down Expand Up @@ -469,7 +484,6 @@ impl Node {
let block = indexer::bitcoin::standardize_bitcoin_block(
&self.config.network,
raw_block,
&mut bitcoin_context,
&self.ctx,
)?;

Expand Down

0 comments on commit f55a5ee

Please sign in to comment.