Skip to content

Commit

Permalink
fix get highest executed and finalized height
Browse files Browse the repository at this point in the history
  • Loading branch information
zhangchiqing committed May 13, 2024
1 parent d9bc232 commit 235fe48
Showing 1 changed file with 17 additions and 12 deletions.
29 changes: 17 additions & 12 deletions engine/execution/state/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -513,35 +513,40 @@ func (s *state) GetHighestFinalizedExecuted() (uint64, error) {
return s.registerStore.LastFinalizedAndExecutedHeight(), nil
}

// last finalized height
var finalizedHeight uint64
err := s.db.View(operation.RetrieveFinalizedHeight(&finalizedHeight))
if err != nil {
return 0, fmt.Errorf("could not retrieve finalized height: %w", err)
}

executedHeight, executedID, err := s.GetHighestExecutedBlockID(context.Background())
// last executed height
executedHeight, _, err := s.GetHighestExecutedBlockID(context.Background())
if err != nil {
return 0, fmt.Errorf("could not get highest executed block: %w", err)
}

var finalizedID flow.Identifier
// find higest finalized and executed height
var highest uint64
if finalizedHeight >= executedHeight {
finalizedID, err = s.headers.BlockIDByHeight(executedHeight)
if err != nil {
return 0, fmt.Errorf("could not get header by height %v: %w", executedHeight, err)
}
highest = executedHeight
} else {
finalizedID, err = s.headers.BlockIDByHeight(finalizedHeight)
if err != nil {
return 0, fmt.Errorf("could not get header by height %v: %w", executedHeight, err)
}
highest = finalizedHeight
}

if finalizedID != executedID {
return 0, fmt.Errorf("finalized block %v does not match executed block %v", finalizedID, executedID)
// double check the higesht block is executed
blockID, err := s.headers.BlockIDByHeight(highest)
if err != nil {
return 0, fmt.Errorf("could not get header by height %v: %w", highest, err)
}

isExecuted, err := s.IsBlockExecuted(highest, blockID)
if err != nil {
return 0, fmt.Errorf("could not check if block %v (height: %v) is executed: %w", blockID, highest, err)
}

if !isExecuted {
return 0, fmt.Errorf("block %v (height: %v) is not executed yet", blockID, highest)
}

return highest, nil
Expand Down

0 comments on commit 235fe48

Please sign in to comment.