Skip to content

Commit

Permalink
Add satpoint and address to index export (ordinals#2284)
Browse files Browse the repository at this point in the history
  • Loading branch information
veryordinally authored and Raylin51 committed Aug 11, 2023
1 parent 8a6b9f0 commit 0efcd7c
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 9 deletions.
59 changes: 51 additions & 8 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -445,24 +445,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(())
}
Expand Down
4 changes: 3 additions & 1 deletion src/subcommand/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,16 @@ pub(crate) struct Export {
help = "<TSV> file to write to"
)]
tsv: String,
#[clap(long, help = "Whether to include addresses in export")]
include_addresses: bool,
}

impl Export {
pub(crate) fn run(self, options: Options) -> Result {
let index = Index::open(&options)?;

index.update()?;
index.export(&self.tsv)?;
index.export(&self.tsv, self.include_addresses)?;

Ok(())
}
Expand Down

0 comments on commit 0efcd7c

Please sign in to comment.