Commit 96cd9a73 authored by denyeart's avatar denyeart
Browse files

[FAB-2009] Add RetrieveTxByBlockNumTranNum



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: default avatardenyeart <enyeart@us.ibm.com>
parent 6a72aac0
......@@ -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()
}
......@@ -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
}
......
......@@ -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()
......
......@@ -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 {
......@@ -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
}
......
......@@ -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
}
......@@ -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]
......
......@@ -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)
......
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