Commit 9662335d authored by denyeart's avatar denyeart
Browse files

Ledger API to retrieve last block



Passing math.MaxUint64 to GetBlockByNumber()
will return the last block.

Change-Id: I110271ee6159544f7da216d2337271ea2aac23ff
Signed-off-by: default avatardenyeart <enyeart@us.ibm.com>
parent dae26f8b
......@@ -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()
}
......@@ -18,6 +18,7 @@ package fsblkstorage
import (
"fmt"
"math"
"sync"
"sync/atomic"
......@@ -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
......
......@@ -18,6 +18,7 @@ package fsblkstorage
import (
"fmt"
"math"
"os"
"testing"
......@@ -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() {
......
......@@ -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)
......
......@@ -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
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment