Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: nodes keep out of sync when missing gossiped block (issue #284) #540

Merged
merged 15 commits into from
Dec 28, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
caching blocks to apply them later when received out-of-order
  • Loading branch information
srene committed Dec 20, 2023
commit 08f36ec175e7ea78c7cda4b005325c45c25cfb6c
17 changes: 17 additions & 0 deletions block/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@ import (
func (m *Manager) applyBlock(ctx context.Context, block *types.Block, commit *types.Commit, blockMetaData blockMetaData) error {
if block.Header.Height != m.store.Height()+1 {
// We crashed after the commit and before updating the store height.
m.prevBlock[block.Header.Height] = block
m.prevCommit[block.Header.Height] = commit
m.prevMetaData[block.Header.Height] = blockMetaData
return nil
}

Expand Down Expand Up @@ -109,6 +112,20 @@ func (m *Manager) applyBlock(ctx context.Context, block *types.Block, commit *ty

m.store.SetHeight(block.Header.Height)

cachedBlock, exists := m.prevBlock[m.store.Height()+1]

if exists {
m.applyBlock(ctx, cachedBlock, m.prevCommit[m.store.Height()+1], m.prevMetaData[m.store.Height()+1])
}

for k := range m.prevBlock {
if k <= block.Header.Height {
delete(m.prevBlock, k)
delete(m.prevCommit, k)
delete(m.prevMetaData, k)
}
}

return nil
}

Expand Down
7 changes: 7 additions & 0 deletions block/manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ type Manager struct {
syncCache map[uint64]*types.Block

logger log.Logger

prevBlock map[uint64]*types.Block
prevCommit map[uint64]*types.Commit
prevMetaData map[uint64]blockMetaData
}

// getInitialState tries to load lastState from Store, and if it's not available it reads GenesisDoc.
Expand Down Expand Up @@ -168,6 +172,9 @@ func NewManager(
shouldProduceBlocksCh: make(chan bool, 1),
produceEmptyBlockCh: make(chan bool, 1),
logger: logger,
prevBlock: make(map[uint64]*types.Block),
prevCommit: make(map[uint64]*types.Commit),
prevMetaData: make(map[uint64]blockMetaData),
}

return agg, nil
Expand Down