From d96d8fafba440d70442bf9d9812a9e7c4c6e4eb7 Mon Sep 17 00:00:00 2001 From: Greg Martin Date: Mon, 7 Aug 2023 00:46:58 +0000 Subject: [PATCH] Add flags to `ord find` to add various information to the results --- src/index.rs | 12 ++++++- src/subcommand/find.rs | 76 ++++++++++++++++++++++++++++++++++++++++-- tests/find.rs | 8 ++++- 3 files changed, 92 insertions(+), 4 deletions(-) diff --git a/src/index.rs b/src/index.rs index 5eba24748f..934f0bedf4 100644 --- a/src/index.rs +++ b/src/index.rs @@ -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, @@ -731,6 +734,13 @@ impl Index { } } + pub(crate) fn get_transaction_info( + &self, + txid: Txid, + ) -> Result { + self.client.get_raw_transaction_info(&txid, None) + } + pub(crate) fn get_transaction_blockhash(&self, txid: Txid) -> Result> { Ok( self diff --git a/src/subcommand/find.rs b/src/subcommand/find.rs index 7971d682f7..c824fc9538 100644 --- a/src/subcommand/find.rs +++ b/src/subcommand/find.rs @@ -14,6 +14,14 @@ pub(crate) struct Find { file: Vec, #[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: Option, #[clap(help = "Find output and offset of all sats in the range -.")] @@ -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>, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub blockhash: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub name: Option, + #[serde(default, skip_serializing_if = "Option::is_none")] + pub timestamp: Option, } impl Find { @@ -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!( @@ -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(()) } diff --git a/tests/find.rs b/tests/find.rs index 2efbcea850..a0efe2b621 100644 --- a/tests/find.rs +++ b/tests/find.rs @@ -8,9 +8,15 @@ fn find_command_returns_satpoint_for_sat() { .rpc_server(&rpc_server) .run_and_check_output::(), Output { + start: 0, + size: 1, satpoint: "4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b:0:0" .parse() - .unwrap() + .unwrap(), + address: None, + blockhash: None, + name: None, + timestamp: None, } ); }