From bd3bfeb92c088489bd9e1f8b7acdb44d1eca44f7 Mon Sep 17 00:00:00 2001 From: dviejokfs Date: Wed, 1 Feb 2023 14:34:42 +0100 Subject: [PATCH] Update --- cmd/start/start.go | 19 ++++++++++ gql/generated.go | 79 +++++++++++++++++++++++++++++++++++++++ gql/models/models.go | 19 +++++++--- gql/resolvers/mutation.go | 34 ++++++++++++++++- schema/mutation.graphql | 7 ++++ 5 files changed, 150 insertions(+), 8 deletions(-) diff --git a/cmd/start/start.go b/cmd/start/start.go index f0da8b5..72bb03c 100644 --- a/cmd/start/start.go +++ b/cmd/start/start.go @@ -141,6 +141,7 @@ hlf-cc-dev listen --forward-to=%s --tunnelAddress="xxx:8082" pdcContents = string(pdcContentsBytes) } var indices []*models.CouchDBIndex + var pdcIndices []*models.CouchDBIndexPdc if c.metaInf != "" { src := c.metaInf // walk through 3 file in the folder @@ -164,6 +165,21 @@ hlf-cc-dev listen --forward-to=%s --tunnelAddress="xxx:8082" Contents: string(contentBytes), } indices = append(indices, index) + log.Infof("found index file: %s - %s", relname, path.Base(relname)) + } + if strings.Contains(relname, "statedb/couchdb/collections") && !fi.IsDir() { + contentBytes, err := ioutil.ReadFile(file) + if err != nil { + return err + } + pdcName := path.Base(path.Join(relname, "../../")) + index := &models.CouchDBIndexPdc{ + ID: path.Base(relname), + PdcName: pdcName, + Contents: string(contentBytes), + } + pdcIndices = append(pdcIndices, index) + log.Infof("found index for PDC %s file: %s - %s", pdcName, relname, path.Base(relname)) } return nil }) @@ -171,6 +187,8 @@ hlf-cc-dev listen --forward-to=%s --tunnelAddress="xxx:8082" return err } } + log.Infof("found %d indexes %v", len(indices), indices) + log.Infof("found %d pdc indexes %v", len(pdcIndices), pdcIndices) input := models.DeployChaincodeInput{ Channel: &c.channel, Name: c.chaincode, @@ -178,6 +196,7 @@ hlf-cc-dev listen --forward-to=%s --tunnelAddress="xxx:8082" Pdc: pdcContents, SignaturePolicy: c.signaturePolicy, Indexes: indices, + PdcIndexes: pdcIndices, } var m struct { DeployChaincode struct { diff --git a/gql/generated.go b/gql/generated.go index 0e4fcec..96962fa 100644 --- a/gql/generated.go +++ b/gql/generated.go @@ -460,12 +460,19 @@ input DeployChaincodeInput { chaincodeAddress: String! signaturePolicy: String! indexes: [CouchDBIndex!] + pdcIndexes: [CouchDBIndexPDC!] channel: String } input CouchDBIndex { id: String! contents: String! } + +input CouchDBIndexPDC { + id: String! + pdcName: String! + contents: String! +} `, BuiltIn: false}, {Name: "schema/query.graphql", Input: `type Query { chaincodes(channel: String): [Chaincode!] @@ -2876,6 +2883,45 @@ func (ec *executionContext) unmarshalInputCouchDBIndex(ctx context.Context, obj return it, nil } +func (ec *executionContext) unmarshalInputCouchDBIndexPDC(ctx context.Context, obj interface{}) (models.CouchDBIndexPdc, error) { + var it models.CouchDBIndexPdc + asMap := map[string]interface{}{} + for k, v := range obj.(map[string]interface{}) { + asMap[k] = v + } + + for k, v := range asMap { + switch k { + case "id": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("id")) + it.ID, err = ec.unmarshalNString2string(ctx, v) + if err != nil { + return it, err + } + case "pdcName": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("pdcName")) + it.PdcName, err = ec.unmarshalNString2string(ctx, v) + if err != nil { + return it, err + } + case "contents": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("contents")) + it.Contents, err = ec.unmarshalNString2string(ctx, v) + if err != nil { + return it, err + } + } + } + + return it, nil +} + func (ec *executionContext) unmarshalInputCreateTenantInput(ctx context.Context, obj interface{}) (models.CreateTenantInput, error) { var it models.CreateTenantInput asMap := map[string]interface{}{} @@ -2956,6 +3002,14 @@ func (ec *executionContext) unmarshalInputDeployChaincodeInput(ctx context.Conte if err != nil { return it, err } + case "pdcIndexes": + var err error + + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("pdcIndexes")) + it.PdcIndexes, err = ec.unmarshalOCouchDBIndexPDC2ᚕᚖgithubᚗcomᚋkfsoftwareᚋhlfᚑccᚑdevᚋgqlᚋmodelsᚐCouchDBIndexPdcᚄ(ctx, v) + if err != nil { + return it, err + } case "channel": var err error @@ -4093,6 +4147,11 @@ func (ec *executionContext) unmarshalNCouchDBIndex2ᚖgithubᚗcomᚋkfsoftware return &res, graphql.ErrorOnPath(ctx, err) } +func (ec *executionContext) unmarshalNCouchDBIndexPDC2ᚖgithubᚗcomᚋkfsoftwareᚋhlfᚑccᚑdevᚋgqlᚋmodelsᚐCouchDBIndexPdc(ctx context.Context, v interface{}) (*models.CouchDBIndexPdc, error) { + res, err := ec.unmarshalInputCouchDBIndexPDC(ctx, v) + return &res, graphql.ErrorOnPath(ctx, err) +} + func (ec *executionContext) unmarshalNDeployChaincodeInput2githubᚗcomᚋkfsoftwareᚋhlfᚑccᚑdevᚋgqlᚋmodelsᚐDeployChaincodeInput(ctx context.Context, v interface{}) (models.DeployChaincodeInput, error) { res, err := ec.unmarshalInputDeployChaincodeInput(ctx, v) return res, graphql.ErrorOnPath(ctx, err) @@ -4562,6 +4621,26 @@ func (ec *executionContext) unmarshalOCouchDBIndex2ᚕᚖgithubᚗcomᚋkfsoftwa return res, nil } +func (ec *executionContext) unmarshalOCouchDBIndexPDC2ᚕᚖgithubᚗcomᚋkfsoftwareᚋhlfᚑccᚑdevᚋgqlᚋmodelsᚐCouchDBIndexPdcᚄ(ctx context.Context, v interface{}) ([]*models.CouchDBIndexPdc, error) { + if v == nil { + return nil, nil + } + var vSlice []interface{} + if v != nil { + vSlice = graphql.CoerceList(v) + } + var err error + res := make([]*models.CouchDBIndexPdc, len(vSlice)) + for i := range vSlice { + ctx := graphql.WithPathContext(ctx, graphql.NewPathWithIndex(i)) + res[i], err = ec.unmarshalNCouchDBIndexPDC2ᚖgithubᚗcomᚋkfsoftwareᚋhlfᚑccᚑdevᚋgqlᚋmodelsᚐCouchDBIndexPdc(ctx, vSlice[i]) + if err != nil { + return nil, err + } + } + return res, nil +} + func (ec *executionContext) unmarshalOOrgInput2ᚕᚖgithubᚗcomᚋkfsoftwareᚋhlfᚑccᚑdevᚋgqlᚋmodelsᚐOrgInputᚄ(ctx context.Context, v interface{}) ([]*models.OrgInput, error) { if v == nil { return nil, nil diff --git a/gql/models/models.go b/gql/models/models.go index 30b4660..ba4b43f 100644 --- a/gql/models/models.go +++ b/gql/models/models.go @@ -13,18 +13,25 @@ type CouchDBIndex struct { Contents string `json:"contents"` } +type CouchDBIndexPdc struct { + ID string `json:"id"` + PdcName string `json:"pdcName"` + Contents string `json:"contents"` +} + type CreateTenantInput struct { Name string `json:"name"` Orgs []*OrgInput `json:"orgs"` } type DeployChaincodeInput struct { - Name string `json:"name"` - Pdc string `json:"pdc"` - ChaincodeAddress string `json:"chaincodeAddress"` - SignaturePolicy string `json:"signaturePolicy"` - Indexes []*CouchDBIndex `json:"indexes"` - Channel *string `json:"channel"` + Name string `json:"name"` + Pdc string `json:"pdc"` + ChaincodeAddress string `json:"chaincodeAddress"` + SignaturePolicy string `json:"signaturePolicy"` + Indexes []*CouchDBIndex `json:"indexes"` + PdcIndexes []*CouchDBIndexPdc `json:"pdcIndexes"` + Channel *string `json:"channel"` } type DeployChaincodeResponse struct { diff --git a/gql/resolvers/mutation.go b/gql/resolvers/mutation.go index 00fd6c0..0e3c710 100644 --- a/gql/resolvers/mutation.go +++ b/gql/resolvers/mutation.go @@ -26,6 +26,7 @@ import ( "github.com/lithammer/shortuuid/v3" "github.com/pkg/errors" "io" + "io/ioutil" "net" "strings" "sync" @@ -97,7 +98,14 @@ func getChaincodePackage(label string, codeTarGz []byte) ([]byte, error) { return buf.Bytes(), nil } -func getCodeTarGz(address string, rootCert string, clientKey string, clientCert string, couchDBIndices []*models.CouchDBIndex) ([]byte, error) { +func getCodeTarGz( + address string, + rootCert string, + clientKey string, + clientCert string, + couchDBIndices []*models.CouchDBIndex, + couchDBIndicesPDC []*models.CouchDBIndexPdc, +) ([]byte, error) { var err error connMap := map[string]interface{}{ "address": address, @@ -148,7 +156,24 @@ func getCodeTarGz(address string, rootCert string, clientKey string, clientCert return nil, err } } - + } + if len(couchDBIndicesPDC) > 0 { + for _, pdcCouchDBIndex := range couchDBIndicesPDC { + header := new(tar.Header) + contentsBytes := []byte(pdcCouchDBIndex.Contents) + header.Mode = 0755 + header.Size = int64(len(contentsBytes)) + header.Name = fmt.Sprintf("META-INF/statedb/couchdb/collections/%s/indexes/%s", pdcCouchDBIndex.PdcName, pdcCouchDBIndex.ID) + // write header + if err := tw.WriteHeader(header); err != nil { + return nil, err + } + // if not a dir, write file content + r := bytes.NewReader(contentsBytes) + if _, err := io.Copy(tw, r); err != nil { + return nil, err + } + } } err = tw.Close() if err != nil { @@ -229,7 +254,11 @@ func (m mutationResolver) DeployChaincode(ctx context.Context, input models.Depl privateKey, certificate, input.Indexes, + input.PdcIndexes, ) + if err != nil { + return nil, err + } resClient, err := resmgmt.New(m.SDKContext) if err != nil { return nil, err @@ -265,6 +294,7 @@ func (m mutationResolver) DeployChaincode(ctx context.Context, input models.Depl if err != nil { return nil, err } + ioutil.WriteFile("chaincode.tar.gz", pkg, 0644) packageID := lifecycle.ComputePackageID(chaincodeName, pkg) signaturePolicy := input.SignaturePolicy sp, err := policydsl.FromString(signaturePolicy) diff --git a/schema/mutation.graphql b/schema/mutation.graphql index ad38739..46147dd 100644 --- a/schema/mutation.graphql +++ b/schema/mutation.graphql @@ -67,9 +67,16 @@ input DeployChaincodeInput { chaincodeAddress: String! signaturePolicy: String! indexes: [CouchDBIndex!] + pdcIndexes: [CouchDBIndexPDC!] channel: String } input CouchDBIndex { id: String! contents: String! } + +input CouchDBIndexPDC { + id: String! + pdcName: String! + contents: String! +}