Page MenuHomePhabricator

[chronik] implement electrum RPC blockchain.block.headers
ClosedPublic

Authored by PiRK on Thu, Jan 9, 20:08.

Details

Reviewers
Fabien
Group Reviewers
Restricted Project
Commits
rABC98839217de42: [chronik] implement electrum RPC blockchain.block.headers
Summary

See https://electrum-cash-protocol.readthedocs.io/en/latest/protocol-methods.html#blockchain-block-headers

Note that we have a minor difference in behavior vs Fulcrum:
Fulcrum has an additional check that height, cp_height and start_height are < Storage::MAX_HEADERS (100'000'000), and returns a "Invalid height/cp_height" error message in such a case. We don't return such an error before these values hit max_int32 + 1.
I don't see any value in hardcoding such an additional limit, as these parameters are all bound by more restrictive values (tip_height, max_count=2016) before we even do any computation. So we still get an error in the same situation, but a different one.

Test Plan

ninja check-functional

Check that the results and error messages are the same as the ones returned by Fulcrum (with the above mentionned minor difference)

Event Timeline

PiRK requested review of this revision.Thu, Jan 9, 20:08
Fabien requested changes to this revision.Thu, Jan 9, 20:20
Fabien added a subscriber: Fabien.
Fabien added inline comments.
chronik/chronik-http/src/electrum.rs
337

last time I checked positive i32 was very close to u32 :) You should use that in your Result and maybe call it json_to_u31 or something like that

This revision now requires changes to proceed.Thu, Jan 9, 20:20

remove a duplicated test and improve another comment

PiRK planned changes to this revision.Thu, Jan 9, 20:29
PiRK added inline comments.
chronik/chronik-http/src/electrum.rs
420 ↗(On Diff #52083)
337

function renamed so i can be used for other params than just the block height (count needs the same checks)

367

This is the error message Fulcrum returns, the previous error message "Invalid height"" was incorrect

rename helper function json_to_u31. We still return a i32, because that's what all functions expect a block height to be in the node and the indexer, but with the added guarantee that it is >=0 . Returning a u32 would just shift the problem from checking >= 0 to checking <=2**31 -1, while requiring a lot of as i32 conversions when calling the indexer functions.

Fabien requested changes to this revision.Fri, Jan 10, 08:51

More questions than requesting changes

chronik/chronik-http/src/electrum.rs
421 ↗(On Diff #52085)

This is not a good implementation (it overrides the user request silently oO), but afaict this is also what fulcrum does... And at least it's documented

461 ↗(On Diff #52085)

Why don't you get them all by range, then serialize in a loop ? That will be more efficient

test/functional/chronik_electrum_blockchain.py
261 ↗(On Diff #52085)
This revision now requires changes to proceed.Fri, Jan 10, 08:51

use headers_by_range which simplifies the code a lot

chronik/chronik-http/src/electrum.rs
421 ↗(On Diff #52085)

Yes it is a weird behavior. It works because the actual count is part of the response, but that is a bit of a waste of bandwidth (and the hardcoded max count is even more wasteful and useless in the response)

Fabien added inline comments.
chronik/chronik-http/src/electrum.rs
457–472 ↗(On Diff #52108)

headers_hex doesn't need to be mutable

This revision is now accepted and ready to land.Fri, Jan 10, 13:11

collect the headers_hex