Skip to content

Instantly share code, notes, and snippets.

@thekaleidoscope
Last active January 7, 2019 12:31
Show Gist options
  • Select an option

  • Save thekaleidoscope/ba7a288ef735e1ab4af7e1109d70cce7 to your computer and use it in GitHub Desktop.

Select an option

Save thekaleidoscope/ba7a288ef735e1ab4af7e1109d70cce7 to your computer and use it in GitHub Desktop.
FabricEndpointConfig
package main
import (
"fmt"
"github.com/Volkswagen_World_Dyn/blockchain"
"os"
"time"
"strings"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
"github.com/hyperledger/fabric-sdk-go/pkg/common/errors/retry"
)
var( channelsConfig = map[string]fab.ChannelEndpointConfig{
"somechannel":{
Orderers: []string{"orderer.volkswagenag.com"},
Peers:map[string]fab.PeerChannelConfig{
"peer0.org1.volkswagenag.com":{
EndorsingPeer: true,
ChaincodeQuery: true,
LedgerQuery: true,
EventSource: true,
},
"peer1.org1.volkswagenag.com":{
},
"peer0.org2.volkswagenag.com":{
EndorsingPeer: true,
ChaincodeQuery: true,
LedgerQuery: true,
EventSource: true,
},
"peer1.org2.volkswagenag.com":{
},
},
Policies: fab.ChannelPolicies{
QueryChannelConfig: fab.QueryChannelConfigPolicy{
MinResponses: 1,
MaxTargets: 1,
RetryOpts: retry.Opts{
Attempts: 5,
InitialBackoff: 500 * time.Millisecond,
MaxBackoff: 5 * time.Second,
BackoffFactor: 2.0,
},
},
},
},
}
channelConfigImpl = &exampleChannelConfig{}
endpointConfigImpls = []interface{}{
channelConfigImpl,
}
)
type exampleChannelConfig struct{}
func (m *exampleChannelConfig) ChannelConfig(name string) (*fab.ChannelEndpointConfig, bool) {
ch, ok := channelsConfig[strings.ToLower(name)]
if !ok {
return nil, false
}
return &ch, true
}
func main() {
// Definition of the Fabric SDK properties
fSetup := blockchain.FabricSetup{
// Network parameters
OrdererID: "orderer.volkswagenag.com",
// Channel parameters
ChannelID: "somechannel",
ChannelEndpointCnf: endpointConfigImpls,
// Chaincode parameters
OrgAdmin: "Admin",
OrgName: "org1",
ConfigFile: "config.yaml",
// User parameters
UserName: "User1",
}
// Initialization of the Fabric SDK from the previously set properties
err := fSetup.Initialize()
if err != nil {
fmt.Printf("Unable to initialize the Fabric SDK: %v\n", err)
return
}
}
package blockchain
import (
"fmt"
"github.com/hyperledger/fabric-sdk-go/pkg/client/channel"
"github.com/hyperledger/fabric-sdk-go/pkg/client/event"
// mspclient "github.com/hyperledger/fabric-sdk-go/pkg/client/msp"
"github.com/hyperledger/fabric-sdk-go/pkg/client/resmgmt"
// "github.com/hyperledger/fabric-sdk-go/pkg/common/errors/retry"
// "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/msp"
// "github.com/hyperledger/fabric-sdk-go/pkg/core/config"
// packager "github.com/hyperledger/fabric-sdk-go/pkg/fab/ccpackager/gopackager"
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
// "github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
// "github.com/hyperledger/fabric-sdk-go/third_party/github.com/hyperledger/fabric/common/cauthdsl"
"github.com/pkg/errors"
"github.com/hyperledger/fabric-sdk-go/pkg/common/providers/fab"
)
// FabricSetup implementation
type FabricSetup struct {
ConfigFile string
OrgID string
OrdererID string
ChannelID string
ChainCodeID string
initialized bool
ChannelEndpointCnf []interface{}
ChaincodeGoPath string
ChaincodePath string
OrgAdmin string
OrgName string
UserName string
client *channel.Client
admin *resmgmt.Client
sdk *fabsdk.FabricSDK
event *event.Client
}
func (setup *FabricSetup) Initialize() error {
if setup.initialized {
return errors.New("sdk already initialized")
}
sdk,err := fabsdk.New(nil,fabsdk.WithEndpointConfig(setup.ChannelEndpointCnf))
if err != nil {
return errors.WithMessage(err, "failed to create SDK")
}
setup.sdk = sdk
return nil
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment