Implement IHAVE replacement with onEmitGossip for partial-message peers (step 9)#470
Draft
lucassaldanha wants to merge 13 commits into
Draft
Implement IHAVE replacement with onEmitGossip for partial-message peers (step 9)#470lucassaldanha wants to merge 13 commits into
lucassaldanha wants to merge 13 commits into
Conversation
Captures the MVP scope, jvm-libp2p/client responsibility boundary, client-facing API, routing semantics, per-group lifecycle and DoS caps, and the implementation plan for the gossipsub partial-messages extension. Lands ahead of implementation so sub-issues of libp2p#435 can reference a stable design anchor.
Step 1 of the partial-messages extension: plumb SubOpts.requestsPartial / SubOpts.supportsSendingPartial through subscribe announcements in both directions, and track the per-peer-per-topic receive state. - AbstractRouter parses the flags with the spec-mandated coercion (supportsSendingPartial := requestsPartial || supportsSendingPartial) and zeroes them on subscribe=false. - New enqueueSubscribe hook unifies outbound subscribe enqueueing so GossipRouter can attach per-topic flags in a single override. - GossipRouter exposes setTopicPartialFlags(topic, ...) to configure flags advertised for a locally-subscribed topic, and stores inbound flags in a new PartialSubscriptionState (plain HashMap on the pubsub event loop). State is cleaned on peer disconnect, topic unsubscribe, and per-peer unsubscribe. - Outbound unsubscribe MUST NOT carry partial flags; enforced at the SubscriptionPart wire-build site. No routing behaviour changes yet. See docs/partial-messages.md §4.5, §5, §6.1 for context.
…t addSubscription overload
- `PartialSubscriptionWireTest`: route reads of `partialSubscriptionState`
through `submitOnEventThread { ... }.join()`. The state container is
not thread-safe; direct access from the JUnit thread races the event
loop and can surface as `ConcurrentModificationException` or stale
reads. Two helpers (`peerFlagsOnEventLoop`, `snapshotPartialStateOnEventLoop`)
establish the happens-before barrier.
- `RpcPartsQueue`: remove the 2-arg `addSubscription(topic, status)`
default overload. The remaining 4-arg abstract method is the single
source of truth; `addSubscribe` / `addUnsubscribe` remain the
convenience entry points.
- `PartialSubFlags.coerce(requestsPartial, supportsSendingPartial)`: single source of truth for the spec coercion rule `supportsSendingPartial := requestsPartial || supportsSendingPartial`. Used from `GossipRouter.setTopicPartialFlags` for the outbound side. AbstractRouter keeps the inline expression for the receive side to avoid a reverse layering dependency (pubsub -> gossip); a comment notes the rule is applied on both sides. - `PartialSubscriptionState.setPeerFlags`: document that passing `PartialSubFlags.NONE` (or any equivalent all-false flags) is treated as a removal. Makes the set-sometimes-deletes invariant explicit for readers. - `AbstractRouter.handleMessageSubscriptions`: add Kdoc now that the method is `protected open`. Documents the "call super" contract for overrides (GossipRouter relies on this to keep peersTopics and partialSubscriptionState in sync) and the flag-normalisation precondition.
…ure/partial-messages-support
Introduces the public partial-messages API surface and the internal state management layer required before any routing logic lands: Public API (io.libp2p.pubsub.gossip.partialmessages): - PartialMessagesHandler<PeerState> — onIncomingRpc + onEmitGossip; PartialMessagesPeerFeedback passed per-call (resolves open question from design doc §9) - PublishAction<PeerState> / PublishActionsFn<PeerState> - PartialMessagesPeerFeedback interface + FeedbackKind enum Internal state management: - GroupId — content-equality ByteArray wrapper for use as map key - GroupState<PeerState> — per-(topic,groupId) container with mutable TTL and app-opaque peerStates - PartialGroupStateStore<PeerState> — TTL countdown, GC on ttl≤0 or empty peerStates, DoS caps (255/topic, 8/topic/peer, matching go-libp2p defaults), onPeerDisconnected, onTopicUnsubscribed - PartialMessagesAdapter (internal interface) / PartialMessagesAdapterImpl<PeerState> — erases PeerState at the GossipRouter boundary via a single @Suppress("UNCHECKED_CAST") in the builder Wiring: - GossipRouterBuilder: partialMessagesHandler field; build-time error if PARTIAL_MESSAGES extension enabled without a handler - GossipRouter: internal var partialMessages: PartialMessagesAdapter? No routing changes in this step.
Replaces the stub in GossipRouter.processPartialMessageExtension with the full flow: drop RPCs missing topicID or groupID, then delegate to PartialMessagesAdapterImpl which gets-or-creates the GroupState (with DoS cap enforcement) and calls handler.onIncomingRpc with the live peerStates map.
Adds the outbound path for the partial-messages extension: - GossipRpcPartsQueue: addPartialMessage queues a PartialMessagePart; takeMerged caps at 1 per RPC (proto field is optional, not repeated). - PartialMessagesAdapter: publishPartial invokes the client's PublishActionsFn, enforces the spec MUST (omit partialMessage when peer supports but did not request), updates nextPeerState atomically, and calls back via enqueueFn. - GossipRouter: publishPartial looks up PeerHandler by PeerId, routes through GossipRpcPartsQueue (not a direct send), and flushes pending. - Gossip facade: publishPartial submits to the event thread and returns CompletableFuture<Unit>. Tests: PartialMessagesOutboundRpcTest (5 wire-level) and 6 new unit tests in PartialMessagesAdapterImplTest.
Exercises the full stack built in steps 1-4 — SubOpts flag plumbing, ControlExtensions handshake, inbound handler dispatch, group-state tracking, and outbound publishPartial — over real TCP/Noise/Mplex using a trivial ByteArray bitmap as PeerState. Four tests: - Unidirectional partial RPC (payload + metadata delivered to handler) - Bidirectional round-trip between two hosts - nextPeerState persisted and visible to subsequent decide() calls - Spec MUST: partialMessage omitted when peer supports but did not request Both hosts bind to port 0 to avoid conflicts with other tests.
Per §5.2 of the partial-messages spec: when we have requested partial messages for a topic T and a peer supports sending partial for T, skip IDONTWANT to that peer. Sending IDONTWANT is redundant — the peer is expected to deliver partial RPCs instead of full messages. Also extends PubsubProtocol.supportsIDontWant() to include Gossip_V_1_3, since v1.3 is a superset of v1.2 and both IDONTWANT and the Extensions control message must coexist for partial-message sessions.
Three one-line additions to GossipRouter that call the already-implemented PartialMessagesAdapter hooks at the right lifecycle points: - heartbeat() → partialMessages?.onHeartbeat() Decrements TTL on all live groups each heartbeat and garbage-collects groups whose TTL has reached zero or whose peerStates map is empty. - onPeerDisconnected() → partialMessages?.onPeerDisconnected(peer.peerId) Removes the disconnected peer from every group's peerStates; GCs groups that become empty as a result. - unsubscribe() → partialMessages?.onTopicUnsubscribed(topic) Drops all group state for the topic when we leave it. TTL is already reset on every publishPartial call via getOrCreateLocalGroup.
…rs (step 9) §5.3 of the partial-messages spec: during the gossipsub heartbeat lazy-push, peers where we support sending partial AND they requested partial are excluded from IHAVE targets. Instead, handler.onEmitGossip is invoked once per locally-initiated (topic, groupId) group with the collected partial peers so the client can drive per-part delivery via publishPartial. - Add onEmitGossip(topic, partialPeers) to PartialMessagesAdapter and implement it in PartialMessagesAdapterImpl: iterates locally-initiated groups for the topic and calls handler.onEmitGossip for each. - Modify GossipRouter.emitGossip: when the adapter is set and we advertise supportsSendingPartial for the topic, partition the selected gossip targets into fullPeers (IHAVE) and partialPeers (onEmitGossip). Only fullPeers receive IHAVE. - Add PartialMessagesEmitGossipTest covering: IHAVE suppression for partial peers, onEmitGossip invocation for locally-initiated groups, no call for peer-initiated groups, and fallback to normal IHAVE when extension is off or flags unset.
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.
Summary
Implements Step 9 of the partial-messages plan: IHAVE replacement with
onEmitGossip(§5.3 of the design doc).PartialMessagesAdapter: AddsonEmitGossip(topic, partialPeers)to the interface. The implementation inPartialMessagesAdapterImpliterates locally-initiated groups for the topic and invokeshandler.onEmitGossiponce per group with the collected partial peers.GossipRouter.emitGossip: When the adapter is configured and we advertisesupportsSendingPartialfor the topic, the selected gossip candidates are partitioned intofullPeers(receive IHAVE as usual) andpartialPeers(receiveonEmitGossipcallbacks instead of IHAVE). Conditions for a peer to be a partial target:peerSupportsPartialMessages(peerId)ANDpeerRequestsPartial(topic, peerId).PartialMessagesEmitGossipTest: New test class covering IHAVE suppression for partial peers,onEmitGossipinvoked for locally-initiated groups, not called for peer-initiated groups or when no groups exist, and IHAVE still sent when the extension is off/topic flags unset/peer didn't request partial.Test plan
PartialMessagesEmitGossipTest(7 new tests, all green)io.libp2p.pubsub.gossip.*test suite — no regressions