Skip to content

Implement outbound publishPartial (step 4)#465

Draft
lucassaldanha wants to merge 8 commits intolibp2p:developfrom
lucassaldanha:partial-messages-step-4
Draft

Implement outbound publishPartial (step 4)#465
lucassaldanha wants to merge 8 commits intolibp2p:developfrom
lucassaldanha:partial-messages-step-4

Conversation

@lucassaldanha
Copy link
Copy Markdown
Collaborator

Summary

Implements Step 4 of the partial-messages roadmap (docs/partial-messages.md §8):

  • Adds Gossip.publishPartial(topic, groupId, actionsFn) as the public outbound API on the Gossip facade.
  • Routes all outbound partial RPCs through GossipRpcPartsQueue — consistent with the design doc note that PR Add partial message support for gossipsub #433 bypassed the queue incorrectly.
  • Enforces the spec MUST: omits partialMessage when the remote peer supports partial but did not request it (peerRequestsPartial == false).

Stacked on #463 (step 3).

Changes

GossipRpcPartsQueue

  • New addPartialMessage(topic, groupId, partialMessage?, partsMetadata?) on the interface and DefaultGossipRpcPartsQueue.
  • PartialMessagePart is a plain class (not data class) to avoid the ByteArray equals/hashCode footgun.
  • takeMerged now caps at 1 partial message per RPC (partialCount = 1) since the proto partial field is optional, not repeated.

PartialMessagesAdapter

  • New publishPartial(topic, groupId, actionsFn, peerRequestsPartial, enqueueFn) on the interface.
  • PartialMessagesAdapterImpl invokes PublishActionsFn.decide, applies the spec MUST suppression, updates nextPeerState atomically per peer, and skips peers with action.error.

GossipRouter

  • publishPartial looks up PeerHandler by PeerId from activePeers, enqueues via the parts queue, then calls flushAllPending().

Gossip facade

  • publishPartial submits to the event thread and returns CompletableFuture<Unit>.

Test plan

  • PartialMessagesOutboundRpcTest (5 new wire-level tests):
    • delivers RPC to peer that requested partial
    • omits partialMessage when peer supports-but-didn't-request (spec MUST)
    • sends nothing when actionsFn returns empty sequence
    • sends nothing when the adapter is not configured
    • two groups produce two separate RPCs (one partial per RPC enforced by the queue)
  • PartialMessagesAdapterImplTest (6 new unit tests): enqueue, partialMessage suppression, error skip, null-fields skip, nextPeerState storage, peerRequestsPartial predicate forwarding.
  • spotlessCheck and detekt pass.

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

1 participant