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(lib/grandpa): fix grandpa stall and various bugs #1708

Merged
merged 25 commits into from
Aug 5, 2021
Merged
Show file tree
Hide file tree
Changes from 18 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
1 change: 0 additions & 1 deletion dot/core/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ type BlockState interface {
GetSlotForBlock(common.Hash) (uint64, error)
GetFinalisedHeader(uint64, uint64) (*types.Header, error)
GetFinalisedHash(uint64, uint64) (common.Hash, error)
SetFinalisedHash(common.Hash, uint64, uint64) error
RegisterImportedChannel(ch chan<- *types.Block) (byte, error)
UnregisterImportedChannel(id byte)
RegisterFinalizedChannel(ch chan<- *types.FinalisationInfo) (byte, error)
Expand Down
48 changes: 24 additions & 24 deletions dot/network/mock_block_state.go

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

9 changes: 7 additions & 2 deletions dot/network/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -343,7 +343,7 @@ func (s *Service) sentBlockIntervalTelemetry() {
}
bestHash := best.Hash()

finalized, err := s.blockState.GetFinalisedHeader(0, 0) //nolint
finalized, err := s.blockState.GetHighestFinalisedHeader() //nolint
if err != nil {
continue
}
Expand Down Expand Up @@ -519,16 +519,21 @@ func (s *Service) GossipMessage(msg NotificationsMessage) {
func (s *Service) SendMessage(to peer.ID, msg NotificationsMessage) error {
s.notificationsMu.Lock()
defer s.notificationsMu.Unlock()

for msgID, prtl := range s.notificationsProtocols {
if msg.Type() != msgID || prtl == nil {
if msg.Type() != msgID {
continue
}

hs, err := prtl.getHandshake()
if err != nil {
return err
}

s.sendData(to, hs, prtl, msg)
return nil
}

return errors.New("message not supported by any notifications protocol")
}

Expand Down
2 changes: 1 addition & 1 deletion dot/network/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type BlockState interface {
BestBlockNumber() (*big.Int, error)
GenesisHash() common.Hash
HasBlockBody(common.Hash) (bool, error)
GetFinalisedHeader(round, setID uint64) (*types.Header, error)
GetHighestFinalisedHeader() (*types.Header, error)
GetHashByNumber(num *big.Int) (common.Hash, error)
}

Expand Down
6 changes: 3 additions & 3 deletions dot/network/sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -351,7 +351,7 @@ func (q *syncQueue) benchmark() {
goal := atomic.LoadInt64(&q.goal)

if before.Number.Int64() >= goal {
finalised, err := q.s.blockState.GetFinalisedHeader(0, 0) //nolint
finalised, err := q.s.blockState.GetHighestFinalisedHeader() //nolint
if err != nil {
continue
}
Expand Down Expand Up @@ -767,7 +767,7 @@ func (q *syncQueue) handleBlockJustification(data []*types.BlockData) {
}

func (q *syncQueue) handleBlockData(data []*types.BlockData) {
finalised, err := q.s.blockState.GetFinalisedHeader(0, 0)
finalised, err := q.s.blockState.GetHighestFinalisedHeader()
if err != nil {
panic(err) // this should never happen
}
Expand Down Expand Up @@ -815,7 +815,7 @@ func (q *syncQueue) handleBlockDataFailure(idx int, err error, data []*types.Blo
logger.Warn("failed to handle block data", "failed on block", q.currStart+int64(idx), "error", err)

if errors.Is(err, chaindb.ErrKeyNotFound) || errors.Is(err, blocktree.ErrParentNotFound) {
finalised, err := q.s.blockState.GetFinalisedHeader(0, 0)
finalised, err := q.s.blockState.GetHighestFinalisedHeader()
if err != nil {
panic(err)
}
Expand Down
7 changes: 3 additions & 4 deletions dot/network/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ import (
)

// NewMockBlockState create and return a network BlockState interface mock
func NewMockBlockState(n *big.Int) *MockBlockState {
func NewMockBlockState(n *big.Int) *mockBlockState {
parentHash, _ := common.HexToHash("0x4545454545454545454545454545454545454545454545454545454545454545")
stateRoot, _ := common.HexToHash("0xb3266de137d20a5d0ff3a6401eb57127525fd9b2693701f0bf5a8a853fa3ebe0")
extrinsicsRoot, _ := common.HexToHash("0x03170a2e7597b7b7e3d84c05391d139a62b157e78786d8c082f29dcf4c111314")
Expand All @@ -31,13 +31,12 @@ func NewMockBlockState(n *big.Int) *MockBlockState {
Digest: types.Digest{},
}

m := new(MockBlockState)
m := new(mockBlockState)
m.On("BestBlockHeader").Return(header, nil)

m.On("GetHighestFinalisedHeader").Return(header, nil)
m.On("GenesisHash").Return(common.NewHash([]byte{}))
m.On("BestBlockNumber").Return(big.NewInt(1), nil)
m.On("HasBlockBody", mock.AnythingOfType("common.Hash")).Return(false, nil)
m.On("GetFinalisedHeader", mock.AnythingOfType("uint64"), mock.AnythingOfType("uint64")).Return(header, nil)
m.On("GetHashByNumber", mock.AnythingOfType("*big.Int")).Return(common.Hash{}, nil)

return m
Expand Down
2 changes: 1 addition & 1 deletion dot/rpc/modules/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ type BlockAPI interface {
GetHeader(hash common.Hash) (*types.Header, error)
BestBlockHash() common.Hash
GetBlockByHash(hash common.Hash) (*types.Block, error)
GetBlockHash(blockNumber *big.Int) (*common.Hash, error)
GetBlockHash(blockNumber *big.Int) (common.Hash, error)
GetFinalisedHash(uint64, uint64) (common.Hash, error)
HasJustification(hash common.Hash) (bool, error)
GetJustification(hash common.Hash) ([]byte, error)
Expand Down
8 changes: 4 additions & 4 deletions dot/rpc/modules/mocks/block_api.go

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

2 changes: 1 addition & 1 deletion dot/rpc/modules/state_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -453,5 +453,5 @@ func setupStateModule(t *testing.T) (*StateModule, *common.Hash, *common.Hash) {

hash, _ := chain.Block.GetBlockHash(big.NewInt(2))
core := newCoreService(t, chain)
return NewStateModule(net, chain.Storage, core), hash, &sr1
return NewStateModule(net, chain.Storage, core), &hash, &sr1
}
17 changes: 8 additions & 9 deletions dot/state/block.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,9 +88,7 @@ func NewBlockState(db chaindb.Database, bt *blocktree.BlockTree) (*BlockState, e
}

bs.importedBytePool = common.NewBytePool256()

bs.finalisedBytePool = common.NewBytePool256()

return bs, nil
}

Expand Down Expand Up @@ -123,15 +121,17 @@ func NewBlockStateFromGenesis(db chaindb.Database, header *types.Header) (*Block

bs.genesisHash = header.Hash()

if err := bs.db.Put(highestRoundAndSetIDKey, roundSetIDKey(0, 0)); err != nil {
return nil, err
}

// set the latest finalised head to the genesis header
if err := bs.SetFinalisedHash(bs.genesisHash, 0, 0); err != nil {
return nil, err
}

bs.importedBytePool = common.NewBytePool256()

bs.finalisedBytePool = common.NewBytePool256()

return bs, nil
}

Expand Down Expand Up @@ -320,14 +320,13 @@ func (bs *BlockState) GetBlockByNumber(num *big.Int) (*types.Block, error) {
}

// GetBlockHash returns block hash for a given blockNumber
func (bs *BlockState) GetBlockHash(blockNumber *big.Int) (*common.Hash, error) {
// First retrieve the block hash in a byte array based on the block number from the database
func (bs *BlockState) GetBlockHash(blockNumber *big.Int) (common.Hash, error) {
byteHash, err := bs.db.Get(headerHashKey(blockNumber.Uint64()))
if err != nil {
return nil, fmt.Errorf("cannot get block %d: %w", blockNumber, err)
return common.Hash{}, fmt.Errorf("cannot get block %d: %w", blockNumber, err)
}
hash := common.NewHash(byteHash)
return &hash, nil

return common.NewHash(byteHash), nil
}

// SetHeader will set the header into DB
Expand Down
Loading