Skip to content

Commit 67c93f4

Browse files
authored
fix(lib/grandpa): fix grandpa stall and various bugs (#1708)
1 parent f73574c commit 67c93f4

34 files changed

+437
-248
lines changed

dot/core/interface.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,6 @@ type BlockState interface {
4141
GetSlotForBlock(common.Hash) (uint64, error)
4242
GetFinalisedHeader(uint64, uint64) (*types.Header, error)
4343
GetFinalisedHash(uint64, uint64) (common.Hash, error)
44-
SetFinalisedHash(common.Hash, uint64, uint64) error
4544
RegisterImportedChannel(ch chan<- *types.Block) (byte, error)
4645
UnregisterImportedChannel(id byte)
4746
RegisterFinalizedChannel(ch chan<- *types.FinalisationInfo) (byte, error)

dot/network/mock_block_state.go

Lines changed: 24 additions & 24 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dot/network/service.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ func (s *Service) sentBlockIntervalTelemetry() {
343343
}
344344
bestHash := best.Hash()
345345

346-
finalized, err := s.blockState.GetFinalisedHeader(0, 0) //nolint
346+
finalized, err := s.blockState.GetHighestFinalisedHeader() //nolint
347347
if err != nil {
348348
continue
349349
}
@@ -519,16 +519,21 @@ func (s *Service) GossipMessage(msg NotificationsMessage) {
519519
func (s *Service) SendMessage(to peer.ID, msg NotificationsMessage) error {
520520
s.notificationsMu.Lock()
521521
defer s.notificationsMu.Unlock()
522+
522523
for msgID, prtl := range s.notificationsProtocols {
523-
if msg.Type() != msgID || prtl == nil {
524+
if msg.Type() != msgID {
524525
continue
525526
}
527+
526528
hs, err := prtl.getHandshake()
527529
if err != nil {
528530
return err
529531
}
532+
530533
s.sendData(to, hs, prtl, msg)
534+
return nil
531535
}
536+
532537
return errors.New("message not supported by any notifications protocol")
533538
}
534539

dot/network/state.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ type BlockState interface {
2929
BestBlockNumber() (*big.Int, error)
3030
GenesisHash() common.Hash
3131
HasBlockBody(common.Hash) (bool, error)
32-
GetFinalisedHeader(round, setID uint64) (*types.Header, error)
32+
GetHighestFinalisedHeader() (*types.Header, error)
3333
GetHashByNumber(num *big.Int) (common.Hash, error)
3434
}
3535

dot/network/sync.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -351,7 +351,7 @@ func (q *syncQueue) benchmark() {
351351
goal := atomic.LoadInt64(&q.goal)
352352

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

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

817817
if errors.Is(err, chaindb.ErrKeyNotFound) || errors.Is(err, blocktree.ErrParentNotFound) {
818-
finalised, err := q.s.blockState.GetFinalisedHeader(0, 0)
818+
finalised, err := q.s.blockState.GetHighestFinalisedHeader()
819819
if err != nil {
820820
panic(err)
821821
}

dot/network/test_helpers.go

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import (
1515
)
1616

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

34-
m := new(MockBlockState)
34+
m := new(mockBlockState)
3535
m.On("BestBlockHeader").Return(header, nil)
36-
36+
m.On("GetHighestFinalisedHeader").Return(header, nil)
3737
m.On("GenesisHash").Return(common.NewHash([]byte{}))
3838
m.On("BestBlockNumber").Return(big.NewInt(1), nil)
3939
m.On("HasBlockBody", mock.AnythingOfType("common.Hash")).Return(false, nil)
40-
m.On("GetFinalisedHeader", mock.AnythingOfType("uint64"), mock.AnythingOfType("uint64")).Return(header, nil)
4140
m.On("GetHashByNumber", mock.AnythingOfType("*big.Int")).Return(common.Hash{}, nil)
4241

4342
return m

dot/rpc/modules/api.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,9 @@ type BlockAPI interface {
2929
GetHeader(hash common.Hash) (*types.Header, error)
3030
BestBlockHash() common.Hash
3131
GetBlockByHash(hash common.Hash) (*types.Block, error)
32-
GetBlockHash(blockNumber *big.Int) (*common.Hash, error)
32+
GetBlockHash(blockNumber *big.Int) (common.Hash, error)
3333
GetFinalisedHash(uint64, uint64) (common.Hash, error)
34+
GetHighestFinalisedHash() (common.Hash, error)
3435
HasJustification(hash common.Hash) (bool, error)
3536
GetJustification(hash common.Hash) ([]byte, error)
3637
RegisterImportedChannel(ch chan<- *types.Block) (byte, error)

dot/rpc/modules/api_mocks.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,13 +20,14 @@ func NewMockStorageAPI() *modulesmocks.MockStorageAPI {
2020
}
2121

2222
// NewMockBlockAPI creates and return an rpc BlockAPI interface mock
23-
func NewMockBlockAPI() *modulesmocks.MockBlockAPI {
24-
m := new(modulesmocks.MockBlockAPI)
23+
func NewMockBlockAPI() *modulesmocks.BlockAPI {
24+
m := new(modulesmocks.BlockAPI)
2525
m.On("GetHeader", mock.AnythingOfType("common.Hash")).Return(nil, nil)
2626
m.On("BestBlockHash").Return(common.Hash{})
2727
m.On("GetBlockByHash", mock.AnythingOfType("common.Hash")).Return(nil, nil)
2828
m.On("GetBlockHash", mock.AnythingOfType("*big.Int")).Return(nil, nil)
2929
m.On("GetFinalisedHash", mock.AnythingOfType("uint64"), mock.AnythingOfType("uint64")).Return(common.Hash{}, nil)
30+
m.On("GetHighestFinalisedHash").Return(common.Hash{}, nil)
3031
m.On("RegisterImportedChannel", mock.AnythingOfType("chan<- *types.Block")).Return(byte(0), nil)
3132
m.On("UnregisterImportedChannel", mock.AnythingOfType("uint8"))
3233
m.On("RegisterFinalizedChannel", mock.AnythingOfType("chan<- *types.FinalisationInfo")).Return(byte(0), nil)

dot/rpc/modules/chain.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ func (cm *ChainModule) GetHead(r *http.Request, req *ChainBlockNumberRequest, re
135135

136136
// GetFinalizedHead returns the most recently finalised block hash
137137
func (cm *ChainModule) GetFinalizedHead(r *http.Request, req *EmptyRequest, res *ChainHashResponse) error {
138-
h, err := cm.blockAPI.GetFinalisedHash(0, 0)
138+
h, err := cm.blockAPI.GetHighestFinalisedHash()
139139
if err != nil {
140140
return err
141141
}

dot/rpc/modules/mocks/block_api.go renamed to dot/rpc/modules/mocks/BlockAPI.go

Lines changed: 41 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)