Skip to content

Commit

Permalink
go/consensus/tendermint/epochtime_mock: Fix initial notify
Browse files Browse the repository at this point in the history
  • Loading branch information
kostko committed Jun 29, 2020
1 parent 6655d24 commit 3f61b02
Showing 1 changed file with 30 additions and 27 deletions.
57 changes: 30 additions & 27 deletions go/consensus/tendermint/epochtime_mock/epochtime_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,30 +137,7 @@ func (t *tendermintMockBackend) worker(ctx context.Context) {
}
defer t.service.Unsubscribe("epochtime-worker", app.QueryApp) // nolint: errcheck

// Populate current epoch (if available).
q, err := t.querier.QueryAt(ctx, consensus.HeightLatest)
if err == nil {
var epoch api.EpochTime
var height int64
epoch, height, err = q.Epoch(ctx)
if err != nil {
t.logger.Error("failed to query epoch",
"err", err,
)
return
}

t.Lock()
t.epoch = epoch
t.currentBlock = height
t.notifier.Broadcast(t.epoch)
t.Unlock()
} else {
t.logger.Warn("unable to query initial epoch",
"err", err,
)
}

var initialNotify bool
for {
var event interface{}

Expand All @@ -174,14 +151,40 @@ func (t *tendermintMockBackend) worker(ctx context.Context) {
return
}

switch ev := event.(type) {
case tmtypes.EventDataNewBlock:
if ev, ok := event.(tmtypes.EventDataNewBlock); ok {
if !initialNotify {
if err = t.queryAndUpdate(ctx, ev.Block.Header.Height); err != nil {
t.logger.Warn("failed to query epoch",
"err", err,
)
continue
}
initialNotify = true
}
t.onEventDataNewBlock(ctx, ev)
default:
}
}
}

func (t *tendermintMockBackend) queryAndUpdate(ctx context.Context, height int64) error {
q, err := t.querier.QueryAt(ctx, height)
if err != nil {
return fmt.Errorf("failed to query epoch: %w", err)
}

var epoch api.EpochTime
epoch, height, err = q.Epoch(ctx)
if err != nil {
return fmt.Errorf("failed to query epoch: %w", err)
}

if t.updateCached(height, epoch) {
t.notifier.Broadcast(epoch)
}

return nil
}

func (t *tendermintMockBackend) onEventDataNewBlock(ctx context.Context, ev tmtypes.EventDataNewBlock) {
events := ev.ResultBeginBlock.GetEvents()

Expand Down

0 comments on commit 3f61b02

Please sign in to comment.