Skip to content

Commit

Permalink
Back port 2023 - skip empty ledge and 2635 - RetrieveBlockByNumber (#…
Browse files Browse the repository at this point in the history
…2648)

* FAB-18276 Ch.Part.API: extractSystemChannel must skip empty ledger (#2023)

- orderer/common/server/main.go extractSystemChannel() panics when
it hits an empty ledger.

- In addition this method is not covered with unit tests at all.

- Remove some dead code.

Signed-off-by: Yoav Tock <tock@il.ibm.com>
Change-Id: Ie13156784d2ec600e3f97d34f20f6836b28f7662

* Added RetrieveBlockByNumber into blockledger (#2635)

GetBlock is used for retrieving a block in ConfigBlockOrPanic
which has overhead of Iterator. It is now simplified by exposing
a BlockStore function RetrieveBlockByNumber.

Signed-off-by: Parameswaran Selvam <parselva@in.ibm.com>

Co-authored-by: Yoav Tock <tock@il.ibm.com>
  • Loading branch information
Param-S and tock-ibm authored Jun 7, 2021
1 parent cc50451 commit 8851da3
Show file tree
Hide file tree
Showing 12 changed files with 404 additions and 79 deletions.
94 changes: 88 additions & 6 deletions common/deliver/mock/block_reader.go

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

6 changes: 6 additions & 0 deletions common/ledger/blockledger/fileledger/impl.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type FileLedgerBlockStore interface {
AddBlock(block *cb.Block) error
GetBlockchainInfo() (*cb.BlockchainInfo, error)
RetrieveBlocks(startBlockNumber uint64) (ledger.ResultsIterator, error)
RetrieveBlockByNumber(blockNum uint64) (*cb.Block, error)
}

// NewFileLedger creates a new FileLedger for interaction with the ledger
Expand Down Expand Up @@ -87,6 +88,7 @@ func (fl *FileLedger) Iterator(startPosition *ab.SeekPosition) (blockledger.Iter

iterator, err := fl.blockStore.RetrieveBlocks(startingBlockNumber)
if err != nil {
logger.Warnw("Failed to initialize block iterator", "blockNum", startingBlockNumber, "error", err)
return &blockledger.NotFoundErrorIterator{}, 0
}

Expand All @@ -111,3 +113,7 @@ func (fl *FileLedger) Append(block *cb.Block) error {
}
return err
}

func (fl *FileLedger) RetrieveBlockByNumber(blockNumber uint64) (*cb.Block, error) {
return fl.blockStore.RetrieveBlockByNumber(blockNumber)
}
2 changes: 2 additions & 0 deletions common/ledger/blockledger/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ type Reader interface {
Iterator(startType *ab.SeekPosition) (Iterator, uint64)
// Height returns the number of blocks on the ledger
Height() uint64
// retrieve blockByNumber
RetrieveBlockByNumber(blockNumber uint64) (*cb.Block, error)
}

// Writer allows the caller to modify the ledger
Expand Down
8 changes: 8 additions & 0 deletions common/ledger/blockledger/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,12 @@ import (
"github.com/golang/protobuf/proto"
cb "github.com/hyperledger/fabric-protos-go/common"
ab "github.com/hyperledger/fabric-protos-go/orderer"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/protoutil"
)

var logger = flogging.MustGetLogger("common.ledger.blockledger.util")

var closedChan chan struct{}

func init() {
Expand Down Expand Up @@ -95,3 +98,8 @@ func GetBlock(rl Reader, index uint64) *cb.Block {
}
return block
}

func GetBlockByNumber(rl Reader, blockNum uint64) (*cb.Block, error) {
logger.Debugw("Retrieving block", "blockNum", blockNum)
return rl.RetrieveBlockByNumber(blockNum)
}
5 changes: 5 additions & 0 deletions core/peer/deliverevents_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,11 @@ func (m *mockReader) Height() uint64 {
return args.Get(0).(uint64)
}

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

// mockChainSupport
type mockChainSupport struct {
mock.Mock
Expand Down
4 changes: 4 additions & 0 deletions core/peer/peer.go
Original file line number Diff line number Diff line change
Expand Up @@ -515,3 +515,7 @@ func (p *Peer) Initialize(
p.initChannel(cid)
}
}

func (flbs fileLedgerBlockStore) RetrieveBlockByNumber(blockNum uint64) (*common.Block, error) {
return flbs.GetBlockByNumber(blockNum)
}
100 changes: 91 additions & 9 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.

Loading

0 comments on commit 8851da3

Please sign in to comment.