Skip to content

Commit

Permalink
Add command to get the next sequence and version for a chaincode base…
Browse files Browse the repository at this point in the history
…d on chaincode definition

Signed-off-by: David VIEJO <[email protected]>
  • Loading branch information
dviejokfs committed Jan 19, 2024
1 parent 4de004c commit b190a10
Show file tree
Hide file tree
Showing 3 changed files with 168 additions and 5 deletions.
1 change: 1 addition & 0 deletions kubectl-hlf/cmd/chaincode/chaincode.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ func NewChaincodeCmd(stdOut io.Writer, stdErr io.Writer) *cobra.Command {
newCalculatePackageIDCMD(stdOut, stdErr),
newGetLatestInfoCMD(stdOut, stdErr),
newCheckCommitReadiness(stdOut, stdErr),
newGetNextCMD(stdOut, stdErr),
)
return consortiumCmd
}
166 changes: 166 additions & 0 deletions kubectl-hlf/cmd/chaincode/getnext.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,166 @@
package chaincode

import (
pb "github.com/hyperledger/fabric-protos-go/peer"
"github.com/hyperledger/fabric-sdk-go/pkg/client/resmgmt"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config"
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
"github.com/hyperledger/fabric/common/policydsl"
"github.com/kfsoftware/hlf-operator/kubectl-hlf/cmd/helpers"
"github.com/pkg/errors"
log "github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"io"
"io/ioutil"
"os"
"strconv"
)

type getNextCmd struct {
configPath string
userName string
channelName string
name string
mspID string
property string
outFile string
peer string
policy string
initRequired bool
collectionsConfig string
}

func (c *getNextCmd) validate() error {
if c.property != "version" && c.property != "sequence" {
return errors.New("property must be either version or sequence")
}
if c.outFile == "" {
return errors.New("output file is required")
}
return nil
}
func (c *getNextCmd) run(out io.Writer, stdErr io.Writer) error {
mspID := c.mspID
configBackend := config.FromFile(c.configPath)
sdk, err := fabsdk.New(configBackend)
if err != nil {
return err
}
org1AdminClientContext := sdk.Context(
fabsdk.WithUser(c.userName),
fabsdk.WithOrg(mspID),
)
resClient, err := resmgmt.New(org1AdminClientContext)
if err != nil {
return err
}
committedCCs, err := resClient.LifecycleQueryCommittedCC(c.channelName, resmgmt.LifecycleQueryCommittedCCRequest{Name: c.name})
if err != nil {
return err
}
if len(committedCCs) == 0 {
return errors.New("no chaincode found")
}
var collections []*pb.CollectionConfig
if c.collectionsConfig != "" {
//
pdcBytes, err := os.ReadFile(c.collectionsConfig)
if err != nil {
return err
}
collections, err = helpers.GetCollectionConfigFromBytes(pdcBytes)
if err != nil {
return err
}
}
sp, err := policydsl.FromString(c.policy)
if err != nil {
return err
}
shouldCommit := len(committedCCs) == 0
if len(committedCCs) > 0 {
firstCommittedCC := committedCCs[0]
signaturePolicyString := firstCommittedCC.SignaturePolicy.String()
newSignaturePolicyString := sp.String()
if signaturePolicyString != newSignaturePolicyString {
log.Debugf("Signature policy changed, old=%s new=%s", signaturePolicyString, newSignaturePolicyString)
shouldCommit = true
} else {
log.Debugf("Signature policy not changed, signaturePolicy=%s", signaturePolicyString)
}
// compare collections
oldCollections := firstCommittedCC.CollectionConfig
newCollections := collections
if len(oldCollections) != len(newCollections) {
log.Infof("Collection config changed, old=%d new=%d", len(oldCollections), len(newCollections))
shouldCommit = true
} else {
for idx, oldCollection := range oldCollections {
oldCollectionPayload := oldCollection.Payload.(*pb.CollectionConfig_StaticCollectionConfig)
newCollection := newCollections[idx]
newCollectionPayload := newCollection.Payload.(*pb.CollectionConfig_StaticCollectionConfig)
if oldCollectionPayload.StaticCollectionConfig.Name != newCollectionPayload.StaticCollectionConfig.Name {
log.Infof("Collection config changed, old=%s new=%s", oldCollectionPayload.StaticCollectionConfig.Name, newCollectionPayload.StaticCollectionConfig.Name)
shouldCommit = true
break
}
oldCollectionPolicy := oldCollection.GetStaticCollectionConfig().MemberOrgsPolicy
newCollectionPolicy := newCollection.GetStaticCollectionConfig().MemberOrgsPolicy
if oldCollectionPolicy.GetSignaturePolicy().String() != newCollectionPolicy.GetSignaturePolicy().String() {
log.Infof("Collection config changed, old=%s new=%s", oldCollectionPolicy.GetSignaturePolicy().String(), newCollectionPolicy.GetSignaturePolicy().String())
shouldCommit = true
break
}
}
}
}

latestCC := committedCCs[len(committedCCs)-1]
var data []byte
if c.property == "version" {
data = []byte(latestCC.Version)
} else {
if shouldCommit {
data = []byte(strconv.FormatInt(latestCC.Sequence+1, 10))
} else {
data = []byte(strconv.FormatInt(latestCC.Sequence, 10))
}
}
err = ioutil.WriteFile(c.outFile, data, 0777)
if err != nil {
return err
}
return nil
}
func newGetNextCMD(out io.Writer, errOut io.Writer) *cobra.Command {
c := &getNextCmd{}
cmd := &cobra.Command{
Use: "getnext",
RunE: func(cmd *cobra.Command, args []string) error {
if err := c.validate(); err != nil {
return err
}
return c.run(out, errOut)
},
}
persistentFlags := cmd.PersistentFlags()
persistentFlags.StringVarP(&c.configPath, "config", "", "", "Configuration file for the SDK")
persistentFlags.StringVarP(&c.userName, "user", "", "", "User name for the transaction")
persistentFlags.StringVarP(&c.channelName, "channel", "", "", "Channel name")
persistentFlags.StringVarP(&c.name, "name", "", "", "Chaincode name")
persistentFlags.StringVarP(&c.mspID, "msp-id", "", "", "MSP ID of the organization")
persistentFlags.StringVarP(&c.property, "property", "", "", "Property to get(\"version\" or \"sequence\")")
persistentFlags.StringVarP(&c.outFile, "out", "o", "", "File to write the property to")
persistentFlags.StringVarP(&c.peer, "peer", "p", "", "Peer org to invoke the updates")
persistentFlags.StringVarP(&c.policy, "policy", "", "", "Policy")
persistentFlags.BoolVarP(&c.initRequired, "init-required", "", false, "Init required")
persistentFlags.StringVarP(&c.collectionsConfig, "collections-config", "", "", "Private data collections")

cmd.MarkPersistentFlagRequired("user")
cmd.MarkPersistentFlagRequired("config")
cmd.MarkPersistentFlagRequired("channel")
cmd.MarkPersistentFlagRequired("name")
cmd.MarkPersistentFlagRequired("msp-id")
cmd.MarkPersistentFlagRequired("out")
return cmd
}
6 changes: 1 addition & 5 deletions kubectl-hlf/cmd/chaincode/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@ package chaincode
import (
"encoding/json"
"fmt"
"io"
"time"

"github.com/hyperledger/fabric-sdk-go/pkg/client/channel"
"github.com/hyperledger/fabric-sdk-go/pkg/core/config"
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
"github.com/kfsoftware/hlf-operator/kubectl-hlf/cmd/helpers"
"github.com/spf13/cobra"
"io"
)

type queryChaincodeCmd struct {
Expand Down Expand Up @@ -73,7 +71,6 @@ func (c *queryChaincodeCmd) run(out io.Writer) error {
return err
}
}
t := time.Now()
response, err := ch.Query(
channel.Request{
ChaincodeID: c.chaincode,
Expand All @@ -88,7 +85,6 @@ func (c *queryChaincodeCmd) run(out io.Writer) error {
if err != nil {
return err
}
fmt.Printf("Query took %s\n", time.Since(t))
_, err = fmt.Fprint(out, string(response.Payload))
if err != nil {
return err
Expand Down

0 comments on commit b190a10

Please sign in to comment.