Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] 1st attempt for build peer cli #146

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 23 additions & 5 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,19 @@ require (
github.com/onsi/ginkgo/v2 v2.8.2
github.com/onsi/gomega v1.27.0
github.com/opentracing/opentracing-go v1.2.0
google.golang.org/grpc v1.57.0
github.com/spf13/cobra v1.7.0
github.com/spf13/viper v1.17.0
google.golang.org/grpc v1.58.2
google.golang.org/protobuf v1.31.0
gopkg.in/yaml.v2 v2.4.0
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/davecgh/go-spew v1.1.2-0.20180830191138-d8f796af33cc // indirect
github.com/eapache/go-resiliency v1.4.0 // indirect
github.com/eapache/go-xerial-snappy v0.0.0-20230731223053-c322873962e3 // indirect
github.com/eapache/queue v1.1.0 // indirect
github.com/fsnotify/fsnotify v1.6.0 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-task/slim-sprig v0.0.0-20210107165309-348f09dbbbc0 // indirect
github.com/golang/protobuf v1.5.3 // indirect
Expand All @@ -33,21 +36,36 @@ require (
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/hashicorp/go-uuid v1.0.3 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/jcmturner/aescts/v2 v2.0.0 // indirect
github.com/jcmturner/dnsutils/v2 v2.0.0 // indirect
github.com/jcmturner/gofork v1.7.6 // indirect
github.com/jcmturner/gokrb5/v8 v8.4.4 // indirect
github.com/jcmturner/rpc/v2 v2.0.3 // indirect
github.com/klauspost/compress v1.16.7 // indirect
github.com/klauspost/compress v1.17.0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/miekg/pkcs11 v1.1.1 // indirect
github.com/pelletier/go-toml/v2 v2.1.0 // indirect
github.com/pierrec/lz4/v4 v4.1.18 // indirect
github.com/rcrowley/go-metrics v0.0.0-20201227073835-cf1acfcdf475 // indirect
github.com/rogpeppe/go-internal v1.11.0 // indirect
github.com/sagikazarmark/locafero v0.3.0 // indirect
github.com/sagikazarmark/slog-shim v0.1.0 // indirect
github.com/sourcegraph/conc v0.3.0 // indirect
github.com/spf13/afero v1.10.0 // indirect
github.com/spf13/cast v1.5.1 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/subosito/gotenv v1.6.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/multierr v1.9.0 // indirect
golang.org/x/crypto v0.14.0 // indirect
golang.org/x/exp v0.0.0-20230905200255-921286631fa9 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
golang.org/x/text v0.13.0 // indirect
golang.org/x/tools v0.6.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230815205213-6bfd019c3878 // indirect
golang.org/x/tools v0.13.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230920204549-e6e6cdab5c13 // indirect
gopkg.in/ini.v1 v1.67.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
414 changes: 401 additions & 13 deletions go.sum

Large diffs are not rendered by default.

1 change: 1 addition & 0 deletions pkg/cmd/osadmin/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
package osadmin
47 changes: 47 additions & 0 deletions pkg/cmd/peer/common/common.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
package common

import (
"github.com/hyperledger/fabric-admin-sdk/pkg/identity"
"github.com/hyperledger/fabric-admin-sdk/pkg/network"
"google.golang.org/grpc"
)

type ConnectionDetail struct {
ID identity.SigningIdentity
Connection grpc.ClientConnInterface
}

func ConstructConnectionDetail(mspID string, SignCert string, PrivKeyPath string, orgPeerAddress string, TLSCACert string) (*ConnectionDetail, error) {
peer := network.Node{
Addr: orgPeerAddress,
TLSCACert: TLSCACert,
}

err := peer.LoadConfig()
if err != nil {
return nil, err
}
peerConnection, err := network.DialConnection(peer)
if err != nil {
return nil, err
}

cert, _, err := identity.ReadCertificate(SignCert)
if err != nil {
return nil, err
}

priv, err := identity.ReadECDSAPrivateKey(PrivKeyPath)
if err != nil {
return nil, err
}

orgMSP, err := identity.NewPrivateKeySigningIdentity(mspID, cert, priv)
if err != nil {
return nil, err
}
return &ConnectionDetail{
Connection: peerConnection,
ID: orgMSP,
}, nil
}
29 changes: 29 additions & 0 deletions pkg/cmd/peer/lifecycle/approve.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package lifecycle

/*
time.Sleep(time.Duration(20) * time.Second)
PolicyStr := "AND ('Org1MSP.peer','Org2MSP.peer')"
applicationPolicy, err := chaincode.NewApplicationPolicy(PolicyStr, "")
Expect(err).NotTo(HaveOccurred())
chaincodeDef := &chaincode.Definition{
ChannelName: channelName,
PackageID: "",
Name: "basic",
Version: "1.0",
EndorsementPlugin: "",
ValidationPlugin: "",
Sequence: 1,
ApplicationPolicy: applicationPolicy,
InitRequired: false,
Collections: nil,
}
Expect(err).NotTo(HaveOccurred())
// Approve chaincode for each org
runParallel(peerConnections, func(target *ConnectionDetails) {
ctx, cancel := context.WithTimeout(specCtx, 30*time.Second)
defer cancel()
err := chaincode.Approve(ctx, target.connection, target.id, chaincodeDef)
printGrpcError(err)
Expect(err).NotTo(HaveOccurred(), "approve chaincode for org %s", target.id.MspID())
})
*/
12 changes: 12 additions & 0 deletions pkg/cmd/peer/lifecycle/checkreadiness.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package lifecycle

/*
// Check chaincode commit readiness
readinessCtx, readinessCancel := context.WithTimeout(specCtx, 30*time.Second)
defer readinessCancel()
readinessResult, err := chaincode.CheckCommitReadiness(readinessCtx, peer1Connection, org1MSP, chaincodeDef)
printGrpcError(err)
Expect(err).NotTo(HaveOccurred(), "check commit readiness")
Expect(readinessResult.GetApprovals()).To(Equal((map[string]bool{org1MspID: true, org2MspID: true})))

*/
10 changes: 10 additions & 0 deletions pkg/cmd/peer/lifecycle/commit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package lifecycle

/*
// Commit chaincode
commitCtx, commitCancel := context.WithTimeout(specCtx, 30*time.Second)
defer commitCancel()
err = chaincode.Commit(commitCtx, peer1Connection, org1MSP, chaincodeDef)
printGrpcError(err)
Expect(err).NotTo(HaveOccurred(), "commit chaincode")
*/
13 changes: 13 additions & 0 deletions pkg/cmd/peer/lifecycle/getinstall.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package lifecycle

/*
// Get installed chaincode package from each peer
runParallel(peerConnections, func(target *ConnectionDetails) {
ctx, cancel := context.WithTimeout(specCtx, 30*time.Second)
defer cancel()
result, err := chaincode.GetInstalled(ctx, target.connection, target.id, packageID)
printGrpcError(err)
Expect(err).NotTo(HaveOccurred(), "get installed chaincode package")
Expect(result).NotTo(BeEmpty())
})
*/
55 changes: 55 additions & 0 deletions pkg/cmd/peer/lifecycle/install.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package lifecycle

import (
"bytes"
"context"
"fmt"
"io"
"os"
"time"

"github.com/hyperledger/fabric-admin-sdk/pkg/chaincode"
"github.com/hyperledger/fabric-admin-sdk/pkg/cmd/peer/common"
"github.com/spf13/cobra"
)

var packageFilePath string

func InstallCmd() *cobra.Command {
chaincodeInstallCmd := &cobra.Command{
Use: "install",
Short: "Install a chaincode.",
Long: "Install a chaincode on a peer.",
ValidArgs: []string{"1"},
RunE: func(cmd *cobra.Command, args []string) error {
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Minute)
defer cancel()
ConnectionDetail, err := common.ConstructConnectionDetail(mspID, SignCert, PrivKeyPath, orgPeerAddress, TLSCACert)
if err != nil {
return err
}
packageReader, err := os.Open(packageFilePath)
if err != nil {
return err
}
chaincodePackage, err := io.ReadAll(packageReader)
if err != nil {
return err
}
result, err := chaincode.Install(ctx, ConnectionDetail.Connection, ConnectionDetail.ID, bytes.NewReader(chaincodePackage))
if err != nil {
return err
}
fmt.Printf("Install success, package id [%s], package label [%s] \n", result.GetPackageId(), result.GetLabel())
return nil
},
}
addInstallFlags(chaincodeInstallCmd)
return chaincodeInstallCmd
}

func addInstallFlags(cmd *cobra.Command) {
flags := cmd.PersistentFlags()
flags.StringVarP(&packageFilePath, "packageFilePath", "", "",
"packageFilePath for chaincode")
}
39 changes: 39 additions & 0 deletions pkg/cmd/peer/lifecycle/lifecycle.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package lifecycle

import (
"github.com/spf13/cobra"
)

var (
mspID string
SignCert string
PrivKeyPath string
orgPeerAddress string
TLSCACert string
)

// Cmd returns the cobra command for lifecycle
func Cmd() *cobra.Command {
lifecycleCmd := &cobra.Command{
Use: "lifecycle",
Short: "Perform _lifecycle operations",
Long: "Perform _lifecycle operations",
}
addPeerCommonFlags(lifecycleCmd)
lifecycleCmd.AddCommand(InstallCmd())
return lifecycleCmd
}

func addPeerCommonFlags(cmd *cobra.Command) {
flags := cmd.PersistentFlags()
flags.StringVarP(&mspID, "msp", "", "",
"msp id for your msp")
flags.StringVarP(&SignCert, "SignCert", "", "",
"Sign Cert for your msp")
flags.StringVarP(&PrivKeyPath, "PrivKeyPath", "", "",
"Priv Key Path for your msp")
flags.StringVarP(&orgPeerAddress, "PeerAddress", "", "",
"Target Peer address")
flags.StringVarP(&TLSCACert, "TLSCACert", "", "",
"TLS CA Cert to connect to peer")
}
15 changes: 15 additions & 0 deletions pkg/cmd/peer/lifecycle/query.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package lifecycle

/*
runParallel(peerConnections, func(target *ConnectionDetails) {
ctx, cancel := context.WithTimeout(specCtx, 30*time.Second)
defer cancel()
result, err := chaincode.QueryInstalled(ctx, target.connection, target.id)
printGrpcError(err)
Expect(err).NotTo(HaveOccurred(), "query installed chaincode")
installedChaincodes := result.GetInstalledChaincodes()
Expect(installedChaincodes).To(HaveLen(1), "number of installed chaincodes")
Expect(installedChaincodes[0].GetPackageId()).To(Equal(packageID), "installed chaincode package ID")
Expect(installedChaincodes[0].GetLabel()).To(Equal(dummyMeta.Label), "installed chaincode label")
})
*/
14 changes: 14 additions & 0 deletions pkg/cmd/peer/lifecycle/queryallcommit.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package lifecycle

/*
// Query all committed chaincode
committedCtx, committedCancel := context.WithTimeout(specCtx, 30*time.Second)
defer committedCancel()
committedResult, err := chaincode.QueryCommitted(committedCtx, peer1Connection, org1MSP, channelName)
printGrpcError(err)
Expect(err).NotTo(HaveOccurred(), "query all committed chaincodes")
committedChaincodes := committedResult.GetChaincodeDefinitions()
Expect(committedChaincodes).To(HaveLen(1), "number of committed chaincodes")
Expect(committedChaincodes[0].GetName()).To(Equal("basic"), "committed chaincode name")
Expect(committedChaincodes[0].GetSequence()).To(Equal(int64(1)), "committed chaincode sequence")
*/
12 changes: 12 additions & 0 deletions pkg/cmd/peer/lifecycle/queryapprove.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package lifecycle

/*
runParallel(peerConnections, func(target *ConnectionDetails) {
ctx, cancel := context.WithTimeout(specCtx, 30*time.Second)
defer cancel()
result, err := chaincode.QueryApproved(ctx, target.connection, target.id, channelName, chaincodeDef.Name, chaincodeDef.Sequence)
printGrpcError(err)
Expect(err).NotTo(HaveOccurred(), "query approved chaincode for org %s", target.id.MspID())
Expect(result.GetVersion()).To(Equal(chaincodeDef.Version))
})
*/
12 changes: 12 additions & 0 deletions pkg/cmd/peer/lifecycle/querycommited.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package lifecycle

/*
// Query named committed chaincode
committedWithNameCtx, committedWithNameCancel := context.WithTimeout(specCtx, 30*time.Second)
defer committedWithNameCancel()
committedWithNameResult, err := chaincode.QueryCommittedWithName(committedWithNameCtx, peer1Connection, org1MSP, channelName, chaincodeDef.Name)
printGrpcError(err)
Expect(err).NotTo(HaveOccurred(), "query committed chaincode with name")
Expect(committedWithNameResult.GetApprovals()).To(Equal(map[string]bool{org1MspID: true, org2MspID: true}), "committed chaincode approvals")
Expect(committedWithNameResult.GetSequence()).To(Equal(chaincodeDef.Sequence), "committed chaincode sequence")
*/
31 changes: 31 additions & 0 deletions pkg/cmd/peer/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package peer

import (
"os"

"github.com/hyperledger/fabric-admin-sdk/pkg/cmd/peer/lifecycle"
"github.com/hyperledger/fabric-admin-sdk/pkg/cmd/peer/version"
"github.com/spf13/cobra"
"github.com/spf13/viper"
)

var mainCmd = &cobra.Command{Use: "peer"}

func main() {
// Define command-line flags that are valid for all peer commands and
// subcommands.
mainFlags := mainCmd.PersistentFlags()

mainFlags.String("logging-level", "", "Legacy logging level flag")
viper.BindPFlag("logging_level", mainFlags.Lookup("logging-level"))
mainFlags.MarkHidden("logging-level")

mainCmd.AddCommand(version.Cmd())
mainCmd.AddCommand(lifecycle.Cmd())

// On failure Cobra prints the usage message and error string, so we only
// need to exit with a non-0 status
if mainCmd.Execute() != nil {
os.Exit(1)
}
}
Loading
Loading