Skip to content

Commit

Permalink
[FAB-2009] Add RetrieveTxByBlockNumTranNum
Browse files Browse the repository at this point in the history
History database returns the list of (blockNum,tranNum) that
updated a key.
RetrieveTxByBlockNumTranNum will allow kvledger
to retrieve transaction history for this list of key updates.

Change-Id: I9ff3ed6c273c57b65223b283c4f602910e5982bf
Signed-off-by: denyeart <enyeart@us.ibm.com>
  • Loading branch information
denyeart committed Feb 4, 2017
1 parent 6a72aac commit 96cd9a7
Show file tree
Hide file tree
Showing 6 changed files with 32 additions and 7 deletions.
1 change: 1 addition & 0 deletions common/ledger/blkstorage/blockstorage.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,6 @@ type BlockStore interface {
RetrieveBlockByHash(blockHash []byte) (*common.Block, error)
RetrieveBlockByNumber(blockNum uint64) (*common.Block, error) // blockNum of math.MaxUint64 will return last block
RetrieveTxByID(txID string) (*common.Envelope, error)
RetrieveTxByBlockNumTranNum(blockNum uint64, tranNum uint64) (*common.Envelope, error)
Shutdown()
}
6 changes: 3 additions & 3 deletions common/ledger/blkstorage/fsblkstorage/blockfile_mgr.go
Original file line number Diff line number Diff line change
Expand Up @@ -448,9 +448,9 @@ func (mgr *blockfileMgr) retrieveTransactionByID(txID string) (*common.Envelope,
return mgr.fetchTransactionEnvelope(loc)
}

func (mgr *blockfileMgr) retrieveTransactionForBlockNumTranNum(blockNum uint64, tranNum uint64) (*common.Envelope, error) {
logger.Debugf("retrieveTransactionForBlockNumTranNum() - blockNum = [%d], tranNum = [%d]", blockNum, tranNum)
loc, err := mgr.index.getTXLocForBlockNumTranNum(blockNum, tranNum)
func (mgr *blockfileMgr) retrieveTransactionByBlockNumTranNum(blockNum uint64, tranNum uint64) (*common.Envelope, error) {
logger.Debugf("retrieveTransactionByBlockNumTranNum() - blockNum = [%d], tranNum = [%d]", blockNum, tranNum)
loc, err := mgr.index.getTXLocByBlockNumTranNum(blockNum, tranNum)
if err != nil {
return nil, err
}
Expand Down
19 changes: 19 additions & 0 deletions common/ledger/blkstorage/fsblkstorage/blockfile_mgr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,25 @@ func TestBlockfileMgrGetTxById(t *testing.T) {
}
}

func TestBlockfileMgrGetTxByBlockNumTranNum(t *testing.T) {
env := newTestEnv(t, NewConf(testPath, 0))
defer env.Cleanup()
blkfileMgrWrapper := newTestBlockfileWrapper(env, "testLedger")
defer blkfileMgrWrapper.close()
blocks := testutil.ConstructTestBlocks(t, 10)
blkfileMgrWrapper.addBlocks(blocks)
for blockIndex, blk := range blocks {
for tranIndex, txEnvelopeBytes := range blk.Data.Data {
// blockNum starts with 1, tranNum starts with 1
txEnvelopeFromFileMgr, err := blkfileMgrWrapper.blockfileMgr.retrieveTransactionByBlockNumTranNum(uint64(blockIndex+1), uint64(tranIndex+1))
testutil.AssertNoError(t, err, "Error while retrieving tx from blkfileMgr")
txEnvelope, err := putil.GetEnvelopeFromBlock(txEnvelopeBytes)
testutil.AssertNoError(t, err, "Error while unmarshalling tx")
testutil.AssertEquals(t, txEnvelopeFromFileMgr, txEnvelope)
}
}
}

func TestBlockfileMgrRestart(t *testing.T) {
env := newTestEnv(t, NewConf(testPath, 0))
defer env.Cleanup()
Expand Down
4 changes: 2 additions & 2 deletions common/ledger/blkstorage/fsblkstorage/blockindex.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type index interface {
getBlockLocByHash(blockHash []byte) (*fileLocPointer, error)
getBlockLocByBlockNum(blockNum uint64) (*fileLocPointer, error)
getTxLoc(txID string) (*fileLocPointer, error)
getTXLocForBlockNumTranNum(blockNum uint64, tranNum uint64) (*fileLocPointer, error)
getTXLocByBlockNumTranNum(blockNum uint64, tranNum uint64) (*fileLocPointer, error)
}

type blockIdxInfo struct {
Expand Down Expand Up @@ -181,7 +181,7 @@ func (index *blockIndex) getTxLoc(txID string) (*fileLocPointer, error) {
return txFLP, nil
}

func (index *blockIndex) getTXLocForBlockNumTranNum(blockNum uint64, tranNum uint64) (*fileLocPointer, error) {
func (index *blockIndex) getTXLocByBlockNumTranNum(blockNum uint64, tranNum uint64) (*fileLocPointer, error) {
if _, ok := index.indexItemsMap[blkstorage.IndexableAttrBlockNumTranNum]; !ok {
return nil, blkstorage.ErrAttrNotIndexed
}
Expand Down
4 changes: 2 additions & 2 deletions common/ledger/blkstorage/fsblkstorage/blockindex_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ func (i *noopIndex) getBlockLocByBlockNum(blockNum uint64) (*fileLocPointer, err
func (i *noopIndex) getTxLoc(txID string) (*fileLocPointer, error) {
return nil, nil
}
func (i *noopIndex) getTXLocForBlockNumTranNum(blockNum uint64, tranNum uint64) (*fileLocPointer, error) {
func (i *noopIndex) getTXLocByBlockNumTranNum(blockNum uint64, tranNum uint64) (*fileLocPointer, error) {
return nil, nil
}

Expand Down Expand Up @@ -158,7 +158,7 @@ func testBlockIndexSelectiveIndexing(t *testing.T, indexItems []blkstorage.Index
}

//test 'retrieveTrasnactionsByBlockNumTranNum
txEnvelope2, err := blockfileMgr.retrieveTransactionForBlockNumTranNum(1, 1)
txEnvelope2, err := blockfileMgr.retrieveTransactionByBlockNumTranNum(1, 1)
if testutil.Contains(indexItems, blkstorage.IndexableAttrBlockNumTranNum) {
testutil.AssertNoError(t, err, "Error while retrieving tx by blockNum and tranNum")
txEnvelopeBytes2 := blocks[0].Data.Data[0]
Expand Down
5 changes: 5 additions & 0 deletions common/ledger/blkstorage/fsblkstorage/fs_blockstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ func (store *fsBlockStore) RetrieveTxByID(txID string) (*common.Envelope, error)
return store.fileMgr.retrieveTransactionByID(txID)
}

// RetrieveTxByID returns a transaction for given transaction id
func (store *fsBlockStore) RetrieveTxByBlockNumTranNum(blockNum uint64, tranNum uint64) (*common.Envelope, error) {
return store.fileMgr.retrieveTransactionByBlockNumTranNum(blockNum, tranNum)
}

// Shutdown shuts down the block store
func (store *fsBlockStore) Shutdown() {
logger.Debugf("closing fs blockStore:%s", store.id)
Expand Down

0 comments on commit 96cd9a7

Please sign in to comment.