Skip to content

Commit 9662335

Browse files
committed
Ledger API to retrieve last block
Passing math.MaxUint64 to GetBlockByNumber() will return the last block. Change-Id: I110271ee6159544f7da216d2337271ea2aac23ff Signed-off-by: denyeart <[email protected]>
1 parent dae26f8 commit 9662335

File tree

5 files changed

+17
-2
lines changed

5 files changed

+17
-2
lines changed

core/ledger/blkstorage/blockstorage.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ type BlockStore interface {
5555
GetBlockchainInfo() (*pb.BlockchainInfo, error)
5656
RetrieveBlocks(startNum uint64) (ledger.ResultsIterator, error)
5757
RetrieveBlockByHash(blockHash []byte) (*common.Block, error)
58-
RetrieveBlockByNumber(blockNum uint64) (*common.Block, error)
58+
RetrieveBlockByNumber(blockNum uint64) (*common.Block, error) // blockNum of math.MaxUint64 will return last block
5959
RetrieveTxByID(txID string) (*pb.Transaction, error)
6060
Shutdown()
6161
}

core/ledger/blkstorage/fsblkstorage/blockfile_mgr.go

+7
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package fsblkstorage
1818

1919
import (
2020
"fmt"
21+
"math"
2122
"sync"
2223
"sync/atomic"
2324

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

413414
func (mgr *blockfileMgr) retrieveBlockByNumber(blockNum uint64) (*common.Block, error) {
414415
logger.Debugf("retrieveBlockByNumber() - blockNum = [%d]", blockNum)
416+
417+
// interpret math.MaxUint64 as a request for last block
418+
if blockNum == math.MaxUint64 {
419+
blockNum = mgr.getBlockchainInfo().Height
420+
}
421+
415422
loc, err := mgr.index.getBlockLocByBlockNum(blockNum)
416423
if err != nil {
417424
return nil, err

core/ledger/blkstorage/fsblkstorage/pkg_test.go

+6
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ package fsblkstorage
1818

1919
import (
2020
"fmt"
21+
"math"
2122
"os"
2223
"testing"
2324

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

8793
func (w *testBlockfileMgrWrapper) close() {

core/ledger/kvledger/kv_ledger.go

+1
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ func (l *KVLedger) GetBlockchainInfo() (*pb.BlockchainInfo, error) {
109109
}
110110

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

core/ledger/ledger_interface.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ import (
2525
type Ledger interface {
2626
// GetBlockchainInfo returns basic info about blockchain
2727
GetBlockchainInfo() (*pb.BlockchainInfo, error)
28-
// GetBlockchainInfo returns block at a given height
28+
// GetBlockByNumber returns block at a given height
29+
// blockNumber of math.MaxUint64 will return last block
2930
GetBlockByNumber(blockNumber uint64) (*common.Block, error)
3031
// GetBlocksIterator returns an iterator that starts from `startBlockNumber`(inclusive).
3132
// The iterator is a blocking iterator i.e., it blocks till the next block gets available in the ledger

0 commit comments

Comments
 (0)