Repository navigation
Conversation
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
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.
Fixes #1261.
What goes wrong
sendHeadssends onestream.send()per head andreceiveHeadsdecodes 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:exchangeHeadsstarts reading only afterdialProtocol()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 withtoo many terminals, data makes no sense, so the exchange is abandoned and those heads are lost.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 withnot 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.jsbuffers what the stream yields and walks the dag-cbor items in it:cborItemLengthis a length walk with no decoding and no allocation; tag 42 (a CID) is followed into its payload like any other item.sendHeadsis 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.jsgains aHeads exchange framingsuite 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
CBOR decode error: too many terminals, data makes no sense; 0 of 4 heads decodedCBOR decode error: not enough data for type; 0 of 1 decoded546 passing(baseline on96e407cwas544 passing; the difference is the two new tests).standard --env=mocha: clean.src/sync.jsturns both new tests red with the exact errors quoted above; restoring it turns them green. Both errors match the ones in the issue.Risk
receiveHeadsno 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/sendHeadsand addstest/sync-missed-peers.test.js; this change is confined toreceiveHeadsplus new helper functions, so the two should apply independently.