|
| 1 | +package chaincode |
| 2 | + |
| 3 | +import ( |
| 4 | + pb "github.com/hyperledger/fabric-protos-go/peer" |
| 5 | + "github.com/hyperledger/fabric-sdk-go/pkg/client/resmgmt" |
| 6 | + "github.com/hyperledger/fabric-sdk-go/pkg/core/config" |
| 7 | + "github.com/hyperledger/fabric-sdk-go/pkg/fabsdk" |
| 8 | + "github.com/hyperledger/fabric/common/policydsl" |
| 9 | + "github.com/kfsoftware/hlf-operator/kubectl-hlf/cmd/helpers" |
| 10 | + "github.com/pkg/errors" |
| 11 | + log "github.com/sirupsen/logrus" |
| 12 | + "github.com/spf13/cobra" |
| 13 | + "io" |
| 14 | + "io/ioutil" |
| 15 | + "os" |
| 16 | + "strconv" |
| 17 | +) |
| 18 | + |
| 19 | +type getNextCmd struct { |
| 20 | + configPath string |
| 21 | + userName string |
| 22 | + channelName string |
| 23 | + name string |
| 24 | + mspID string |
| 25 | + property string |
| 26 | + outFile string |
| 27 | + peer string |
| 28 | + policy string |
| 29 | + initRequired bool |
| 30 | + collectionsConfig string |
| 31 | +} |
| 32 | + |
| 33 | +func (c *getNextCmd) validate() error { |
| 34 | + if c.property != "version" && c.property != "sequence" { |
| 35 | + return errors.New("property must be either version or sequence") |
| 36 | + } |
| 37 | + if c.outFile == "" { |
| 38 | + return errors.New("output file is required") |
| 39 | + } |
| 40 | + return nil |
| 41 | +} |
| 42 | +func (c *getNextCmd) run(out io.Writer, stdErr io.Writer) error { |
| 43 | + mspID := c.mspID |
| 44 | + configBackend := config.FromFile(c.configPath) |
| 45 | + sdk, err := fabsdk.New(configBackend) |
| 46 | + if err != nil { |
| 47 | + return err |
| 48 | + } |
| 49 | + org1AdminClientContext := sdk.Context( |
| 50 | + fabsdk.WithUser(c.userName), |
| 51 | + fabsdk.WithOrg(mspID), |
| 52 | + ) |
| 53 | + resClient, err := resmgmt.New(org1AdminClientContext) |
| 54 | + if err != nil { |
| 55 | + return err |
| 56 | + } |
| 57 | + committedCCs, err := resClient.LifecycleQueryCommittedCC(c.channelName, resmgmt.LifecycleQueryCommittedCCRequest{Name: c.name}) |
| 58 | + if err != nil { |
| 59 | + return err |
| 60 | + } |
| 61 | + if len(committedCCs) == 0 { |
| 62 | + return errors.New("no chaincode found") |
| 63 | + } |
| 64 | + var collections []*pb.CollectionConfig |
| 65 | + if c.collectionsConfig != "" { |
| 66 | + // |
| 67 | + pdcBytes, err := os.ReadFile(c.collectionsConfig) |
| 68 | + if err != nil { |
| 69 | + return err |
| 70 | + } |
| 71 | + collections, err = helpers.GetCollectionConfigFromBytes(pdcBytes) |
| 72 | + if err != nil { |
| 73 | + return err |
| 74 | + } |
| 75 | + } |
| 76 | + sp, err := policydsl.FromString(c.policy) |
| 77 | + if err != nil { |
| 78 | + return err |
| 79 | + } |
| 80 | + shouldCommit := len(committedCCs) == 0 |
| 81 | + if len(committedCCs) > 0 { |
| 82 | + firstCommittedCC := committedCCs[0] |
| 83 | + signaturePolicyString := firstCommittedCC.SignaturePolicy.String() |
| 84 | + newSignaturePolicyString := sp.String() |
| 85 | + if signaturePolicyString != newSignaturePolicyString { |
| 86 | + log.Debugf("Signature policy changed, old=%s new=%s", signaturePolicyString, newSignaturePolicyString) |
| 87 | + shouldCommit = true |
| 88 | + } else { |
| 89 | + log.Debugf("Signature policy not changed, signaturePolicy=%s", signaturePolicyString) |
| 90 | + } |
| 91 | + // compare collections |
| 92 | + oldCollections := firstCommittedCC.CollectionConfig |
| 93 | + newCollections := collections |
| 94 | + if len(oldCollections) != len(newCollections) { |
| 95 | + log.Infof("Collection config changed, old=%d new=%d", len(oldCollections), len(newCollections)) |
| 96 | + shouldCommit = true |
| 97 | + } else { |
| 98 | + for idx, oldCollection := range oldCollections { |
| 99 | + oldCollectionPayload := oldCollection.Payload.(*pb.CollectionConfig_StaticCollectionConfig) |
| 100 | + newCollection := newCollections[idx] |
| 101 | + newCollectionPayload := newCollection.Payload.(*pb.CollectionConfig_StaticCollectionConfig) |
| 102 | + if oldCollectionPayload.StaticCollectionConfig.Name != newCollectionPayload.StaticCollectionConfig.Name { |
| 103 | + log.Infof("Collection config changed, old=%s new=%s", oldCollectionPayload.StaticCollectionConfig.Name, newCollectionPayload.StaticCollectionConfig.Name) |
| 104 | + shouldCommit = true |
| 105 | + break |
| 106 | + } |
| 107 | + oldCollectionPolicy := oldCollection.GetStaticCollectionConfig().MemberOrgsPolicy |
| 108 | + newCollectionPolicy := newCollection.GetStaticCollectionConfig().MemberOrgsPolicy |
| 109 | + if oldCollectionPolicy.GetSignaturePolicy().String() != newCollectionPolicy.GetSignaturePolicy().String() { |
| 110 | + log.Infof("Collection config changed, old=%s new=%s", oldCollectionPolicy.GetSignaturePolicy().String(), newCollectionPolicy.GetSignaturePolicy().String()) |
| 111 | + shouldCommit = true |
| 112 | + break |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + latestCC := committedCCs[len(committedCCs)-1] |
| 119 | + var data []byte |
| 120 | + if c.property == "version" { |
| 121 | + data = []byte(latestCC.Version) |
| 122 | + } else { |
| 123 | + if shouldCommit { |
| 124 | + data = []byte(strconv.FormatInt(latestCC.Sequence+1, 10)) |
| 125 | + } else { |
| 126 | + data = []byte(strconv.FormatInt(latestCC.Sequence, 10)) |
| 127 | + } |
| 128 | + } |
| 129 | + err = ioutil.WriteFile(c.outFile, data, 0777) |
| 130 | + if err != nil { |
| 131 | + return err |
| 132 | + } |
| 133 | + return nil |
| 134 | +} |
| 135 | +func newGetNextCMD(out io.Writer, errOut io.Writer) *cobra.Command { |
| 136 | + c := &getNextCmd{} |
| 137 | + cmd := &cobra.Command{ |
| 138 | + Use: "getnext", |
| 139 | + RunE: func(cmd *cobra.Command, args []string) error { |
| 140 | + if err := c.validate(); err != nil { |
| 141 | + return err |
| 142 | + } |
| 143 | + return c.run(out, errOut) |
| 144 | + }, |
| 145 | + } |
| 146 | + persistentFlags := cmd.PersistentFlags() |
| 147 | + persistentFlags.StringVarP(&c.configPath, "config", "", "", "Configuration file for the SDK") |
| 148 | + persistentFlags.StringVarP(&c.userName, "user", "", "", "User name for the transaction") |
| 149 | + persistentFlags.StringVarP(&c.channelName, "channel", "", "", "Channel name") |
| 150 | + persistentFlags.StringVarP(&c.name, "name", "", "", "Chaincode name") |
| 151 | + persistentFlags.StringVarP(&c.mspID, "msp-id", "", "", "MSP ID of the organization") |
| 152 | + persistentFlags.StringVarP(&c.property, "property", "", "", "Property to get(\"version\" or \"sequence\")") |
| 153 | + persistentFlags.StringVarP(&c.outFile, "out", "o", "", "File to write the property to") |
| 154 | + persistentFlags.StringVarP(&c.peer, "peer", "p", "", "Peer org to invoke the updates") |
| 155 | + persistentFlags.StringVarP(&c.policy, "policy", "", "", "Policy") |
| 156 | + persistentFlags.BoolVarP(&c.initRequired, "init-required", "", false, "Init required") |
| 157 | + persistentFlags.StringVarP(&c.collectionsConfig, "collections-config", "", "", "Private data collections") |
| 158 | + |
| 159 | + cmd.MarkPersistentFlagRequired("user") |
| 160 | + cmd.MarkPersistentFlagRequired("config") |
| 161 | + cmd.MarkPersistentFlagRequired("channel") |
| 162 | + cmd.MarkPersistentFlagRequired("name") |
| 163 | + cmd.MarkPersistentFlagRequired("msp-id") |
| 164 | + cmd.MarkPersistentFlagRequired("out") |
| 165 | + return cmd |
| 166 | +} |
0 commit comments