Repository navigation
Conversation
Add tests for replication Fix bugs getting pending_missing_nodes
`Hypercore::update(UpdateOptions { wait, force, timeout })` resolves `Ok(true)`
when the core's verified length grew, `Ok(false)` when peers confirmed there is
nothing newer, and `Err(HypercoreError::Timeout)` when nobody answered in time.
Previously a reader could only discover new data by polling `info().length` or
by `get()`ing an index and hoping replication had reached it.
Two supporting changes were needed:
1. A peer registry. Each `ChannelState` now owns an `Arc<PeerSyncState>` of
atomics (remote_synced / remote_length / remote_fork / remote_can_upgrade /
length_acked / upgrade_inflight) and self-registers a `Weak` of it into
`HypercoreInner::peers`. This lets the core see every peer's advertised
length regardless of who drives replication — `Replicator`,
`attach_replicator`, or corestore's `attach_channel` — which is what makes
an honest `Ok(false)` possible. It is a port of JS's
`_checkUpgradeIfAvailable` (replicator.js:2244). `Weak` means there is no
deregistration to get wrong: entries are pruned on read.
2. Upgrade requests are retried. The request used to be built only inside
`on_synchronize`, so a single misfire of its `length_acked == info.length`
guard meant it was never sent at all. It is now
`ChannelState::maybe_request_upgrade`, called from `on_synchronize` and once
per poll, deduped on (fork, our length, their length) — Rust's `updateAll()`.
Two payload-free `Event` variants (`PeerSync`, `Upgrade`) carry the wake-ups.
The event channel is lossy (broadcast(32), overflow: true), so they are only
ever wake-ups; all authoritative state is read back from the registry.
Also adds `Hypercore::peer_count()`, a `HypercoreError::Timeout` variant, and a
plain tokio dependency for the timer. Drops a stray `dbg!()` from `GetFuture`.
Tested by 7 new cases in tests/replication.rs. Note `update` does not subsume
the existing `wait_for_length` helper: it guarantees verified length, not that
blocks have been downloaded (contiguous_length), and swapping it in fails
deterministically.
Known test gaps, to be addressed next: the per-poll retry in (2) is not covered
(removing it leaves all tests passing), and every test uses exactly one peer, so
the multi-peer case the registry exists for is unexercised, as is peer teardown.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
Adds multi-peer coverage for `Hypercore::update`, and refactors the decision it rests on so that coverage is worth something. The obvious integration tests turned out not to test anything. Two peers attached to one reader, 60 consecutive green runs — and then mutating the peer loop to `peers.iter().take(1)`, the "first responder wins" design the registry exists to avoid, left all 17 integration tests passing. In the all-in-sync case both peers are useless so checking one or all gives the same answer, and in the mixed case the retry loop masked the difference, because `maybe_request_upgrade` upgrades the reader regardless of what the predicate decides. So `any_peer_may_upgrade` moves out of `impl UpdateFuture` into a free function over `(&[Arc<PeerSyncState>], &Info, &Snapshot, wait)` and is unit tested directly, with 8 cases in src/replication/update.rs. Multi-peer states are a race to stage over real connections and exact to construct by hand. The multi-peer case asserts both peer orderings so it cannot pass by luck. Five mutations were used to confirm the tests bite: consulting only peers[0] (kills 2), a useless peer ending the search (3), dropping the fork check (1), never waiting for an unsynced peer (1), and ignoring upgrade_inflight (1). The integration test `update_with_two_peers_only_one_useful` also asserted something the design does not guarantee. A probe showed a single useless peer makes `update` return Ok(false) in ~58ms rather than holding out for peers that have not connected — correct, and matching JS's `_checkUpgradeIfAvailable`, which likewise iterates only the peers present at that instant. It passed on timing luck; had the empty peer won the registration race the assert would have failed. It now asserts the postcondition, with the reasoning in a comment. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
… bits `Entry::decode` tested flag bit 2 (tree nodes) for the tree upgrade and bitfield update as well, where `encode` writes bits 4 and 8. Local appends set all three, so they decoded by accident. Replicated blocks store nodes and a bitfield update without an upgrade, so reopening a core that had received data from a peer failed with "Compact encoding failed, out of bounds". Entries without tree nodes silently lost their upgrade and bitfield update. The bytes on disk were always correct, so existing stores open without migration. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Also add some logs for debugging when replications goes wrong
A peer requests an upgrade only once we have acked its length, and the ack travels in our Synchronize. `on_synchronize` only answered the first one, so if the writer appended, and sent its data-upgrade Synchronizes, before it learned the reader's new length, every one of them carried a stale length and nothing ever sent a fresh one. The reader stalled one upgrade behind for good. JS `onsync` calls `signalUpgrade()` whenever its length is greater than the peer's; do the same. Timing-dependent, so it only showed on a multi-threaded runtime (found through hrss). The regression test polls both replicators by hand to force the ordering, and fails without the fix. Also records the two earlier oplog fixes in the CHANGELOG. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
`get` on a block not downloaded yet only waited when a replicator was attached with `attach_replicator`. With a channel from `attach_channel` (corestore's path) or a spawned `replicate`, it returned `Ok(None)` at once, which hyperbee reported as `NoBlockAtSeqError` right after an upgrade. It now also waits while the core's peer registry has a live channel, and still returns `Ok(None)` straight away when nothing is replicating. It also subscribed to events after emitting `Get`, despite its comment; with the channel driven by another task the reply could be missed. Subscribe first. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
When a peer advertised more than we had verified, queued block indices past our length were requested anyway. Verifying the reply needs tree nodes we only get with an upgrade, so if the block request reached the peer ahead of our upgrade request, replication failed with "Could not read from store tree ... out of bounds". JS bundles an upgrade into such a request; without that, wait for the upgrade instead. `pop_requestable_index` is now the one place indices leave `pending_request_indices`, and drops those at or past our length. They are not lost: sequential download resumes from the old length after the upgrade, and a waiting `get` re-emits `Get` on every `DataUpgrade`. The regression test polls one side at a time to force the ordering. Also adds a `send Request (block)` trace. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
seek_untrusted_tree went on to seek_trusted_tree before the subtree's byte offset was known, with bytes not yet made relative to the subtree. When the trusted seek could finish from in-memory nodes, it returned that wrong answer and dropped the pending storage reads. Return the reads first, as JS awaits the offset before seeking. Latent until more nodes stay in memory (next commit); then the core_create_proof_block_and_seek_* tests catch it. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
flush_bitfield_and_tree_and_oplog drained tree.unflushed under the lock and returned the future that writes the nodes, so until that write completed they were in neither memory nor storage. On disk storage, a get or proof running alongside an append (which flushes every 4th call) read past the end of the tree file: "Could not read from store tree, index 1840 / length 40 is out of bounds for store length 1640". MerkleTree::flush now moves the nodes into flushes_in_flight, which lookups check after unflushed. The write future sets that flush's written flag, and the next flush prunes the written ones. Truncation forgets in-flight nodes past the truncation point, as it does unflushed ones. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
ChannelState::on_message kept one slot for an incoming Request (proof being built) and one for incoming Data (proof being applied), and dropped whatever arrived while the slot was busy. With a disk-backed writer this happened constantly. Dropped block requests were masked by the reader re-sending them, but a dropped upgrade request is never re-sent, so the reader stopped following the writer. JS queues both; now so do we, with a trace line when one waits behind another. Test: an_upgrade_request_that_arrives_while_a_block_proof_is_being_built_is_answered. It appends with both replicators polled (new while_replicating helper): appending while a disk-backed replicator is paused mid-read deadlocks on random-access-disk's per-file lock. replicate_until's panic now names the side that failed. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
get used to answer None at once while no replication was attached,
which raced spawning replicate() and forced dependents into poll loops
(hrss's await_keys_entry, corestore's tests). It now matches JS's
core.get(index, opts): get_with_options(index, GetOptions { wait,
timeout }) -> Result<Option<Vec<u8>>>.
With wait (the default, and what get uses) a missing block is waited
for even with no peer, until it arrives or the timeout gives
HypercoreError::Timeout; it never answers None. An attached replicator
finishing no longer ends the wait. GetOptions::without_waiting() reads
local data only, and CreateProofFuture uses it. Without the replication
feature nothing can deliver a block, so a get never waits.
A waiting get re-emits Get on Have, DataUpgrade and PeerSync. PeerSync
fires when a channel registers and now also from attach_replicator, so
a get that started before replication existed is heard by the new peer.
Call sites relying on the immediate None (examples, model, js_interop,
a unit test) use without_waiting(). New tests cover both "replication
starts after the get" paths and were checked to fail without their
wake-ups; the waiting get runs on its own task because tokio::join!
re-polls every future on any wake and hides a missing one.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Streaming a 485-block podcast episode through the hrss aggregator, block requests roughly doubled from one block to the next and a get timed out around block 34: the duplicate replies queued ahead of the one that mattered. Two amplifiers: - GetFuture re-emitted Get on every Have, and every reply is a Have. A Have now only makes it re-check the bitfield; Get is re-emitted on DataUpgrade (the block may now be inside the tree) and PeerSync (a new peer to ask). Re-sending on every Have used to hide dropped requests, which 4968b42 fixed at the source. - After applying any Data the replicator queued a request for the next block, even one already local or already requested, so each duplicate reply brought another duplicate request. Each peer now keeps the set of blocks requested but not yet answered (blocks_requested_in_flight), cleared when Data for the block arrives, and local blocks are skipped. Test: sequential_gets_of_many_blocks_do_not_stall, 100 gets in order. Before: stalled at block 15. With the first fix alone: about 51 requests per block, 13.7 s. With both: exactly 100 requests, about 0.3 s. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Port cowlicks/replicator#9 to the replication code in core: the remote peer's bitfield is now a RangeSetBlaze<u64> instead of a BTreeSet<u64>, so contiguous runs announced by peers are stored as single ranges and set_range(true) is one ranges_insert rather than a per-index loop. Differences from the original PR: - Zero-length ranges return early. range-set-blaze panics on an empty range, and the length comes from a remote peer's Range message. - Clearing a range uses set difference instead of per-index removes. range-set-blaze is an optional dependency enabled by the `replication` feature. Co-Authored-By: Louis Gregg <9048899+louisgregg@users.noreply.github.com> Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
It returned None when every page up to the biggest one was full, though every index past that page is unset. Nothing called it with false yet; download ranges will, to find the first missing block. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
core.download(3..10) or core.download(0..) returns a Download handle. Replicators fetch the range's blocks for as long as the handle lives; dropping it cancels. Download::done() resolves once every block in a bounded range is local, driving an attached replicator like get does. - Ranges live in a registry of Weaks on the core, like the peer list, so a lost event cannot lose one; DownloadRequested only wakes. - Each poll, a channel with fewer than 16 requests outstanding takes the first block of the oldest range that is missing locally, inside the verified and peer lengths, held by the peer, and unclaimed. - Block requests are claimed core-wide (index -> Weak<PeerSyncState>), so with several peers each block is requested from one at a time. A claim dies with its peer's channel. Gets use the same claims. - poll_attached_replicator is shared by get, update and done. The old per-block readahead still runs alongside; removed next. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Every Data reply queued a request for the next block, and every upgrade one for the old length, so a reader quietly downloaded everything after the first block it read, one round trip at a time. It never filled gaps before that block and stopped at the first block already held. JS v10.31.10 fetches nothing unasked; now neither do we: a block is requested for a waiting get or a download range. The tests that copied a whole core without asking now hold download(0..), as does the upgrade-during-proof test, whose reader has to be downloading. New: a bounded download fetches only its range, and dropping a download stops it after at most one window of requests. bee, blobs, corestore and hrss tests pass unchanged. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
… RTT Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Two peers sending one reader the same upgrade both passed the commitable check after verifying, then the second failed at MerkleTree::commit with "Tree was modified during changeset" -- after flushing its oplog entry, replacing the header and updating the bitfield. The error ended that channel's replication (hrss then dropped all the reader's connections) and left the core inconsistent: in a live run, readers hit by it never served their feed even after reconnecting. Like JS v10.31.10's _verifyExclusive, a per-core async lock is now held from the commitable check to the commit, and a changeset that no longer applies resolves Ok(false) before anything is written. Verification itself still runs concurrently. Test: two upgrade proofs applied concurrently to a disk-backed reader (it failed with the live error before); exactly one applies and the reopened core agrees. Live: three aggregators joining one after another now connect with no dropped connections (was: two of three failed). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
…or fixes Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
A block request claimed by a peer that never answered was never asked again of anyone, so a download or get stalled on that block (seen live: a reader with four peers timed out on one block). - NoData, as JS does: a peer that can't build a proof says so, and the requester releases the claim and asks another peer. - A 20 s request timeout (beyond JS, as a backstop for a peer that neither answers nor closes): the claim is released and Cancel sent. - Either way the peer is not asked for that block again, and a new BlockRequestReleased event makes waiting gets and download ranges request it elsewhere. - Block request ids are index + 2, so a NoData names its block and never collides with the upgrade request (id 1). - At sync a peer advertises every run of blocks it holds, not only its contiguous prefix, so a reader that streamed part of a feed can serve it. Incoming Range now honours drop. - Logging: peers get ids; claims and releases are traced; a timed-out get says which peer holds its block's request and for how long. Tests: NoData (a seeder that cleared an advertised block), the timeout (a seeder that goes silent, on a paused clock), and gap advertisement. Each fails with its mechanism disabled. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
See #148 for more info
Also removes remaining references to
async-std