Skip to content

Conversation

@lejeunerenard
Copy link
Contributor

Add the property .remoteContiguousLength for the maximum known peer contiguous length and the remote-contiguous-length event for when it updates.

Added documentation noting the following exceptions:

  • The remote contiguous length is not updated when a peer is not fully contiguous.
  • The event is not fired due to truncation.

mafintosh and others added 13 commits October 27, 2025 09:11
To save messages being sent by all peers, the existing behavior of
sending a `range` request in `broadcastRange()` only when fully
contiguous is being kept. This means the `.remoteContiguousLength` and
the `remote-contiguous-length` event only update / fire when the remote
peer is fully contiguous and not when partially contiguous.
This emphasizes that the variable is true only when it is fully
contiguous, ie having the full range of blocks. This is in contrast to
being partially contiguous.
Includes note that it is only updated when the peer thinks it is fully
contiguous (aka non-sparse).
Now allows appends after truncating to fire remote contig event.
lejeunerenard added a commit to holepunchto/hypercore-storage that referenced this pull request Oct 28, 2025
To support holepunchto/hypercore#743 persisting
`remoteContiguousLength`.
@lejeunerenard
Copy link
Contributor Author

Currently depends on holepunchto/hypercore-storage#92 for persistence test.

@lejeunerenard lejeunerenard marked this pull request as ready for review October 28, 2025 20:54

const i = Math.floor(start / DEFAULT_SEGMENT_SIZE)
const contig = this.core.header.hints.contiguousLength === this.core.state.length
const fullyContig = this.core.header.hints.contiguousLength === this.core.state.length
Copy link
Contributor

Choose a reason for hiding this comment

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

(detail) unneeded renaming of the variable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The renaming was because initially I thought it meant that the local core was contiguous even if partially so. So renamed only to clarify that it is fully contiguous in the sense that it matches the latest length.

But yeah, no functionality change.

Not likely to happen in practice, but just to be safe.
Peers always send `range` requests starting with `0` if they are
contiguous. Partial ranges would imply they are not contiguous so
shouldn't be able to sparsely update the `remoteContiguousLength`.

Because there is no need check the `start` of the range &
`core.updateRemoteContiguousLength()` already checks if the `length` is
larger, the logic is simplified to only call
`updateRemoteContiguousLength()` when updating the
`_remoteContiguousLength` for the peer when the range isnt a drop.
Could mask underlying error and its assumed that storage errors at this
point should not be caught.
if (length > this._remoteContiguousLength) this._remoteContiguousLength = length
if (length > this._remoteContiguousLength) {
this._remoteContiguousLength = length
this.core.updateRemoteContiguousLength(length)
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not sure we can let it go uncaught either.

I think either we need a dedicated error handler, but we don't have any standard place to emit the event..

What do you think about changing updateRemoteContiguousLength to be sync and have it set a needsHintFlush flag on the core, then SessionState.flush always checks that flag and when flushed these lines are triggered by calling a _onhintflush hook instead?

Copy link
Contributor

Choose a reason for hiding this comment

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

it would mean that remoteContiguousLength may be stale on restart, not sure of the ramifications there

Copy link
Contributor

Choose a reason for hiding this comment

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

seems like we aren't bubbling up from the replicator anyways, so can just catch with an error handler that mimics here

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@chm-diederichs so a safetycatch and is the WRITE_FAILED the only error code we want to pause from?

Copy link
Contributor

Choose a reason for hiding this comment

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

no I think we should pause on any error, my link was misleading, I meant to refer to just the safetyCatch and the pause

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Fixed in 52a3e89

if (length > this._remoteContiguousLength) this._remoteContiguousLength = length
if (length > this._remoteContiguousLength) {
this._remoteContiguousLength = length
if (this.remoteFork >= this.core.state.fork) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done in 925dadf

`core.updateRemoteContiguousLength()` is only called from `onrange` so
moving the check doesn't allow unviable lengths through from elsewhere.
mafintosh pushed a commit to holepunchto/hypercore-storage that referenced this pull request Nov 7, 2025
* Update schema code gen files

Includes formatting & adding the `"strings"` flag to the JSON for enums.

* Add `remoteContiguousLength` field to hints

To support holepunchto/hypercore#743 persisting
`remoteContiguousLength`.

* Update setHint test to update remoteContiguousLength
lejeunerenard and others added 3 commits November 7, 2025 16:04
Use ternary instead of a combination of optional chaining and null
coalescing.
@mafintosh mafintosh merged commit 5a73498 into main Nov 10, 2025
4 checks passed
@mafintosh mafintosh deleted the remote-contig-length branch November 10, 2025 16:19
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants