diff --git a/src/index.rs b/src/index.rs index 1d747aec93..69a50ad35a 100644 --- a/src/index.rs +++ b/src/index.rs @@ -385,24 +385,67 @@ impl Index { Updater::update(self) } - pub(crate) fn export(&self, filename: &String) -> Result { + pub(crate) fn export(&self, filename: &String, include_addresses: bool) -> Result { let mut writer = BufWriter::new(File::create(filename)?); + let rtx = self.database.begin_read()?; - for result in self - .database - .begin_read()? + let blocks_indexed = rtx + .open_table(HEIGHT_TO_BLOCK_HASH)? + .range(0..)? + .next_back() + .and_then(|result| result.ok()) + .map(|(height, _hash)| height.value() + 1) + .unwrap_or(0); + + writeln!(writer, "# export at block height {}", blocks_indexed)?; + + log::info!("exporting database tables to {filename}"); + + for result in rtx .open_table(INSCRIPTION_NUMBER_TO_INSCRIPTION_ID)? .iter()? { let (number, id) = result?; - writeln!( + let inscription_id = InscriptionId::load(*id.value()); + + let satpoint = self + .get_inscription_satpoint_by_id(inscription_id)? + .unwrap(); + + write!( writer, - "{}\t{}", + "{}\t{}\t{}", number.value(), - InscriptionId::load(*id.value()) + inscription_id, + satpoint )?; - } + if include_addresses { + let address = if satpoint.outpoint == unbound_outpoint() { + "unbound".to_string() + } else { + let output = self + .get_transaction(satpoint.outpoint.txid)? + .unwrap() + .output + .into_iter() + .nth(satpoint.outpoint.vout.try_into().unwrap()) + .unwrap(); + self + .options + .chain() + .address_from_script(&output.script_pubkey) + .map(|address| address.to_string()) + .unwrap_or_else(|e| e.to_string()) + }; + write!(writer, "\t{}", address)?; + } + writeln!(writer)?; + + if SHUTTING_DOWN.load(atomic::Ordering::Relaxed) { + break; + } + } writer.flush()?; Ok(()) } diff --git a/src/subcommand/index.rs b/src/subcommand/index.rs index 448e8cc3ef..0193011636 100644 --- a/src/subcommand/index.rs +++ b/src/subcommand/index.rs @@ -25,6 +25,8 @@ pub(crate) struct Export { help = " file to write to" )] tsv: String, + #[clap(long, help = "Whether to include addresses in export")] + include_addresses: bool, } impl Export { @@ -32,7 +34,7 @@ impl Export { let index = Index::open(&options)?; index.update()?; - index.export(&self.tsv)?; + index.export(&self.tsv, self.include_addresses)?; Ok(()) }