Skip to content

Commit 96cd9a7

Browse files
committed
[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: denyeart <[email protected]>
1 parent 6a72aac commit 96cd9a7

File tree

6 files changed

+32
-7
lines changed

6 files changed

+32
-7
lines changed

common/ledger/blkstorage/blockstorage.go

+1
Original file line numberDiff line numberDiff line change
@@ -65,5 +65,6 @@ type BlockStore interface {
6565
RetrieveBlockByHash(blockHash []byte) (*common.Block, error)
6666
RetrieveBlockByNumber(blockNum uint64) (*common.Block, error) // blockNum of math.MaxUint64 will return last block
6767
RetrieveTxByID(txID string) (*common.Envelope, error)
68+
RetrieveTxByBlockNumTranNum(blockNum uint64, tranNum uint64) (*common.Envelope, error)
6869
Shutdown()
6970
}

common/ledger/blkstorage/fsblkstorage/blockfile_mgr.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -448,9 +448,9 @@ func (mgr *blockfileMgr) retrieveTransactionByID(txID string) (*common.Envelope,
448448
return mgr.fetchTransactionEnvelope(loc)
449449
}
450450

451-
func (mgr *blockfileMgr) retrieveTransactionForBlockNumTranNum(blockNum uint64, tranNum uint64) (*common.Envelope, error) {
452-
logger.Debugf("retrieveTransactionForBlockNumTranNum() - blockNum = [%d], tranNum = [%d]", blockNum, tranNum)
453-
loc, err := mgr.index.getTXLocForBlockNumTranNum(blockNum, tranNum)
451+
func (mgr *blockfileMgr) retrieveTransactionByBlockNumTranNum(blockNum uint64, tranNum uint64) (*common.Envelope, error) {
452+
logger.Debugf("retrieveTransactionByBlockNumTranNum() - blockNum = [%d], tranNum = [%d]", blockNum, tranNum)
453+
loc, err := mgr.index.getTXLocByBlockNumTranNum(blockNum, tranNum)
454454
if err != nil {
455455
return nil, err
456456
}

common/ledger/blkstorage/fsblkstorage/blockfile_mgr_test.go

+19
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,25 @@ func TestBlockfileMgrGetTxById(t *testing.T) {
155155
}
156156
}
157157

158+
func TestBlockfileMgrGetTxByBlockNumTranNum(t *testing.T) {
159+
env := newTestEnv(t, NewConf(testPath, 0))
160+
defer env.Cleanup()
161+
blkfileMgrWrapper := newTestBlockfileWrapper(env, "testLedger")
162+
defer blkfileMgrWrapper.close()
163+
blocks := testutil.ConstructTestBlocks(t, 10)
164+
blkfileMgrWrapper.addBlocks(blocks)
165+
for blockIndex, blk := range blocks {
166+
for tranIndex, txEnvelopeBytes := range blk.Data.Data {
167+
// blockNum starts with 1, tranNum starts with 1
168+
txEnvelopeFromFileMgr, err := blkfileMgrWrapper.blockfileMgr.retrieveTransactionByBlockNumTranNum(uint64(blockIndex+1), uint64(tranIndex+1))
169+
testutil.AssertNoError(t, err, "Error while retrieving tx from blkfileMgr")
170+
txEnvelope, err := putil.GetEnvelopeFromBlock(txEnvelopeBytes)
171+
testutil.AssertNoError(t, err, "Error while unmarshalling tx")
172+
testutil.AssertEquals(t, txEnvelopeFromFileMgr, txEnvelope)
173+
}
174+
}
175+
}
176+
158177
func TestBlockfileMgrRestart(t *testing.T) {
159178
env := newTestEnv(t, NewConf(testPath, 0))
160179
defer env.Cleanup()

common/ledger/blkstorage/fsblkstorage/blockindex.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ type index interface {
4141
getBlockLocByHash(blockHash []byte) (*fileLocPointer, error)
4242
getBlockLocByBlockNum(blockNum uint64) (*fileLocPointer, error)
4343
getTxLoc(txID string) (*fileLocPointer, error)
44-
getTXLocForBlockNumTranNum(blockNum uint64, tranNum uint64) (*fileLocPointer, error)
44+
getTXLocByBlockNumTranNum(blockNum uint64, tranNum uint64) (*fileLocPointer, error)
4545
}
4646

4747
type blockIdxInfo struct {
@@ -181,7 +181,7 @@ func (index *blockIndex) getTxLoc(txID string) (*fileLocPointer, error) {
181181
return txFLP, nil
182182
}
183183

184-
func (index *blockIndex) getTXLocForBlockNumTranNum(blockNum uint64, tranNum uint64) (*fileLocPointer, error) {
184+
func (index *blockIndex) getTXLocByBlockNumTranNum(blockNum uint64, tranNum uint64) (*fileLocPointer, error) {
185185
if _, ok := index.indexItemsMap[blkstorage.IndexableAttrBlockNumTranNum]; !ok {
186186
return nil, blkstorage.ErrAttrNotIndexed
187187
}

common/ledger/blkstorage/fsblkstorage/blockindex_test.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ func (i *noopIndex) getBlockLocByBlockNum(blockNum uint64) (*fileLocPointer, err
4343
func (i *noopIndex) getTxLoc(txID string) (*fileLocPointer, error) {
4444
return nil, nil
4545
}
46-
func (i *noopIndex) getTXLocForBlockNumTranNum(blockNum uint64, tranNum uint64) (*fileLocPointer, error) {
46+
func (i *noopIndex) getTXLocByBlockNumTranNum(blockNum uint64, tranNum uint64) (*fileLocPointer, error) {
4747
return nil, nil
4848
}
4949

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

160160
//test 'retrieveTrasnactionsByBlockNumTranNum
161-
txEnvelope2, err := blockfileMgr.retrieveTransactionForBlockNumTranNum(1, 1)
161+
txEnvelope2, err := blockfileMgr.retrieveTransactionByBlockNumTranNum(1, 1)
162162
if testutil.Contains(indexItems, blkstorage.IndexableAttrBlockNumTranNum) {
163163
testutil.AssertNoError(t, err, "Error while retrieving tx by blockNum and tranNum")
164164
txEnvelopeBytes2 := blocks[0].Data.Data[0]

common/ledger/blkstorage/fsblkstorage/fs_blockstore.go

+5
Original file line numberDiff line numberDiff line change
@@ -72,6 +72,11 @@ func (store *fsBlockStore) RetrieveTxByID(txID string) (*common.Envelope, error)
7272
return store.fileMgr.retrieveTransactionByID(txID)
7373
}
7474

75+
// RetrieveTxByID returns a transaction for given transaction id
76+
func (store *fsBlockStore) RetrieveTxByBlockNumTranNum(blockNum uint64, tranNum uint64) (*common.Envelope, error) {
77+
return store.fileMgr.retrieveTransactionByBlockNumTranNum(blockNum, tranNum)
78+
}
79+
7580
// Shutdown shuts down the block store
7681
func (store *fsBlockStore) Shutdown() {
7782
logger.Debugf("closing fs blockStore:%s", store.id)

0 commit comments

Comments
 (0)