Skip to content
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

feat: send core "haves" bitfield on first connect #254

Merged
merged 7 commits into from
Sep 13, 2023

Conversation

gmaclennan
Copy link
Member

This implements a solution to #124 so that we can know how much data there is to sync before starting replicating the hypercores.

The code here implements a new custom hypercore extension to send core bitfields over the creator core replication stream, enabling the other peer to build up remote bitfields for each core for the connected peer. This can then be compared with a device's own bitfields to know what data needs to be sent.

@gmaclennan gmaclennan linked an issue Sep 6, 2023 that may be closed by this pull request
@gmaclennan gmaclennan self-assigned this Sep 6, 2023
@gmaclennan gmaclennan marked this pull request as ready for review September 12, 2023 11:23
* main:
  feat: Add set & get deviceInfo & datatype (#250)
  expose member namespace in MapeoProject (#253)
  blobStore.createReadStream should not wait (#243)
  update MapeoManager to return and handle project public IDs (#247)
  return content hash from blobApi.create (#242)
@gmaclennan
Copy link
Member Author

gmaclennan commented Sep 12, 2023

This ended up being a bit more work than originally estimated. What this does:

  1. The bitfields for all cores on a device are shared with a peer over the replication stream for the project creator core. The project creator core replication stream is always the first, and is connected before other cores are replicated to the stream.
  2. The bitfields are written to RemoteBitfield instances, which is copied directly from the Hypercore remoteBitfield (just added types).
  3. These bitfields are stored by core discoveryId and by peer public key id.

The bitfield are compressed using run-length-encoding. The implementation is mainly copied from bitfield-rle, but modified to work with Uint32Array bitfields (which is what Hypercore uses). The code for encoding Uint32Arrays that works with different "endian-ness" was copied from compact-encoding.

I am leaving the use of these bitfields to a follow-up PR. We will use them for evaluating sync progress / the need for sync before actually replicating all cores between peers. We can either:

  1. Copy these bitfields to peer.remoteBitfield when the peer connects, via peer.remoteBitfield.insert().
  2. Replace peer.remoteBitfield directly with our "pre-updated" remote bitfield
  3. Use both bitfields (our "pre-updated" bitfield and peer.remoteBitfield) when calculating progress

I think (1) is the preferable option, we just need to make sure it happens before the bitfield changes for the peer.

There is a lot in this PR. Some notes to help what to focus on:

  1. src/core-manager/remote-bitfield.js and src/core-manager/compat.js is pretty much a direct copy of the code in Hypercore.
  2. src/core-manager/bitfield-rle.js and tests/bitfield-rle.js are modified from bitfield-rle

As part of this PR I also updated how extensions are handled, by handing off decoding/encoding to hypercore (rather than encoding/decoding in our own code). There is also a small fix to handling the existing extensions (for sharing core keys) that was uncovered as part of this work.

gmaclennan added a commit that referenced this pull request Sep 13, 2023
@achou11 achou11 self-requested a review September 13, 2023 14:09
Copy link
Member

@achou11 achou11 left a comment

Choose a reason for hiding this comment

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

not much to comment on. excited about what this will enable!

Comment on lines +5 to +6
// Export the appropriate version of `quickbit-universal` as the plain import
// may resolve to an older version in some environments
Copy link
Member

Choose a reason for hiding this comment

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

purely out of curiosity, what's this about?

Copy link
Member Author

Choose a reason for hiding this comment

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

Don't actually know. This is copied from hypercore code

@gmaclennan gmaclennan merged commit 4042a8f into main Sep 13, 2023
6 checks passed
@gmaclennan gmaclennan deleted the feat/haves-extension branch September 13, 2023 15:26
gmaclennan added a commit that referenced this pull request Sep 14, 2023
* main:
  feat: send core "haves" bitfield on first connect (#254)
gmaclennan added a commit that referenced this pull request Sep 14, 2023
* fix findPeer bug (also in #254)

* feat: share all core keys via extension messages

Fixes #251

This adds new fields to the project extension to share keys for other
namespaces and updates tests which manually added cores (manual adding
is no longer needed, but do need to wait for cores to be added)
gmaclennan added a commit that referenced this pull request Sep 19, 2023
* main:
  chore: condense setup code in MemberAPI tests (#267)
  chore: replace queries opt in MemberAPI with dataType (#266)
  feat: add getMany method to member api (#263)
  feat: add getById method to member api (#262)
  chore: Update @digidem/types and remove patch (#269)
  feat: share all core keys via extension messages (#264)
  feat: send core "haves" bitfield on first connect (#254)
  fix: invite.encryptionKeys should be required (#260)
  feat: Add set & get deviceInfo & datatype (#250)
  expose member namespace in MapeoProject (#253)
  blobStore.createReadStream should not wait (#243)
  update MapeoManager to return and handle project public IDs (#247)
  return content hash from blobApi.create (#242)
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.

Pre-emptively send all project haves via extension
2 participants