@@ -38,6 +38,9 @@ const (
3838
3939 // headerTopicSuffix is added after namespace to create pubsub topic for block header gossiping.
4040 headerTopicSuffix = "-header"
41+
42+ // commitTopicSuffix is added after namespace to create pubsub topic for block commit gossiping.
43+ commitTopicSuffix = "-commit"
4144)
4245
4346// Client is a P2P client, implemented with libp2p.
@@ -60,6 +63,9 @@ type Client struct {
6063 headerGossiper * Gossiper
6164 headerValidator GossipValidator
6265
66+ commitGossiper * Gossiper
67+ commitValidator GossipValidator
68+
6369 // cancel is used to cancel context passed to libp2p functions
6470 // it's required because of discovery.Advertise call
6571 cancel context.CancelFunc
@@ -165,6 +171,17 @@ func (c *Client) SetHeaderValidator(validator GossipValidator) {
165171 c .headerValidator = validator
166172}
167173
174+ // GossipCommit sends the block commit to the P2P network.
175+ func (c * Client ) GossipCommit (ctx context.Context , commitBytes []byte ) error {
176+ c .logger .Debug ("Gossiping block commit" , "len" , len (commitBytes ))
177+ return c .commitGossiper .Publish (ctx , commitBytes )
178+ }
179+
180+ // SetCommitValidator sets the callback function, that will be invoked after block commit is received from P2P network.
181+ func (c * Client ) SetCommitValidator (validator GossipValidator ) {
182+ c .commitValidator = validator
183+ }
184+
168185// Addrs returns listen addresses of Client.
169186func (c * Client ) Addrs () []multiaddr.Multiaddr {
170187 return c .host .Addrs ()
@@ -312,13 +329,22 @@ func (c *Client) setupGossiping(ctx context.Context) error {
312329 }
313330 go c .txGossiper .ProcessMessages (ctx )
314331
315- c .headerGossiper , err = NewGossiper (c .host , ps , c .getHeaderTopic (), c .logger ,
316- WithValidator (c .headerValidator ))
332+ c .headerGossiper , err = NewGossiper (c .host , ps , c .getHeaderTopic (), c .logger , WithValidator (c .headerValidator ))
317333 if err != nil {
318334 return err
319335 }
320336 go c .headerGossiper .ProcessMessages (ctx )
321337
338+ var opts []GossiperOption
339+ if c .commitValidator != nil {
340+ opts = append (opts , WithValidator (c .commitValidator ))
341+ }
342+ c .commitGossiper , err = NewGossiper (c .host , ps , c .getCommitTopic (), c .logger , opts ... )
343+ if err != nil {
344+ return err
345+ }
346+ go c .commitGossiper .ProcessMessages (ctx )
347+
322348 return nil
323349}
324350
@@ -359,3 +385,7 @@ func (c *Client) getTxTopic() string {
359385func (c * Client ) getHeaderTopic () string {
360386 return c .getNamespace () + headerTopicSuffix
361387}
388+
389+ func (c * Client ) getCommitTopic () string {
390+ return c .getNamespace () + commitTopicSuffix
391+ }
0 commit comments