From 7e705fb2c84684dee1c23321d05645592bb63bd6 Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Wed, 9 Feb 2022 15:25:14 -0800 Subject: [PATCH] Test that index handles out-of-order blockfiles (#124) --- tests/index.rs | 16 ++++++++++++++++ tests/integration.rs | 24 +++++++++++++++++++++--- 2 files changed, 37 insertions(+), 3 deletions(-) diff --git a/tests/index.rs b/tests/index.rs index 768118f3f5..894f19195f 100644 --- a/tests/index.rs +++ b/tests/index.rs @@ -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() +} diff --git a/tests/integration.rs b/tests/integration.rs index b120a3ec3d..e3c572bbd4 100644 --- a/tests/integration.rs +++ b/tests/integration.rs @@ -67,6 +67,7 @@ struct Test { expected_stdout: String, ignore_stdout: bool, tempdir: TempDir, + reverse_blockfiles: bool, } impl Test { @@ -80,6 +81,7 @@ impl Test { expected_stdout: String::new(), ignore_stdout: false, tempdir: TempDir::new()?, + reverse_blockfiles: false, }) } @@ -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 { - self.populate_blocksdir()?; + self.create_blockfiles()?; let output = Command::new(executable_path("ord")) .current_dir(&self.tempdir) @@ -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)?; @@ -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> = 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())?;