-
Notifications
You must be signed in to change notification settings - Fork 37.7k
Add block height support in rpc call getblock #8457
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
Conversation
I don't like overloading the argument, making it ambiguous. It seems messy, and we don't do that anywhere else on the API. |
Given that
For a new RPC I also share your opinion, about the |
I think |
Disagree, there is a place for overloading in APIs. In this case blocks are usually referenced by height or hash. There is also overloading in the protocol:
From
Unlikely? Still it's simple to distinguish that block hash from a number.
From the API point of view, I think it's fair to fetch a block from it's height. From a technical point of view, making 2 calls causing locks on cs_main etc on a heavy loaded daemon is resource wasting. So, +1 for |
I like the concept of one RPC call. But unfortunately there is one big caveat. In the future (and because of probability even in not so distant one), we can have a block whose hash is a hexadecimal number parseable as a decimal one resulting in the uncertain block referenced. We can't afford to have uncertain result... So NACK. So if we want to have an API to get block by hash, we can have one more RPC call or to live with
I'm fine even without the new RPC call... FWIW: as of block 423792 we do not have any block whose hash written in hex is decimal number. |
IMO it is possible to detect if a given input is a block hash, as it comes as string, and, given that block hashes have fixed length, I don't think that if we want get the block 5125122 we call like this:
So it will push a fix to this special case.
Imagine that you need to scan the whole blockchain, given that
So, IMO, a call like |
Compare results of
|
b6638a6
to
f32de13
Compare
@laanwj @promag @paveljanik WDYT of supporting something like this:
|
It's better. But still this is special casing to work around one specific round-trip. This opens the flood-gates to many more. To solve this more generally I plan on working on a cute little extension RFC to the JSONRPC 2.0 spec. See discussion in #7783 (comment) . This will add a simple form of (in-batch) promise pipelining to JSON-RPC batching, which will allow doing this kind of composition in a more flexible way. Eg [
{
"version": "x.x",
"method": "getblockhash",
"id": 0,
"params": ["123456"],
"ext_store": True // Store the result of this request for later use
},
{
"version": "x.x",
"method": "getblock",
"id": 1,
"params": [
null // Placeholder for argument 0, will be replaced by ext_load below
],
"ext_load": [
{
"src": [0, "result"], // Load the output of request id 0, ["result"]
"dst": [0] // into into params[0]
}
],
}
] The effect of which will be: tmp0 = getblockhash(123456);
tmp1 = getblock(tmp0); In contrast to simple nesting of calls, which would be the alternative extension, this also allows for more complex composition, and re-use of values. Returning: [
{
"id": 0,
"error": null,
"result": "0000000000002917ed80650c6174aac8dfc46f5fe36480aaef682ff6cd83c3ca"
},
{
"id": 1,
"error": null,
"result": {
"hash": ...,
"confirmations": ...,
...
}
}
] |
Nice JSONRPC feature, delegating the responsibility of forwarding the output from the RPC client to the RPC server, but in our case it will still have the wallet locks and performance issues. |
@pedrobranco None of these calls should grab a wallet lock... |
Sorry, I've meant locks on cs_main. |
|
@luke-jr granted, but sometimes a quick way to check for re-orgs on a locally managed chain is to compare various heights between the chains [local & bitcoind], if they don't match up, you can start to check for where the orphan-fork occurred. |
That's an implementation detail and will likely go away in the future, and we should not design the RPC protocol around that. In any case what use-case to you have that requires shaving a few milliseconds from a getblock call? Have you benchmarked? I'm fairly sure most of the bottleneck is reading and parsing the block and formatting JSON [hopefully done without holding the cs_main lock] - if the latter, have you looked at the REST interface? Edit: If you need a lot of getblock calls you can pipeline without any RPC extensions: in the first batch, run getblockhash on block number 0..X - this is what our linearize.py script does too. In the second batch you can then request all the actual block data using
Interesting point. It's safe for old blocks, but for working at the tip, using it probably means you're not handling reorgs properly. |
Closing this. See posts above for argumentation. |
This is a simple PR: add support for getting a block by its height via RPC call
getblock
.Instead of 2 calls
bitcoin-cli getblockhash 0
+bitcoin-cli getblock <hash>
we call directly
bitcoin-cli getblock 0
.Note: it will continue to support via block hash.