Skip to content

Commit

Permalink
Add GetTxID function to Stub interface (FAB-306)
Browse files Browse the repository at this point in the history
The name of the field UUID was also changed to TxID.

Change-Id: Iedeed9af13a99671a756c32944a8c6814d4b2b20
Signed-off-by: Gabor Hosszu <gabor@digitalasset.com>
  • Loading branch information
gaborh-da committed Sep 6, 2016
1 parent 0f959c0 commit 47053cd
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 17 deletions.
24 changes: 14 additions & 10 deletions core/chaincode/shim/chaincode.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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{}
Expand All @@ -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

Expand All @@ -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.
Expand Down Expand Up @@ -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
Expand Down
3 changes: 3 additions & 0 deletions core/chaincode/shim/interfaces.go
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
18 changes: 11 additions & 7 deletions core/chaincode/shim/mockstub.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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
Expand Down Expand Up @@ -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()?")
}
Expand Down Expand Up @@ -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
}
Expand Down

0 comments on commit 47053cd

Please sign in to comment.