Repository navigation
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. ReviewsSee the guideline for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, this pull request conflicts with the following ones:
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. LLM Linter (✨ experimental)Possible places where named args for integral literals may be used (e.g.
2026-03-18 06:50:01 |
There was a problem hiding this comment.
I'm not convinced this would be a good addition to the kernel interface.
Without this, callers of libbitcoinkernel have no canonical way to recover from a stale tip.
Why can't kernel users construct their own locator by reverse iterating from the stale tip using any logic they want to? I don't think there is or should be a canonical way to do so? The way Bitcoin Core implements how they get filled should in my view just be an implementation detail?
edit: I do think we need to improve the interface to make it easier to efficiently build a locator performantly. I would love to generalize btck_chain_get_by_height to work on any chain (including a stale tip), and not just the currently active chain.
|
Looking this over briefly, this still needs work. If we expose locators, we should expose their primitive, not just a list of block hashes. The API could be pretty simple in that case: Just a create, destroy, and to bytes function. I agree that locators can generally be seen as an implementation detail. However I'd like to advocate in their favor a bit. We do have a function that finds forks with block locators ( |
A block locator is the primitive a node uses to announce its chain position to peers. Exposing it lets callers recover from a stale-tip shutdown by sending their locator so a peer can identify the fork point and serve the missing blocks. Exposes btck_BlockLocator as an opaque type with create/destroy/to_bytes functions Re-uses GetLocator(tip) from src/chain.cpp. Includes a C++ wrapper and Boost tests for the genesis-only and post-IBD cases
a7c27b1 to
9582658
Compare
|
Updated to address the feedback. Exposes |
|
Concept NACK, I think this belongs in a p2p library. I would be interested in reviewing related changes such as
Right, but that's true for a lot of p2p functionality. I don't see why that means kernel should start exposing p2p?
I think |
Initially I thought the immediate benefit of having a higher level method is that it already takes care of the required synchronization. We don't have a mechanism to expose a stable view of the chain, or lock cs_main and for stuff like finding ancestors in the chain, that seemed relevant. Looking at it a bit more closely now, I'm not sure that is something actually desirable. A common ancestor function as you suggest it, does not necessarily require additional synchronization , the usual assumption that a looked up entry from the chain immediately turns stale still holds. If a block request formed by such a stale entry ends up putting us on a fork, we should just repeat the operation. The guarantee we want to provide is eventual consistency. This means I'd support your suggestion @stickies-v and would recommend closing this pull request for now. If exposing such a lower level function proves not good enough, this can always be revisited. |
I think this questions reveals the ambiguity of where responsibilities are for library code. As I understand it, block validation is dangerous for a p2p crate to recreate because it risks forking or partitioning the network if done incorrectly. However, a p2p crate that exports code to perform various network functions like ping, do not cause connected clients to get wrong information if it's not implemented correctly (since the node would be booted soon). If the chain locator is implemented incorrectly, the node would stay network connected, but possibly relay false information to the network being on a stale tip? Therefore, this locator code seems somewhat more integral than your common p2p functionally as I understand it. That is to say, if implementation of the locator code is buggy, could it bomb some portion of the network? I'm not necessarily against having this be in a supported p2p crate, however it would be nice to have some assurance that locator code is functionally correct. For example, identify some common agreed upon behavior that the p2p crate must pass to correctly implement the locator. In other words, how can we make sure that various libs implement it exactly if it's not to be exposed here (for now and in the future). |
|
Per the discussion, I will close out this PR and plan to add |
I'm not sure if I understand you correctly, but I think you're saying that because bugs in block locator logic could lead to forking or network partitioning, we should consider not letting p2p libraries handle this? If so, I strongly disagree. Kernel should be really good at validation, a p2p library should be really good at p2p.
I agree (and I don't think block locators have anything to do with block validation) |
Yes, that is what I was getting at. My understanding is that critical consensus code was being provided via the C api to prevent other implementations from duplicating this functionality and making mistakes. There is also the issue of how to keep things fresh in that if block code is changed, and core updates the locator code, and there is a bunch of clones of locator code, how does one keep things in sync.
@pzafonte Sorry to bump you around so much. Consider opening a PR for the locator stuff in p2p lib here https://github.com/rust-bitcoin/rust-bitcoin/tree/master/p2p. |
0f09920 p2p: add BlockLocator newtype with logarithmic build (Peter Zafonte) Pull request description: As [suggested by yancyribbens](bitcoin/bitcoin#34843 (comment)) in bitcoin/bitcoin#34843, I'm opening this PR to give consideration to a BlockLocator newtype and an associated build method. There is currently no standard implementation within in a p2p library and locator algorithm has to be duplicated across implementations. A p2p crate seems to be the right home for this, as it could provide a single generally agreed upon implementation that downstream projects can depend on rather than having to re-implement the algorithm correctly and efficiently on their own. This replaces the bare `Vec<BlockHash>` in GetBlocksMessage and GetHeadersMessage with a BlockLocator newtype. I modeled it and its encoding/decoding implementations after existing types in this crate. Please let me know if anything is missing or inconsistent. It also includes a build method, which matches the algorithm that is used by Bitcoin Core's LocatorEntries() in [src/chain.cpp](https://github.com/bitcoin/bitcoin/blob/4f8bd396f8ab04f7ea8bde0dce3829c345d6c54b/src/chain.cpp#L26-L43) and caps at [MAX_LOCATOR_SZ (101)](https://github.com/bitcoin/bitcoin/blob/4f8bd396f8ab04f7ea8bde0dce3829c345d6c54b/src/net_processing.cpp#L123-L124). I considered how to keep chain access out of this crate and settled on a caller-supplied get_ancestor closure, in order to keep the greatest degree of consistency as possible with the crate's scope. (As an aside I currently have a PR open in the core kernel to expose get_ancestor here: bitcoin/bitcoin#34885 ) I'm uncertain about the right level of test coverage for the build method. The encoding tests for GetBlocksMessage and GetHeadersMessage already exist. ACKs for top commit: apoelstra: ACK 0f09920; successfully ran local tests Tree-SHA512: 8cc909575936cb80b7534d63a2944397892c446655d32bc4c450f7972e3bbe39d74c055b45997851460a0fa546de4b228257ca5ffa219f9f25d75eede5ec89b1
df44afd kernel: expose btck_block_tree_entry_get_ancestor (Peter Zafonte) Pull request description: Callers building a block locator from a stale tip currently have to walk back one entry at a time using btck_block_tree_entry_get_previous. For large step sizes this means hundreds of C API calls per locator entry. This exposes GetAncestor via btck_block_tree_entry_get_ancestor, which uses the existing CBlockIndex skiplist to reach any ancestor height in O(log N) and works on any chain, not just the active chain. Includes a C++ convenience wrapper and tests ~~covering same-height, genesis, and out-of-range cases~~ in btck_block_tree_entry_tests. See previous discussion here #34843 ACKs for top commit: alexanderwiederin: ACK df44afd sedited: ACK df44afd Tree-SHA512: e953b634235129fa972e7f148016e8aee7bad61e16d50191d6724d11c71fe0f7623152ff61aa4c4ec5c8a5b500aaeb36613b5b2ff1e404d01ed7b65aa861f7aa
The block locator is the primitive a node uses to announce its chain position to peers. When a node resumes after shutting down on a stale tip, it sends its locator so a peer can identify the fork point and serve the blocks needed to reach the best chain. Without this, callers of libbitcoinkernel have no canonical way to recover from a stale tip.
Adds three C API functions:
btck_chainstate_manager_get_locatorto retrieve the active tip's locator as abtck_BlockLocator,btck_block_locator_to_bytesto serialize it, andbtck_block_locator_destroyto free it.Includes a C++ convenience wrapper and Boost tests covering the genesis-only and post-IBD cases.
Motivated by kernel-node/pull/30 (edit: idea for the PR came from rustaceanrob's comment ), which needs this primitive to implement sync recovery in a Rust consumer of libbitcoinkernel.