Skip to content

Commit

Permalink
BFT synchronizer
Browse files Browse the repository at this point in the history
Signed-off-by: Yoav Tock <tock@il.ibm.com>
Change-Id: If590c9d4ed727f7c2805142dde36cef3c3f14fb1
  • Loading branch information
tock-ibm committed Jan 29, 2024
1 parent 5b23e62 commit a9b369e
Show file tree
Hide file tree
Showing 10 changed files with 324 additions and 16 deletions.
10 changes: 6 additions & 4 deletions common/deliver/mock/block_iterator.go

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

65 changes: 65 additions & 0 deletions common/deliver/mock/block_reader.go

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

10 changes: 10 additions & 0 deletions common/ledger/blockledger/fileledger/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ func (fl *FileLedger) Height() uint64 {
return info.Height
}

// GetCurrentBlockHash returns the hash of the current block's header
func (fl *FileLedger) GetCurrentBlockHash() []byte {
info, err := fl.blockStore.GetBlockchainInfo()
if err != nil {
logger.Panic(err)
}

return info.CurrentBlockHash
}

// Append a new block to the ledger
func (fl *FileLedger) Append(block *cb.Block) error {
err := fl.blockStore.AddBlock(block)
Expand Down
14 changes: 11 additions & 3 deletions common/ledger/blockledger/fileledger/impl_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ SPDX-License-Identifier: Apache-2.0
package fileledger

import (
"bytes"
"errors"
"fmt"
"testing"
Expand Down Expand Up @@ -129,6 +130,10 @@ func TestInitialization(t *testing.T) {
block := blockledger.GetBlock(fl, 0)
require.NotNil(t, block, "Error retrieving genesis block")
require.Equal(t, protoutil.BlockHeaderHash(genesisBlock.Header), protoutil.BlockHeaderHash(block.Header), "Block hashes did no match")

h := fl.GetCurrentBlockHash()
require.NotNil(t, h, "block hash is nil")
require.True(t, bytes.Equal(h, protoutil.BlockHeaderHash(block.Header)), "block hash mismatch")
}

func TestReinitialization(t *testing.T) {
Expand Down Expand Up @@ -175,16 +180,19 @@ func TestReinitialization(t *testing.T) {
func TestAddition(t *testing.T) {
tev, fl := initialize(t)
defer tev.tearDown()
info, _ := fl.blockStore.GetBlockchainInfo()
prevHash := info.CurrentBlockHash

prevHash := fl.GetCurrentBlockHash()
envelope := getSampleEnvelopeWithSignatureHeader()
b1 := blockledger.CreateNextBlock(fl, []*cb.Envelope{envelope})
fl.Append(b1)
require.Equal(t, uint64(2), fl.Height(), "Block height should be 2")

block := blockledger.GetBlock(fl, 1)
require.NotNil(t, block, "Error retrieving genesis block")
require.NotNil(t, block, "Error retrieving second block")
require.Equal(t, prevHash, block.Header.PreviousHash, "Block hashes did no match")

secondHash := fl.GetCurrentBlockHash()
require.True(t, bytes.Equal(secondHash, protoutil.BlockHeaderHash(block.Header)), "block hash mismatch")
}

func TestRetrieval(t *testing.T) {
Expand Down
7 changes: 4 additions & 3 deletions common/ledger/blockledger/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ type Iterator interface {

// Reader allows the caller to inspect the ledger
type Reader interface {
// Iterator returns an Iterator, as specified by an ab.SeekInfo message, and
// its starting block number
// Iterator returns an Iterator, as specified by an ab.SeekInfo message, and its starting block number
Iterator(startType *ab.SeekPosition) (Iterator, uint64)
// Height returns the number of blocks on the ledger
Height() uint64
// retrieve blockByNumber
// RetrieveBlockByNumber retrieve blockByNumber
RetrieveBlockByNumber(blockNumber uint64) (*cb.Block, error)
// GetCurrentBlockHash returns the block header hash of the last block in the ledger.
GetCurrentBlockHash() []byte
}

// Writer allows the caller to modify the ledger
Expand Down
9 changes: 7 additions & 2 deletions core/peer/deliverevents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,21 @@ func (m *mockReader) Iterator(startType *orderer.SeekPosition) (blockledger.Iter
return args.Get(0).(blockledger.Iterator), args.Get(1).(uint64)
}

func (m *mockReader) Height() uint64 {
func (m *mockReader) GetCurrentBlockHash() []byte {
args := m.Called()
return args.Get(0).(uint64)
return args.Get(0).([]byte)
}

func (m *mockReader) RetrieveBlockByNumber(blockNum uint64) (*common.Block, error) {
args := m.Called()
return args.Get(0).(*common.Block), args.Error(1)
}

func (m *mockReader) Height() uint64 {
args := m.Called()
return args.Get(0).(uint64)
}

// mockChainSupport
type mockChainSupport struct {
mock.Mock
Expand Down
2 changes: 1 addition & 1 deletion orderer/common/cluster/deliver.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ type BlockPuller struct {

// A 'stopper' goroutine may signal the go-routine servicing PullBlock & HeightsByEndpoints to stop by closing this
// channel. Note: all methods of the BlockPuller must be serviced by a single goroutine, it is not thread safe.
// It is the responsibility of the 'stopper' not to close the channel more then once.
// It is the responsibility of the 'stopper' not to close the channel more than once.
StopChannel chan struct{}

// Internal state
Expand Down
65 changes: 65 additions & 0 deletions orderer/common/multichannel/mocks/read_writer.go

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

6 changes: 3 additions & 3 deletions orderer/consensus/smartbft/chain.go
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ type BFTChain struct {
RuntimeConfig *atomic.Value
Channel string
Config types.Configuration
BlockPuller BlockPuller
BlockPuller BlockPuller // TODO make bft-synchro
Comm cluster.Communicator
SignerSerializer signerSerializer
PolicyManager policies.Manager
Expand All @@ -97,7 +97,7 @@ func NewChain(
selfID uint64,
config types.Configuration,
walDir string,
blockPuller BlockPuller,
blockPuller BlockPuller, // TODO make bft-synchro
comm cluster.Communicator,
signerSerializer signerSerializer,
policyManager policies.Manager,
Expand Down Expand Up @@ -202,7 +202,7 @@ func bftSmartConsensusBuild(
// report cluster size
c.Metrics.ClusterSize.Set(float64(clusterSize))

sync := &Synchronizer{
sync := &Synchronizer{ // TODO make bft-synchro
selfID: rtc.id,
BlockToDecision: c.blockToDecision,
OnCommit: func(block *cb.Block) types.Reconfig {
Expand Down
Loading

0 comments on commit a9b369e

Please sign in to comment.