Skip to content

Commit

Permalink
Add flags to ord find to add various information to the results
Browse files Browse the repository at this point in the history
  • Loading branch information
gmart7t2 committed Aug 7, 2023
1 parent cf5f64f commit d96d8fa
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 4 deletions.
12 changes: 11 additions & 1 deletion src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ use {
super::*,
crate::wallet::Wallet,
bitcoin::block::Header,
bitcoincore_rpc::{json::GetBlockHeaderResult, Client},
bitcoincore_rpc::{
json::{GetBlockHeaderResult, GetRawTransactionResult},
Client,
},
chrono::SubsecRound,
indicatif::{ProgressBar, ProgressStyle},
log::log_enabled,
Expand Down Expand Up @@ -731,6 +734,13 @@ impl Index {
}
}

pub(crate) fn get_transaction_info(
&self,
txid: Txid,
) -> Result<GetRawTransactionResult, bitcoincore_rpc::Error> {
self.client.get_raw_transaction_info(&txid, None)
}

pub(crate) fn get_transaction_blockhash(&self, txid: Txid) -> Result<Option<BlockHash>> {
Ok(
self
Expand Down
76 changes: 74 additions & 2 deletions src/subcommand/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,14 @@ pub(crate) struct Find {
file: Vec<PathBuf>,
#[clap(long, help = "Ignore bad sat ranges.")]
ignore: bool,
#[clap(long, help = "Show addresses in the results.")]
show_address: bool,
#[clap(long, help = "Show blockhashes in the results.")]
show_blockhash: bool,
#[clap(long, help = "Show sat names in the results.")]
show_name: bool,
#[clap(long, help = "Show timestamps in the results.")]
show_time: bool,
#[clap(help = "Find output and offset of <SAT>.")]
sat: Option<Sat>,
#[clap(help = "Find output and offset of all sats in the range <SAT>-<END>.")]
Expand All @@ -22,7 +30,17 @@ pub(crate) struct Find {

#[derive(Debug, PartialEq, Serialize, Deserialize)]
pub struct Output {
pub start: u64,
pub size: u64,
pub satpoint: SatPoint,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub address: Option<Address<NetworkUnchecked>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub blockhash: Option<bitcoin::BlockHash>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub timestamp: Option<usize>,
}

impl Find {
Expand Down Expand Up @@ -107,8 +125,12 @@ impl Find {

// loop through targets
for (sat, end) in targets {
// eprintln!("find {sat}-{end}");
match index.find(sat, end, &self.outpoint)? {
Some(result) => results.extend(result),
Some(result) => {
// eprintln!(" found {} satpoints", result.len());
results.extend(result);
}
None => {
if !self.ignore {
return Err(anyhow!(
Expand All @@ -119,7 +141,57 @@ impl Find {
}
}

print_json(results)?;
let mut detailed_results = Vec::new();

// let gbt = options.chain().genesis_block().coinbase().unwrap().clone();
// print_json(&gbt)?;
// println!("gbt.output = {:?}", options.chain().address_from_script(&gbt.output[0].script_pubkey));
// result.satpoint.outpoint.txid == gbt.txid()

for result in results {
let tx = if self.show_address || self.show_blockhash || self.show_time {
index
.get_transaction_info(result.satpoint.outpoint.txid)
.ok()
} else {
None
};

let mut result = Output {
start: result.start,
size: result.size,
satpoint: result.satpoint,
address: None,
blockhash: None,
name: None,
timestamp: None,
};

if let Some(tx) = tx.clone() {
if self.show_address {
result.address = tx.vout[result.satpoint.outpoint.vout as usize]
.script_pub_key
.address
.clone();
}

if self.show_blockhash {
result.blockhash = tx.blockhash;
}

if self.show_time {
result.timestamp = tx.time;
}
}

if self.show_name {
result.name = Some(Sat(result.start).name());
}

detailed_results.push(result);
}

print_json(detailed_results)?;

Ok(())
}
Expand Down
8 changes: 7 additions & 1 deletion tests/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,15 @@ fn find_command_returns_satpoint_for_sat() {
.rpc_server(&rpc_server)
.run_and_check_output::<Output>(),
Output {
start: 0,
size: 1,
satpoint: "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b:0:0"
.parse()
.unwrap()
.unwrap(),
address: None,
blockhash: None,
name: None,
timestamp: None,
}
);
}
Expand Down

0 comments on commit d96d8fa

Please sign in to comment.