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(dot/state): update *state.BlockState.AddBlockToBlockTree to store block in unfinalisedBlocksMap #2006

Merged
merged 3 commits into from
Nov 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 4 additions & 7 deletions dot/state/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,10 +221,6 @@ func (bs *BlockState) getAndDeleteUnfinalisedBlock(hash common.Hash) (*types.Blo
return block.(*types.Block), true
}

func (bs *BlockState) deleteUnfinalisedBlock(hash common.Hash) {
bs.unfinalisedBlocks.Delete(hash)
}

// HasHeader returns if the db contains a header with the given hash
func (bs *BlockState) HasHeader(hash common.Hash) (bool, error) {
if bs.hasUnfinalisedBlock(hash) {
Expand Down Expand Up @@ -434,16 +430,17 @@ func (bs *BlockState) AddBlockWithArrivalTime(block *types.Block, arrivalTime ti

// AddBlockToBlockTree adds the given block to the blocktree. It does not write it to the database.
// TODO: remove this func and usage from sync (after sync refactor?)
func (bs *BlockState) AddBlockToBlockTree(header *types.Header) error {
func (bs *BlockState) AddBlockToBlockTree(block *types.Block) error {
bs.Lock()
defer bs.Unlock()

arrivalTime, err := bs.GetArrivalTime(header.Hash())
arrivalTime, err := bs.GetArrivalTime(block.Header.Hash())
if err != nil {
arrivalTime = time.Now()
}

return bs.bt.AddBlock(header, arrivalTime)
bs.storeUnfinalisedBlock(block)
return bs.bt.AddBlock(&block.Header, arrivalTime)
}

// GetAllBlocksAtNumber returns all unfinalised blocks with the given number
Expand Down
4 changes: 2 additions & 2 deletions dot/state/block_finalisation.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,6 @@ func (bs *BlockState) SetFinalisedHash(hash common.Hash, round, setID uint64) er
return fmt.Errorf("could not send 'notify.finalized' telemetry message, error: %s", err)
}

// return bs.setHighestRoundAndSetID(round, setID)
bs.lastFinalised = hash
return nil
}
Expand Down Expand Up @@ -252,7 +251,8 @@ func (bs *BlockState) handleFinalisedBlock(curr common.Hash) error {
return err
}

bs.deleteUnfinalisedBlock(hash)
// the block will be deleted from the unfinalisedBlockMap in the pruning loop
// in `SetFinalisedHash()`, which calls this function
}

return batch.Flush()
Expand Down
5 changes: 4 additions & 1 deletion dot/state/block_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,7 +470,10 @@ func TestAddBlockToBlockTree(t *testing.T) {
err := bs.setArrivalTime(header.Hash(), time.Now())
require.NoError(t, err)

err = bs.AddBlockToBlockTree(header)
err = bs.AddBlockToBlockTree(&types.Block{
Header: *header,
Body: types.Body{},
})
require.NoError(t, err)
require.Equal(t, bs.BestBlockHash(), header.Hash())
}
Expand Down
2 changes: 1 addition & 1 deletion dot/sync/chain_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func (s *chainProcessor) processBlockData(bd *types.BlockData) error {

logger.Debug("skipping block, already have", "hash", bd.Hash, "number", block.Header.Number)

err = s.blockState.AddBlockToBlockTree(&block.Header)
err = s.blockState.AddBlockToBlockTree(block)
if errors.Is(err, blocktree.ErrBlockExists) {
return nil
} else if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion dot/sync/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ type BlockState interface {
GetJustification(common.Hash) ([]byte, error)
SetJustification(hash common.Hash, data []byte) error
SetFinalisedHash(hash common.Hash, round, setID uint64) error
AddBlockToBlockTree(header *types.Header) error
AddBlockToBlockTree(block *types.Block) error
GetHashByNumber(*big.Int) (common.Hash, error)
GetBlockByHash(common.Hash) (*types.Block, error)
GetRuntime(*common.Hash) (runtime.Instance, error)
Expand Down
10 changes: 5 additions & 5 deletions dot/sync/mocks/block_state.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.