From 47053cdcee147a2f04186008693667a5dd194350 Mon Sep 17 00:00:00 2001 From: Gabor Hosszu Date: Thu, 1 Sep 2016 10:53:28 +0200 Subject: [PATCH] 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 --- core/chaincode/shim/chaincode.go | 24 ++++++++++++++---------- core/chaincode/shim/interfaces.go | 3 +++ core/chaincode/shim/mockstub.go | 18 +++++++++++------- 3 files changed, 28 insertions(+), 17 deletions(-) diff --git a/core/chaincode/shim/chaincode.go b/core/chaincode/shim/chaincode.go index 12b2f42624e..1787e94871e 100644 --- a/core/chaincode/shim/chaincode.go +++ b/core/chaincode/shim/chaincode.go @@ -50,7 +50,7 @@ var handler *Handler // ChaincodeStub is an object passed to chaincode for shim side handling of // APIs. type ChaincodeStub struct { - UUID string + TxID string securityContext *pb.ChaincodeSecurityContext chaincodeEvent *pb.ChaincodeEvent args [][]byte @@ -234,8 +234,8 @@ func chatWithPeer(chaincodename string, stream PeerChaincodeStream, cc Chaincode // -- init stub --- // ChaincodeInvocation functionality -func (stub *ChaincodeStub) init(uuid string, secContext *pb.ChaincodeSecurityContext) { - stub.UUID = uuid +func (stub *ChaincodeStub) init(txid string, secContext *pb.ChaincodeSecurityContext) { + stub.TxID = txid stub.securityContext = secContext stub.args = [][]byte{} newCI := pb.ChaincodeInput{} @@ -247,6 +247,10 @@ func (stub *ChaincodeStub) init(uuid string, secContext *pb.ChaincodeSecurityCon } } +func (stub *ChaincodeStub) GetTxID() string { + return stub.TxID +} + // --------- Security functions ---------- //CHAINCODE SEC INTERFACE FUNCS TOBE IMPLEMENTED BY ANGELO @@ -256,31 +260,31 @@ func (stub *ChaincodeStub) init(uuid string, secContext *pb.ChaincodeSecurityCon // same transaction context; that is, chaincode calling chaincode doesn't // create a new transaction message. func (stub *ChaincodeStub) InvokeChaincode(chaincodeName string, args [][]byte) ([]byte, error) { - return handler.handleInvokeChaincode(chaincodeName, args, stub.UUID) + return handler.handleInvokeChaincode(chaincodeName, args, stub.TxID) } // QueryChaincode locally calls the specified chaincode `Query` using the // same transaction context; that is, chaincode calling chaincode doesn't // create a new transaction message. func (stub *ChaincodeStub) QueryChaincode(chaincodeName string, args [][]byte) ([]byte, error) { - return handler.handleQueryChaincode(chaincodeName, args, stub.UUID) + return handler.handleQueryChaincode(chaincodeName, args, stub.TxID) } // --------- State functions ---------- // GetState returns the byte array value specified by the `key`. func (stub *ChaincodeStub) GetState(key string) ([]byte, error) { - return handler.handleGetState(key, stub.UUID) + return handler.handleGetState(key, stub.TxID) } // PutState writes the specified `value` and `key` into the ledger. func (stub *ChaincodeStub) PutState(key string, value []byte) error { - return handler.handlePutState(key, value, stub.UUID) + return handler.handlePutState(key, value, stub.TxID) } // DelState removes the specified `key` and its value from the ledger. func (stub *ChaincodeStub) DelState(key string) error { - return handler.handleDelState(key, stub.UUID) + return handler.handleDelState(key, stub.TxID) } //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 { // between the startKey and endKey, inclusive. The order in which keys are // returned by the iterator is random. func (stub *ChaincodeStub) RangeQueryState(startKey, endKey string) (StateRangeQueryIteratorInterface, error) { - response, err := handler.handleRangeQueryState(startKey, endKey, stub.UUID) + response, err := handler.handleRangeQueryState(startKey, endKey, stub.TxID) if err != nil { return nil, err } - return &StateRangeQueryIterator{handler, stub.UUID, response, 0}, nil + return &StateRangeQueryIterator{handler, stub.TxID, response, 0}, nil } // HasNext returns true if the range query iterator contains additional keys diff --git a/core/chaincode/shim/interfaces.go b/core/chaincode/shim/interfaces.go index 1e07bab6e90..ce83417dea3 100644 --- a/core/chaincode/shim/interfaces.go +++ b/core/chaincode/shim/interfaces.go @@ -47,6 +47,9 @@ type ChaincodeStubInterface interface { // Get the arguments to the stub call as a string array GetStringArgs() []string + // Get the transaction ID + GetTxID() string + // InvokeChaincode locally calls the specified chaincode `Invoke` using the // same transaction context; that is, chaincode calling chaincode doesn't // create a new transaction message. diff --git a/core/chaincode/shim/mockstub.go b/core/chaincode/shim/mockstub.go index e5b8f40801b..a5845e23e01 100644 --- a/core/chaincode/shim/mockstub.go +++ b/core/chaincode/shim/mockstub.go @@ -55,8 +55,12 @@ type MockStub struct { Invokables map[string]*MockStub // stores a transaction uuid while being Invoked / Deployed - // TODO if a chaincode uses recursion this may need to be a stack of UUIDs or possibly a reference counting map - Uuid string + // TODO if a chaincode uses recursion this may need to be a stack of TxIDs or possibly a reference counting map + TxID string +} + +func (stub *MockStub) GetTxID() string { + return stub.TxID } func (stub *MockStub) GetArgs() [][]byte { @@ -75,13 +79,13 @@ func (stub *MockStub) GetStringArgs() []string { // Used to indicate to a chaincode that it is part of a transaction. // This is important when chaincodes invoke each other. // MockStub doesn't support concurrent transactions at present. -func (stub *MockStub) MockTransactionStart(uuid string) { - stub.Uuid = uuid +func (stub *MockStub) MockTransactionStart(txid string) { + stub.TxID = txid } // End a mocked transaction, clearing the UUID. func (stub *MockStub) MockTransactionEnd(uuid string) { - stub.Uuid = "" + stub.TxID = "" } // Register a peer chaincode with this MockStub @@ -126,7 +130,7 @@ func (stub *MockStub) GetState(key string) ([]byte, error) { // PutState writes the specified `value` and `key` into the ledger. func (stub *MockStub) PutState(key string, value []byte) error { - if stub.Uuid == "" { + if stub.TxID == "" { mockLogger.Error("Cannot PutState without a transactions - call stub.MockTransactionStart()?") return errors.New("Cannot PutState without a transactions - call stub.MockTransactionStart()?") } @@ -236,7 +240,7 @@ func (stub *MockStub) InvokeChaincode(chaincodeName string, args [][]byte) ([]by otherStub := stub.Invokables[chaincodeName] mockLogger.Debug("MockStub", stub.Name, "Invoking peer chaincode", otherStub.Name, args) // function, strings := getFuncArgs(args) - bytes, err := otherStub.MockInvoke(stub.Uuid, function, params) + bytes, err := otherStub.MockInvoke(stub.TxID, function, params) mockLogger.Debug("MockStub", stub.Name, "Invoked peer chaincode", otherStub.Name, "got", bytes, err) return bytes, err }