Skip to content

feat: WithValidatorData publishing option #603

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 13, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions topic.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,9 +213,10 @@ type RouterReady func(rt PubSubRouter, topic string) (bool, error)
type ProvideKey func() (crypto.PrivKey, peer.ID)

type PublishOptions struct {
ready RouterReady
customKey ProvideKey
local bool
ready RouterReady
customKey ProvideKey
local bool
validatorData any
}

type PubOpt func(pub *PublishOptions) error
Expand Down Expand Up @@ -308,7 +309,7 @@ func (t *Topic) Publish(ctx context.Context, data []byte, opts ...PubOpt) error
}
}

return t.p.val.PushLocal(&Message{m, "", t.p.host.ID(), nil, pub.local})
return t.p.val.PushLocal(&Message{m, "", t.p.host.ID(), pub.validatorData, pub.local})
}

// WithReadiness returns a publishing option for only publishing when the router is ready.
Expand All @@ -332,6 +333,15 @@ func WithLocalPublication(local bool) PubOpt {
}
}

// WithValidatorData returns a publishing option to set custom validator data for the message.
// This allows users to avoid deserialization of the message data when validating the message locally.
func WithValidatorData(data any) PubOpt {
return func(pub *PublishOptions) error {
pub.validatorData = data
return nil
}
}

// WithSecretKeyAndPeerId returns a publishing option for providing a custom private key and its corresponding peer ID
// This option is useful when we want to send messages from "virtual", never-connectable peers in the network
func WithSecretKeyAndPeerId(key crypto.PrivKey, pid peer.ID) PubOpt {
Expand Down