Skip to content

Commit

Permalink
Merge pull request #344 from hirosystems/develop
Browse files Browse the repository at this point in the history
release to main
  • Loading branch information
rafaelcr authored Jul 23, 2024
2 parents 7348ef7 + 7ee763a commit e96be13
Show file tree
Hide file tree
Showing 24 changed files with 559 additions and 709 deletions.
14 changes: 12 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,20 @@ jobs:
target/
key: ${{ runner.os }}-cargo-${{ hashFiles('**/Cargo.lock') }}

- name: Cargo test
- name: Update Rust
run: |
rustup update
RUST_BACKTRACE=1 cargo test --all -- --test-threads=1
cargo install --force cargo-tarpaulin
- name: Run Core tests
run: |
cd ${{ github.workspace }}/components/ordhook-core
cargo tarpaulin --skip-clean --out lcov -- --test-threads=1
- name: Run CLI tests
run: |
cd ${{ github.workspace }}/components/ordhook-cli
cargo tarpaulin --skip-clean --out lcov -- --test-threads=1
- name: Upload coverage reports to Codecov
uses: codecov/[email protected]
Expand Down
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ components/chainhook-types-js/dist
*.tar.gz
*.zip
*.rdb
Ordhook.toml

cache/
./tests
Expand Down
27 changes: 2 additions & 25 deletions components/ordhook-cli/src/cli/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ use crate::config::file::ConfigFile;
use crate::config::generator::generate_config;
use clap::{Parser, Subcommand};
use hiro_system_kit;
use ordhook::chainhook_sdk::bitcoincore_rpc::{Auth, Client, RpcApi};
use ordhook::chainhook_sdk::chainhooks::types::{
BitcoinChainhookSpecification, HttpHook, InscriptionFeedData, OrdinalsMetaProtocol,
};
Expand Down Expand Up @@ -35,6 +34,7 @@ use ordhook::download::download_ordinals_dataset_if_required;
use ordhook::scan::bitcoin::scan_bitcoin_chainstate_via_rpc_using_predicate;
use ordhook::service::observers::initialize_observers_db;
use ordhook::service::{start_observer_forwarding, Service};
use ordhook::utils::bitcoind::bitcoind_get_block_height;
use ordhook::{hex, initialize_db};
use reqwest::Client as HttpClient;
use std::collections::HashSet;
Expand Down Expand Up @@ -553,7 +553,7 @@ async fn handle_command(opts: Opts, ctx: &Context) -> Result<(), String> {
ctx.expect_logger(),
"Checking {}...", config.network.bitcoind_rpc_url
);
let tip = check_bitcoind_connection(&config).await?;
let tip = bitcoind_get_block_height(&config, ctx);
if let Some(highest_desired) = block_range.pop_back() {
if tip < highest_desired {
error!(ctx.expect_logger(), "Unable to scan desired block range: underlying bitcoind synchronized until block #{} ", tip);
Expand Down Expand Up @@ -1035,29 +1035,6 @@ pub fn build_predicate_from_cli(
Ok(predicate)
}

pub async fn check_bitcoind_connection(config: &Config) -> Result<u64, String> {
let auth = Auth::UserPass(
config.network.bitcoind_rpc_username.clone(),
config.network.bitcoind_rpc_password.clone(),
);

let bitcoin_rpc = match Client::new(&config.network.bitcoind_rpc_url, auth) {
Ok(con) => con,
Err(message) => {
return Err(format!("unable to connect to bitcoind: {}", message));
}
};

let end_block = match bitcoin_rpc.get_blockchain_info() {
Ok(result) => result.blocks,
Err(e) => {
return Err(format!("unable to connect to bitcoind: {}", e));
}
};

Ok(end_block)
}

fn parse_blocks_heights_spec(
blocks_interval: &Option<String>,
blocks: &Option<String>,
Expand Down
103 changes: 48 additions & 55 deletions components/ordhook-core/src/core/meta_protocols/brc20/db.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
use std::{collections::HashMap, path::PathBuf};

use crate::db::{
create_or_open_readwrite_db, open_existing_readonly_db, perform_query_one, perform_query_set,
use crate::{
db::{
create_or_open_readwrite_db, open_existing_readonly_db, perform_query_one,
perform_query_set,
},
try_warn,
};
use chainhook_sdk::{
types::{
Expand Down Expand Up @@ -66,13 +70,13 @@ pub fn initialize_brc20_db(base_dir: Option<&PathBuf>, ctx: &Context) -> Connect
)",
[],
) {
ctx.try_log(|logger| warn!(logger, "Unable to create table tokens: {}", e.to_string()));
try_warn!(ctx, "Unable to create table tokens: {}", e.to_string());
} else {
if let Err(e) = conn.execute(
"CREATE INDEX IF NOT EXISTS index_tokens_on_block_height ON tokens(block_height);",
[],
) {
ctx.try_log(|logger| warn!(logger, "unable to create brc20.sqlite: {}", e.to_string()));
try_warn!(ctx, "unable to create brc20.sqlite: {}", e.to_string());
}
}
if let Err(e) = conn.execute(
Expand All @@ -90,37 +94,37 @@ pub fn initialize_brc20_db(base_dir: Option<&PathBuf>, ctx: &Context) -> Connect
)",
[],
) {
ctx.try_log(|logger| warn!(logger, "Unable to create table ledger: {}", e.to_string()));
try_warn!(ctx, "Unable to create table ledger: {}", e.to_string());
} else {
if let Err(e) = conn.execute(
"CREATE INDEX IF NOT EXISTS index_ledger_on_tick_address ON ledger(tick, address);",
[],
) {
ctx.try_log(|logger| warn!(logger, "unable to create brc20.sqlite: {}", e.to_string()));
try_warn!(ctx, "unable to create brc20.sqlite: {}", e.to_string());
}
if let Err(e) = conn.execute(
"CREATE INDEX IF NOT EXISTS index_ledger_on_ordinal_number_operation ON ledger(ordinal_number, operation);",
[],
) {
ctx.try_log(|logger| warn!(logger, "unable to create brc20.sqlite: {}", e.to_string()));
try_warn!(ctx, "unable to create brc20.sqlite: {}", e.to_string());
}
if let Err(e) = conn.execute(
"CREATE INDEX IF NOT EXISTS index_ledger_on_block_height_operation ON ledger(block_height, operation);",
[],
) {
ctx.try_log(|logger| warn!(logger, "unable to create brc20.sqlite: {}", e.to_string()));
try_warn!(ctx, "unable to create brc20.sqlite: {}", e.to_string());
}
if let Err(e) = conn.execute(
"CREATE INDEX IF NOT EXISTS index_ledger_on_inscription_id ON ledger(inscription_id);",
[],
) {
ctx.try_log(|logger| warn!(logger, "unable to create brc20.sqlite: {}", e.to_string()));
try_warn!(ctx, "unable to create brc20.sqlite: {}", e.to_string());
}
if let Err(e) = conn.execute(
"CREATE INDEX IF NOT EXISTS index_ledger_on_inscription_number ON ledger(inscription_number);",
[],
) {
ctx.try_log(|logger| warn!(logger, "unable to create brc20.sqlite: {}", e.to_string()));
try_warn!(ctx, "unable to create brc20.sqlite: {}", e.to_string());
}
}

Expand Down Expand Up @@ -155,14 +159,14 @@ pub fn delete_activity_in_block_range(
"DELETE FROM ledger WHERE block_height >= ?1 AND block_height <= ?2",
rusqlite::params![&start_block, &end_block],
) {
ctx.try_log(|logger| warn!(logger, "unable to query brc20.sqlite: {}", e.to_string()));
try_warn!(ctx, "unable to query brc20.sqlite: {}", e.to_string());
std::thread::sleep(std::time::Duration::from_secs(1));
}
while let Err(e) = db_tx.execute(
"DELETE FROM tokens WHERE block_height >= ?1 AND block_height <= ?2",
rusqlite::params![&start_block, &end_block],
) {
ctx.try_log(|logger| warn!(logger, "unable to query brc20.sqlite: {}", e.to_string()));
try_warn!(ctx, "unable to query brc20.sqlite: {}", e.to_string());
std::thread::sleep(std::time::Duration::from_secs(1));
}
}
Expand Down Expand Up @@ -277,12 +281,12 @@ pub fn insert_ledger_rows(rows: &Vec<Brc20DbLedgerRow>, db_tx: &Connection, ctx:
&row.trans_balance,
&row.operation
]) {
ctx.try_log(|logger| warn!(logger, "unable to insert into brc20.sqlite: {}", e.to_string()));
try_warn!(ctx, "unable to insert into brc20.sqlite: {}", e.to_string());
std::thread::sleep(std::time::Duration::from_secs(1));
}
}
},
Err(error) => ctx.try_log(|logger| warn!(logger, "unable to prepare statement for brc20.sqlite: {}", error.to_string()))
Err(error) => {try_warn!(ctx, "unable to prepare statement for brc20.sqlite: {}", error.to_string());}
}
}

Expand All @@ -305,24 +309,18 @@ pub fn insert_token_rows(rows: &Vec<Brc20DbTokenRow>, db_tx: &Connection, ctx: &
&row.address,
&row.self_mint,
]) {
ctx.try_log(|logger| {
warn!(
logger,
"unable to insert into brc20.sqlite: {}",
e.to_string()
)
});
try_warn!(ctx, "unable to insert into brc20.sqlite: {}", e.to_string());
std::thread::sleep(std::time::Duration::from_secs(1));
}
}
}
Err(error) => ctx.try_log(|logger| {
warn!(
logger,
Err(error) => {
try_warn!(
ctx,
"unable to prepare statement for brc20.sqlite: {}",
error.to_string()
)
}),
);
}
}
}

Expand Down Expand Up @@ -409,7 +407,10 @@ pub fn augment_transaction_with_brc20_operation_data(
let Some(receiver_address) =
get_transfer_send_receiver_address(entry.ordinal_number, &db_conn, &ctx)
else {
unreachable!("Unable to fetch receiver address for transfer_send operation {:?}", entry);
unreachable!(
"Unable to fetch receiver address for transfer_send operation {:?}",
entry
);
};
tx.metadata.brc20_operation = Some(Brc20Operation::TransferSend(Brc20TransferData {
tick: entry.tick.clone(),
Expand Down Expand Up @@ -475,13 +476,11 @@ pub fn write_augmented_block_to_brc20_db(
match brc20_operation {
Brc20Operation::Deploy(token) => {
let Some(reveal) = find_reveal_in_tx(&token.inscription_id, tx) else {
ctx.try_log(|logger| {
warn!(
logger,
"Could not find BRC-20 deploy inscription in augmented block: {}",
token.inscription_id
)
});
try_warn!(
ctx,
"Could not find BRC-20 deploy inscription in augmented block: {}",
token.inscription_id
);
continue;
};
tokens.push(Brc20DbTokenRow {
Expand Down Expand Up @@ -510,13 +509,11 @@ pub fn write_augmented_block_to_brc20_db(
}
Brc20Operation::Mint(balance) => {
let Some(reveal) = find_reveal_in_tx(&balance.inscription_id, tx) else {
ctx.try_log(|logger| {
warn!(
logger,
"Could not find BRC-20 mint inscription in augmented block: {}",
balance.inscription_id
)
});
try_warn!(
ctx,
"Could not find BRC-20 mint inscription in augmented block: {}",
balance.inscription_id
);
continue;
};
ledger_rows.push(Brc20DbLedgerRow {
Expand All @@ -534,13 +531,11 @@ pub fn write_augmented_block_to_brc20_db(
}
Brc20Operation::Transfer(balance) => {
let Some(reveal) = find_reveal_in_tx(&balance.inscription_id, tx) else {
ctx.try_log(|logger| {
warn!(
logger,
"Could not find BRC-20 transfer inscription in augmented block: {}",
balance.inscription_id
)
});
try_warn!(
ctx,
"Could not find BRC-20 transfer inscription in augmented block: {}",
balance.inscription_id
);
continue;
};
ledger_rows.push(Brc20DbLedgerRow {
Expand Down Expand Up @@ -575,13 +570,11 @@ pub fn write_augmented_block_to_brc20_db(
inscription_number = info.0;
ordinal_number = info.1;
} else {
ctx.try_log(|logger| {
warn!(
logger,
"Could not find BRC-20 transfer inscription in brc20 db: {}",
transfer.inscription_id
)
});
try_warn!(
ctx,
"Could not find BRC-20 transfer inscription in brc20 db: {}",
transfer.inscription_id
);
continue;
};
let amt = transfer.amt.parse::<f64>().unwrap().abs();
Expand Down
29 changes: 3 additions & 26 deletions components/ordhook-core/src/core/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,12 @@ use std::hash::BuildHasherDefault;
use std::ops::Div;
use std::path::PathBuf;

use chainhook_sdk::{
bitcoincore_rpc::{Auth, Client, RpcApi},
utils::Context,
};
use chainhook_sdk::utils::Context;

use crate::{
config::{Config, LogConfig, MetaProtocolsConfig, ResourcesConfig},
db::{find_pinned_block_bytes_at_block_height, open_ordhook_db_conn_rocks_db_loop},
utils::bitcoind::bitcoind_get_block_height,
};

use crate::db::{
Expand Down Expand Up @@ -144,18 +142,6 @@ pub fn should_sync_ordhook_db(
config: &Config,
ctx: &Context,
) -> Result<Option<(u64, u64, usize)>, String> {
let auth = Auth::UserPass(
config.network.bitcoind_rpc_username.clone(),
config.network.bitcoind_rpc_password.clone(),
);

let bitcoin_rpc = match Client::new(&config.network.bitcoind_rpc_url, auth) {
Ok(con) => con,
Err(message) => {
return Err(format!("Bitcoin RPC error: {}", message.to_string()));
}
};

let blocks_db = open_ordhook_db_conn_rocks_db_loop(
true,
&config.expected_cache_path(),
Expand Down Expand Up @@ -186,17 +172,8 @@ pub fn should_sync_ordhook_db(
}
};

let end_block = match bitcoin_rpc.get_blockchain_info() {
Ok(result) => result.blocks,
Err(e) => {
return Err(format!(
"unable to retrieve Bitcoin chain tip ({})",
e.to_string()
));
}
};

// TODO: Gracefully handle Regtest, Testnet and Signet
let end_block = bitcoind_get_block_height(config, ctx);
let (mut end_block, speed) = if start_block < 200_000 {
(end_block.min(200_000), 10_000)
} else if start_block < 550_000 {
Expand Down
Loading

0 comments on commit e96be13

Please sign in to comment.