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 export command for number to id table #2208

Merged
merged 8 commits into from
Jun 23, 2023
Merged
Show file tree
Hide file tree
Changes from 7 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use {
log::log_enabled,
redb::{Database, ReadableTable, Table, TableDefinition, WriteStrategy, WriteTransaction},
std::collections::HashMap,
std::io::{BufWriter, Write},
std::sync::atomic::{self, AtomicBool},
};

Expand Down Expand Up @@ -362,6 +363,27 @@ impl Index {
Updater::update(self)
}

pub(crate) fn export(&self, filename: &String) -> Result {
let mut writer = BufWriter::new(File::create(filename)?);

for (number, id) in self
.database
.begin_read()?
.open_table(INSCRIPTION_NUMBER_TO_INSCRIPTION_ID)?
.iter()?
{
writeln!(
writer,
"{}\t{}",
number.value(),
InscriptionId::load(*id.value())
)?;
}

writer.flush()?;
Ok(())
}

pub(crate) fn is_reorged(&self) -> bool {
self.reorged.load(atomic::Ordering::Relaxed)
}
Expand Down
85 changes: 53 additions & 32 deletions src/options.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,29 +270,41 @@ mod tests {
#[test]
fn rpc_url_overrides_network() {
assert_eq!(
Arguments::try_parse_from(["ord", "--rpc-url=127.0.0.1:1234", "--chain=signet", "index"])
.unwrap()
.options
.rpc_url(),
Arguments::try_parse_from([
"ord",
"--rpc-url=127.0.0.1:1234",
"--chain=signet",
"index",
"run"
])
.unwrap()
.options
.rpc_url(),
"127.0.0.1:1234"
);
}

#[test]
fn cookie_file_overrides_network() {
assert_eq!(
Arguments::try_parse_from(["ord", "--cookie-file=/foo/bar", "--chain=signet", "index"])
.unwrap()
.options
.cookie_file()
.unwrap(),
Arguments::try_parse_from([
"ord",
"--cookie-file=/foo/bar",
"--chain=signet",
"index",
"run"
])
.unwrap()
.options
.cookie_file()
.unwrap(),
Path::new("/foo/bar")
);
}

#[test]
fn use_default_network() {
let arguments = Arguments::try_parse_from(["ord", "index"]).unwrap();
let arguments = Arguments::try_parse_from(["ord", "index", "run"]).unwrap();

assert_eq!(arguments.options.rpc_url(), "127.0.0.1:8332/wallet/ord");

Expand All @@ -305,7 +317,7 @@ mod tests {

#[test]
fn uses_network_defaults() {
let arguments = Arguments::try_parse_from(["ord", "--chain=signet", "index"]).unwrap();
let arguments = Arguments::try_parse_from(["ord", "--chain=signet", "index", "run"]).unwrap();

assert_eq!(arguments.options.rpc_url(), "127.0.0.1:38332/wallet/ord");

Expand All @@ -324,7 +336,7 @@ mod tests {

#[test]
fn mainnet_cookie_file_path() {
let cookie_file = Arguments::try_parse_from(["ord", "index"])
let cookie_file = Arguments::try_parse_from(["ord", "index", "run"])
.unwrap()
.options
.cookie_file()
Expand All @@ -343,7 +355,7 @@ mod tests {

#[test]
fn othernet_cookie_file_path() {
let arguments = Arguments::try_parse_from(["ord", "--chain=signet", "index"]).unwrap();
let arguments = Arguments::try_parse_from(["ord", "--chain=signet", "index", "run"]).unwrap();

let cookie_file = arguments
.options
Expand All @@ -363,9 +375,14 @@ mod tests {

#[test]
fn cookie_file_defaults_to_bitcoin_data_dir() {
let arguments =
Arguments::try_parse_from(["ord", "--bitcoin-data-dir=foo", "--chain=signet", "index"])
.unwrap();
let arguments = Arguments::try_parse_from([
"ord",
"--bitcoin-data-dir=foo",
"--chain=signet",
"index",
"run",
])
.unwrap();

let cookie_file = arguments
.options
Expand All @@ -383,7 +400,7 @@ mod tests {

#[test]
fn mainnet_data_dir() {
let data_dir = Arguments::try_parse_from(["ord", "index"])
let data_dir = Arguments::try_parse_from(["ord", "index", "run"])
.unwrap()
.options
.data_dir()
Expand All @@ -398,7 +415,7 @@ mod tests {

#[test]
fn othernet_data_dir() {
let data_dir = Arguments::try_parse_from(["ord", "--chain=signet", "index"])
let data_dir = Arguments::try_parse_from(["ord", "--chain=signet", "index", "run"])
.unwrap()
.options
.data_dir()
Expand All @@ -418,7 +435,7 @@ mod tests {
#[test]
fn network_is_joined_with_data_dir() {
let data_dir =
Arguments::try_parse_from(["ord", "--chain=signet", "--data-dir", "foo", "index"])
Arguments::try_parse_from(["ord", "--chain=signet", "--data-dir", "foo", "index", "run"])
.unwrap()
.options
.data_dir()
Expand All @@ -438,7 +455,7 @@ mod tests {
#[test]
fn network_accepts_aliases() {
fn check_network_alias(alias: &str, suffix: &str) {
let data_dir = Arguments::try_parse_from(["ord", "--chain", alias, "index"])
let data_dir = Arguments::try_parse_from(["ord", "--chain", alias, "index", "run"])
.unwrap()
.options
.data_dir()
Expand Down Expand Up @@ -513,48 +530,51 @@ mod tests {

#[test]
fn chain_flags() {
Arguments::try_parse_from(["ord", "--signet", "--chain", "signet", "index"]).unwrap_err();
Arguments::try_parse_from(["ord", "--signet", "--chain", "signet", "index", "run"])
.unwrap_err();
assert_eq!(
Arguments::try_parse_from(["ord", "--signet", "index"])
Arguments::try_parse_from(["ord", "--signet", "index", "run"])
.unwrap()
.options
.chain(),
Chain::Signet
);
assert_eq!(
Arguments::try_parse_from(["ord", "-s", "index"])
Arguments::try_parse_from(["ord", "-s", "index", "run"])
.unwrap()
.options
.chain(),
Chain::Signet
);

Arguments::try_parse_from(["ord", "--regtest", "--chain", "signet", "index"]).unwrap_err();
Arguments::try_parse_from(["ord", "--regtest", "--chain", "signet", "index", "run"])
.unwrap_err();
assert_eq!(
Arguments::try_parse_from(["ord", "--regtest", "index"])
Arguments::try_parse_from(["ord", "--regtest", "index", "run"])
.unwrap()
.options
.chain(),
Chain::Regtest
);
assert_eq!(
Arguments::try_parse_from(["ord", "-r", "index"])
Arguments::try_parse_from(["ord", "-r", "index", "run"])
.unwrap()
.options
.chain(),
Chain::Regtest
);

Arguments::try_parse_from(["ord", "--testnet", "--chain", "signet", "index"]).unwrap_err();
Arguments::try_parse_from(["ord", "--testnet", "--chain", "signet", "index", "run"])
.unwrap_err();
assert_eq!(
Arguments::try_parse_from(["ord", "--testnet", "index"])
Arguments::try_parse_from(["ord", "--testnet", "index", "run"])
.unwrap()
.options
.chain(),
Chain::Testnet
);
assert_eq!(
Arguments::try_parse_from(["ord", "-t", "index"])
Arguments::try_parse_from(["ord", "-t", "index", "run"])
.unwrap()
.options
.chain(),
Expand Down Expand Up @@ -584,7 +604,7 @@ mod tests {
#[test]
fn default_config_is_returned_if_config_option_is_not_passed() {
assert_eq!(
Arguments::try_parse_from(["ord", "index"])
Arguments::try_parse_from(["ord", "index", "run"])
.unwrap()
.options
.load_config()
Expand All @@ -604,7 +624,7 @@ mod tests {
fs::write(&path, format!("hidden:\n- \"{id}\"")).unwrap();

assert_eq!(
Arguments::try_parse_from(["ord", "--config", path.to_str().unwrap(), "index",])
Arguments::try_parse_from(["ord", "--config", path.to_str().unwrap(), "index", "run"])
.unwrap()
.options
.load_config()
Expand All @@ -627,7 +647,7 @@ mod tests {
.unwrap();

assert_eq!(
Arguments::try_parse_from(["ord", "--config", path.to_str().unwrap(), "index",])
Arguments::try_parse_from(["ord", "--config", path.to_str().unwrap(), "index", "run"])
.unwrap()
.options
.load_config()
Expand Down Expand Up @@ -660,6 +680,7 @@ mod tests {
"--config-dir",
tempdir.path().to_str().unwrap(),
"index",
"run"
])
.unwrap()
.options
Expand Down
6 changes: 3 additions & 3 deletions src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ pub(crate) enum Subcommand {
Preview(preview::Preview),
#[clap(about = "Find a satoshi's current location")]
Find(find::Find),
#[clap(about = "Update the index")]
Index,
#[clap(subcommand, about = "Index commands")]
Index(index::IndexSubcommand),
#[clap(about = "Display index statistics")]
Info(info::Info),
#[clap(about = "List the satoshis in an output")]
Expand All @@ -53,7 +53,7 @@ impl Subcommand {
Self::Epochs => epochs::run(),
Self::Preview(preview) => preview.run(),
Self::Find(find) => find.run(options),
Self::Index => index::run(options),
Self::Index(index) => index.run(options),
Self::Info(info) => info.run(options),
Self::List(list) => list.run(options),
Self::Parse(parse) => parse.run(),
Expand Down
38 changes: 38 additions & 0 deletions src/subcommand/index.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,43 @@
use super::*;

#[derive(Debug, Parser)]
pub(crate) enum IndexSubcommand {
#[clap(about = "Write inscription number and id to a file")]
veryordinally marked this conversation as resolved.
Show resolved Hide resolved
Export(Export),
#[clap(about = "Update the index")]
Run,
}

impl IndexSubcommand {
pub(crate) fn run(self, options: Options) -> Result {
match self {
Self::Export(export) => export.run(options),
Self::Run => index::run(options),
}
}
}

#[derive(Debug, Parser)]
pub(crate) struct Export {
#[clap(
long,
default_value = "inscription_number_to_id.tsv",
help = "<TSV> file to write to"
)]
tsv: String,
}

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

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

Ok(())
}
}

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

Expand Down
26 changes: 23 additions & 3 deletions tests/command_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,10 @@ impl CommandBuilder {
}
}

pub(crate) fn temp_dir(self, tempdir: TempDir) -> Self {
Self { tempdir, ..self }
}

pub(crate) fn command(&self) -> Command {
let mut command = Command::new(executable_path("ord"));

Expand Down Expand Up @@ -116,11 +120,27 @@ impl CommandBuilder {
command
}

pub(crate) fn run(self) -> String {
pub(crate) fn run_and_extract_file(self, path: impl AsRef<Path>) -> String {
let output = self.command().output().unwrap();
let stdout = str::from_utf8(&output.stdout).unwrap();
let stderr = str::from_utf8(&output.stderr).unwrap();
if output.status.code() != Some(self.expected_exit_code) {
panic!(
"Test failed: {}\nstdout:\n{}\nstderr:\n{}",
output.status, stdout, stderr
);
}

self.expected_stderr.assert_match(stderr);
self.expected_stdout.assert_match(stdout);

fs::read_to_string(self.tempdir.path().join(path)).unwrap()
}

pub(crate) fn run_and_extract_stdout(self) -> String {
let output = self.command().output().unwrap();
let stdout = str::from_utf8(&output.stdout).unwrap();
let stderr = str::from_utf8(&output.stderr).unwrap();
if output.status.code() != Some(self.expected_exit_code) {
panic!(
"Test failed: {}\nstdout:\n{}\nstderr:\n{}",
Expand All @@ -134,8 +154,8 @@ impl CommandBuilder {
stdout.into()
}

pub(crate) fn output<T: DeserializeOwned>(self) -> T {
let stdout = self.stdout_regex(".*").run();
pub(crate) fn run_and_check_output<T: DeserializeOwned>(self) -> T {
let stdout = self.stdout_regex(".*").run_and_extract_stdout();
serde_json::from_str(&stdout)
.unwrap_or_else(|err| panic!("Failed to deserialize JSON: {err}\n{stdout}"))
}
Expand Down
2 changes: 1 addition & 1 deletion tests/epochs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use {super::*, ord::subcommand::epochs::Output, ord::Sat};
#[test]
fn empty() {
assert_eq!(
CommandBuilder::new("epochs").output::<Output>(),
CommandBuilder::new("epochs").run_and_check_output::<Output>(),
Output {
starting_sats: vec![
Sat(0),
Expand Down
Loading
Loading