Skip to content

Commit

Permalink
Take number of organizations to approve from channel
Browse files Browse the repository at this point in the history
  • Loading branch information
dviejokfs committed Apr 14, 2022
1 parent daad665 commit 32219e5
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 35 deletions.
1 change: 1 addition & 0 deletions cmd/serve/serve.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,7 @@ func (c *serveCmd) run(conf *serveConfig) error {
opts := server.BlockchainServerOpts{
Address: c.address,
MetricsAddress: c.metricsAddress,
ConfigBackend: configBackend,
SDK: sdk,
GWClient: gwClient,
SDKContext: sdkContext,
Expand Down
7 changes: 3 additions & 4 deletions cmd/start/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type startCmd struct {
signaturePolicy string
envFile string
tunnelAddress string
channel string
}

func (c startCmd) validate() error {
Expand Down Expand Up @@ -131,10 +132,6 @@ hlf-cc-dev listen --forward-to=%s --tunnelAddress="xxx:8082"
}

chaincodeAddress := tunnelCFGItem.SNI
//chaincodeAddress, _, err := net.SplitHostPort(fullChaincodeAddress)
//if err != nil {
// return errors.Wrapf(err, "failed to parse chaincode address %s", fullChaincodeAddress)
//}
pdcContents := ""
if c.pdcFile != "" {
pdcContentsBytes, err := ioutil.ReadFile(c.pdcFile)
Expand Down Expand Up @@ -175,6 +172,7 @@ hlf-cc-dev listen --forward-to=%s --tunnelAddress="xxx:8082"
}
}
input := models.DeployChaincodeInput{
Channel: c.channel,
Name: c.chaincode,
ChaincodeAddress: chaincodeAddress,
Pdc: pdcContents,
Expand Down Expand Up @@ -313,5 +311,6 @@ func NewStartCmd() *cobra.Command {
f.StringVar(&c.metaInf, "metaInf", "", "metadata")
f.StringVar(&c.signaturePolicy, "signaturePolicy", "", "Signature policy")
f.StringVar(&c.envFile, "env-file", "", "Env file to write the environments")
f.StringVar(&c.channel, "channel", "", "Channel name")
return cmd
}
62 changes: 49 additions & 13 deletions gql/generated.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

41 changes: 35 additions & 6 deletions gql/resolvers/mutation.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,12 +10,16 @@ import (
"encoding/pem"
"fmt"
"github.com/gosimple/slug"
"github.com/hyperledger/fabric-config/configtx"
"github.com/hyperledger/fabric-gateway/pkg/client"
pb "github.com/hyperledger/fabric-protos-go/peer"
clientmsp "github.com/hyperledger/fabric-sdk-go/pkg/client/msp"
"github.com/hyperledger/fabric-sdk-go/pkg/client/resmgmt"
hlfcontext "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/context"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/ccpackager/lifecycle"
"github.com/hyperledger/fabric-sdk-go/pkg/fab/resource"
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
"github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/common/policydsl"
"github.com/kfsoftware/hlf-cc-dev/gql/models"
"github.com/kfsoftware/hlf-cc-dev/log"
Expand Down Expand Up @@ -167,6 +171,10 @@ func (f *mspFilter) Accept(peer fab.Peer) bool {
}
func (m mutationResolver) DeployChaincode(ctx context.Context, input models.DeployChaincodeInput) (*models.DeployChaincodeResponse, error) {
chaincodeName := slug.Make(input.Name)
channel := m.Channel
if input.Channel != "" {
channel = input.Channel
}
address := input.ChaincodeAddress
host, _, err := net.SplitHostPort(address)
if err != nil {
Expand Down Expand Up @@ -226,6 +234,30 @@ func (m mutationResolver) DeployChaincode(ctx context.Context, input models.Depl
if err != nil {
return nil, err
}
block, err := resClient.QueryConfigBlockFromOrderer(channel)
if err != nil {
return nil, err
}
cfgBlock, err := resource.ExtractConfigFromBlock(block)
if err != nil {
return nil, err
}
cftxGen := configtx.New(cfgBlock)
appConf, err := cftxGen.Application().Configuration()
if err != nil {
return nil, err
}
mapSdkContext := map[string]hlfcontext.ClientProvider{}
for _, organization := range appConf.Organizations {
sdk, err := fabsdk.New(m.ConfigBackend)
if err != nil {
return nil, err
}
mapSdkContext[organization.Name] = sdk.Context(
fabsdk.WithUser(m.User),
fabsdk.WithOrg(organization.MSP.Name),
)
}
version := "1"
sequence := 1

Expand All @@ -239,10 +271,7 @@ func (m mutationResolver) DeployChaincode(ctx context.Context, input models.Depl
if err != nil {
return nil, err
}
channel := m.Channel
if input.Channel != "" {
channel = input.Channel
}

committedCCs, err := resClient.LifecycleQueryCommittedCC(channel, resmgmt.LifecycleQueryCommittedCCRequest{Name: chaincodeName})
if err != nil {
log.Warnf("Error when getting commited chaincodes: %v", err)
Expand Down Expand Up @@ -271,8 +300,8 @@ func (m mutationResolver) DeployChaincode(ctx context.Context, input models.Depl
InitRequired: false,
}
var wg sync.WaitGroup
wg.Add(len(m.SDKContextMap))
for mspID, sdkContext := range m.SDKContextMap {
wg.Add(len(mapSdkContext))
for mspID, sdkContext := range mapSdkContext {
mspID := mspID
sdkContext := sdkContext
go func() {
Expand Down
16 changes: 12 additions & 4 deletions gql/resolvers/query.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,16 @@ import (
"github.com/kfsoftware/hlf-cc-dev/gql/models"
)

func (r *queryResolver) Chaincodes(ctx context.Context) ([]*models.Chaincode, error) {
func (r *queryResolver) Chaincodes(ctx context.Context, channel *string) ([]*models.Chaincode, error) {
resClient, err := resmgmt.New(r.SDKContext)
if err != nil {
return nil, err
}
committedCCs, err := resClient.LifecycleQueryCommittedCC(r.Channel, resmgmt.LifecycleQueryCommittedCCRequest{})
channelName := r.Channel
if channel != nil && *channel != "" {
channelName = *channel
}
committedCCs, err := resClient.LifecycleQueryCommittedCC(channelName, resmgmt.LifecycleQueryCommittedCCRequest{})
if err != nil {
return nil, err
}
Expand All @@ -26,12 +30,16 @@ func (r *queryResolver) Chaincodes(ctx context.Context) ([]*models.Chaincode, er
return chaincodes, nil
}

func (r *queryResolver) Chaincode(ctx context.Context, name string) (*models.Chaincode, error) {
func (r *queryResolver) Chaincode(ctx context.Context, channel *string, name string) (*models.Chaincode, error) {
channelName := r.Channel
if channel != nil && *channel != "" {
channelName = *channel
}
resClient, err := resmgmt.New(r.SDKContext)
if err != nil {
return nil, err
}
committedCCs, err := resClient.LifecycleQueryCommittedCC(r.Channel, resmgmt.LifecycleQueryCommittedCCRequest{
committedCCs, err := resClient.LifecycleQueryCommittedCC(channelName, resmgmt.LifecycleQueryCommittedCCRequest{
Name: name,
})
if err != nil {
Expand Down
3 changes: 2 additions & 1 deletion gql/resolvers/resolvers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"github.com/hyperledger/fabric-gateway/pkg/client"
clientmsp "github.com/hyperledger/fabric-sdk-go/pkg/client/msp"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/context"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/msp"
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
"github.com/kfsoftware/hlf-cc-dev/gql"
Expand All @@ -16,6 +17,7 @@ type Resolver struct {
GWClient *client.Gateway
SDKContext context.ClientProvider
SDKContextMap map[string]context.ClientProvider
ConfigBackend core.ConfigProvider
Channel string
MSPClient *clientmsp.Client
CAConfig *msp.CAConfig
Expand All @@ -32,4 +34,3 @@ func (r *Resolver) Query() gql.QueryResolver { return &queryResolver{r} }
type mutationResolver struct{ *Resolver }

type queryResolver struct{ *Resolver }

4 changes: 2 additions & 2 deletions schema/query.graphql
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
type Query {
chaincodes: [Chaincode!]
chaincode(name: String!): Chaincode
chaincodes(channel: String): [Chaincode!]
chaincode(channel: String, name: String!): Chaincode
}

type Chaincode {
Expand Down
13 changes: 8 additions & 5 deletions server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/hyperledger/fabric-gateway/pkg/client"
clientmsp "github.com/hyperledger/fabric-sdk-go/pkg/client/msp"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/context"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/core"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/msp"
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
"github.com/kfsoftware/hlf-cc-dev/gql"
Expand Down Expand Up @@ -37,11 +38,12 @@ type BlockchainServerOpts struct {
SDKContextMap map[string]context.ClientProvider
GWClient *client.Gateway

Channel string
MSPClient *clientmsp.Client
CAConfig *msp.CAConfig
Organization string
User string
Channel string
MSPClient *clientmsp.Client
CAConfig *msp.CAConfig
Organization string
User string
ConfigBackend core.ConfigProvider
}

type BlockchainAPIServer struct {
Expand Down Expand Up @@ -82,6 +84,7 @@ func (a *BlockchainAPIServer) setupHttpServer() http.Handler {
CAConfig: a.CAConfig,
SDKContextMap: a.SDKContextMap,
Organization: a.Organization,
ConfigBackend: a.ConfigBackend,
User: a.User,
GWClient: a.GWClient,
},
Expand Down

0 comments on commit 32219e5

Please sign in to comment.