Skip to content

Commit 992aff8

Browse files
yuki-konmastersingh24
authored andcommitted
[FAB-12614]MockPeerChaincode receives channel name
Chaincode's MockStub has an API "InvokeChaincode", which is used for unit tests invoking other chaincode. This function converts chaincodeName to a composite name internally: chaincodeName = chaincodeName + "/" + channel. However, this conversion is not applied to "MockPeerChaincode", which registers another Mockstub. When writing unit tests invoking chaincode on other channel, developers have to follow the naming rule manually. In addition, the rule is not described in the API usage. This CR updates "MockPeerChaincode" to receive channel name as a parameter for CC2CC on another channel. FAB-12614 #done Change-Id: Ifcabc19bd78cbaa8bcc702c403da112096b16643 Signed-off-by: Yuki Kondo <yuki.kondo@hal.hitachi.com>
1 parent 0c1a229 commit 992aff8

File tree

2 files changed

+14
-9
lines changed

2 files changed

+14
-9
lines changed

core/chaincode/shim/mockstub.go

Lines changed: 13 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -118,10 +118,15 @@ func (stub *MockStub) MockTransactionEnd(uuid string) {
118118
stub.TxID = ""
119119
}
120120

121-
// Register a peer chaincode with this MockStub
122-
// invokableChaincodeName is the name or hash of the peer
123-
// otherStub is a MockStub of the peer, already intialised
124-
func (stub *MockStub) MockPeerChaincode(invokableChaincodeName string, otherStub *MockStub) {
121+
// Register another MockStub chaincode with this MockStub.
122+
// invokableChaincodeName is the name of a chaincode.
123+
// otherStub is a MockStub of the chaincode, already initialized.
124+
// channel is the name of a channel on which another MockStub is called.
125+
func (stub *MockStub) MockPeerChaincode(invokableChaincodeName string, otherStub *MockStub, channel string) {
126+
// Internally we use chaincode name as a composite name
127+
if channel != "" {
128+
invokableChaincodeName = invokableChaincodeName + "/" + channel
129+
}
125130
stub.Invokables[invokableChaincodeName] = otherStub
126131
}
127132

@@ -339,10 +344,10 @@ func (stub *MockStub) GetQueryResultWithPagination(query string, pageSize int32,
339344
return nil, nil, nil
340345
}
341346

342-
// InvokeChaincode calls a peered chaincode.
343-
// E.g. stub1.InvokeChaincode("stub2Hash", funcArgs, channel)
344-
// Before calling this make sure to create another MockStub stub2, call stub2.MockInit(uuid, func, args)
345-
// and register it with stub1 by calling stub1.MockPeerChaincode("stub2Hash", stub2)
347+
// InvokeChaincode locally calls the specified chaincode `Invoke`.
348+
// E.g. stub1.InvokeChaincode("othercc", funcArgs, channel)
349+
// Before calling this make sure to create another MockStub stub2, call shim.NewMockStub("othercc", Chaincode)
350+
// and register it with stub1 by calling stub1.MockPeerChaincode("othercc", stub2, channel)
346351
func (stub *MockStub) InvokeChaincode(chaincodeName string, args [][]byte, channel string) pb.Response {
347352
// Internally we use chaincode name as a composite name
348353
if channel != "" {

core/chaincode/shim/mockstub_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -305,7 +305,7 @@ func TestMockMock(t *testing.T) {
305305
stub.GetStateByRange("start", "end")
306306
stub.GetQueryResult("q")
307307
stub2 := NewMockStub("othercc", &shimTestCC{})
308-
stub.MockPeerChaincode("othercc/mychan", stub2)
308+
stub.MockPeerChaincode("othercc", stub2, "mychan")
309309
stub.InvokeChaincode("othercc", nil, "mychan")
310310
stub.GetCreator()
311311
stub.GetTransient()

0 commit comments

Comments
 (0)