Skip to content

Commit

Permalink
Added RetrieveBlockByNumber into blockledger (#2635)
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Param-S authored Jun 7, 2021
1 parent 1ad4422 commit 85ae90c
Show file tree
Hide file tree
Showing 11 changed files with 460 additions and 40 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 @@ -29,6 +29,7 @@ type FileLedgerBlockStore interface {
GetBlockchainInfo() (*cb.BlockchainInfo, error)
RetrieveBlocks(startBlockNumber uint64) (ledger.ResultsIterator, error)
Shutdown()
RetrieveBlockByNumber(blockNum uint64) (*cb.Block, error)
}

// NewFileLedger creates a new FileLedger for interaction with the ledger
Expand Down Expand Up @@ -93,6 +94,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 @@ -117,3 +119,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)
}
103 changes: 93 additions & 10 deletions common/ledger/blockledger/fileledger/mock/file_ledger_block_store.go

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

2 changes: 2 additions & 0 deletions common/ledger/blockledger/ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,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 @@ -95,6 +95,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 @@ -513,3 +513,7 @@ func (p *Peer) Initialize(
p.initChannel(cid)
}
}

func (flbs fileLedgerBlockStore) RetrieveBlockByNumber(blockNum uint64) (*common.Block, error) {
return flbs.GetBlockByNumber(blockNum)
}
Loading

0 comments on commit 85ae90c

Please sign in to comment.