Skip to content

Simplify partial messages - #671

Merged
MarcoPolo merged 6 commits into
masterfrom
simplify-partial-messages
Feb 27, 2026
Merged

MarcoPolo merged 6 commits into
masterfrom
simplify-partial-messages

Conversation

@MarcoPolo

Copy link
Copy Markdown
Contributor

PeerState is now only used by the application, but its lifetime is
managed by gossipsub. This attempts to strike a reasonable tradeoff
between giving the application complete responsibility of handling every
peer's state, and the library from having to know too much about the
structure of PartsMetadata.

Applications can use the PeerState to store objects representing the
node's view of a remote's state, and the node's view of the remote's
view of our state. Applications SHOULD use this to avoid sending
redundant or duplicate data.


A future PR may use generics here instead of any, but otherwise very similar.

@@ -1,290 +0,0 @@
package partialmessages

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MarcoPolo Why have completely removed this ?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was used when the Message interface had more methods and the library side expected more from the application. With these changes, the library side expects very little from the Message implementation, so these invariants are no longer required by the library. An application should have similar tests though, and they can be more specific.

Comment thread partialmessages/partialmsgs.go Resolved
Comment thread partialmessages/partialmsgs.go Resolved
Comment thread partialmessages/partialmsgs.go Outdated
type PeerState struct {
// Opaque (to gossipsub) state used by the application to track a peer's
// state (e.g. PartsMetadata)
RecvdState any

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT:

Suggested change
RecvdState any
ReceivedState any

I also like RecvState the 4 letters in Recv line up nicely with Sent

Comment thread partialmessages/partialmsgs.go Outdated
// will not see the latest PeerState.
//
// If the rpc should be ignored, the application can return an empty PeerState.
OnIncomingRPC func(from peer.ID, peerState PeerState, rpc *pb.PartialMessagesExtension) (PeerState, error)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
OnIncomingRPC func(from peer.ID, peerState PeerState, rpc *pb.PartialMessagesExtension) (PeerState, error)
OnIncomingRPC func(from peer.ID, previousPeerState PeerState, rpc *pb.PartialMessagesExtension) (newPeerState PeerState, _ error)

Comment thread partialmessages/partialmsgs.go Resolved
Comment thread partialmessages/partialmsgs.go Outdated
// SHOULD still return relevant partsMetadataToSend.
//
// The implementation should update:
// - peerState.RecvdState to remember what parts we've sent to this peer.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this should be

Suggested change
// - peerState.RecvdState to remember what parts we've sent to this peer.
// - peerState.RecvdState to remember what parts the peer has sent to us.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hm, this can probably be rephrased. The original is closer to what I want because we are updating their recvdState to remember that we sent them part X. That way, next time we are publishing, we don't resend them the part we've previously sent.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm confused. This is the peer's partsmetadata that we've received from this peer, right?

The very next line:

	// peerState.RecvdState is nil if the node has not seen its peer's
	// partsMetadata, applications can still eagerly push data in this case.

Comment thread partialmessages/partialmsgs.go Outdated
return err
}
if !pState.IsZero() {
state, err := e.groupState(topic, groupID, true, from)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NIT: I think this will read better if we rename state -> gState

Comment thread partialmessages/partialmsgs.go Resolved
Comment thread partialmessages/partialmsgs.go Outdated
pState = &peerState{}
state.peerState[p] = pState
// Ask the application for message data
pState, pm, partsMetadata, err := partial.ForPeer(p, requestedPartial, pState)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@MarcoPolo @sukunrt

There is one problem here. Either we solve it here or document it well.
Basically, we pass a copy of the peerState to the "ForPeer", "GossipForPeer" and "OnIncomingRPC" functions. Then, if these functions do not return an error, we update the peerState with what these functions return.

The problem is that if "RecvdState" and "SentState" are reference types(
for eg: a pointer), the app layer can shoot itself in the foot by updating them in-place and then return an error anyways. So either we do a deep copy of the PeerState here before passing it to the app layer and restore the state with the deep copy if there's an error or the app does it.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

  1. We can mandate that it is the app layer's responsibility to not mutate the PeerState in place if it ultimately errors out (by operating on a deep copy of the passed PeerState and then returning the modified deep copy here) but we should document it well here

OR

  1. Add a deep copy interface that the app layer must implement for PeerState -> use that to create a deep copy here before passing the PeerState to the app callbacks and then update the mapping with the returned PeerState only if there is no error (which we already do).

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For now, I've handled this in prysm by doing a deep copy of the PeerState before mutating it.
See OffchainLabs/prysm@eda07c0.

@sukunrt sukunrt Feb 27, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So the latest version here makes it very clear that the app is responsible for state updates. Does that work?

#674 (review)

The original motivations for this are gone with the future
partialmessages.Message interface being simpler
PeerState is now only used by the application, but its lifetime is
managed by gossipsub. This attempts to strike a reasonable tradeoff
between giving the application complete responsibility of handling every
peer's state, and the library from having to know too much about the
structure of PartsMetadata.

Applications can use the PeerState to store objects representing the
node's view of a remote's state, and the node's view of the remote's
view of our state. Applications SHOULD use this to avoid sending
redundant or duplicate data.
We only need a single function from the application. Have the
application provide this function in PublishPartial instead of a
interface type.
It was only a wrapper around the group state map, and topicPeer state
map. The latter is already capture by gossipsub, so we don't want to
duplicate state.
@MarcoPolo
MarcoPolo force-pushed the revert-partsmetadata-interface branch from 2f962f7 to d6e34cf Compare February 27, 2026 22:09
@MarcoPolo
MarcoPolo force-pushed the simplify-partial-messages branch from 2613503 to 63d7f56 Compare February 27, 2026 22:11
@MarcoPolo
MarcoPolo changed the base branch from revert-partsmetadata-interface to master February 27, 2026 22:11
@MarcoPolo
MarcoPolo merged commit 46a8135 into master Feb 27, 2026
6 of 7 checks passed
@MarcoPolo
MarcoPolo deleted the simplify-partial-messages branch February 27, 2026 22:12
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.

3 participants