Skip to content

fix(sync): split merged heads and join split ones in the heads exchange - #1265

Open
Goldloli wants to merge 1 commit into
orbitdb:mainfrom
Goldloli:fix/heads-exchange-framing
Open

Goldloli wants to merge 1 commit into
orbitdb:mainfrom
Goldloli:fix/heads-exchange-framing

Conversation

@Goldloli

Copy link
Copy Markdown

Fixes #1261.

What goes wrong

sendHeads sends one stream.send() per head and receiveHeads decodes every chunk it is handed as exactly one entry, so the chunk boundaries are the only thing that says where a head ends. On libp2p v3 those boundaries do not match the sends:

  • Merged heads. A stream buffers what arrives while nobody reads it and hands the buffer on as one chunk. On the dialling side exchangeHeads starts reading only after dialProtocol() resolves, by which point the answering side has usually sent all of its heads — they arrive as one chunk. Entry.decode() then sees several CBOR items in one buffer and fails with too many terminals, data makes no sense, so the exchange is abandoned and those heads are lost.
  • Split heads. send() appends to a write buffer that the stream then sends in pieces of at most the muxer's message size, so a head larger than that is split. Every piece is decoded on its own and fails with not enough data for type, in both directions.

A single head of normal size always works, which is why this went unnoticed.

What this changes

src/sync.js buffers what the stream yields and walks the dag-cbor items in it:

  • dag-cbor is encoded with definite lengths only, so every item declares its own size up front and the headers of nested items add up to the size of the whole. cborItemLength is a length walk with no decoding and no allocation; tag 42 (a CID) is followed into its payload like any other item.
  • Bytes of a head that is still arriving stay in the buffer and are completed by the next chunk, so a split head is decoded once instead of failing per piece.
  • sendHeads is unchanged: nothing is added to the wire, so a peer that only has merged or only has split chunks decodes the same as before.

test/sync.test.js gains a Heads exchange framing suite with two cases that reproduce each half of the issue over two connected Helia nodes: four heads sent as one chunk, and one 300 KB head sent in 64 KB pieces.

Verification

Case Before After
4 heads sent as one chunk CBOR decode error: too many terminals, data makes no sense; 0 of 4 heads decoded 4 of 4 decoded, hashes match
300 KB head sent in 64 KB pieces CBOR decode error: not enough data for type; 0 of 1 decoded 1 of 1 decoded, hash matches
  • Full suite: 546 passing (baseline on 96e407c was 544 passing; the difference is the two new tests).
  • standard --env=mocha: clean.
  • A/B: reverting only src/sync.js turns both new tests red with the exact errors quoted above; restoring it turns them green. Both errors match the ones in the issue.
  • The scanner was also checked against the shapes the exchange can produce without a node: 300 KB text strings (4-byte length header), deep nesting, maps with 40+ keys, a CID tag, and mid-head split points with a merged tail — correct item counts and no leftover bytes.

Risk

receiveHeads no longer decodes per chunk, so a head whose bytes are never completed would stay buffered until the stream closes rather than raising per chunk. That cannot happen for well-formed sends: the sender only ever sends whole heads. A peer with a different implementation that sends something other than whole dag-cbor items would now hit the item walk's own error (indefinite length is not allowed in dag-cbor) instead of a CBOR decode error, which is arguably a better diagnostic.

On a merge conflict with #1260: it changes onPeerJoined/sendHeads and adds test/sync-missed-peers.test.js; this change is confined to receiveHeads plus new helper functions, so the two should apply independently.

libp2p hands a stream's peer the bytes that arrived while nobody was
reading as one chunk, and it splits a message larger than the muxer's
limit across chunks. sendHeads relies on one chunk per head, so the
receiver decodes all merged heads as a single CBOR item and fails with
"too many terminals", and any head over the message size never decodes
at all.

Walk the dag-cbor items in the buffer instead: dag-cbor uses definite
lengths, so the item headers give the boundaries without decoding.
Bytes of a head that is still arriving stay buffered for the next chunk.

Closes orbitdb#1261

This branch has not been deployed

No deployments
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.

Heads exchange has no framing: heads that arrive in one chunk fail with "CBOR decode error: too many terminals" and are lost

1 participant