@@ -19,6 +19,7 @@ package chaincode
19
19
import (
20
20
"encoding/json"
21
21
"fmt"
22
+ "math/rand"
22
23
"net"
23
24
"os"
24
25
"path/filepath"
@@ -317,7 +318,7 @@ func deploy2(ctx context.Context, cccid *ccprovider.CCContext, chaincodeDeployme
317
318
318
319
// Invoke a chaincode.
319
320
func invoke (ctx context.Context , chainID string , spec * pb.ChaincodeSpec , blockNumber uint64 ) (ccevt * pb.ChaincodeEvent , uuid string , retval []byte , err error ) {
320
- return invokeWithVersion (ctx , chainID , "0" , spec , blockNumber )
321
+ return invokeWithVersion (ctx , chainID , spec . GetChaincodeId (). Version , spec , blockNumber )
321
322
}
322
323
323
324
// Invoke a chaincode with version (needed for upgrade)
@@ -569,13 +570,13 @@ func checkFinalState(cccid *ccprovider.CCContext) error {
569
570
}
570
571
571
572
// Invoke chaincode_example02
572
- func invokeExample02Transaction (ctxt context.Context , cccid * ccprovider.CCContext , cID * pb.ChaincodeID , args []string , destroyImage bool ) error {
573
+ func invokeExample02Transaction (ctxt context.Context , cccid * ccprovider.CCContext , cID * pb.ChaincodeID , chaincodeType pb. ChaincodeSpec_Type , args []string , destroyImage bool ) error {
573
574
574
575
var nextBlockNumber uint64
575
576
576
577
f := "init"
577
578
argsDeploy := util .ToChaincodeArgs (f , "a" , "100" , "b" , "200" )
578
- spec := & pb.ChaincodeSpec {Type : 1 , ChaincodeId : cID , Input : & pb.ChaincodeInput {Args : argsDeploy }}
579
+ spec := & pb.ChaincodeSpec {Type : chaincodeType , ChaincodeId : cID , Input : & pb.ChaincodeInput {Args : argsDeploy }}
579
580
_ , err := deploy (ctxt , cccid , spec , nextBlockNumber )
580
581
nextBlockNumber ++
581
582
ccID := spec .ChaincodeId .Name
@@ -598,7 +599,7 @@ func invokeExample02Transaction(ctxt context.Context, cccid *ccprovider.CCContex
598
599
599
600
f = "invoke"
600
601
invokeArgs := append ([]string {f }, args ... )
601
- spec = & pb.ChaincodeSpec {Type : 1 , ChaincodeId : cID , Input : & pb.ChaincodeInput {Args : util .ToChaincodeArgs (invokeArgs ... )}}
602
+ spec = & pb.ChaincodeSpec {ChaincodeId : cID , Input : & pb.ChaincodeInput {Args : util .ToChaincodeArgs (invokeArgs ... )}}
602
603
_ , uuid , _ , err := invoke (ctxt , cccid .ChainID , spec , nextBlockNumber )
603
604
nextBlockNumber ++
604
605
if err != nil {
@@ -614,7 +615,7 @@ func invokeExample02Transaction(ctxt context.Context, cccid *ccprovider.CCContex
614
615
// Test for delete state
615
616
f = "delete"
616
617
delArgs := util .ToChaincodeArgs (f , "a" )
617
- spec = & pb.ChaincodeSpec {Type : 1 , ChaincodeId : cID , Input : & pb.ChaincodeInput {Args : delArgs }}
618
+ spec = & pb.ChaincodeSpec {ChaincodeId : cID , Input : & pb.ChaincodeInput {Args : delArgs }}
618
619
_ , _ , _ , err = invoke (ctxt , cccid .ChainID , spec , nextBlockNumber )
619
620
if err != nil {
620
621
return fmt .Errorf ("Error deleting state in <%s>: %s" , cccid .Name , err )
@@ -623,34 +624,56 @@ func invokeExample02Transaction(ctxt context.Context, cccid *ccprovider.CCContex
623
624
return nil
624
625
}
625
626
627
+ const (
628
+ chaincodeExample02GolangPath = "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02"
629
+ chaincodeExample02JavaPath = "../../examples/chaincode/java/chaincode_example02"
630
+ chaincodeExample06JavaPath = "../../examples/chaincode/java/chaincode_example06"
631
+ )
632
+
626
633
func TestExecuteInvokeTransaction (t * testing.T ) {
627
- chainID := util .GetTestChainID ()
628
634
629
- lis , err := initPeer (chainID )
630
- if err != nil {
631
- t .Fail ()
632
- t .Logf ("Error creating peer: %s" , err )
635
+ testCases := []struct {
636
+ chaincodeType pb.ChaincodeSpec_Type
637
+ chaincodePath string
638
+ }{
639
+ {pb .ChaincodeSpec_GOLANG , chaincodeExample02GolangPath },
640
+ {pb .ChaincodeSpec_JAVA , chaincodeExample02JavaPath },
633
641
}
634
642
635
- defer finitPeer (lis , chainID )
643
+ for _ , tc := range testCases {
644
+ t .Run (tc .chaincodeType .String (), func (t * testing.T ) {
636
645
637
- var ctxt = context . Background ()
646
+ chainID := util . GetTestChainID ()
638
647
639
- cccid := ccprovider .NewCCContext (chainID , "example02" , "0" , "" , false , nil , nil )
640
- url := "github.com/hyperledger/fabric/examples/chaincode/go/chaincode_example02"
641
- ccID := & pb.ChaincodeID {Name : "example02" , Path : url , Version : "0" }
648
+ lis , err := initPeer (chainID )
649
+ if err != nil {
650
+ t .Fail ()
651
+ t .Logf ("Error creating peer: %s" , err )
652
+ }
642
653
643
- args := []string {"a" , "b" , "10" }
644
- err = invokeExample02Transaction (ctxt , cccid , ccID , args , true )
645
- if err != nil {
646
- t .Fail ()
647
- t .Logf ("Error invoking transaction: %s" , err )
648
- } else {
649
- fmt .Print ("Invoke test passed\n " )
650
- t .Log ("Invoke test passed" )
654
+ defer finitPeer (lis , chainID )
655
+
656
+ var ctxt = context .Background ()
657
+ chaincodeName := generateChaincodeName (tc .chaincodeType )
658
+ chaincodeVersion := "1.0.0.0"
659
+ cccid := ccprovider .NewCCContext (chainID , chaincodeName , chaincodeVersion , "" , false , nil , nil )
660
+ ccID := & pb.ChaincodeID {Name : chaincodeName , Path : tc .chaincodePath , Version : chaincodeVersion }
661
+
662
+ args := []string {"a" , "b" , "10" }
663
+ err = invokeExample02Transaction (ctxt , cccid , ccID , tc .chaincodeType , args , true )
664
+ if err != nil {
665
+ t .Fail ()
666
+ t .Logf ("Error invoking transaction: %s" , err )
667
+ } else {
668
+ fmt .Print ("Invoke test passed\n " )
669
+ t .Log ("Invoke test passed" )
670
+ }
671
+
672
+ theChaincodeSupport .Stop (ctxt , cccid , & pb.ChaincodeDeploymentSpec {ChaincodeSpec : & pb.ChaincodeSpec {ChaincodeId : ccID }})
673
+
674
+ })
651
675
}
652
676
653
- theChaincodeSupport .Stop (ctxt , cccid , & pb.ChaincodeDeploymentSpec {ChaincodeSpec : & pb.ChaincodeSpec {ChaincodeId : ccID }})
654
677
}
655
678
656
679
// Test the execution of an invalid transaction.
@@ -674,7 +697,7 @@ func TestExecuteInvokeInvalidTransaction(t *testing.T) {
674
697
675
698
//FAIL, FAIL!
676
699
args := []string {"x" , "-1" }
677
- err = invokeExample02Transaction (ctxt , cccid , ccID , args , false )
700
+ err = invokeExample02Transaction (ctxt , cccid , ccID , pb . ChaincodeSpec_GOLANG , args , false )
678
701
679
702
//this HAS to fail with expectedDeltaStringPrefix
680
703
if err != nil {
@@ -1509,6 +1532,76 @@ func TestChaincodeInvokesSystemChaincode(t *testing.T) {
1509
1532
theChaincodeSupport .Stop (ctxt , cccid , & pb.ChaincodeDeploymentSpec {ChaincodeSpec : spec })
1510
1533
}
1511
1534
1535
+ func TestChaincodeInitializeInitError (t * testing.T ) {
1536
+ testCases := []struct {
1537
+ name string
1538
+ chaincodeType pb.ChaincodeSpec_Type
1539
+ chaincodePath string
1540
+ args []string
1541
+ }{
1542
+ {"NotSuccessResponse" , pb .ChaincodeSpec_GOLANG , chaincodeExample02GolangPath , []string {"init" , "not" , "enough" , "args" }},
1543
+ {"NotSuccessResponse" , pb .ChaincodeSpec_JAVA , chaincodeExample02JavaPath , []string {"init" , "not" , "enough" , "args" }},
1544
+ {"RuntimeException" , pb .ChaincodeSpec_JAVA , chaincodeExample06JavaPath , []string {"runtimeException" }},
1545
+ }
1546
+
1547
+ channelID := util .GetTestChainID ()
1548
+
1549
+ for _ , tc := range testCases {
1550
+ t .Run (tc .name + "_" + tc .chaincodeType .String (), func (t * testing.T ) {
1551
+
1552
+ // initialize peer
1553
+ if listener , err := initPeer (channelID ); err != nil {
1554
+ t .Errorf ("Error creating peer: %s" , err )
1555
+ } else {
1556
+ defer finitPeer (listener , channelID )
1557
+ }
1558
+
1559
+ var nextBlockNumber uint64
1560
+
1561
+ // the chaincode to install and instanciate
1562
+ chaincodeName := generateChaincodeName (tc .chaincodeType )
1563
+ chaincodePath := tc .chaincodePath
1564
+ chaincodeVersion := "1.0.0.0"
1565
+ chaincodeType := tc .chaincodeType
1566
+ chaincodeDeployArgs := util .ArrayToChaincodeArgs (tc .args )
1567
+
1568
+ // new chaincode context for passing around parameters
1569
+ chaincodeCtx := ccprovider .NewCCContext (channelID , chaincodeName , chaincodeVersion , "" , false , nil , nil )
1570
+
1571
+ // attempt to deploy chaincode
1572
+ _ , err := deployChaincode (context .Background (), chaincodeCtx , chaincodeType , chaincodePath , chaincodeDeployArgs , nextBlockNumber )
1573
+
1574
+ // deploy should of failed
1575
+ if err == nil {
1576
+ t .Fatal ("Deployment should have failed." )
1577
+ }
1578
+ t .Log (err )
1579
+
1580
+ })
1581
+ }
1582
+ }
1583
+
1584
+ func deployChaincode (ctx context.Context , chaincodeCtx * ccprovider.CCContext , chaincodeType pb.ChaincodeSpec_Type , path string , args [][]byte , nextBlockNumber uint64 ) ([]byte , error ) {
1585
+
1586
+ chaincodeSpec := & pb.ChaincodeSpec {
1587
+ ChaincodeId : & pb.ChaincodeID {
1588
+ Name : chaincodeCtx .Name ,
1589
+ Version : chaincodeCtx .Version ,
1590
+ Path : path ,
1591
+ },
1592
+ Type : chaincodeType ,
1593
+ Input : & pb.ChaincodeInput {
1594
+ Args : args ,
1595
+ },
1596
+ }
1597
+
1598
+ result , err := deploy (ctx , chaincodeCtx , chaincodeSpec , nextBlockNumber )
1599
+ if err != nil {
1600
+ return nil , fmt .Errorf ("Error deploying <%s:%s>: %s" , chaincodeSpec .ChaincodeId .Name , chaincodeSpec .ChaincodeId .Version , err )
1601
+ }
1602
+ return result , nil
1603
+ }
1604
+
1512
1605
var signer msp.SigningIdentity
1513
1606
1514
1607
func TestMain (m * testing.M ) {
@@ -1527,3 +1620,18 @@ func TestMain(m *testing.M) {
1527
1620
SetupTestConfig ()
1528
1621
os .Exit (m .Run ())
1529
1622
}
1623
+
1624
+ var rng * rand.Rand = rand .New (rand .NewSource (time .Now ().UnixNano ()))
1625
+
1626
+ func generateChaincodeName (chaincodeType pb.ChaincodeSpec_Type ) string {
1627
+ prefix := "cc_"
1628
+ switch chaincodeType {
1629
+ case pb .ChaincodeSpec_GOLANG :
1630
+ prefix = "cc_go_"
1631
+ case pb .ChaincodeSpec_JAVA :
1632
+ prefix = "cc_java_"
1633
+ case pb .ChaincodeSpec_NODE :
1634
+ prefix = "cc_js_"
1635
+ }
1636
+ return fmt .Sprintf ("%s%06d" , prefix , rng .Intn (999999 ))
1637
+ }
0 commit comments