Skip to content

Commit 9ca80f1

Browse files
committed
[FAB-1885] GetTransactionByID to return Tran Envelope
QSCC GetTransactionByID should return a Transaction Envelope, not a Transaction. The Envelope contains the signature and Payload, which contains the transaction header and Transaction. Clients will want the transaction header information when retrieving the transaction. They may also want the signature to verify that the transaction hasn't changed since the time they submitted it. The return type will change from Transaction proto to Envelope proto. Change-Id: I1de238035c3b6cd00abb3ed8506c54566ee0f2b0 Signed-off-by: denyeart <[email protected]>
1 parent 9a4181c commit 9ca80f1

File tree

8 files changed

+35
-43
lines changed

8 files changed

+35
-43
lines changed

core/ledger/blkstorage/blockstorage.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,6 @@ type BlockStore interface {
6666
RetrieveBlocks(startNum uint64) (ledger.ResultsIterator, error)
6767
RetrieveBlockByHash(blockHash []byte) (*common.Block, error)
6868
RetrieveBlockByNumber(blockNum uint64) (*common.Block, error) // blockNum of math.MaxUint64 will return last block
69-
RetrieveTxByID(txID string) (*pb.Transaction, error)
69+
RetrieveTxByID(txID string) (*common.Envelope, error)
7070
Shutdown()
7171
}

core/ledger/blkstorage/fsblkstorage/blockfile_mgr.go

+6-24
Original file line numberDiff line numberDiff line change
@@ -440,22 +440,22 @@ func (mgr *blockfileMgr) retrieveBlocks(startNum uint64) (*blocksItr, error) {
440440
return newBlockItr(mgr, startNum), nil
441441
}
442442

443-
func (mgr *blockfileMgr) retrieveTransactionByID(txID string) (*pb.Transaction, error) {
443+
func (mgr *blockfileMgr) retrieveTransactionByID(txID string) (*common.Envelope, error) {
444444
logger.Debugf("retrieveTransactionByID() - txId = [%s]", txID)
445445
loc, err := mgr.index.getTxLoc(txID)
446446
if err != nil {
447447
return nil, err
448448
}
449-
return mgr.fetchTransaction(loc)
449+
return mgr.fetchTransactionEnvelope(loc)
450450
}
451451

452-
func (mgr *blockfileMgr) retrieveTransactionForBlockNumTranNum(blockNum uint64, tranNum uint64) (*pb.Transaction, error) {
452+
func (mgr *blockfileMgr) retrieveTransactionForBlockNumTranNum(blockNum uint64, tranNum uint64) (*common.Envelope, error) {
453453
logger.Debugf("retrieveTransactionForBlockNumTranNum() - blockNum = [%d], tranNum = [%d]", blockNum, tranNum)
454454
loc, err := mgr.index.getTXLocForBlockNumTranNum(blockNum, tranNum)
455455
if err != nil {
456456
return nil, err
457457
}
458-
return mgr.fetchTransaction(loc)
458+
return mgr.fetchTransactionEnvelope(loc)
459459
}
460460

461461
func (mgr *blockfileMgr) fetchBlock(lp *fileLocPointer) (*common.Block, error) {
@@ -470,14 +470,14 @@ func (mgr *blockfileMgr) fetchBlock(lp *fileLocPointer) (*common.Block, error) {
470470
return block, nil
471471
}
472472

473-
func (mgr *blockfileMgr) fetchTransaction(lp *fileLocPointer) (*pb.Transaction, error) {
473+
func (mgr *blockfileMgr) fetchTransactionEnvelope(lp *fileLocPointer) (*common.Envelope, error) {
474474
var err error
475475
var txEnvelopeBytes []byte
476476
if txEnvelopeBytes, err = mgr.fetchRawBytes(lp); err != nil {
477477
return nil, err
478478
}
479479
_, n := proto.DecodeVarint(txEnvelopeBytes)
480-
return extractTransaction(txEnvelopeBytes[n:])
480+
return putil.GetEnvelopeFromBlock(txEnvelopeBytes[n:])
481481
}
482482

483483
func (mgr *blockfileMgr) fetchBlockBytes(lp *fileLocPointer) ([]byte, error) {
@@ -562,24 +562,6 @@ func scanForLastCompleteBlock(rootDir string, fileNum int, startingOffset int64)
562562
return blockStream.currentOffset, numBlocks, errRead
563563
}
564564

565-
func extractTransaction(txEnvelopeBytes []byte) (*pb.Transaction, error) {
566-
var err error
567-
var txEnvelope *common.Envelope
568-
var txPayload *common.Payload
569-
var tx *pb.Transaction
570-
571-
if txEnvelope, err = putil.GetEnvelopeFromBlock(txEnvelopeBytes); err != nil {
572-
return nil, err
573-
}
574-
if txPayload, err = putil.GetPayload(txEnvelope); err != nil {
575-
return nil, err
576-
}
577-
if tx, err = putil.GetTransaction(txPayload.Data); err != nil {
578-
return nil, err
579-
}
580-
return tx, nil
581-
}
582-
583565
// checkpointInfo
584566
type checkpointInfo struct {
585567
latestFileChunkSuffixNum int

core/ledger/blkstorage/fsblkstorage/blockfile_mgr_test.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import (
2525

2626
"github.com/hyperledger/fabric/protos/common"
2727
pb "github.com/hyperledger/fabric/protos/peer"
28+
putil "github.com/hyperledger/fabric/protos/utils"
2829
)
2930

3031
func TestBlockfileMgrBlockReadWrite(t *testing.T) {
@@ -146,11 +147,11 @@ func TestBlockfileMgrGetTxById(t *testing.T) {
146147
// blockNum starts with 1
147148
txID, err := extractTxID(blk.Data.Data[j])
148149
testutil.AssertNoError(t, err, "")
149-
txFromFileMgr, err := blkfileMgrWrapper.blockfileMgr.retrieveTransactionByID(txID)
150+
txEnvelopeFromFileMgr, err := blkfileMgrWrapper.blockfileMgr.retrieveTransactionByID(txID)
150151
testutil.AssertNoError(t, err, "Error while retrieving tx from blkfileMgr")
151-
tx, err := extractTransaction(txEnvelopeBytes)
152+
txEnvelope, err := putil.GetEnvelopeFromBlock(txEnvelopeBytes)
152153
testutil.AssertNoError(t, err, "Error while unmarshalling tx")
153-
testutil.AssertEquals(t, txFromFileMgr, tx)
154+
testutil.AssertEquals(t, txEnvelopeFromFileMgr, txEnvelope)
154155
}
155156
}
156157
}

core/ledger/blkstorage/fsblkstorage/blockindex_test.go

+9-6
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ import (
2222

2323
"github.com/hyperledger/fabric/core/ledger/blkstorage"
2424
"github.com/hyperledger/fabric/core/ledger/testutil"
25+
putil "github.com/hyperledger/fabric/protos/utils"
2526
)
2627

2728
type noopIndex struct {
@@ -145,23 +146,25 @@ func testBlockIndexSelectiveIndexing(t *testing.T, indexItems []blkstorage.Index
145146
// test 'retrieveTransactionByID'
146147
txid, err := extractTxID(blocks[0].Data.Data[0])
147148
testutil.AssertNoError(t, err, "")
148-
tx, err := blockfileMgr.retrieveTransactionByID(txid)
149+
txEnvelope, err := blockfileMgr.retrieveTransactionByID(txid)
149150
if testutil.Contains(indexItems, blkstorage.IndexableAttrTxID) {
150151
testutil.AssertNoError(t, err, "Error while retrieving tx by id")
151-
txOrig, err := extractTransaction(blocks[0].Data.Data[0])
152+
txEnvelopeBytes := blocks[0].Data.Data[0]
153+
txEnvelopeOrig, err := putil.GetEnvelopeFromBlock(txEnvelopeBytes)
152154
testutil.AssertNoError(t, err, "")
153-
testutil.AssertEquals(t, tx, txOrig)
155+
testutil.AssertEquals(t, txEnvelope, txEnvelopeOrig)
154156
} else {
155157
testutil.AssertSame(t, err, blkstorage.ErrAttrNotIndexed)
156158
}
157159

158160
//test 'retrieveTrasnactionsByBlockNumTranNum
159-
tx2, err := blockfileMgr.retrieveTransactionForBlockNumTranNum(1, 1)
161+
txEnvelope2, err := blockfileMgr.retrieveTransactionForBlockNumTranNum(1, 1)
160162
if testutil.Contains(indexItems, blkstorage.IndexableAttrBlockNumTranNum) {
161163
testutil.AssertNoError(t, err, "Error while retrieving tx by blockNum and tranNum")
162-
txOrig2, err2 := extractTransaction(blocks[0].Data.Data[0])
164+
txEnvelopeBytes2 := blocks[0].Data.Data[0]
165+
txEnvelopeOrig2, err2 := putil.GetEnvelopeFromBlock(txEnvelopeBytes2)
163166
testutil.AssertNoError(t, err2, "")
164-
testutil.AssertEquals(t, tx2, txOrig2)
167+
testutil.AssertEquals(t, txEnvelope2, txEnvelopeOrig2)
165168
} else {
166169
testutil.AssertSame(t, err, blkstorage.ErrAttrNotIndexed)
167170
}

core/ledger/blkstorage/fsblkstorage/fs_blockstore.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ func (store *fsBlockStore) RetrieveBlockByNumber(blockNum uint64) (*common.Block
6969
}
7070

7171
// RetrieveTxByID returns a transaction for given transaction id
72-
func (store *fsBlockStore) RetrieveTxByID(txID string) (*pb.Transaction, error) {
72+
func (store *fsBlockStore) RetrieveTxByID(txID string) (*common.Envelope, error) {
7373
return store.fileMgr.retrieveTransactionByID(txID)
7474
}
7575

core/ledger/kvledger/kv_ledger.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ func recommitLostBlocks(l *kvLedger, savepoint uint64, blockHeight uint64, recov
210210
}
211211

212212
// GetTransactionByID retrieves a transaction by id
213-
func (l *kvLedger) GetTransactionByID(txID string) (*pb.Transaction, error) {
213+
func (l *kvLedger) GetTransactionByID(txID string) (*common.Envelope, error) {
214214
return l.blockStore.RetrieveTxByID(txID)
215215
}
216216

core/ledger/ledger_interface.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ type OrdererLedgerProvider interface {
7676
type PeerLedger interface {
7777
Ledger
7878
// GetTransactionByID retrieves a transaction by id
79-
GetTransactionByID(txID string) (*pb.Transaction, error)
79+
GetTransactionByID(txID string) (*common.Envelope, error)
8080
// GetBlockByHash returns a block given it's hash
8181
GetBlockByHash(blockHash []byte) (*common.Block, error)
8282
// NewTxSimulator gives handle to a transaction simulator.
@@ -173,9 +173,8 @@ type KV struct {
173173

174174
// KeyModification - QueryResult for History.
175175
type KeyModification struct {
176-
TxID string
177-
Value []byte
178-
Transaction *pb.Transaction
176+
TxID string
177+
Value []byte
179178
}
180179

181180
// QueryRecord - Result structure for query records. Holds a namespace, key and record.

core/scc/qscc/querier.go

+10-3
Original file line numberDiff line numberDiff line change
@@ -192,13 +192,14 @@ func getTransactionByID(vledger ledger.PeerLedger, tid []byte) pb.Response {
192192
if tid == nil {
193193
return shim.Error("Transaction ID must not be nil.")
194194
}
195-
tx, err := vledger.GetTransactionByID(string(tid))
195+
txEnvelope, err := vledger.GetTransactionByID(string(tid))
196196
if err != nil {
197197
return shim.Error(fmt.Sprintf("Failed to get transaction with id %s, error %s", string(tid), err))
198198
}
199-
// TODO: tx is *pb.Transaction, what should we return?
199+
// TODO In the returned transaction, need to replace binary simulation results with a proto
200+
// structure including write set, so that clients know what this transaction wrote
200201

201-
bytes, err := utils.Marshal(tx)
202+
bytes, err := utils.Marshal(txEnvelope)
202203
if err != nil {
203204
return shim.Error(err.Error())
204205
}
@@ -219,6 +220,9 @@ func getBlockByNumber(vledger ledger.PeerLedger, number []byte) pb.Response {
219220
return shim.Error(fmt.Sprintf("Failed to get block number %d, error %s", bnum, err))
220221
}
221222
// TODO: consider trim block content before returning
223+
// Specifically, trim transaction 'data' out of the transaction array Payloads
224+
// This will preserve the transaction Payload header,
225+
// and client can do GetTransactionByID() if they want the full transaction details
222226

223227
bytes, err := utils.Marshal(block)
224228
if err != nil {
@@ -237,6 +241,9 @@ func getBlockByHash(vledger ledger.PeerLedger, hash []byte) pb.Response {
237241
return shim.Error(fmt.Sprintf("Failed to get block hash %s, error %s", string(hash), err))
238242
}
239243
// TODO: consider trim block content before returning
244+
// Specifically, trim transaction 'data' out of the transaction array Payloads
245+
// This will preserve the transaction Payload header,
246+
// and client can do GetTransactionByID() if they want the full transaction details
240247

241248
bytes, err := utils.Marshal(block)
242249
if err != nil {

0 commit comments

Comments
 (0)