Skip to content

Commit

Permalink
fix nodes panic during synchronization
Browse files Browse the repository at this point in the history
Signed-off-by: Fedor Partanskiy <fedor.partanskiy@atme.com>
  • Loading branch information
pfi79 committed Dec 18, 2024
1 parent 0501b0d commit 25e8517
Show file tree
Hide file tree
Showing 8 changed files with 102 additions and 11 deletions.
3 changes: 3 additions & 0 deletions orderer/consensus/consensus.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ type ConsenterSupport interface {
// WriteBlock commits a block to the ledger.
WriteBlock(block *cb.Block, encodedMetadataValue []byte)

// WriteBlockSync commits a block to the ledger.
WriteBlockSync(block *cb.Block, encodedMetadataValue []byte)

// WriteConfigBlock commits a block to the ledger, and applies the config update inside.
WriteConfigBlock(block *cb.Block, encodedMetadataValue []byte)

Expand Down
46 changes: 46 additions & 0 deletions orderer/consensus/mocks/mock_consenter_support.go

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

36 changes: 35 additions & 1 deletion orderer/consensus/smartbft/mocks/consenter_support.go

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

5 changes: 2 additions & 3 deletions orderer/consensus/smartbft/synchronizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ func (s *Synchronizer) synchronize() (*types.Decision, error) {
if protoutil.IsConfigBlock(block) {
s.Support.WriteConfigBlock(block, nil)
} else {
s.Support.WriteBlock(block, nil)
s.Support.WriteBlockSync(block, nil)
}
s.Logger.Debugf("Fetched and committed block [%d] from cluster", seq)
lastPulledBlock = block
Expand All @@ -141,9 +141,8 @@ func (s *Synchronizer) synchronize() (*types.Decision, error) {
return nil, errors.Errorf("failed pulling block %d", seq)
}

startSeq := startHeight
s.Logger.Infof("Finished synchronizing with cluster, fetched %d blocks, starting from block [%d], up until and including block [%d]",
blocksFetched, startSeq, lastPulledBlock.Header.Number)
blocksFetched, startHeight, lastPulledBlock.Header.Number)

viewMetadata, lastConfigSqn := s.getViewMetadataLastConfigSqnFromBlock(lastPulledBlock)

Expand Down
5 changes: 2 additions & 3 deletions orderer/consensus/smartbft/synchronizer_bft.go
Original file line number Diff line number Diff line change
Expand Up @@ -258,7 +258,7 @@ func (s *BFTSynchronizer) getBlocksFromSyncBuffer(startHeight, targetHeight uint
s.Support.WriteConfigBlock(block, nil)
s.Logger.Debugf("Fetched and committed config block [%d] from cluster", seq)
} else {
s.Support.WriteBlock(block, nil)
s.Support.WriteBlockSync(block, nil)
s.Logger.Debugf("Fetched and committed block [%d] from cluster", seq)
}
lastPulledBlock = block
Expand All @@ -277,9 +277,8 @@ func (s *BFTSynchronizer) getBlocksFromSyncBuffer(startHeight, targetHeight uint
return nil, errors.Errorf("failed pulling block %d", seq)
}

startSeq := startHeight
s.Logger.Infof("Finished synchronizing with cluster, fetched %d blocks, starting from block [%d], up until and including block [%d]",
blocksFetched, startSeq, lastPulledBlock.Header.Number)
blocksFetched, startHeight, lastPulledBlock.Header.Number)

return lastPulledBlock, nil
}
8 changes: 4 additions & 4 deletions orderer/consensus/smartbft/synchronizer_bft_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -306,7 +306,7 @@ func TestBFTSynchronizer(t *testing.T) {
fakeCS.WriteConfigBlockCalls(func(b *cb.Block, m []byte) {
ledger = append(ledger, b)
})
fakeCS.WriteBlockCalls(func(b *cb.Block, m []byte) {
fakeCS.WriteBlockSyncCalls(func(b *cb.Block, m []byte) {
ledger = append(ledger, b)
})

Expand Down Expand Up @@ -395,7 +395,7 @@ func TestBFTSynchronizer(t *testing.T) {
require.NotNil(t, resp)
require.Equal(t, *decision, resp)
require.Equal(t, 102, len(ledger))
require.Equal(t, 1, fakeCS.WriteBlockCallCount())
require.Equal(t, 1, fakeCS.WriteBlockSyncCallCount())
require.Equal(t, 1, fakeCS.WriteConfigBlockCallCount())
wg.Wait()
})
Expand Down Expand Up @@ -436,7 +436,7 @@ func TestBFTSynchronizer(t *testing.T) {
fakeCS.WriteConfigBlockCalls(func(b *cb.Block, m []byte) {
ledger = append(ledger, b)
})
fakeCS.WriteBlockCalls(func(b *cb.Block, m []byte) {
fakeCS.WriteBlockSyncCalls(func(b *cb.Block, m []byte) {
ledger = append(ledger, b)
})

Expand Down Expand Up @@ -533,7 +533,7 @@ func TestBFTSynchronizer(t *testing.T) {
require.NotNil(t, resp)
require.Equal(t, *decision, resp)
require.Equal(t, 103, len(ledger))
require.Equal(t, 2, fakeCS.WriteBlockCallCount())
require.Equal(t, 2, fakeCS.WriteBlockSyncCallCount())
require.Equal(t, 1, fakeCS.WriteConfigBlockCallCount())
wg.Wait()
})
Expand Down
5 changes: 5 additions & 0 deletions orderer/consensus/smartbft/util_network_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,6 +515,11 @@ func createBFTChainUsingMocks(t *testing.T, node *Node, configInfo *ConfigInfo)
node.State.AddBlock(block)
node.Logger.Infof("Node %d appended block number %v to ledger", node.NodeId, block.Header.Number)
}).Maybe()
supportMock.EXPECT().WriteBlockSync(mock.Anything, mock.Anything).Run(
func(block *cb.Block, encodedMetadataValue []byte) {
node.State.AddBlock(block)
node.Logger.Infof("Node %d appended block number %v to ledger", node.NodeId, block.Header.Number)
}).Maybe()

supportMock.EXPECT().WriteConfigBlock(mock.Anything, mock.Anything).Run(
func(block *cb.Block, encodedMetadataValue []byte) {
Expand Down
5 changes: 5 additions & 0 deletions orderer/mocks/common/multichannel/multichannel.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,11 @@ func (mcs *ConsenterSupport) WriteBlock(block *cb.Block, encodedMetadataValue []
mcs.Append(block)
}

// WriteBlock writes data to the Blocks channel
func (mcs *ConsenterSupport) WriteBlockSync(block *cb.Block, encodedMetadataValue []byte) {
mcs.WriteBlock(block, encodedMetadataValue)
}

// WriteConfigBlock calls WriteBlock
func (mcs *ConsenterSupport) WriteConfigBlock(block *cb.Block, encodedMetadataValue []byte) {
mcs.WriteBlock(block, encodedMetadataValue)
Expand Down

0 comments on commit 25e8517

Please sign in to comment.