-
Notifications
You must be signed in to change notification settings - Fork 91
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add command to get the next sequence and version for a chaincode base…
…d on chaincode definition Signed-off-by: David VIEJO <[email protected]>
- Loading branch information
Showing
3 changed files
with
168 additions
and
5 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
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 | ||
} |
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