6 Star 47 Fork 28

Hyperledger/fabric

加入 Gitee
与超过 1200万 开发者一起发现、参与优秀开源项目,私有仓库也完全免费 :)
免费加入
文件
克隆/下载
common.go 7.75 KB
一键复制 编辑 原始数据 按行查看 历史
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package common
import (
"fmt"
"os"
"strings"
"github.com/hyperledger/fabric/bccsp/factory"
"github.com/hyperledger/fabric/common/channelconfig"
"github.com/hyperledger/fabric/common/flogging"
"github.com/hyperledger/fabric/common/viperutil"
"github.com/hyperledger/fabric/core/config"
"github.com/hyperledger/fabric/core/peer"
"github.com/hyperledger/fabric/core/scc/cscc"
"github.com/hyperledger/fabric/msp"
mspmgmt "github.com/hyperledger/fabric/msp/mgmt"
pcommon "github.com/hyperledger/fabric/protos/common"
pb "github.com/hyperledger/fabric/protos/peer"
putils "github.com/hyperledger/fabric/protos/utils"
"github.com/op/go-logging"
"github.com/pkg/errors"
"github.com/spf13/viper"
"golang.org/x/net/context"
)
// UndefinedParamValue defines what undefined parameters in the command line will initialise to
const UndefinedParamValue = ""
var (
// These function variables (xyzFnc) can be used to invoke corresponding xyz function
// this will allow the invoking packages to mock these functions in their unit test cases
// GetEndorserClientFnc is a function that returns a new endorser client connection,
// by default it is set to GetEndorserClient function
GetEndorserClientFnc func() (pb.EndorserClient, error)
// GetDefaultSignerFnc is a function that returns a default Signer(Default/PERR)
// by default it is set to GetDefaultSigner function
GetDefaultSignerFnc func() (msp.SigningIdentity, error)
// GetBroadcastClientFnc returns an instance of the BroadcastClient interface
// by default it is set to GetBroadcastClient function
GetBroadcastClientFnc func(orderingEndpoint string, tlsEnabled bool,
caFile string) (BroadcastClient, error)
// GetOrdererEndpointOfChainFnc returns orderer endpoints of given chain
// by default it is set to GetOrdererEndpointOfChain function
GetOrdererEndpointOfChainFnc func(chainID string, signer msp.SigningIdentity,
endorserClient pb.EndorserClient) ([]string, error)
)
func init() {
GetEndorserClientFnc = GetEndorserClient
GetDefaultSignerFnc = GetDefaultSigner
GetBroadcastClientFnc = GetBroadcastClient
GetOrdererEndpointOfChainFnc = GetOrdererEndpointOfChain
}
//InitConfig initializes viper config
func InitConfig(cmdRoot string) error {
config.InitViper(nil, cmdRoot)
err := viper.ReadInConfig() // Find and read the config file
if err != nil { // Handle errors reading the config file
return errors.WithMessage(err, fmt.Sprintf("error when reading %s config file", cmdRoot))
}
return nil
}
//InitCrypto initializes crypto for this peer
func InitCrypto(mspMgrConfigDir string, localMSPID string) error {
var err error
// Check whenever msp folder exists
_, err = os.Stat(mspMgrConfigDir)
if os.IsNotExist(err) {
// No need to try to load MSP from folder which is not available
return errors.Errorf("cannot init crypto, missing %s folder", mspMgrConfigDir)
}
// Init the BCCSP
var bccspConfig *factory.FactoryOpts
err = viperutil.EnhancedExactUnmarshalKey("peer.BCCSP", &bccspConfig)
if err != nil {
return errors.WithMessage(err, "could not parse YAML config")
}
err = mspmgmt.LoadLocalMsp(mspMgrConfigDir, bccspConfig, localMSPID)
if err != nil {
return errors.WithMessage(err, fmt.Sprintf("error when setting up MSP from directory %s", mspMgrConfigDir))
}
return nil
}
// GetEndorserClient returns a new endorser client connection for this peer
func GetEndorserClient() (pb.EndorserClient, error) {
clientConn, err := peer.NewPeerClientConnection()
if err != nil {
return nil, errors.WithMessage(err, "error trying to connect to local peer")
}
endorserClient := pb.NewEndorserClient(clientConn)
return endorserClient, nil
}
// GetAdminClient returns a new admin client connection for this peer
func GetAdminClient() (pb.AdminClient, error) {
clientConn, err := peer.NewPeerClientConnection()
if err != nil {
return nil, errors.WithMessage(err, "error trying to connect to local peer")
}
adminClient := pb.NewAdminClient(clientConn)
return adminClient, nil
}
// GetDefaultSigner return a default Signer(Default/PERR) for cli
func GetDefaultSigner() (msp.SigningIdentity, error) {
signer, err := mspmgmt.GetLocalMSP().GetDefaultSigningIdentity()
if err != nil {
return nil, errors.WithMessage(err, "error obtaining the default signing identity")
}
return signer, err
}
// GetOrdererEndpointOfChain returns orderer endpoints of given chain
func GetOrdererEndpointOfChain(chainID string, signer msp.SigningIdentity, endorserClient pb.EndorserClient) ([]string, error) {
// query cscc for chain config block
invocation := &pb.ChaincodeInvocationSpec{
ChaincodeSpec: &pb.ChaincodeSpec{
Type: pb.ChaincodeSpec_Type(pb.ChaincodeSpec_Type_value["GOLANG"]),
ChaincodeId: &pb.ChaincodeID{Name: "cscc"},
Input: &pb.ChaincodeInput{Args: [][]byte{[]byte(cscc.GetConfigBlock), []byte(chainID)}},
},
}
creator, err := signer.Serialize()
if err != nil {
return nil, errors.WithMessage(err, fmt.Sprintf("error serializing identity for %s", signer.GetIdentifier()))
}
prop, _, err := putils.CreateProposalFromCIS(pcommon.HeaderType_CONFIG, "", invocation, creator)
if err != nil {
return nil, errors.WithMessage(err, "error creating GetConfigBlock proposal")
}
signedProp, err := putils.GetSignedProposal(prop, signer)
if err != nil {
return nil, errors.WithMessage(err, "error creating signed GetConfigBlock proposal")
}
proposalResp, err := endorserClient.ProcessProposal(context.Background(), signedProp)
if err != nil {
return nil, errors.WithMessage(err, "error endorsing GetConfigBlock")
}
if proposalResp == nil {
return nil, errors.WithMessage(err, "error nil proposal response")
}
if proposalResp.Response.Status != 0 && proposalResp.Response.Status != 200 {
return nil, errors.Errorf("error bad proposal response %d", proposalResp.Response.Status)
}
// parse config block
block, err := putils.GetBlockFromBlockBytes(proposalResp.Response.Payload)
if err != nil {
return nil, errors.WithMessage(err, "error unmarshaling config block")
}
envelopeConfig, err := putils.ExtractEnvelope(block, 0)
if err != nil {
return nil, errors.WithMessage(err, "error extracting config block envelope")
}
bundle, err := channelconfig.NewBundleFromEnvelope(envelopeConfig)
if err != nil {
return nil, errors.WithMessage(err, "error loading config block")
}
return bundle.ChannelConfig().OrdererAddresses(), nil
}
// SetLogLevelFromViper sets the log level for 'module' logger to the value in
// core.yaml
func SetLogLevelFromViper(module string) error {
var err error
if module == "" {
return errors.New("log level not set, no module name provided")
}
logLevelFromViper := viper.GetString("logging." + module)
err = CheckLogLevel(logLevelFromViper)
if err != nil {
return err
}
// replace period in module name with forward slash to allow override
// of logging submodules
module = strings.Replace(module, ".", "/", -1)
// only set logging modules that begin with the supplied module name here
_, err = flogging.SetModuleLevel("^"+module, logLevelFromViper)
return err
}
// CheckLogLevel checks that a given log level string is valid
func CheckLogLevel(level string) error {
_, err := logging.LogLevel(level)
if err != nil {
err = errors.Errorf("invalid log level provided - %s", level)
}
return err
}
Loading...
马建仓 AI 助手
尝试更多
代码解读
代码找茬
代码优化
1
https://gitee.com/hyperledger/fabric.git
git@gitee.com:hyperledger/fabric.git
hyperledger
fabric
fabric
v1.1.0-preview

搜索帮助