Skip to content

Commit ad29e8f

Browse files
author
Luis Sanchez
committed
[FAB-2852] Java chaincode support for cc events
Change-Id: I46fea860d8a7916fe289615a31c1bfc959f7a782 Signed-off-by: Luis Sanchez <[email protected]>
1 parent c5c60c3 commit ad29e8f

File tree

7 files changed

+1293
-1059
lines changed

7 files changed

+1293
-1059
lines changed

core/chaincode/exectransaction_test.go

+62-40
Original file line numberDiff line numberDiff line change
@@ -624,9 +624,11 @@ func invokeExample02Transaction(ctxt context.Context, cccid *ccprovider.CCContex
624624
}
625625

626626
const (
627-
chaincodeExample02GolangPath = "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02"
628-
chaincodeExample02JavaPath = "../../examples/chaincode/java/chaincode_example02"
629-
chaincodeExample06JavaPath = "../../examples/chaincode/java/chaincode_example06"
627+
chaincodeExample02GolangPath = "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02"
628+
chaincodeEventSenderGolangPath = "github.com/hyperledger/fabric/examples/chaincode/go/eventsender"
629+
chaincodeExample02JavaPath = "../../examples/chaincode/java/chaincode_example02"
630+
chaincodeExample06JavaPath = "../../examples/chaincode/java/chaincode_example06"
631+
chaincodeEventSenderJavaPath = "../../examples/chaincode/java/eventsender"
630632
)
631633

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

13091311
func TestGetEvent(t *testing.T) {
1312+
1313+
testCases := []struct {
1314+
chaincodeType pb.ChaincodeSpec_Type
1315+
chaincodePath string
1316+
}{
1317+
{pb.ChaincodeSpec_GOLANG, chaincodeEventSenderGolangPath},
1318+
{pb.ChaincodeSpec_JAVA, chaincodeEventSenderJavaPath},
1319+
}
1320+
13101321
chainID := util.GetTestChainID()
1322+
var nextBlockNumber uint64
13111323

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

1330+
nextBlockNumber++
1331+
13181332
defer finitPeer(lis, chainID)
13191333

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

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

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

1328-
cccid := ccprovider.NewCCContext(chainID, "esender", "0", "", false, nil, nil)
1329-
var nextBlockNumber uint64 = 1
1330-
_, err = deploy(ctxt, cccid, spec, nextBlockNumber)
1331-
nextBlockNumber++
1332-
ccID := spec.ChaincodeId.Name
1333-
if err != nil {
1334-
t.Fail()
1335-
t.Logf("Error initializing chaincode %s(%s)", ccID, err)
1336-
theChaincodeSupport.Stop(ctxt, cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: spec})
1337-
return
1338-
}
1343+
cID := &pb.ChaincodeID{Name: generateChaincodeName(tc.chaincodeType), Path: tc.chaincodePath, Version: "0"}
1344+
f := "init"
1345+
spec := &pb.ChaincodeSpec{Type: tc.chaincodeType, ChaincodeId: cID, Input: &pb.ChaincodeInput{Args: util.ToChaincodeArgs(f)}}
13391346

1340-
time.Sleep(time.Second)
1347+
cccid := ccprovider.NewCCContext(chainID, cID.Name, cID.Version, "", false, nil, nil)
1348+
_, err = deploy(ctxt, cccid, spec, nextBlockNumber)
1349+
nextBlockNumber++
1350+
ccID := spec.ChaincodeId.Name
1351+
if err != nil {
1352+
t.Fail()
1353+
t.Logf("Error initializing chaincode %s(%s)", ccID, err)
1354+
theChaincodeSupport.Stop(ctxt, cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: spec})
1355+
return
1356+
}
13411357

1342-
args := util.ToChaincodeArgs("invoke", "i", "am", "satoshi")
1358+
time.Sleep(time.Second)
13431359

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

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

1349-
if err != nil {
1350-
t.Logf("Error invoking chaincode %s(%s)", ccID, err)
1351-
t.Fail()
1352-
}
1364+
var ccevt *pb.ChaincodeEvent
1365+
ccevt, _, _, err = invoke(ctxt, chainID, spec, nextBlockNumber, nil)
1366+
nextBlockNumber++
13531367

1354-
if ccevt == nil {
1355-
t.Logf("Error ccevt is nil %s(%s)", ccID, err)
1356-
t.Fail()
1357-
}
1368+
if err != nil {
1369+
t.Logf("Error invoking chaincode %s(%s)", ccID, err)
1370+
t.Fail()
1371+
}
13581372

1359-
if ccevt.ChaincodeId != ccID {
1360-
t.Logf("Error ccevt id(%s) != cid(%s)", ccevt.ChaincodeId, ccID)
1361-
t.Fail()
1362-
}
1373+
if ccevt == nil {
1374+
t.Logf("Error ccevt is nil %s(%s)", ccID, err)
1375+
t.Fail()
1376+
}
13631377

1364-
if strings.Index(string(ccevt.Payload), "i,am,satoshi") < 0 {
1365-
t.Logf("Error expected event not found (%s)", string(ccevt.Payload))
1366-
t.Fail()
1378+
if ccevt.ChaincodeId != ccID {
1379+
t.Logf("Error ccevt id(%s) != cid(%s)", ccevt.ChaincodeId, ccID)
1380+
t.Fail()
1381+
}
1382+
1383+
if strings.Index(string(ccevt.Payload), "i,am,satoshi") < 0 {
1384+
t.Logf("Error expected event not found (%s)", string(ccevt.Payload))
1385+
t.Fail()
1386+
}
1387+
1388+
theChaincodeSupport.Stop(ctxt, cccid, &pb.ChaincodeDeploymentSpec{ChaincodeSpec: spec})
1389+
})
13671390
}
13681391

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

13721394
// Test the execution of a chaincode that queries another chaincode

core/chaincode/shim/java/src/main/java/org/hyperledger/fabric/shim/ChaincodeBase.java

+2-2
Original file line numberDiff line numberDiff line change
@@ -174,9 +174,9 @@ public void chatWithPeer(ManagedChannel connection) {
174174

175175
@Override
176176
public void onNext(ChaincodeMessage message) {
177-
logger.info("Got message from peer: " + toJsonString(message));
177+
logger.debug("Got message from peer: " + toJsonString(message));
178178
try {
179-
logger.info(String.format("[%s]Received message %s from org.hyperledger.fabric.shim",
179+
logger.debug(String.format("[%s]Received message %s from org.hyperledger.fabric.shim",
180180
Handler.shortID(message.getTxid()), message.getType()));
181181
handler.handleMessage(message);
182182
} catch (Exception e) {

0 commit comments

Comments
 (0)