Skip to content

Conversation

JeremyRubin
Copy link
Contributor

One feature in the REST API not in the JSON RPC (afaict) is that you can fetch a page of headers (up to 2k) at a time via rest but only single headers via RPC. This patch adds a count argument to the rpc to allow for similar functionality without having to make N RPC calls (more efficient).

The API i chose is if the count = 0, then a single header will be returned (matching the previous behavior). If count > 0, then the returned result will either be a concatenated hex string array or a json object array. Perhaps a bikesheddable semantic, but I think it's reasonable to do it this way given that consumers of the API probably never actually want to return 0 headers, but may want the return type to be consistently an array whether 1 or 2 or 1000 headers are requested.

@JeremyRubin
Copy link
Contributor Author

I had a great question come in on twitter: https://twitter.com/lightclients/status/1450955679116587016

Q: Why not just encourage RPC Batching?
A: Because this RPC takes parameters of a Hash (and not height), we can't call the i+1st RPC until we know the result of the i-th.

count = request.params[2].get_int();
if (count < 0)
throw JSONRPCError(RPC_INVALID_PARAMETER, "Count must be >= 0");
if (count > 2000)
Copy link
Contributor

Choose a reason for hiding this comment

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

maybe else if here?

Copy link
Member

Choose a reason for hiding this comment

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

The early-return statements are already mutually exclusive, so no need for else. Though {} would be good for new code.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yea, sorry, didn't notice the throw

Copy link
Contributor Author

Choose a reason for hiding this comment

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

ah -- I didn't realize the preference was to have it on a single line as per:

If an if only has a single-statement then-clause, it can appear on the same line as the if, without braces. In every other case, braces are required, and the then and else clauses must appear correctly indented on a new line.

@luke-jr
Copy link
Member

luke-jr commented Oct 29, 2021

Batch getblockhash and then batch this?

if (count < 0)
throw JSONRPCError(RPC_INVALID_PARAMETER, "Count must be >= 0");
if (count > 2000)
throw JSONRPCError(RPC_INVALID_PARAMETER, "Count must be <= 2000");
Copy link
Member

Choose a reason for hiding this comment

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

Maybe it should just stop at 2000?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

goal was to match the Rest API

Comment on lines +941 to +943
ssBlocks << pblockindex->GetBlockHeader();
}
std::string strHex = HexStr(ssBlocks);
Copy link
Member

Choose a reason for hiding this comment

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

IMO it would make more sense to return an Array of Strings, each with a single block header serialised.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

goal was to have the same output format as the rest API.

@JeremyRubin
Copy link
Contributor Author

Batch getblockhash and then batch this?

you actually need to getblockheader to get the height, then getblockhash for the N you need, and then call getblockheader for the N. And this still has a concurrency bug since the getblockhash do not execute atomically afaiu (a reorg could happen in between).

@DrahtBot
Copy link
Contributor

DrahtBot commented Nov 19, 2021

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

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #23703 (scripted-diff: Use named args in RPC docs by MarcoFalke)

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.

@DrahtBot
Copy link
Contributor

DrahtBot commented Dec 9, 2021

🐙 This pull request conflicts with the target branch and needs rebase.

Want to unsubscribe from rebase notifications on this pull request? Just convert this pull request to a "draft".

@DrahtBot
Copy link
Contributor

There hasn't been much activity lately and the patch still needs rebase. What is the status here?
  • Is it still relevant? ➡️ Please solve the conflicts to make it ready for review and to ensure the CI passes.
  • Is it no longer relevant? ➡️ Please close.
  • Did the author lose interest or time to work on this? ➡️ Please close it and mark it 'Up for grabs' with the label, so that it can be picked up in the future.

@maflcko
Copy link
Member

maflcko commented May 12, 2022

Marked "up for grabs"

@natanleung
Copy link

As this PR is marked as "up for grabs", I have opened a new PR #25261 that reimplements this functionality.

@fanquake fanquake closed this Jun 1, 2022
@bitcoin bitcoin locked and limited conversation to collaborators Jun 1, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

7 participants