@@ -19,6 +19,7 @@ package chaincode
1919import (
2020 "encoding/json"
2121 "fmt"
22+ "math/rand"
2223 "net"
2324 "os"
2425 "path/filepath"
@@ -317,7 +318,7 @@ func deploy2(ctx context.Context, cccid *ccprovider.CCContext, chaincodeDeployme
317318
318319// Invoke a chaincode.
319320func 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 )
321322}
322323
323324// Invoke a chaincode with version (needed for upgrade)
@@ -569,13 +570,13 @@ func checkFinalState(cccid *ccprovider.CCContext) error {
569570}
570571
571572// 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 {
573574
574575 var nextBlockNumber uint64
575576
576577 f := "init"
577578 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 }}
579580 _ , err := deploy (ctxt , cccid , spec , nextBlockNumber )
580581 nextBlockNumber ++
581582 ccID := spec .ChaincodeId .Name
@@ -598,7 +599,7 @@ func invokeExample02Transaction(ctxt context.Context, cccid *ccprovider.CCContex
598599
599600 f = "invoke"
600601 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 ... )}}
602603 _ , uuid , _ , err := invoke (ctxt , cccid .ChainID , spec , nextBlockNumber )
603604 nextBlockNumber ++
604605 if err != nil {
@@ -614,7 +615,7 @@ func invokeExample02Transaction(ctxt context.Context, cccid *ccprovider.CCContex
614615 // Test for delete state
615616 f = "delete"
616617 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 }}
618619 _ , _ , _ , err = invoke (ctxt , cccid .ChainID , spec , nextBlockNumber )
619620 if err != nil {
620621 return fmt .Errorf ("Error deleting state in <%s>: %s" , cccid .Name , err )
@@ -623,34 +624,56 @@ func invokeExample02Transaction(ctxt context.Context, cccid *ccprovider.CCContex
623624 return nil
624625}
625626
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+
626633func TestExecuteInvokeTransaction (t * testing.T ) {
627- chainID := util .GetTestChainID ()
628634
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 },
633641 }
634642
635- defer finitPeer (lis , chainID )
643+ for _ , tc := range testCases {
644+ t .Run (tc .chaincodeType .String (), func (t * testing.T ) {
636645
637- var ctxt = context . Background ()
646+ chainID := util . GetTestChainID ()
638647
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+ }
642653
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+ })
651675 }
652676
653- theChaincodeSupport .Stop (ctxt , cccid , & pb.ChaincodeDeploymentSpec {ChaincodeSpec : & pb.ChaincodeSpec {ChaincodeId : ccID }})
654677}
655678
656679// Test the execution of an invalid transaction.
@@ -674,7 +697,7 @@ func TestExecuteInvokeInvalidTransaction(t *testing.T) {
674697
675698 //FAIL, FAIL!
676699 args := []string {"x" , "-1" }
677- err = invokeExample02Transaction (ctxt , cccid , ccID , args , false )
700+ err = invokeExample02Transaction (ctxt , cccid , ccID , pb . ChaincodeSpec_GOLANG , args , false )
678701
679702 //this HAS to fail with expectedDeltaStringPrefix
680703 if err != nil {
@@ -1509,6 +1532,76 @@ func TestChaincodeInvokesSystemChaincode(t *testing.T) {
15091532 theChaincodeSupport .Stop (ctxt , cccid , & pb.ChaincodeDeploymentSpec {ChaincodeSpec : spec })
15101533}
15111534
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+
15121605var signer msp.SigningIdentity
15131606
15141607func TestMain (m * testing.M ) {
@@ -1527,3 +1620,18 @@ func TestMain(m *testing.M) {
15271620 SetupTestConfig ()
15281621 os .Exit (m .Run ())
15291622}
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