Skip to content

Commit

Permalink
hare3: switch to sync validation for gossip messages (#4937)
Browse files Browse the repository at this point in the history
currently every validation request spawns new goroutine, the main reason for that was
to enable fetching over network without blocking other threads.

however hare3 will be spawning many goroutines and it will be much more efficient to execute protocol inline.
the same is true for beacon.
  • Loading branch information
dshulyak committed Sep 1, 2023
1 parent 1a69f51 commit f1c1dd8
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 16 deletions.
4 changes: 2 additions & 2 deletions hare/consensus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -498,7 +498,7 @@ func (ps *delayedPubSub) Publish(ctx context.Context, protocol string, msg []byt
return nil
}

func (ps *delayedPubSub) Register(protocol string, handler pubsub.GossipHandler) {
func (ps *delayedPubSub) Register(protocol string, handler pubsub.GossipHandler, opts ...pubsub.ValidatorOpt) {
if ps.recvDelay != 0 {
handler = func(ctx context.Context, pid p2p.Peer, msg []byte) error {
rng := time.Duration(rand.Uint32()) * time.Second % ps.recvDelay
Expand Down Expand Up @@ -609,6 +609,6 @@ func (eps *equivocatePubSub) Publish(ctx context.Context, protocol string, data
return nil
}

func (eps *equivocatePubSub) Register(protocol string, handler pubsub.GossipHandler) {
func (eps *equivocatePubSub) Register(protocol string, handler pubsub.GossipHandler, opts ...pubsub.ValidatorOpt) {
eps.ps.Register(protocol, handler)
}
2 changes: 1 addition & 1 deletion hare/flows_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ type p2pManipulator struct {
err error
}

func (m *p2pManipulator) Register(protocol string, handler pubsub.GossipHandler) {
func (m *p2pManipulator) Register(protocol string, handler pubsub.GossipHandler, opts ...pubsub.ValidatorOpt) {
m.nd.Register(protocol, handler)
}

Expand Down
2 changes: 1 addition & 1 deletion hare3/hare.go
Original file line number Diff line number Diff line change
Expand Up @@ -207,7 +207,7 @@ func (h *Hare) Coins() <-chan WeakCoinOutput {
}

func (h *Hare) Start() {
h.pubsub.Register(h.config.ProtocolName, h.Handler)
h.pubsub.Register(h.config.ProtocolName, h.Handler, pubsub.WithValidatorInline(true))
current := h.nodeclock.CurrentLayer() + 1
enabled := types.MaxLayer(current, h.config.EnableLayer)
enabled = types.MaxLayer(enabled, types.GetEffectiveGenesis()+1)
Expand Down
2 changes: 1 addition & 1 deletion hare3/hare_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ func (n *node) withOracle() *node {

func (n *node) withPublisher() *node {
n.mpublisher = pmocks.NewMockPublishSubsciber(n.ctrl)
n.mpublisher.EXPECT().Register(gomock.Any(), gomock.Any()).AnyTimes()
n.mpublisher.EXPECT().Register(gomock.Any(), gomock.Any(), gomock.Any()).AnyTimes()
return n
}

Expand Down
26 changes: 18 additions & 8 deletions p2p/pubsub/mocks/publisher.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 5 additions & 1 deletion p2p/pubsub/pubsub.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,13 @@ type Publisher interface {

// Subscriber is an interface for subcribing to messages.
type Subscriber interface {
Register(string, GossipHandler)
Register(string, GossipHandler, ...ValidatorOpt)
}

type ValidatorOpt = pubsub.ValidatorOpt

var WithValidatorInline = pubsub.WithValidatorInline

// PublishSubsciber common interface for publisher and subscribing.
type PublishSubsciber interface {
Publisher
Expand Down
4 changes: 2 additions & 2 deletions p2p/pubsub/wrapper.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ type PubSub struct {
}

// Register handler for topic.
func (ps *PubSub) Register(topic string, handler GossipHandler) {
func (ps *PubSub) Register(topic string, handler GossipHandler, opts ...ValidatorOpt) {
ps.mu.Lock()
defer ps.mu.Unlock()
if _, exist := ps.topics[topic]; exist {
Expand All @@ -47,7 +47,7 @@ func (ps *PubSub) Register(topic string, handler GossipHandler) {
default:
return pubsub.ValidationAccept
}
})
}, opts...)
topich, err := ps.pubsub.Join(topic)
if err != nil {
ps.logger.With().Panic("failed to join a topic", log.String("topic", topic), log.Err(err))
Expand Down

0 comments on commit f1c1dd8

Please sign in to comment.