Skip to content

Commit

Permalink
fix: add timeout when selecting clients (#104)
Browse files Browse the repository at this point in the history
  • Loading branch information
forcodedancing authored Dec 7, 2023
1 parent 0030740 commit 6c96cc3
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 9 deletions.
5 changes: 4 additions & 1 deletion executor/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package executor
import (
"context"
"sync"
"time"

gnfdclient "github.com/bnb-chain/greenfield-go-sdk/client"
"github.com/bnb-chain/greenfield-go-sdk/types"
Expand Down Expand Up @@ -75,7 +76,9 @@ func (gc *GnfdCompositeClients) GetClient() *GnfdCompositeClient {

func getClientBlockHeight(clientChan chan *GnfdCompositeClient, wg *sync.WaitGroup, client *GnfdCompositeClient) {
defer wg.Done()
status, err := client.TmClient.Status(context.Background())
ctxWithTimeout, cancel := context.WithTimeout(context.Background(), time.Second)
defer cancel()
status, err := client.GetStatus(ctxWithTimeout)
if err != nil {
return
}
Expand Down
21 changes: 13 additions & 8 deletions monitor/monitor.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,6 +139,7 @@ func (m *Monitor) getBlockAndBlockResult(height uint64) (*ctypes.ResultBlockResu
logging.Logger.Infof("retrieve greenfield block at height=%d", height)
block, blockResults, err := m.executor.GetBlockAndBlockResultAtHeight(int64(height))
if err != nil {
logging.Logger.Errorf("retrieve greenfield block error at height=%d, err=%v", height, err)
return nil, nil, err
}
return blockResults, block, nil
Expand All @@ -147,6 +148,7 @@ func (m *Monitor) getBlockAndBlockResult(height uint64) (*ctypes.ResultBlockResu
func (m *Monitor) monitorChallengeEvents(block *tmtypes.Block, blockResults *ctypes.ResultBlockResults) error {
parsedEvents, err := m.parseEvents(blockResults)
if err != nil {
logging.Logger.Errorf("failed to parse event, error: %v", err)
return err
}
b := &model.Block{
Expand All @@ -156,31 +158,33 @@ func (m *Monitor) monitorChallengeEvents(block *tmtypes.Block, blockResults *cty
}
events := EntitiesToDtos(uint64(block.Height), parsedEvents)
err = m.dataProvider.SaveBlockAndEvents(b, events)
if err != nil {
logging.Logger.Errorf("failed to save block and events, error: %v", err)
return err
}

for _, event := range events {
logging.Logger.Debugf("monitor event saved for challengeId: %d %s", event.ChallengeId, time.Now().Format("15:04:05.000000"))
m.metricService.SetGnfdSavedEvent(event.ChallengeId)
m.metricService.IncGnfdSavedEventCount()
}
m.metricService.SetGnfdSavedBlock(b.Height)
m.metricService.IncGnfdSavedBlockCount()
if err != nil {
return err
}
logging.Logger.Infof("monitor saved block: %d", block.Height)

return nil
}

func (m *Monitor) calNextHeight() (uint64, error) {
latestPolledBlock, err := m.dataProvider.GetLatestBlock()
if err != nil && err != gorm.ErrRecordNotFound {
latestHeight, err := m.executor.GetLatestBlockHeight()
if err != nil {
return 0, err
}
return latestHeight, err
logging.Logger.Errorf("get latest block from database error: %v", err)
return 0, err
}
if latestPolledBlock.Height == 0 { // a fresh database
latestHeight, err := m.executor.GetLatestBlockHeight()
if err != nil {
logging.Logger.Errorf("get latest block from blockchain error: %v, database height:%d", err, latestPolledBlock.Height)
return m.executor.GetCachedBlockHeight()
}
return latestHeight, nil
Expand All @@ -189,6 +193,7 @@ func (m *Monitor) calNextHeight() (uint64, error) {

latestBlockHeight, err := m.executor.GetLatestBlockHeight()
if err != nil {
logging.Logger.Errorf("get latest block from blockchain error: %v, database height:%d", err, latestPolledBlock.Height)
return 0, err
}
// pauses challenger for a bit since it already caught the newest block
Expand Down

0 comments on commit 6c96cc3

Please sign in to comment.