Skip to content

Commit

Permalink
Allow setting index size (ordinals#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
terror committed Feb 6, 2022
1 parent a0b20e0 commit 04d102f
Show file tree
Hide file tree
Showing 16 changed files with 78 additions and 21 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 1 addition & 2 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@ export RUST_LOG := log

ci: clippy forbid
cargo fmt -- --check
cargo test --release
cargo test

forbid:
Expand All @@ -16,5 +15,5 @@ fmt:
clippy:
cargo clippy

watch +args='ltest --release':
watch +args='ltest':
cargo watch --clear --exec '{{args}}'
15 changes: 15 additions & 0 deletions src/arguments.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
use super::*;

#[derive(StructOpt)]
pub(crate) struct Arguments {
#[structopt(long)]
index_size: Option<usize>,
#[structopt(subcommand)]
subcommand: Subcommand,
}

impl Arguments {
pub(crate) fn run(self) -> Result<()> {
self.subcommand.run(self.index_size)
}
}
4 changes: 2 additions & 2 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<Self> {
pub(crate) fn new(blocksdir: Option<&Path>, index_size: Option<usize>) -> Result<Self> {
let blocksdir = if let Some(blocksdir) = blocksdir {
blocksdir.to_owned()
} else if cfg!(target_os = "macos") {
Expand All @@ -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,
};

Expand Down
10 changes: 7 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -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},
Expand All @@ -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<T = (), E = Box<dyn std::error::Error>> = std::result::Result<T, E>;

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);
}
Expand Down
10 changes: 5 additions & 5 deletions src/command.rs → src/subcommand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand All @@ -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<usize>) -> 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(),
Expand Down
File renamed without changes.
4 changes: 2 additions & 2 deletions src/command/find.rs → src/subcommand/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize>) -> 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();
Expand Down
4 changes: 2 additions & 2 deletions src/command/list.rs → src/subcommand/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<usize>) -> Result<()> {
let index = Index::new(self.blocksdir.as_deref(), index_size)?;
let ranges = index.list(self.outpoint)?;

for (start, end) in ranges {
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
29 changes: 29 additions & 0 deletions tests/index.rs
Original file line number Diff line number Diff line change
@@ -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(())
}
15 changes: 12 additions & 3 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ use {

mod epochs;
mod find;
mod index;
mod list;
mod name;
mod range;
Expand All @@ -29,6 +30,11 @@ mod traits;

type Result<T = ()> = std::result::Result<T, Box<dyn Error>>;

struct Output {
stdout: String,
tempdir: TempDir,
}

struct Test {
args: Vec<String>,
blockfiles: Vec<usize>,
Expand Down Expand Up @@ -101,10 +107,10 @@ impl Test {
}

fn run(self) -> Result {
self.run_with_stdout().map(|_| ())
self.output().map(|_| ())
}

fn run_with_stdout(self) -> Result<String> {
fn output(self) -> Result<Output> {
self.populate_blocksdir()?;

let output = Command::new(executable_path("ord"))
Expand All @@ -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 {
Expand Down
3 changes: 2 additions & 1 deletion tests/traits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ fn traits(ordinal: u64) -> Result<BTreeSet<String>> {
Test::new()?
.args(&["traits", &ordinal.to_string()])
.ignore_stdout()
.run_with_stdout()?
.output()?
.stdout
.lines()
.map(str::to_owned)
.collect(),
Expand Down

0 comments on commit 04d102f

Please sign in to comment.