Skip to content

Commit

Permalink
Test commands which return errors when not tracking rare ordinals (or…
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Dec 1, 2022
1 parent 690b5ae commit b00ff89
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 14 deletions.
22 changes: 12 additions & 10 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ impl Index {

fn require_ordinal_index(&self, feature: &str) -> Result {
if !self.has_ordinal_index()? {
bail!("{feature} requires `--index-ordinals` flag")
bail!("{feature} requires index created with `--index-ordinals` flag")
}

Ok(())
Expand Down Expand Up @@ -377,20 +377,22 @@ impl Index {
Ok(blocks)
}

pub(crate) fn rare_ordinal_satpoints(&self) -> Result<Vec<(Ordinal, SatPoint)>> {
self.require_ordinal_index("looking up rare ordinals")?;
pub(crate) fn rare_ordinal_satpoints(&self) -> Result<Option<Vec<(Ordinal, SatPoint)>>> {
if self.has_ordinal_index()? {
let mut result = Vec::new();

let mut result = Vec::new();
let rtx = self.database.begin_read()?;

let rtx = self.database.begin_read()?;
let ordinal_to_satpoint = rtx.open_table(ORDINAL_TO_SATPOINT)?;

let ordinal_to_satpoint = rtx.open_table(ORDINAL_TO_SATPOINT)?;
for (ordinal, satpoint) in ordinal_to_satpoint.range(0..)? {
result.push((Ordinal(ordinal), decode_satpoint(*satpoint)));
}

for (ordinal, satpoint) in ordinal_to_satpoint.range(0..)? {
result.push((Ordinal(ordinal), decode_satpoint(*satpoint)));
Ok(Some(result))
} else {
Ok(None)
}

Ok(result)
}

pub(crate) fn block_header(&self, hash: BlockHash) -> Result<Option<BlockHeader>> {
Expand Down
26 changes: 22 additions & 4 deletions src/subcommand/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,9 +385,18 @@ impl Server {
}

async fn rare_txt(Extension(index): Extension<Arc<Index>>) -> ServerResult<RareTxt> {
Ok(RareTxt(index.rare_ordinal_satpoints().map_err(|err| {
ServerError::Internal(anyhow!("error getting rare ordinal satpoints: {err}"))
})?))
Ok(RareTxt(
index
.rare_ordinal_satpoints()
.map_err(|err| {
ServerError::Internal(anyhow!("error getting rare ordinal satpoints: {err}"))
})?
.ok_or_else(|| {
ServerError::NotFound(
"tracking rare ordinals requires index created with `--index-ordinals` flag".into(),
)
})?,
))
}

async fn home(
Expand Down Expand Up @@ -1348,7 +1357,7 @@ next.*",
}

#[test]
fn rare() {
fn rare_with_index() {
TestServer::new_with_args(&["--index-ordinals"]).assert_response(
"/rare.txt",
StatusCode::OK,
Expand All @@ -1358,6 +1367,15 @@ next.*",
);
}

#[test]
fn rare_without_index() {
TestServer::new_with_args(&[]).assert_response(
"/rare.txt",
StatusCode::NOT_FOUND,
"tracking rare ordinals requires index created with `--index-ordinals` flag",
);
}

#[test]
fn show_rare_txt_in_header_with_ordinal_index() {
TestServer::new_with_args(&["--index-ordinals"]).assert_response_regex(
Expand Down
10 changes: 10 additions & 0 deletions tests/find.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,3 +18,13 @@ fn unmined_ordinal() {
.expected_exit_code(1)
.run();
}

#[test]
fn no_ordinal_index() {
let rpc_server = test_bitcoincore_rpc::spawn();
CommandBuilder::new("find 0")
.rpc_server(&rpc_server)
.expected_stderr("error: find requires index created with `--index-ordinals` flag\n")
.expected_exit_code(1)
.run();
}
10 changes: 10 additions & 0 deletions tests/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,13 @@ fn output_not_found() {
.expected_stderr("error: output not found\n")
.run();
}

#[test]
fn no_ordinal_index() {
let rpc_server = test_bitcoincore_rpc::spawn();
CommandBuilder::new("list 4a5e1e4baab89f3a32518a88c31bc87f618f76673e2cc77ab2127b7afdeda33b:0")
.rpc_server(&rpc_server)
.expected_stderr("error: list requires index created with `--index-ordinals` flag\n")
.expected_exit_code(1)
.run();
}

0 comments on commit b00ff89

Please sign in to comment.