From 9662335d9a54d050f90341b5c17df5ecf0e0f9b1 Mon Sep 17 00:00:00 2001 From: denyeart Date: Sat, 10 Dec 2016 11:42:16 -0500 Subject: [PATCH] Ledger API to retrieve last block Passing math.MaxUint64 to GetBlockByNumber() will return the last block. Change-Id: I110271ee6159544f7da216d2337271ea2aac23ff Signed-off-by: denyeart --- core/ledger/blkstorage/blockstorage.go | 2 +- core/ledger/blkstorage/fsblkstorage/blockfile_mgr.go | 7 +++++++ core/ledger/blkstorage/fsblkstorage/pkg_test.go | 6 ++++++ core/ledger/kvledger/kv_ledger.go | 1 + core/ledger/ledger_interface.go | 3 ++- 5 files changed, 17 insertions(+), 2 deletions(-) diff --git a/core/ledger/blkstorage/blockstorage.go b/core/ledger/blkstorage/blockstorage.go index 12b88813763..458826f591c 100644 --- a/core/ledger/blkstorage/blockstorage.go +++ b/core/ledger/blkstorage/blockstorage.go @@ -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() } diff --git a/core/ledger/blkstorage/fsblkstorage/blockfile_mgr.go b/core/ledger/blkstorage/fsblkstorage/blockfile_mgr.go index fba44999a06..424ee49bf6e 100644 --- a/core/ledger/blkstorage/fsblkstorage/blockfile_mgr.go +++ b/core/ledger/blkstorage/fsblkstorage/blockfile_mgr.go @@ -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 diff --git a/core/ledger/blkstorage/fsblkstorage/pkg_test.go b/core/ledger/blkstorage/fsblkstorage/pkg_test.go index bd18402ce28..013e8f6a5b4 100644 --- a/core/ledger/blkstorage/fsblkstorage/pkg_test.go +++ b/core/ledger/blkstorage/fsblkstorage/pkg_test.go @@ -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() { diff --git a/core/ledger/kvledger/kv_ledger.go b/core/ledger/kvledger/kv_ledger.go index 8e22f0c4aab..0069e0eab20 100644 --- a/core/ledger/kvledger/kv_ledger.go +++ b/core/ledger/kvledger/kv_ledger.go @@ -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) diff --git a/core/ledger/ledger_interface.go b/core/ledger/ledger_interface.go index 34c705cdadc..5addb391a77 100644 --- a/core/ledger/ledger_interface.go +++ b/core/ledger/ledger_interface.go @@ -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