Skip to content

rest: Query predecessor headers using negative count param#33752

Open
A-Manning wants to merge 1 commit intobitcoin:masterfrom
A-Manning:2025-10-31-rest-headers-downward
Open

rest: Query predecessor headers using negative count param#33752
A-Manning wants to merge 1 commit intobitcoin:masterfrom
A-Manning:2025-10-31-rest-headers-downward

Conversation

@A-Manning
Copy link
Contributor

There is currently no mechanism by which to batch-query headers from tip, backwards towards genesis.
This PR extends the REST Blockheaders API to support batch-querying predecessor headers, by providing a negative count parameter.

@DrahtBot
Copy link
Contributor

DrahtBot commented Oct 31, 2025

The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

Code Coverage & Benchmarks

For details see: https://corecheck.dev/bitcoin/bitcoin/pulls/33752.

Reviews

See the guideline for information on the review process.

Type Reviewers
Concept NACK stickies-v

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #34654 ([RFC] BlockMap and CChain Concurrency Improvement by alexanderwiederin)

If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

@A-Manning A-Manning force-pushed the 2025-10-31-rest-headers-downward branch 2 times, most recently from ae13046 to b638085 Compare October 31, 2025 10:45
@A-Manning A-Manning force-pushed the 2025-10-31-rest-headers-downward branch 2 times, most recently from 475bb22 to 1f5b786 Compare October 31, 2025 16:40
@A-Manning A-Manning requested a review from maflcko October 31, 2025 18:50
@maflcko
Copy link
Member

maflcko commented Nov 3, 2025

Though, I wonder what the use case is. If someone knows a header, it seems likely they also know about the previous headers and a feature to support asking for them would have no users?

@A-Manning
Copy link
Contributor Author

Though, I wonder what the use case is. If someone knows a header, it seems likely they also know about the previous headers and a feature to support asking for them would have no users?

Knowing a header only means that you know the previous block hash. If you want to request headers from best tip to genesis, There is no existing mechanism other than requesting headers one-at-a-time. This makes it possible to batch request headers from tip to genesis, which is much more efficient than requesting one-at-a-time.

Copy link
Contributor

@stickies-v stickies-v left a comment

Choose a reason for hiding this comment

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

There is no existing mechanism other than requesting headers one-at-a-time.

Request the starting hash with GET /rest/blockhashbyheight/<HEIGHT>.<bin|hex|json> and then request headers upward with /rest/headers/?

Concept NACK, unless I'm misunderstanding the limitations/use case.

@A-Manning
Copy link
Contributor Author

Request the starting hash with GET /rest/blockhashbyheight/.<bin|hex|json> and then request headers upward with /rest/headers/?

We currently use this approach at L2L. This strategy is far from ideal - any of the upward requests may return headers from a different active chain, in the event of a re-org. It is necessary to check that the final block hash is the same as the tip hash we want to sync to, and if it is not, then the only way to retrieve the headers would be requesting headers via JSON-RPC, one at a time.

@stickies-v
Copy link
Contributor

The reorg potential does make using the interface more cumbersome in general, I'm not sure your approach solves that? You're still limited by what's in the active_chain, so you still need logic to deal with what if the index you're requesting a negative count from isn't in the active chain anymore?

@A-Manning A-Manning force-pushed the 2025-10-31-rest-headers-downward branch from 1f5b786 to 200a9c9 Compare November 6, 2025 17:54
@A-Manning
Copy link
Contributor Author

@stickies-v Updated to handle the case where the requested block header is no longer in the active chain.

@sedited
Copy link
Contributor

sedited commented Mar 5, 2026

I'm still a bit confused at what this is solving. Re-orgs are just a couple of blocks deep, so it shouldn't make a difference which direction you are querying from. Whether you need to handle reorgs around the tip after querying headers beginning from your current state, or beginning from the last known tip, you still need to reconcile reorgs either way, no?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants