-
Notifications
You must be signed in to change notification settings - Fork 8.9k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add cmd "peer chaincode list --installed" to get installed chaincodes on the peer. Use "peer chaincode list --instantiated -C <channelID>" to get intantiated chaincodes on the channel. Get these infos from lscc "getinstalledchaincodes" and "getchaincodes" interfaces. Change-Id: I1db548bde55388e518e383ec46de25b237c0b318 Signed-off-by: jiangyaoguo <jiangyaoguo@gmail.com>
- Loading branch information
1 parent
283eb0e
commit b48178c
Showing
5 changed files
with
233 additions
and
35 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
/* | ||
Copyright IBM Corp. 2017 All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package chaincode | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/golang/protobuf/proto" | ||
"golang.org/x/net/context" | ||
|
||
pb "github.com/hyperledger/fabric/protos/peer" | ||
"github.com/hyperledger/fabric/protos/utils" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var getInstalledChaincodes bool | ||
var getInstantiatedChaincodes bool | ||
var chaincodeListCmd *cobra.Command | ||
|
||
const list_cmdname = "list" | ||
|
||
// installCmd returns the cobra command for Chaincode Deploy | ||
func listCmd(cf *ChaincodeCmdFactory) *cobra.Command { | ||
chaincodeListCmd = &cobra.Command{ | ||
Use: "list", | ||
Short: "Get the instantiated chaincodes on a channel or installed chaincodes on a peer.", | ||
Long: "Get the instantiated chaincodes in the channel if specify channel, or get installed chaincodes on the peer", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
return getChaincodes(cmd, cf) | ||
}, | ||
} | ||
|
||
flagList := []string{ | ||
"channelID", | ||
"installed", | ||
"instantiated", | ||
} | ||
attachFlags(chaincodeListCmd, flagList) | ||
|
||
return chaincodeListCmd | ||
} | ||
|
||
func getChaincodes(cmd *cobra.Command, cf *ChaincodeCmdFactory) error { | ||
var err error | ||
if cf == nil { | ||
cf, err = InitCmdFactory(true, false) | ||
if err != nil { | ||
return err | ||
} | ||
} | ||
|
||
creator, err := cf.Signer.Serialize() | ||
if err != nil { | ||
return fmt.Errorf("Error serializing identity for %s: %s", cf.Signer.GetIdentifier(), err) | ||
} | ||
|
||
var prop *pb.Proposal | ||
if getInstalledChaincodes && (!getInstantiatedChaincodes) { | ||
prop, _, err = utils.CreateGetInstalledChaincodesProposal(creator) | ||
} else if getInstantiatedChaincodes && (!getInstalledChaincodes) { | ||
prop, _, err = utils.CreateGetChaincodesProposal(chainID, creator) | ||
} else { | ||
return fmt.Errorf("Must explicitly specify \"--installed\" or \"--instantiated\"") | ||
} | ||
|
||
if err != nil { | ||
return fmt.Errorf("Error creating proposal %s: %s", chainFuncName, err) | ||
} | ||
|
||
var signedProp *pb.SignedProposal | ||
signedProp, err = utils.GetSignedProposal(prop, cf.Signer) | ||
if err != nil { | ||
return fmt.Errorf("Error creating signed proposal %s: %s", chainFuncName, err) | ||
} | ||
|
||
proposalResponse, err := cf.EndorserClient.ProcessProposal(context.Background(), signedProp) | ||
if err != nil { | ||
return fmt.Errorf("Error endorsing %s: %s", chainFuncName, err) | ||
} | ||
|
||
cqr := &pb.ChaincodeQueryResponse{} | ||
err = proto.Unmarshal(proposalResponse.Response.Payload, cqr) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
if getInstalledChaincodes { | ||
fmt.Println("Get installed chaincodes on peer:") | ||
} else { | ||
fmt.Printf("Get instantiated chaincodes on channel %s:\n", chainID) | ||
} | ||
for _, chaincode := range cqr.Chaincodes { | ||
fmt.Printf("%v\n", chaincode) | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
/* | ||
Copyright IBM Corp. 2017 All Rights Reserved. | ||
SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
|
||
package chaincode | ||
|
||
import ( | ||
"fmt" | ||
"testing" | ||
|
||
"github.com/golang/protobuf/proto" | ||
"github.com/hyperledger/fabric/peer/common" | ||
pb "github.com/hyperledger/fabric/protos/peer" | ||
) | ||
|
||
func TestChaincodeListCmd(t *testing.T) { | ||
InitMSP() | ||
|
||
signer, err := common.GetDefaultSigner() | ||
if err != nil { | ||
t.Fatalf("Get default signer error: %s", err) | ||
} | ||
|
||
installedCqr := &pb.ChaincodeQueryResponse{ | ||
Chaincodes: []*pb.ChaincodeInfo{ | ||
&pb.ChaincodeInfo{Name: "mycc1", Version: "1.0", Path: "codePath1", Input: "input", Escc: "escc", Vscc: "vscc"}, | ||
&pb.ChaincodeInfo{Name: "mycc2", Version: "1.0", Path: "codePath2", Input: "input", Escc: "escc", Vscc: "vscc"}, | ||
}, | ||
} | ||
installedCqrBytes, err := proto.Marshal(installedCqr) | ||
if err != nil { | ||
t.Fatalf("Marshale error: %s", err) | ||
} | ||
|
||
mockResponse := &pb.ProposalResponse{ | ||
Response: &pb.Response{Status: 200, Payload: installedCqrBytes}, | ||
Endorsement: &pb.Endorsement{}, | ||
} | ||
|
||
mockEndorerClient := common.GetMockEndorserClient(mockResponse, nil) | ||
|
||
mockBroadcastClient := common.GetMockBroadcastClient(nil) | ||
|
||
mockCF := &ChaincodeCmdFactory{ | ||
EndorserClient: mockEndorerClient, | ||
Signer: signer, | ||
BroadcastClient: mockBroadcastClient, | ||
} | ||
|
||
// Get installed chaincodes | ||
installedChaincodesCmd := listCmd(mockCF) | ||
|
||
args := []string{"--installed"} | ||
installedChaincodesCmd.SetArgs(args) | ||
|
||
if err := installedChaincodesCmd.Execute(); err != nil { | ||
t.Errorf("Run chaincode list cmd to get installed chaincodes error:%v", err) | ||
} | ||
|
||
resetFlags() | ||
|
||
// Get instantiated chaincodes | ||
instantiatedChaincodesCmd := listCmd(mockCF) | ||
|
||
args = []string{"--instantiated"} | ||
instantiatedChaincodesCmd.SetArgs(args) | ||
|
||
if err := instantiatedChaincodesCmd.Execute(); err != nil { | ||
t.Errorf("Run chaincode list cmd to get instantiated chaincodes error:%v", err) | ||
} | ||
|
||
resetFlags() | ||
|
||
// Wrong case: Set both "--installed" and "--instantiated" | ||
Cmd := listCmd(mockCF) | ||
|
||
args = []string{"--installed", "--instantiated"} | ||
Cmd.SetArgs(args) | ||
|
||
expectErr := fmt.Errorf("Must explicitly specify \"--installed\" or \"--instantiated\"") | ||
if err := Cmd.Execute(); err == nil || err.Error() != expectErr.Error() { | ||
t.Errorf("Expect error: %s", expectErr) | ||
} | ||
|
||
resetFlags() | ||
|
||
// Wrong case: Miss "--intsalled" and "--instantiated" | ||
nilCmd := listCmd(mockCF) | ||
|
||
args = []string{} | ||
nilCmd.SetArgs(args) | ||
|
||
expectErr = fmt.Errorf("Must explicitly specify \"--installed\" or \"--instantiated\"") | ||
if err := nilCmd.Execute(); err == nil || err.Error() != expectErr.Error() { | ||
t.Errorf("Expect error: %s", expectErr) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters