Skip to content

fix(gossipsub): allow configuring max outbound message size - #3486

Open
paschal533 wants to merge 2 commits into
libp2p:mainfrom
paschal533:fix/gossipsub-max-outbound-data-length
Open

fix(gossipsub): allow configuring max outbound message size#3486
paschal533 wants to merge 2 commits into
libp2p:mainfrom
paschal533:fix/gossipsub-max-outbound-data-length

Conversation

@paschal533

Copy link
Copy Markdown
Contributor

Problem

Publishing a message larger than 4 MiB via pubsub.publish() fails with an opaque error thrown deep inside the length-prefixed encoder:

InvalidDataLengthError: Message length too long
    at validateMaxDataLength (it-length-prefixed/encode.ts)
    at encode.single (it-length-prefixed/encode.ts)
    at OutboundStream.push (gossipsub/stream.ts)
    at GossipSub.sendRpc (gossipsub/gossipsub.ts)
    at GossipSub.publish (gossipsub/gossipsub.ts)

The it-length-prefixed encoder has a hardcoded 4 MiB default (MAX_DATA_LENGTH). The gossipsub outbound path never passed options to override it — unlike the inbound path which already exposes maxInboundDataLength. There was no way to raise the limit.

Closes #3427

Changes

src/index.ts

Add maxOutboundDataLength?: number to GossipsubOpts, mirroring the existing maxInboundDataLength option.

src/stream.ts

Add maxDataLength? to OutboundStreamOpts, store it on the OutboundStream instance, and pass it to encode.single() so the encoder uses the configured value instead of the library default.

src/gossipsub.ts

Three changes:

  • createOutboundStream() — pass maxDataLength: this.opts.maxOutboundDataLength when constructing OutboundStream
  • sendRpcInBatch() — pass { maxDataLength: this.opts.maxOutboundDataLength } to its own encode.single() call (previously uncovered)
  • publish() — add an early guard that fires before any protobuf encoding or network I/O, throwing with a clear, actionable message:
Message is too large (6291456 bytes). The current limit is 4194304 bytes.
Increase the maxOutboundDataLength option to send larger messages.

test/message-size.spec.ts (new)

Four test cases:

  1. Oversized message with default config rejects with an actionable error
  2. Message exactly at the 4 MiB default limit passes
  3. Configuring maxOutboundDataLength: 8 MiB allows a 6 MiB message through
  4. Error message includes actual size, limit, and option name

User fix

libp2p.services.pubsub = gossipsub({
  maxOutboundDataLength: 8 * 1024 * 1024  // raise to 8 MiB
})

Test plan

  • All 4 new tests pass
  • Full gossipsub node test suite passes (97 tests)
  • Verified live with a demo script: scenario 1 rejects clearly, scenario 2 publishes successfully to a peer, scenario 3 passes at the exact boundary

…large messages

Publishing a message larger than 4 MiB via pubsub.publish() failed with an
opaque InvalidDataLengthError thrown deep inside the it-length-prefixed encoder.
There was no way to raise the limit - unlike the inbound side which already
exposed maxInboundDataLength.

Add a matching maxOutboundDataLength option to GossipsubOpts. Wire it through
OutboundStream so encode.single() respects the configured value instead of the
hardcoded 4 MiB library default. Also pass it to the sendRpcInBatch() encode
path which was previously uncovered.

Add an early guard in publish() that fires before any protobuf encoding or
network I/O, throwing with an actionable message that names the option and
includes both the actual payload size and the current limit.

Fixes libp2p#3427
@paschal533
paschal533 requested a review from a team as a code owner May 4, 2026 12:07
@paschal533 paschal533 changed the title fix(gossipsub): add maxOutboundDataLength option to allow publishing large messages fix(gossipsub): allow configuring max outbound message size May 4, 2026
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.

libp2p/gossipsub: If the message size exceeds 4MB, the transmission will fail.

1 participant