Skip to content

Commit 47053cd

Browse files
committed
Add GetTxID function to Stub interface (FAB-306)
The name of the field UUID was also changed to TxID. Change-Id: Iedeed9af13a99671a756c32944a8c6814d4b2b20 Signed-off-by: Gabor Hosszu <[email protected]>
1 parent 0f959c0 commit 47053cd

File tree

3 files changed

+28
-17
lines changed

3 files changed

+28
-17
lines changed

core/chaincode/shim/chaincode.go

+14-10
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ var handler *Handler
5050
// ChaincodeStub is an object passed to chaincode for shim side handling of
5151
// APIs.
5252
type ChaincodeStub struct {
53-
UUID string
53+
TxID string
5454
securityContext *pb.ChaincodeSecurityContext
5555
chaincodeEvent *pb.ChaincodeEvent
5656
args [][]byte
@@ -234,8 +234,8 @@ func chatWithPeer(chaincodename string, stream PeerChaincodeStream, cc Chaincode
234234
// -- init stub ---
235235
// ChaincodeInvocation functionality
236236

237-
func (stub *ChaincodeStub) init(uuid string, secContext *pb.ChaincodeSecurityContext) {
238-
stub.UUID = uuid
237+
func (stub *ChaincodeStub) init(txid string, secContext *pb.ChaincodeSecurityContext) {
238+
stub.TxID = txid
239239
stub.securityContext = secContext
240240
stub.args = [][]byte{}
241241
newCI := pb.ChaincodeInput{}
@@ -247,6 +247,10 @@ func (stub *ChaincodeStub) init(uuid string, secContext *pb.ChaincodeSecurityCon
247247
}
248248
}
249249

250+
func (stub *ChaincodeStub) GetTxID() string {
251+
return stub.TxID
252+
}
253+
250254
// --------- Security functions ----------
251255
//CHAINCODE SEC INTERFACE FUNCS TOBE IMPLEMENTED BY ANGELO
252256

@@ -256,31 +260,31 @@ func (stub *ChaincodeStub) init(uuid string, secContext *pb.ChaincodeSecurityCon
256260
// same transaction context; that is, chaincode calling chaincode doesn't
257261
// create a new transaction message.
258262
func (stub *ChaincodeStub) InvokeChaincode(chaincodeName string, args [][]byte) ([]byte, error) {
259-
return handler.handleInvokeChaincode(chaincodeName, args, stub.UUID)
263+
return handler.handleInvokeChaincode(chaincodeName, args, stub.TxID)
260264
}
261265

262266
// QueryChaincode locally calls the specified chaincode `Query` using the
263267
// same transaction context; that is, chaincode calling chaincode doesn't
264268
// create a new transaction message.
265269
func (stub *ChaincodeStub) QueryChaincode(chaincodeName string, args [][]byte) ([]byte, error) {
266-
return handler.handleQueryChaincode(chaincodeName, args, stub.UUID)
270+
return handler.handleQueryChaincode(chaincodeName, args, stub.TxID)
267271
}
268272

269273
// --------- State functions ----------
270274

271275
// GetState returns the byte array value specified by the `key`.
272276
func (stub *ChaincodeStub) GetState(key string) ([]byte, error) {
273-
return handler.handleGetState(key, stub.UUID)
277+
return handler.handleGetState(key, stub.TxID)
274278
}
275279

276280
// PutState writes the specified `value` and `key` into the ledger.
277281
func (stub *ChaincodeStub) PutState(key string, value []byte) error {
278-
return handler.handlePutState(key, value, stub.UUID)
282+
return handler.handlePutState(key, value, stub.TxID)
279283
}
280284

281285
// DelState removes the specified `key` and its value from the ledger.
282286
func (stub *ChaincodeStub) DelState(key string) error {
283-
return handler.handleDelState(key, stub.UUID)
287+
return handler.handleDelState(key, stub.TxID)
284288
}
285289

286290
//ReadCertAttribute is used to read an specific attribute from the transaction certificate, *attributeName* is passed as input parameter to this function.
@@ -331,11 +335,11 @@ type StateRangeQueryIterator struct {
331335
// between the startKey and endKey, inclusive. The order in which keys are
332336
// returned by the iterator is random.
333337
func (stub *ChaincodeStub) RangeQueryState(startKey, endKey string) (StateRangeQueryIteratorInterface, error) {
334-
response, err := handler.handleRangeQueryState(startKey, endKey, stub.UUID)
338+
response, err := handler.handleRangeQueryState(startKey, endKey, stub.TxID)
335339
if err != nil {
336340
return nil, err
337341
}
338-
return &StateRangeQueryIterator{handler, stub.UUID, response, 0}, nil
342+
return &StateRangeQueryIterator{handler, stub.TxID, response, 0}, nil
339343
}
340344

341345
// HasNext returns true if the range query iterator contains additional keys

core/chaincode/shim/interfaces.go

+3
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,9 @@ type ChaincodeStubInterface interface {
4747
// Get the arguments to the stub call as a string array
4848
GetStringArgs() []string
4949

50+
// Get the transaction ID
51+
GetTxID() string
52+
5053
// InvokeChaincode locally calls the specified chaincode `Invoke` using the
5154
// same transaction context; that is, chaincode calling chaincode doesn't
5255
// create a new transaction message.

core/chaincode/shim/mockstub.go

+11-7
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,12 @@ type MockStub struct {
5555
Invokables map[string]*MockStub
5656

5757
// stores a transaction uuid while being Invoked / Deployed
58-
// TODO if a chaincode uses recursion this may need to be a stack of UUIDs or possibly a reference counting map
59-
Uuid string
58+
// TODO if a chaincode uses recursion this may need to be a stack of TxIDs or possibly a reference counting map
59+
TxID string
60+
}
61+
62+
func (stub *MockStub) GetTxID() string {
63+
return stub.TxID
6064
}
6165

6266
func (stub *MockStub) GetArgs() [][]byte {
@@ -75,13 +79,13 @@ func (stub *MockStub) GetStringArgs() []string {
7579
// Used to indicate to a chaincode that it is part of a transaction.
7680
// This is important when chaincodes invoke each other.
7781
// MockStub doesn't support concurrent transactions at present.
78-
func (stub *MockStub) MockTransactionStart(uuid string) {
79-
stub.Uuid = uuid
82+
func (stub *MockStub) MockTransactionStart(txid string) {
83+
stub.TxID = txid
8084
}
8185

8286
// End a mocked transaction, clearing the UUID.
8387
func (stub *MockStub) MockTransactionEnd(uuid string) {
84-
stub.Uuid = ""
88+
stub.TxID = ""
8589
}
8690

8791
// Register a peer chaincode with this MockStub
@@ -126,7 +130,7 @@ func (stub *MockStub) GetState(key string) ([]byte, error) {
126130

127131
// PutState writes the specified `value` and `key` into the ledger.
128132
func (stub *MockStub) PutState(key string, value []byte) error {
129-
if stub.Uuid == "" {
133+
if stub.TxID == "" {
130134
mockLogger.Error("Cannot PutState without a transactions - call stub.MockTransactionStart()?")
131135
return errors.New("Cannot PutState without a transactions - call stub.MockTransactionStart()?")
132136
}
@@ -236,7 +240,7 @@ func (stub *MockStub) InvokeChaincode(chaincodeName string, args [][]byte) ([]by
236240
otherStub := stub.Invokables[chaincodeName]
237241
mockLogger.Debug("MockStub", stub.Name, "Invoking peer chaincode", otherStub.Name, args)
238242
// function, strings := getFuncArgs(args)
239-
bytes, err := otherStub.MockInvoke(stub.Uuid, function, params)
243+
bytes, err := otherStub.MockInvoke(stub.TxID, function, params)
240244
mockLogger.Debug("MockStub", stub.Name, "Invoked peer chaincode", otherStub.Name, "got", bytes, err)
241245
return bytes, err
242246
}

0 commit comments

Comments
 (0)