Skip to content

Commit

Permalink
fix(dot/state): fix deadlock, fixes bootstrap syncing (ChainSafe#1959)
Browse files Browse the repository at this point in the history
  • Loading branch information
noot authored and timwu20 committed Dec 6, 2021
1 parent 2164490 commit 13c92ea
Show file tree
Hide file tree
Showing 6 changed files with 26 additions and 19 deletions.
1 change: 0 additions & 1 deletion dot/core/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -324,7 +324,6 @@ func (s *Service) handleBlocksAsync() {
}

s.maintainTransactionPool(block)

case <-s.ctx.Done():
return
}
Expand Down
8 changes: 5 additions & 3 deletions dot/state/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,10 @@ 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 @@ -327,6 +331,7 @@ func (bs *BlockState) GetBlockByHash(hash common.Hash) (*types.Block, error) {
if err != nil {
return nil, err
}

return &types.Block{Header: *header, Body: *blockBody}, nil
}

Expand Down Expand Up @@ -360,9 +365,6 @@ func (bs *BlockState) HasBlockBody(hash common.Hash) (bool, error) {

// GetBlockBody will return Body for a given hash
func (bs *BlockState) GetBlockBody(hash common.Hash) (*types.Body, error) {
bs.RLock()
defer bs.RUnlock()

block, has := bs.getUnfinalisedBlock(hash)
if has {
return &block.Body, nil
Expand Down
4 changes: 3 additions & 1 deletion dot/state/block_finalisation.go
Original file line number Diff line number Diff line change
Expand Up @@ -226,7 +226,7 @@ func (bs *BlockState) handleFinalisedBlock(curr common.Hash) error {
continue
}

block, has := bs.getAndDeleteUnfinalisedBlock(hash)
block, has := bs.getUnfinalisedBlock(hash)
if !has {
return fmt.Errorf("failed to find block in unfinalised block map, block=%s", hash)
}
Expand All @@ -251,6 +251,8 @@ func (bs *BlockState) handleFinalisedBlock(curr common.Hash) error {
if err = batch.Put(headerHashKey(block.Header.Number.Uint64()), hash.ToBytes()); err != nil {
return err
}

bs.deleteUnfinalisedBlock(hash)
}

return batch.Flush()
Expand Down
17 changes: 8 additions & 9 deletions dot/sync/chain_processor.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,11 +115,6 @@ func (s *chainProcessor) processBlockData(bd *types.BlockData) error {
return ErrNilBlockData
}

err := s.blockState.CompareAndSetBlockData(bd)
if err != nil {
return fmt.Errorf("failed to compare and set data: %w", err)
}

hasHeader, _ := s.blockState.HasHeader(bd.Hash)
hasBody, _ := s.blockState.HasBlockBody(bd.Hash)
if hasHeader && hasBody {
Expand Down Expand Up @@ -164,8 +159,10 @@ func (s *chainProcessor) processBlockData(bd *types.BlockData) error {
return nil
}

logger.Debug("processing block data", "hash", bd.Hash)

if bd.Header != nil && bd.Body != nil {
if err = s.handleHeader(bd.Header); err != nil {
if err := s.handleHeader(bd.Header); err != nil {
return err
}

Expand All @@ -176,9 +173,7 @@ func (s *chainProcessor) processBlockData(bd *types.BlockData) error {
Body: *bd.Body,
}

logger.Debug("processing block", "hash", bd.Hash)

if err = s.handleBlock(block); err != nil {
if err := s.handleBlock(block); err != nil {
logger.Error("failed to handle block", "number", block.Header.Number, "error", err)
return err
}
Expand All @@ -191,6 +186,10 @@ func (s *chainProcessor) processBlockData(bd *types.BlockData) error {
s.handleJustification(bd.Header, *bd.Justification)
}

if err := s.blockState.CompareAndSetBlockData(bd); err != nil {
return fmt.Errorf("failed to compare and set data: %w", err)
}

return nil
}

Expand Down
13 changes: 9 additions & 4 deletions lib/runtime/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,6 @@ func GetRuntimeBlob(testRuntimeFilePath, testRuntimeURL string) (n int64, err er
if err != nil {
return 0, err
}
defer func() {
_ = out.Close()
}()

/* #nosec */
resp, err := http.Get(testRuntimeURL)
Expand All @@ -103,7 +100,15 @@ func GetRuntimeBlob(testRuntimeFilePath, testRuntimeURL string) (n int64, err er
}()

n, err = io.Copy(out, resp.Body)
return n, err
if err != nil {
return 0, err
}

if err = out.Close(); err != nil {
return 0, err
}

return n, nil
}

// TestRuntimeNetwork ...
Expand Down
2 changes: 1 addition & 1 deletion scripts/install-lint.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ fi

if ! command -v golangci-lint &> /dev/null
then
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.41.0
curl -sSfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(go env GOPATH)/bin v1.43.0
fi

export PATH=$PATH:$(go env GOPATH)/bin

0 comments on commit 13c92ea

Please sign in to comment.