Skip to content

Query block hash by by block height #13

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

gcomte
Copy link
Contributor

@gcomte gcomte commented Sep 16, 2022

Give the possibility to query a block hash by providing its corresponding block height

@coveralls
Copy link

coveralls commented Sep 19, 2022

Pull Request Test Coverage Report for Build 3227896333

Warning: This coverage report may be inaccurate.

This pull request's base commit is no longer the HEAD commit of its target branch. This means it includes changes from outside the original pull request, including, potentially, unrelated coverage changes.

Details

  • 37 of 43 (86.05%) changed or added relevant lines in 3 files are covered.
  • No unchanged relevant lines lost coverage.
  • Overall coverage increased (+0.3%) to 78.615%

Changes Missing Coverage Covered Lines Changed/Added Lines %
src/async.rs 14 16 87.5%
src/blocking.rs 13 17 76.47%
Totals Coverage Status
Change from base Build 3128702855: 0.3%
Covered Lines: 647
Relevant Lines: 823

💛 - Coveralls

@notmandatory
Copy link
Member

This looks good, @tnull and @vladimirfomene are also working on examples for testing, once their stuff is in you should add a similar test for this new query also.

Copy link
Contributor

@tnull tnull left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally looks good. Maybe we want to reuse these methods in get_header, since it also gets the block hash by height?

@vladimirfomene
Copy link
Contributor

I believe we need to be careful when reusing some of these methods in other methods as it might result into doing more than one round trip to the server.

@tnull
Copy link
Contributor

tnull commented Sep 21, 2022

I believe we need to be careful when reusing some of these methods in other methods as it might result into doing more than one round trip to the server.

Ah, excuse the mistake, I meant get_header, in which two request are sent anyways currently?:

pub fn get_header(&self, block_height: u32) -> Result<BlockHeader, Error> {
let resp = self
.agent
.get(&format!("{}/block-height/{}", self.url, block_height))
.call();
let bytes = match resp {
Ok(resp) => Ok(into_bytes(resp)?),
Err(ureq::Error::Status(code, _)) => Err(Error::HttpResponse(code)),
Err(e) => Err(Error::Ureq(e)),
}?;
let hash =
std::str::from_utf8(&bytes).map_err(|_| Error::HeaderHeightNotFound(block_height))?;
let resp = self
.agent
.get(&format!("{}/block/{}/header", self.url, hash))
.call();

@gcomte
Copy link
Contributor Author

gcomte commented Sep 21, 2022

Imo reusing get_block_hash within get_header makes sense.

Looking at the get_header code, I realize that I should check specifically for HTTP Error 404, in order to return HeaderHeightNotFound. However, this is only done by the async client, the blocking one just directly returns the HTTP error code. What's the reasoning behind this difference?

Copy link
Contributor

@tnull tnull left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Generally looks good, two nits.

src/async.rs Outdated
let bytes = resp.bytes().await?;
let hash =
std::str::from_utf8(&bytes).map_err(|_| Error::HeaderHeightNotFound(block_height))?;
let block_hash = self.get_block_hash(block_height).await?.to_string();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I think clippy will complain otherwise since BlockHash implements Display.

Suggested change
let block_hash = self.get_block_hash(block_height).await?.to_string();
let block_hash = self.get_block_hash(block_height).await?;

src/blocking.rs Outdated

let hash =
std::str::from_utf8(&bytes).map_err(|_| Error::HeaderHeightNotFound(block_height))?;
let block_hash = self.get_block_hash(block_height)?.to_string();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: Likewise.

Suggested change
let block_hash = self.get_block_hash(block_height)?.to_string();
let block_hash = self.get_block_hash(block_height)?;

@gcomte gcomte force-pushed the feature/query-block-hash-by-height branch from 3072fd0 to 6b2f84f Compare September 23, 2022 12:26
@gcomte
Copy link
Contributor Author

gcomte commented Sep 23, 2022

Generally looks good, two nits.

Fixed and rebased onto master. Clippy happy now.

@vladimirfomene
Copy link
Contributor

vladimirfomene commented Sep 26, 2022

@gcomte, I think it will be cleaner if we squash these changes into one commit.

@gcomte
Copy link
Contributor Author

gcomte commented Sep 26, 2022

Yeah, that could be done when merging, right?

@vladimirfomene
Copy link
Contributor

Hey @gcomte ! can you squash it here? It makes things easy for us.

@gcomte gcomte force-pushed the feature/query-block-hash-by-height branch from 6b2f84f to 05e1045 Compare October 5, 2022 15:58
@gcomte
Copy link
Contributor Author

gcomte commented Oct 5, 2022

Hey @gcomte ! can you squash it here? It makes things easy for us.

Done 👍

@gcomte
Copy link
Contributor Author

gcomte commented Oct 5, 2022

I guess I should write some tests as well, as this PR has been merged now: #10

@vladimirfomene
Copy link
Contributor

@gcomte, can you add test to this so that I can merge it?

@gcomte gcomte force-pushed the feature/query-block-hash-by-height branch from 05e1045 to b6d663b Compare October 11, 2022 14:53
@gcomte
Copy link
Contributor Author

gcomte commented Oct 11, 2022

@vladimirfomene : b6d663b

Do you want me to squash it into the other commit?

let (blocking_client, async_client) = setup_clients().await;
let block_hash = blocking_client.get_block_hash(21).unwrap();
let block_hash_async = async_client.get_block_hash(21).await.unwrap();
assert_eq!(block_hash, block_hash_async);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You may want to add another check comparing the Esplora results to RPC, i.e., BITCOIND.client.get_block_hash(21)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That's right. Added: 7c66cf1

@gcomte gcomte force-pushed the feature/query-block-hash-by-height branch from b6d663b to 7c66cf1 Compare October 11, 2022 15:36
@vladimirfomene vladimirfomene merged commit 8364d05 into bitcoindevkit:master Oct 12, 2022
@gcomte gcomte deleted the feature/query-block-hash-by-height branch October 12, 2022 07:39
vladimirfomene added a commit that referenced this pull request Oct 13, 2022
92606cc Test `get_header_by_hash` rather than `get_height` (Elias Rohrer)
6673912 Deprecate `get_height` (Elias Rohrer)
9e233ef Allow to retrieve a block header by hash (Elias Rohrer)

Pull request description:

  ~~Based on #13 since it probably land soon.~~

  This PR adds a method allowing to retrieve a `BlockHeader` via a given `BlockHash` and reuses it in `get_height`.

Top commit has no ACKs.

Tree-SHA512: 2be894f7e745e317b02512183e073806e90145cbde9ad89f10e5782d6bc302da6d72adb0cef0fce7cd746e4f7b93fba0844a640bc30457ccafbcf19dd5673254
@notmandatory notmandatory added this to the Release 0.2.0 milestone Oct 22, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants