Skip to content

Commit

Permalink
[FAB-2852] Java chaincode support for cc events
Browse files Browse the repository at this point in the history
Change-Id: I46fea860d8a7916fe289615a31c1bfc959f7a782
Signed-off-by: Luis Sanchez <sanchezl@us.ibm.com>
  • Loading branch information
Luis Sanchez committed Apr 26, 2017
1 parent c5c60c3 commit ad29e8f
Show file tree
Hide file tree
Showing 7 changed files with 1,293 additions and 1,059 deletions.
102 changes: 62 additions & 40 deletions core/chaincode/exectransaction_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -624,9 +624,11 @@ func invokeExample02Transaction(ctxt context.Context, cccid *ccprovider.CCContex
}

const (
chaincodeExample02GolangPath = "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02"
chaincodeExample02JavaPath = "../../examples/chaincode/java/chaincode_example02"
chaincodeExample06JavaPath = "../../examples/chaincode/java/chaincode_example06"
chaincodeExample02GolangPath = "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02"
chaincodeEventSenderGolangPath = "github.com/hyperledger/fabric/examples/chaincode/go/eventsender"
chaincodeExample02JavaPath = "../../examples/chaincode/java/chaincode_example02"
chaincodeExample06JavaPath = "../../examples/chaincode/java/chaincode_example06"
chaincodeEventSenderJavaPath = "../../examples/chaincode/java/eventsender"
)

func runChaincodeInvokeChaincode(t *testing.T, chainID1 string, chainID2 string, _ string) (err error) {
Expand Down Expand Up @@ -1307,66 +1309,86 @@ func TestQueries(t *testing.T) {
}

func TestGetEvent(t *testing.T) {

testCases := []struct {
chaincodeType pb.ChaincodeSpec_Type
chaincodePath string
}{
{pb.ChaincodeSpec_GOLANG, chaincodeEventSenderGolangPath},
{pb.ChaincodeSpec_JAVA, chaincodeEventSenderJavaPath},
}

chainID := util.GetTestChainID()
var nextBlockNumber uint64

lis, err := initPeer(chainID)
if err != nil {
t.Fail()
t.Logf("Error creating peer: %s", err)
}

nextBlockNumber++

defer finitPeer(lis, chainID)

var ctxt = context.Background()
for _, tc := range testCases {
t.Run(tc.chaincodeType.String(), func(t *testing.T) {

url := "github.com/hyperledger/fabric/examples/chaincode/go/eventsender"
if tc.chaincodeType == pb.ChaincodeSpec_JAVA && runtime.GOARCH != "amd64" {
t.Skip("No Java chaincode support yet on non-x86_64.")
}

cID := &pb.ChaincodeID{Name: "esender", Path: url, Version: "0"}
f := "init"
spec := &pb.ChaincodeSpec{Type: 1, ChaincodeId: cID, Input: &pb.ChaincodeInput{Args: util.ToChaincodeArgs(f)}}
var ctxt = context.Background()

cccid := ccprovider.NewCCContext(chainID, "esender", "0", "", false, nil, nil)
var nextBlockNumber uint64 = 1
_, err = deploy(ctxt, cccid, spec, nextBlockNumber)
nextBlockNumber++
ccID := spec.ChaincodeId.Name
if err != nil {
t.Fail()
t.Logf("Error initializing chaincode %s(%s)", ccID, err)
theChaincodeSupport.Stop(ctxt, cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: spec})
return
}
cID := &pb.ChaincodeID{Name: generateChaincodeName(tc.chaincodeType), Path: tc.chaincodePath, Version: "0"}
f := "init"
spec := &pb.ChaincodeSpec{Type: tc.chaincodeType, ChaincodeId: cID, Input: &pb.ChaincodeInput{Args: util.ToChaincodeArgs(f)}}

time.Sleep(time.Second)
cccid := ccprovider.NewCCContext(chainID, cID.Name, cID.Version, "", false, nil, nil)
_, err = deploy(ctxt, cccid, spec, nextBlockNumber)
nextBlockNumber++
ccID := spec.ChaincodeId.Name
if err != nil {
t.Fail()
t.Logf("Error initializing chaincode %s(%s)", ccID, err)
theChaincodeSupport.Stop(ctxt, cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: spec})
return
}

args := util.ToChaincodeArgs("invoke", "i", "am", "satoshi")
time.Sleep(time.Second)

spec = &pb.ChaincodeSpec{Type: 1, ChaincodeId: cID, Input: &pb.ChaincodeInput{Args: args}}
args := util.ToChaincodeArgs("invoke", "i", "am", "satoshi")

var ccevt *pb.ChaincodeEvent
ccevt, _, _, err = invoke(ctxt, chainID, spec, nextBlockNumber, nil)
spec = &pb.ChaincodeSpec{Type: 1, ChaincodeId: cID, Input: &pb.ChaincodeInput{Args: args}}

if err != nil {
t.Logf("Error invoking chaincode %s(%s)", ccID, err)
t.Fail()
}
var ccevt *pb.ChaincodeEvent
ccevt, _, _, err = invoke(ctxt, chainID, spec, nextBlockNumber, nil)
nextBlockNumber++

if ccevt == nil {
t.Logf("Error ccevt is nil %s(%s)", ccID, err)
t.Fail()
}
if err != nil {
t.Logf("Error invoking chaincode %s(%s)", ccID, err)
t.Fail()
}

if ccevt.ChaincodeId != ccID {
t.Logf("Error ccevt id(%s) != cid(%s)", ccevt.ChaincodeId, ccID)
t.Fail()
}
if ccevt == nil {
t.Logf("Error ccevt is nil %s(%s)", ccID, err)
t.Fail()
}

if strings.Index(string(ccevt.Payload), "i,am,satoshi") < 0 {
t.Logf("Error expected event not found (%s)", string(ccevt.Payload))
t.Fail()
if ccevt.ChaincodeId != ccID {
t.Logf("Error ccevt id(%s) != cid(%s)", ccevt.ChaincodeId, ccID)
t.Fail()
}

if strings.Index(string(ccevt.Payload), "i,am,satoshi") < 0 {
t.Logf("Error expected event not found (%s)", string(ccevt.Payload))
t.Fail()
}

theChaincodeSupport.Stop(ctxt, cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: spec})
})
}

theChaincodeSupport.Stop(ctxt, cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: spec})
}

// Test the execution of a chaincode that queries another chaincode
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -174,9 +174,9 @@ public void chatWithPeer(ManagedChannel connection) {

@Override
public void onNext(ChaincodeMessage message) {
logger.info("Got message from peer: " + toJsonString(message));
logger.debug("Got message from peer: " + toJsonString(message));
try {
logger.info(String.format("[%s]Received message %s from org.hyperledger.fabric.shim",
logger.debug(String.format("[%s]Received message %s from org.hyperledger.fabric.shim",
Handler.shortID(message.getTxid()), message.getType()));
handler.handleMessage(message);
} catch (Exception e) {
Expand Down
Loading

0 comments on commit ad29e8f

Please sign in to comment.