Skip to content

Commit

Permalink
fix(manager): proposer must be updated in the state before the batch …
Browse files Browse the repository at this point in the history
…is commited (dymensionxyz#1176)
  • Loading branch information
keruch authored Oct 29, 2024
1 parent 4e23fdb commit a564314
Showing 1 changed file with 17 additions and 4 deletions.
21 changes: 17 additions & 4 deletions block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -133,18 +133,34 @@ func (m *Manager) applyBlock(block *types.Block, commit *types.Commit, blockMeta
m.Executor.UpdateStateAfterCommit(m.State, responses, appHash, block.Header.Height, block.Header.Hash())
}

// save the proposer to store to be queried over RPC
// Update the store:
// 1. Save the proposer for the current height to the store.
// 2. Update the proposer in the state in case of rotation.
// 3. Save the state to the store (independently of the height). Here the proposer might differ from (1).
// here, (3) helps properly handle reboots (specifically when there's rotation).
// If reboot happens after block H (which rotates seqA -> seqB):
// - Block H+1 will be signed by seqB.
// - The state must have seqB as proposer.

// Proposer cannot be empty while applying the block
proposer := m.State.GetProposer()
if proposer == nil {
return fmt.Errorf("logic error: got nil proposer while applying block")
}

batch := m.Store.NewBatch()

// 1. Save the proposer for the current height to the store.
// Proposer in the store is used for RPC queries.
batch, err = m.Store.SaveProposer(block.Header.Height, *proposer, batch)
if err != nil {
return fmt.Errorf("save proposer: %w", err)
}

// 2. Update the proposer in the state in case of rotation.
switchRole := m.Executor.UpdateProposerFromBlock(m.State, m.Sequencers, block)

// 3. Save the state to the store (independently of the height). Here the proposer might differ from (1).
batch, err = m.Store.SaveState(m.State, batch)
if err != nil {
return fmt.Errorf("update state: %w", err)
Expand All @@ -159,9 +175,6 @@ func (m *Manager) applyBlock(block *types.Block, commit *types.Commit, blockMeta

m.blockCache.Delete(block.Header.Height)

// check if the proposer needs to be changed and change it if that's the case
switchRole := m.Executor.UpdateProposerFromBlock(m.State, m.Sequencers, block)

if switchRole {
// TODO: graceful role change (https://github.com/dymensionxyz/dymint/issues/1008)
m.logger.Info("Node changing to proposer role")
Expand Down

0 comments on commit a564314

Please sign in to comment.