From 04d102f326214e7a232b2dd56647f32e6d70a868 Mon Sep 17 00:00:00 2001 From: liam <31192478+terror@users.noreply.github.com> Date: Sun, 6 Feb 2022 13:56:38 -0500 Subject: [PATCH] Allow setting index size (#120) --- .github/workflows/ci.yaml | 2 +- justfile | 3 +-- src/arguments.rs | 15 ++++++++++++++ src/index.rs | 4 ++-- src/main.rs | 10 ++++++--- src/{command.rs => subcommand.rs} | 10 ++++----- src/{command => subcommand}/epochs.rs | 0 src/{command => subcommand}/find.rs | 4 ++-- src/{command => subcommand}/list.rs | 4 ++-- src/{command => subcommand}/name.rs | 0 src/{command => subcommand}/range.rs | 0 src/{command => subcommand}/supply.rs | 0 src/{command => subcommand}/traits.rs | 0 tests/index.rs | 29 +++++++++++++++++++++++++++ tests/integration.rs | 15 +++++++++++--- tests/traits.rs | 3 ++- 16 files changed, 78 insertions(+), 21 deletions(-) create mode 100644 src/arguments.rs rename src/{command.rs => subcommand.rs} (69%) rename src/{command => subcommand}/epochs.rs (100%) rename src/{command => subcommand}/find.rs (90%) rename src/{command => subcommand}/list.rs (67%) rename src/{command => subcommand}/name.rs (100%) rename src/{command => subcommand}/range.rs (100%) rename src/{command => subcommand}/supply.rs (100%) rename src/{command => subcommand}/traits.rs (100%) create mode 100644 tests/index.rs diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 6707c45f7c..85ea05852f 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -44,7 +44,7 @@ jobs: cargo update --locked --package ord - name: Test - run: cargo test --all --release + run: cargo test --all - name: Clippy run: cargo clippy --all --all-targets diff --git a/justfile b/justfile index b78be81a99..6d4a21d5ee 100644 --- a/justfile +++ b/justfile @@ -4,7 +4,6 @@ export RUST_LOG := log ci: clippy forbid cargo fmt -- --check - cargo test --release cargo test forbid: @@ -16,5 +15,5 @@ fmt: clippy: cargo clippy -watch +args='ltest --release': +watch +args='ltest': cargo watch --clear --exec '{{args}}' diff --git a/src/arguments.rs b/src/arguments.rs new file mode 100644 index 0000000000..9a656548b8 --- /dev/null +++ b/src/arguments.rs @@ -0,0 +1,15 @@ +use super::*; + +#[derive(StructOpt)] +pub(crate) struct Arguments { + #[structopt(long)] + index_size: Option, + #[structopt(subcommand)] + subcommand: Subcommand, +} + +impl Arguments { + pub(crate) fn run(self) -> Result<()> { + self.subcommand.run(self.index_size) + } +} diff --git a/src/index.rs b/src/index.rs index b24988b8c9..f80ab41370 100644 --- a/src/index.rs +++ b/src/index.rs @@ -12,7 +12,7 @@ impl Index { const HEIGHT_TO_HASH: &'static str = "HEIGHT_TO_HASH"; const OUTPOINT_TO_ORDINAL_RANGES: &'static str = "OUTPOINT_TO_ORDINAL_RANGES"; - pub(crate) fn new(blocksdir: Option<&Path>) -> Result { + pub(crate) fn new(blocksdir: Option<&Path>, index_size: Option) -> Result { let blocksdir = if let Some(blocksdir) = blocksdir { blocksdir.to_owned() } else if cfg!(target_os = "macos") { @@ -30,7 +30,7 @@ impl Index { }; let index = Self { - database: unsafe { Database::open("index.redb", 50 << 30)? }, + database: unsafe { Database::open("index.redb", index_size.unwrap_or(1 << 20))? }, blocksdir, }; diff --git a/src/main.rs b/src/main.rs index 803ad1353c..09d746aa54 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,8 @@ use { - crate::{epoch::Epoch, height::Height, index::Index, ordinal::Ordinal, sat_point::SatPoint}, + crate::{ + arguments::Arguments, epoch::Epoch, height::Height, index::Index, ordinal::Ordinal, + sat_point::SatPoint, subcommand::Subcommand, + }, bitcoin::{ blockdata::constants::COIN_VALUE, consensus::{Decodable, Encodable}, @@ -26,19 +29,20 @@ use { structopt::StructOpt, }; -mod command; +mod arguments; mod epoch; mod height; mod index; mod ordinal; mod sat_point; +mod subcommand; type Result> = std::result::Result; fn main() { env_logger::init(); - if let Err(error) = command::Command::from_args().run() { + if let Err(error) = Arguments::from_args().run() { eprintln!("error: {}", error); process::exit(1); } diff --git a/src/command.rs b/src/subcommand.rs similarity index 69% rename from src/command.rs rename to src/subcommand.rs index 903e18939b..276ebf22ff 100644 --- a/src/command.rs +++ b/src/subcommand.rs @@ -9,7 +9,7 @@ mod supply; mod traits; #[derive(StructOpt)] -pub(crate) enum Command { +pub(crate) enum Subcommand { Epochs, Find(find::Find), Name(name::Name), @@ -19,13 +19,13 @@ pub(crate) enum Command { Traits(traits::Traits), } -impl Command { - pub(crate) fn run(self) -> Result<()> { +impl Subcommand { + pub(crate) fn run(self, index_size: Option) -> Result<()> { match self { Self::Epochs => epochs::run(), - Self::Find(find) => find.run(), + Self::Find(find) => find.run(index_size), Self::Name(name) => name.run(), - Self::List(list) => list.run(), + Self::List(list) => list.run(index_size), Self::Range(range) => range.run(), Self::Supply => supply::run(), Self::Traits(traits) => traits.run(), diff --git a/src/command/epochs.rs b/src/subcommand/epochs.rs similarity index 100% rename from src/command/epochs.rs rename to src/subcommand/epochs.rs diff --git a/src/command/find.rs b/src/subcommand/find.rs similarity index 90% rename from src/command/find.rs rename to src/subcommand/find.rs index 6ba1502a36..85d914b667 100644 --- a/src/command/find.rs +++ b/src/subcommand/find.rs @@ -12,8 +12,8 @@ pub(crate) struct Find { } impl Find { - pub(crate) fn run(self) -> Result<()> { - let index = Index::new(self.blocksdir.as_deref())?; + pub(crate) fn run(self, index_size: Option) -> Result<()> { + let index = Index::new(self.blocksdir.as_deref(), index_size)?; let creation_height = self.ordinal.height().n(); let block = index.block(creation_height)?.unwrap(); diff --git a/src/command/list.rs b/src/subcommand/list.rs similarity index 67% rename from src/command/list.rs rename to src/subcommand/list.rs index 7519ecb354..35bdf96d42 100644 --- a/src/command/list.rs +++ b/src/subcommand/list.rs @@ -8,8 +8,8 @@ pub(crate) struct List { } impl List { - pub(crate) fn run(self) -> Result<()> { - let index = Index::new(self.blocksdir.as_deref())?; + pub(crate) fn run(self, index_size: Option) -> Result<()> { + let index = Index::new(self.blocksdir.as_deref(), index_size)?; let ranges = index.list(self.outpoint)?; for (start, end) in ranges { diff --git a/src/command/name.rs b/src/subcommand/name.rs similarity index 100% rename from src/command/name.rs rename to src/subcommand/name.rs diff --git a/src/command/range.rs b/src/subcommand/range.rs similarity index 100% rename from src/command/range.rs rename to src/subcommand/range.rs diff --git a/src/command/supply.rs b/src/subcommand/supply.rs similarity index 100% rename from src/command/supply.rs rename to src/subcommand/supply.rs diff --git a/src/command/traits.rs b/src/subcommand/traits.rs similarity index 100% rename from src/command/traits.rs rename to src/subcommand/traits.rs diff --git a/tests/index.rs b/tests/index.rs new file mode 100644 index 0000000000..768118f3f5 --- /dev/null +++ b/tests/index.rs @@ -0,0 +1,29 @@ +use super::*; + +#[test] +fn default_index_size() -> Result { + let tempdir = Test::new()? + .command("find --blocksdir blocks 0 --as-of-height 0") + .expected_stdout("0396bc915f141f7de025f72ae9b6bb8dcdb5f444fc245d8fac486ba67a38eef9:0:0\n") + .block() + .output()? + .tempdir; + + assert_eq!(tempdir.path().join("index.redb").metadata()?.len(), 1 << 20); + + Ok(()) +} + +#[test] +fn custom_index_size() -> Result { + let tempdir = Test::new()? + .command("--index-size 2097152 find --blocksdir blocks 0 --as-of-height 0") + .expected_stdout("0396bc915f141f7de025f72ae9b6bb8dcdb5f444fc245d8fac486ba67a38eef9:0:0\n") + .block() + .output()? + .tempdir; + + assert_eq!(tempdir.path().join("index.redb").metadata()?.len(), 2 << 20); + + Ok(()) +} diff --git a/tests/integration.rs b/tests/integration.rs index a6d76ec49a..421442178b 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -21,6 +21,7 @@ use { mod epochs; mod find; +mod index; mod list; mod name; mod range; @@ -29,6 +30,11 @@ mod traits; type Result = std::result::Result>; +struct Output { + stdout: String, + tempdir: TempDir, +} + struct Test { args: Vec, blockfiles: Vec, @@ -101,10 +107,10 @@ impl Test { } fn run(self) -> Result { - self.run_with_stdout().map(|_| ()) + self.output().map(|_| ()) } - fn run_with_stdout(self) -> Result { + fn output(self) -> Result { self.populate_blocksdir()?; let output = Command::new(executable_path("ord")) @@ -126,7 +132,10 @@ impl Test { assert_eq!(stdout, self.expected_stdout); } - Ok(stdout.to_owned()) + Ok(Output { + stdout: stdout.to_string(), + tempdir: self.tempdir, + }) } fn block(self) -> Self { diff --git a/tests/traits.rs b/tests/traits.rs index 4709579987..19674ca779 100644 --- a/tests/traits.rs +++ b/tests/traits.rs @@ -5,7 +5,8 @@ fn traits(ordinal: u64) -> Result> { Test::new()? .args(&["traits", &ordinal.to_string()]) .ignore_stdout() - .run_with_stdout()? + .output()? + .stdout .lines() .map(str::to_owned) .collect(),