Skip to content

Commit

Permalink
Ledger API to retrieve last block
Browse files Browse the repository at this point in the history
Passing math.MaxUint64 to GetBlockByNumber()
will return the last block.

Change-Id: I110271ee6159544f7da216d2337271ea2aac23ff
Signed-off-by: denyeart <enyeart@us.ibm.com>
  • Loading branch information
denyeart committed Dec 10, 2016
1 parent dae26f8 commit 9662335
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 2 deletions.
2 changes: 1 addition & 1 deletion core/ledger/blkstorage/blockstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ type BlockStore interface {
GetBlockchainInfo() (*pb.BlockchainInfo, error)
RetrieveBlocks(startNum uint64) (ledger.ResultsIterator, error)
RetrieveBlockByHash(blockHash []byte) (*common.Block, error)
RetrieveBlockByNumber(blockNum uint64) (*common.Block, error)
RetrieveBlockByNumber(blockNum uint64) (*common.Block, error) // blockNum of math.MaxUint64 will return last block
RetrieveTxByID(txID string) (*pb.Transaction, error)
Shutdown()
}
7 changes: 7 additions & 0 deletions core/ledger/blkstorage/fsblkstorage/blockfile_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package fsblkstorage

import (
"fmt"
"math"
"sync"
"sync/atomic"

Expand Down Expand Up @@ -412,6 +413,12 @@ func (mgr *blockfileMgr) retrieveBlockByHash(blockHash []byte) (*common.Block, e

func (mgr *blockfileMgr) retrieveBlockByNumber(blockNum uint64) (*common.Block, error) {
logger.Debugf("retrieveBlockByNumber() - blockNum = [%d]", blockNum)

// interpret math.MaxUint64 as a request for last block
if blockNum == math.MaxUint64 {
blockNum = mgr.getBlockchainInfo().Height
}

loc, err := mgr.index.getBlockLocByBlockNum(blockNum)
if err != nil {
return nil, err
Expand Down
6 changes: 6 additions & 0 deletions core/ledger/blkstorage/fsblkstorage/pkg_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package fsblkstorage

import (
"fmt"
"math"
"os"
"testing"

Expand Down Expand Up @@ -82,6 +83,11 @@ func (w *testBlockfileMgrWrapper) testGetBlockByNumber(blocks []*common.Block, s
testutil.AssertNoError(w.t, err, fmt.Sprintf("Error while retrieving [%d]th block from blockfileMgr", i))
testutil.AssertEquals(w.t, b, blocks[i])
}
// test getting the last block
b, err := w.blockfileMgr.retrieveBlockByNumber(math.MaxUint64)
iLastBlock := len(blocks) - 1
testutil.AssertNoError(w.t, err, fmt.Sprintf("Error while retrieving last block from blockfileMgr"))
testutil.AssertEquals(w.t, b, blocks[iLastBlock])
}

func (w *testBlockfileMgrWrapper) close() {
Expand Down
1 change: 1 addition & 0 deletions core/ledger/kvledger/kv_ledger.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ func (l *KVLedger) GetBlockchainInfo() (*pb.BlockchainInfo, error) {
}

// GetBlockByNumber returns block at a given height
// blockNumber of math.MaxUint64 will return last block
func (l *KVLedger) GetBlockByNumber(blockNumber uint64) (*common.Block, error) {
return l.blockStore.RetrieveBlockByNumber(blockNumber)

Expand Down
3 changes: 2 additions & 1 deletion core/ledger/ledger_interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,8 @@ import (
type Ledger interface {
// GetBlockchainInfo returns basic info about blockchain
GetBlockchainInfo() (*pb.BlockchainInfo, error)
// GetBlockchainInfo returns block at a given height
// GetBlockByNumber returns block at a given height
// blockNumber of math.MaxUint64 will return last block
GetBlockByNumber(blockNumber uint64) (*common.Block, error)
// GetBlocksIterator returns an iterator that starts from `startBlockNumber`(inclusive).
// The iterator is a blocking iterator i.e., it blocks till the next block gets available in the ledger
Expand Down

0 comments on commit 9662335

Please sign in to comment.