Skip to content

Commit

Permalink
Test that index handles out-of-order blockfiles (#124)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Feb 9, 2022
1 parent 3dc3820 commit 7e705fb
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 3 deletions.
16 changes: 16 additions & 0 deletions tests/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,19 @@ fn custom_index_size() -> Result {

Ok(())
}

#[test]
fn out_of_order_blockfiles() -> Result {
Test::new()?
.command("find --blocksdir blocks 0 --as-of-height 1 --slot")
.expected_stdout("1.1.0.0\n")
.block()
.block()
.transaction(TransactionOptions {
slots: &[(0, 0, 0)],
output_count: 1,
fee: 0,
})
.reverse_blockfiles()
.run()
}
24 changes: 21 additions & 3 deletions tests/integration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ struct Test {
expected_stdout: String,
ignore_stdout: bool,
tempdir: TempDir,
reverse_blockfiles: bool,
}

impl Test {
Expand All @@ -80,6 +81,7 @@ impl Test {
expected_stdout: String::new(),
ignore_stdout: false,
tempdir: TempDir::new()?,
reverse_blockfiles: false,
})
}

Expand Down Expand Up @@ -129,12 +131,19 @@ impl Test {
}
}

fn reverse_blockfiles(self) -> Self {
Self {
reverse_blockfiles: true,
..self
}
}

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

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

let output = Command::new(executable_path("ord"))
.current_dir(&self.tempdir)
Expand Down Expand Up @@ -268,7 +277,7 @@ impl Test {
self
}

fn populate_blocksdir(&self) -> io::Result<()> {
fn create_blockfiles(&self) -> io::Result<()> {
let blocksdir = self.tempdir.path().join("blocks");
fs::create_dir(&blocksdir)?;

Expand All @@ -283,7 +292,16 @@ impl Test {
{
let mut blockfile = File::create(blocksdir.join(format!("blk{:05}.dat", i)))?;

for (bi, block) in self.blocks[start..end].iter().enumerate() {
let blocks = self.blocks[start..end].iter().enumerate();

let blocks: Box<dyn std::iter::Iterator<Item = (usize, &Block)>> = if self.reverse_blockfiles
{
Box::new(blocks.rev())
} else {
Box::new(blocks)
};

for (bi, block) in blocks {
let mut encoded = Vec::new();
block.consensus_encode(&mut encoded)?;
blockfile.write_all(&Network::Bitcoin.magic().to_le_bytes())?;
Expand Down

0 comments on commit 7e705fb

Please sign in to comment.