Skip to content
This repository has been archived by the owner on Nov 8, 2023. It is now read-only.

Commit

Permalink
fix(mock_stub)[#97]: dump state for inner cc2cc calls on outer tx fin…
Browse files Browse the repository at this point in the history
…ish (like in Fabric) (#98)

Co-authored-by: nusanovkornilov <[email protected]>
  • Loading branch information
kolayuk and nusanovkornilov authored Apr 19, 2022
1 parent 4c0989d commit 5f0edc5
Showing 1 changed file with 28 additions and 14 deletions.
42 changes: 28 additions & 14 deletions testing/mockstub.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,9 @@ type MockStub struct {
chaincodeEventSubscriptions []chan *peer.ChaincodeEvent // multiple event subscriptions

PrivateKeys map[string]*list.List
// flag for cc2cc invokation via InvokeChaincode to dump state on outer tx finish
// https://github.com/s7techlab/cckit/issues/97
cc2ccInvokation bool
}

type CreatorTransformer func(...interface{}) (mspID string, certPEM []byte, err error)
Expand Down Expand Up @@ -223,6 +226,7 @@ func (stub *MockStub) InvokeChaincode(chaincodeName string, args [][]byte, chann
}

otherStub.mockCreator = stub.mockCreator
otherStub.cc2ccInvokation = true
res := otherStub.MockInvoke(stub.TxID, args)
return res
}
Expand Down Expand Up @@ -298,23 +302,25 @@ func (stub *MockStub) DumpStateBuffer() {
_ = stub.MockStub.PutState(s.Key, s.Value)
}
}

if stub.ChaincodeEvent != nil {
// send only last event
for _, sub := range stub.chaincodeEventSubscriptions {
// subscription can be closed
if sub != nil {
sub <- stub.ChaincodeEvent
}
}
stub.ChaincodeEventsChannel <- stub.ChaincodeEvent
}
} else {
stub.ChaincodeEvent = nil
}
stub.StateBuffer = nil
}

func (stub *MockStub) dumpEvents() {
if stub.ChaincodeEvent != nil {
// send only last event
for _, sub := range stub.chaincodeEventSubscriptions {
// subscription can be closed
if sub != nil {
sub <- stub.ChaincodeEvent
}
}
stub.ChaincodeEventsChannel <- stub.ChaincodeEvent
}
}

// MockQuery
func (stub *MockStub) MockQuery(uuid string, args [][]byte) peer.Response {
return stub.MockInvoke(uuid, args)
Expand All @@ -332,9 +338,17 @@ func (stub *MockStub) MockTransactionStart(uuid string) {

func (stub *MockStub) MockTransactionEnd(uuid string) {
stub.LastTxID = stub.TxID
stub.DumpStateBuffer()

stub.MockStub.MockTransactionEnd(uuid)
if !stub.cc2ccInvokation { // skip for inner tx cc2cc calls
stub.DumpStateBuffer()
stub.dumpEvents() // events works only for outer stub in Fabric

// dump buffer to state on outer tx finishing (https://github.com/s7techlab/cckit/issues/97)
for _, invokableStub := range stub.Invokables {
invokableStub.DumpStateBuffer()
invokableStub.cc2ccInvokation = false
}
stub.MockStub.MockTransactionEnd(uuid)
}

if stub.ClearCreatorAfterInvoke {
stub.mockCreator = nil
Expand Down

0 comments on commit 5f0edc5

Please sign in to comment.