consensus: update state from store before use in reactor (#8576)

Closes: #8575

This PR aims to fix the `LastCommitRound can only be negative for initial height 0` issue we see in the e2e tests by initializing the `state` object before starting the receive routines in the consensus reactor. This is somewhat inelegant, but it should fix the issue.
This commit is contained in:
William Banfield
2022-05-19 15:35:30 -04:00
committed by GitHub
parent 4a9bbe047f
commit ad73e6da2f
2 changed files with 13 additions and 7 deletions

View File

@@ -216,6 +216,8 @@ func (r *Reactor) OnStart(ctx context.Context) error {
if err := r.state.Start(ctx); err != nil {
return err
}
} else if err := r.state.updateStateFromStore(); err != nil {
return err
}
go r.updateRoundStateRoutine(ctx)

View File

@@ -122,9 +122,8 @@ type State struct {
// store blocks and commits
blockStore sm.BlockStore
stateStore sm.Store
initialStatePopulated bool
skipBootstrapping bool
stateStore sm.Store
skipBootstrapping bool
// create and execute blocks
blockExec *sm.BlockExecutor
@@ -248,9 +247,6 @@ func NewState(
}
func (cs *State) updateStateFromStore() error {
if cs.initialStatePopulated {
return nil
}
state, err := cs.stateStore.Load()
if err != nil {
return fmt.Errorf("loading state: %w", err)
@@ -259,6 +255,15 @@ func (cs *State) updateStateFromStore() error {
return nil
}
eq, err := state.Equals(cs.state)
if err != nil {
return fmt.Errorf("comparing state: %w", err)
}
// if the new state is equivalent to the old state, we should not trigger a state update.
if eq {
return nil
}
// We have no votes, so reconstruct LastCommit from SeenCommit.
if state.LastBlockHeight > 0 {
cs.reconstructLastCommit(state)
@@ -266,7 +271,6 @@ func (cs *State) updateStateFromStore() error {
cs.updateToState(state)
cs.initialStatePopulated = true
return nil
}