Skip to content

Commit

Permalink
fix: terminate stateUpdateHandler upon subscription termination (#295)
Browse files Browse the repository at this point in the history
  • Loading branch information
omritoptix authored Mar 22, 2023
1 parent 6eac411 commit 162b3e6
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
23 changes: 15 additions & 8 deletions settlement/base.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ type BaseLayerClient struct {
ctx context.Context
cancel context.CancelFunc
client HubClient
// eventMap map[string]string
}

// Config for the BaseLayerClient
Expand Down Expand Up @@ -87,7 +86,11 @@ func (b *BaseLayerClient) Init(config []byte, pubsub *pubsub.Server, logger log.
// Start is called once, after init. It initializes the query client.
func (b *BaseLayerClient) Start() error {
b.logger.Debug("settlement Layer Client starting.")
go b.stateUpdatesHandler()

// Wait until the state updates handler is ready
ready := make(chan bool, 1)
go b.stateUpdatesHandler(ready)
<-ready

err := b.client.Start()
if err != nil {
Expand Down Expand Up @@ -209,21 +212,25 @@ func (b *BaseLayerClient) validateBatch(batch *types.Batch) error {
return nil
}

func (b *BaseLayerClient) stateUpdatesHandler() {
b.logger.Info("Started state updates handler loop")
subscription, err := b.pubsub.Subscribe(context.Background(), "stateUpdatesHandler", EventQueryNewSettlementBatchAccepted)
func (b *BaseLayerClient) stateUpdatesHandler(ready chan bool) {
b.logger.Info("started state updates handler loop")
subscription, err := b.pubsub.Subscribe(b.ctx, "stateUpdatesHandler", EventQueryNewSettlementBatchAccepted)
if err != nil {
b.logger.Error("failed to subscribe to state update events")
b.logger.Error("failed to subscribe to state update events", "error", err)
panic(err)
}
ready <- true
for {
select {
case event := <-subscription.Out():
b.logger.Debug("Received state update event", "eventData", event.Data())
b.logger.Debug("received state update event", "eventData", event.Data())
eventData := event.Data().(*EventDataNewSettlementBatchAccepted)
atomic.StoreUint64(&b.latestHeight, eventData.EndHeight)
case <-subscription.Cancelled():
b.logger.Info("Subscription canceled")
b.logger.Info("subscription canceled")
return
case <-b.ctx.Done():
return
}
}
}
5 changes: 5 additions & 0 deletions settlement/settlement_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ func TestGetSequencersEmptyList(t *testing.T) {
func TestGetSequencers(t *testing.T) {
hubClientMock := mocks.NewHubClient(t)
hubClientMock.On("Start", tsmock.Anything).Return(nil)
hubClientMock.On("Stop", tsmock.Anything).Return(nil)
hubClientMock.On("GetLatestBatch", tsmock.Anything).Return(nil, settlement.ErrBatchNotFound)
// Mock a sequencer response by the sequencerByRollapp query
totalSequencers := 5
Expand All @@ -159,6 +160,10 @@ func TestGetSequencers(t *testing.T) {

assert.Len(t, sequencersList, len(sequencers))
assert.Equal(t, settlementClient.GetProposer().PublicKey, proposer.PublicKey)

err := settlementClient.Stop()
assert.NoError(t, err)

// Validate the amount of inactive sequencers
var inactiveSequencerAmount int
for _, sequencer := range sequencersList {
Expand Down

0 comments on commit 162b3e6

Please sign in to comment.