Skip to content

Commit c82aba7

Browse files
envestccCoderZhi
andauthored
Fix proposal pool not updated when received block by sync (#4644)
* proposalpool and blockbuilder as blockchain subscriber * fix test --------- Co-authored-by: CoderZhi <[email protected]>
1 parent 20c91bb commit c82aba7

File tree

2 files changed

+10
-13
lines changed

2 files changed

+10
-13
lines changed

consensus/scheme/rolldpos/chainmanager.go

Lines changed: 5 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -109,13 +109,16 @@ func NewChainManager(bc blockchain.Blockchain, srf StateReaderFactory, bbf Block
109109
if err != nil {
110110
log.L().Panic("Failed to generate prometheus timer factory.", zap.Error(err))
111111
}
112+
pool := newProposalPool()
113+
bc.AddSubscriber(bbf)
114+
bc.AddSubscriber(pool)
112115

113116
return &chainManager{
114117
bc: bc,
115118
bbf: bbf,
116119
timerFactory: timerFactory,
117120
srf: srf,
118-
pool: newProposalPool(),
121+
pool: pool,
119122
}
120123
}
121124

@@ -243,18 +246,7 @@ func (cm *chainManager) ValidateBlock(blk *block.Block) error {
243246

244247
// CommitBlock validates and appends a block to the chain
245248
func (cm *chainManager) CommitBlock(blk *block.Block) error {
246-
if err := cm.bc.CommitBlock(blk); err != nil {
247-
return err
248-
}
249-
if err := cm.bbf.ReceiveBlock(blk); err != nil {
250-
blkHash := blk.HashBlock()
251-
log.L().Error("failed to receive block", zap.Error(err), zap.Uint64("height", blk.Height()), log.Hex("hash", blkHash[:]))
252-
}
253-
if err := cm.pool.ReceiveBlock(blk); err != nil {
254-
blkHash := blk.HashBlock()
255-
log.L().Error("failed to receive block", zap.Error(err), zap.Uint64("height", blk.Height()), log.Hex("hash", blkHash[:]))
256-
}
257-
return nil
249+
return cm.bc.CommitBlock(blk)
258250
}
259251

260252
func (cm *chainManager) Start(ctx context.Context) error {

consensus/scheme/rolldpos/rolldpos_test.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ func TestNewRollDPoS(t *testing.T) {
7777
sk := identityset.PrivateKey(0)
7878
chain := mock_blockchain.NewMockBlockchain(ctrl)
7979
chain.EXPECT().ChainID().Return(uint32(1)).AnyTimes()
80+
chain.EXPECT().AddSubscriber(gomock.Any()).Return(nil).AnyTimes()
8081
r, err := NewRollDPoSBuilder().
8182
SetConfig(builderCfg).
8283
SetPriKey(sk).
@@ -95,6 +96,7 @@ func TestNewRollDPoS(t *testing.T) {
9596
sk := identityset.PrivateKey(0)
9697
chain := mock_blockchain.NewMockBlockchain(ctrl)
9798
chain.EXPECT().ChainID().Return(uint32(1)).AnyTimes()
99+
chain.EXPECT().AddSubscriber(gomock.Any()).Return(nil).AnyTimes()
98100
r, err := NewRollDPoSBuilder().
99101
SetConfig(builderCfg).
100102
SetPriKey(sk).
@@ -117,6 +119,7 @@ func TestNewRollDPoS(t *testing.T) {
117119
sk := identityset.PrivateKey(0)
118120
chain := mock_blockchain.NewMockBlockchain(ctrl)
119121
chain.EXPECT().ChainID().Return(uint32(1)).AnyTimes()
122+
chain.EXPECT().AddSubscriber(gomock.Any()).Return(nil).AnyTimes()
120123
r, err := NewRollDPoSBuilder().
121124
SetConfig(builderCfg).
122125
SetPriKey(sk).
@@ -209,6 +212,7 @@ func TestValidateBlockFooter(t *testing.T) {
209212
bc.EXPECT().TipHeight().Return(blockHeight).AnyTimes()
210213
bc.EXPECT().BlockHeaderByHeight(blockHeight).Return(&block.Header{}, nil).AnyTimes()
211214
bc.EXPECT().TipHash().Return(hash.ZeroHash256).AnyTimes()
215+
bc.EXPECT().AddSubscriber(gomock.Any()).Return(nil).AnyTimes()
212216
sk1 := identityset.PrivateKey(1)
213217
g := genesis.TestDefault()
214218
g.NumDelegates = 4
@@ -303,6 +307,7 @@ func TestRollDPoS_Metrics(t *testing.T) {
303307
bc.EXPECT().BlockFooterByHeight(blockHeight).Return(footer, nil).AnyTimes()
304308
bc.EXPECT().ChainID().Return(uint32(1)).AnyTimes()
305309
bc.EXPECT().BlockHeaderByHeight(blockHeight).Return(&block.Header{}, nil).AnyTimes()
310+
bc.EXPECT().AddSubscriber(gomock.Any()).Return(nil).AnyTimes()
306311

307312
sk1 := identityset.PrivateKey(1)
308313
cfg := DefaultConfig

0 commit comments

Comments
 (0)