Skip to content

Commit

Permalink
Allow searching for block hashes, txids, and outputs things (#549)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Sep 23, 2022
1 parent e16a50f commit fc3ddfa
Show file tree
Hide file tree
Showing 6 changed files with 236 additions and 190 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

3 changes: 2 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ env_logger = "0.9.0"
futures = "0.3.21"
html-escaper = "0.2.0"
http = "0.2.6"
lazy_static = "1.4.0"
log = "0.4.14"
mime_guess = "2.0.4"
rayon = "1.5.1"
redb = { version = "0.6.0", git = "https://github.com/casey/redb.git", branch = "add-write-lock" }
regex = "1.6.0"
rust-embed = "6.4.0"
rustls = "0.20.6"
rustls-acme = { version = "0.5.0", features = ["axum"] }
Expand All @@ -49,7 +51,6 @@ jsonrpc-http-server = "18.0.0"
log = "0.4.14"
nix = "0.24.1"
pretty_assertions = "1.2.1"
regex = "1.6.0"
reqwest = { version = "0.11.10", features = ["blocking"] }
tempfile = "3.2.0"
unindent = "0.1.7"
Expand Down
75 changes: 39 additions & 36 deletions src/index.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use {
super::*,
bitcoin::consensus::encode::serialize,
bitcoin::BlockHeader,
bitcoincore_rpc::{Auth, Client, RpcApi},
rayon::iter::{IntoParallelRefIterator, ParallelIterator},
redb::WriteStrategy,
Expand Down Expand Up @@ -39,6 +40,29 @@ impl From<Statistic> for u64 {
}
}

trait BitcoinCoreRpcResultExt<T> {
fn into_option(self) -> Result<Option<T>>;
}

impl<T> BitcoinCoreRpcResultExt<T> for Result<T, bitcoincore_rpc::Error> {
fn into_option(self) -> Result<Option<T>> {
match self {
Ok(ok) => Ok(Some(ok)),
Err(bitcoincore_rpc::Error::JsonRpc(bitcoincore_rpc::jsonrpc::error::Error::Rpc(
bitcoincore_rpc::jsonrpc::error::RpcError { code: -8, .. },
))) => Ok(None),
Err(bitcoincore_rpc::Error::JsonRpc(bitcoincore_rpc::jsonrpc::error::Error::Rpc(
bitcoincore_rpc::jsonrpc::error::RpcError { message, .. },
)))
if message.ends_with("not found") =>
{
Ok(None)
}
Err(err) => Err(err.into()),
}
}
}

impl Index {
pub(crate) fn open(options: &Options) -> Result<Self> {
let rpc_url = options.rpc_url();
Expand Down Expand Up @@ -397,47 +421,26 @@ impl Index {
}

fn block_at_height(&self, height: u64) -> Result<Option<Block>> {
match self.client.get_block_hash(height) {
Ok(hash) => Ok(Some(self.client.get_block(&hash)?)),
Err(bitcoincore_rpc::Error::JsonRpc(bitcoincore_rpc::jsonrpc::error::Error::Rpc(
bitcoincore_rpc::jsonrpc::error::RpcError { code: -8, .. },
))) => Ok(None),
Err(bitcoincore_rpc::Error::JsonRpc(bitcoincore_rpc::jsonrpc::error::Error::Rpc(
bitcoincore_rpc::jsonrpc::error::RpcError { message, .. },
)))
if message == "Block not found" =>
{
Ok(None)
}
Err(err) => Err(err.into()),
}
Ok(
self
.client
.get_block_hash(height)
.into_option()?
.map(|hash| self.client.get_block(&hash))
.transpose()?,
)
}

pub(crate) fn block_with_hash(&self, hash: sha256d::Hash) -> Result<Option<Block>> {
match self.client.get_block(&BlockHash::from_hash(hash)) {
Ok(block) => Ok(Some(block)),
Err(bitcoincore_rpc::Error::JsonRpc(bitcoincore_rpc::jsonrpc::error::Error::Rpc(
bitcoincore_rpc::jsonrpc::error::RpcError { code: -8, .. },
))) => Ok(None),
Err(bitcoincore_rpc::Error::JsonRpc(bitcoincore_rpc::jsonrpc::error::Error::Rpc(
bitcoincore_rpc::jsonrpc::error::RpcError { message, .. },
)))
if message == "Block not found" =>
{
Ok(None)
}
Err(err) => Err(err.into()),
}
pub(crate) fn block_header(&self, hash: BlockHash) -> Result<Option<BlockHeader>> {
self.client.get_block_header(&hash).into_option()
}

pub(crate) fn block_with_hash(&self, hash: BlockHash) -> Result<Option<Block>> {
self.client.get_block(&hash).into_option()
}

pub(crate) fn transaction(&self, txid: Txid) -> Result<Option<Transaction>> {
match self.client.get_raw_transaction(&txid, None) {
Ok(transaction) => Ok(Some(transaction)),
Err(bitcoincore_rpc::Error::JsonRpc(bitcoincore_rpc::jsonrpc::error::Error::Rpc(
bitcoincore_rpc::jsonrpc::error::RpcError { code: -8, .. },
))) => Ok(None),
Err(err) => Err(err.into()),
}
self.client.get_raw_transaction(&txid, None).into_option()
}

pub(crate) fn find(&self, ordinal: Ordinal) -> Result<Option<SatPoint>> {
Expand Down
3 changes: 2 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ use {
blockdata::{constants::COIN_VALUE, transaction::TxOut},
consensus::{Decodable, Encodable},
hash_types::BlockHash,
hashes::{sha256d, Hash},
hashes::Hash,
secp256k1::{
rand::{self},
Secp256k1,
Expand All @@ -45,6 +45,7 @@ use {
clap::Parser,
derive_more::{Display, FromStr},
redb::{Database, ReadableTable, Table, TableDefinition, WriteTransaction},
regex::Regex,
serde::{Deserialize, Serialize},
std::{
collections::VecDeque,
Expand Down
Loading

0 comments on commit fc3ddfa

Please sign in to comment.