Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add satpoint and (optionaly) address to index export #2284

Merged
merged 10 commits into from
Jul 19, 2023
59 changes: 51 additions & 8 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(())
}
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
Loading