标签:cacerts public result 入口 tps spi out util ecif
区块链教程Fabric1.0源代码分析Peer peer根命令入口及加载子命令二。func init() {
????logger = logging.MustGetLogger(pkgLogID) //创建仅在flogging包内代码使用的logging.Logger对象
????Reset() //全局变量初始化为默认值
????initgrpclogger() //初始化gRPC Logger,即创建logging.Logger对象,并用这个对象设置grpclog
}
//代码在common/flogging/logging.go
init()执行结束后,peer/main.go中调用flogging.InitFromSpec(loggingSpec),将再次初始化全局日志级别为loggingSpec,之前默认为logging.INFO。
func InitFromSpec(spec string) string代码如下。
其中spec格式为:[<module>[,<module>...]=]<level>[:[<module>[,<module>...]=]<level>...]。
此处传入spec为"",将""模块日志级别设置为defaultLevel,并会将modules初始化为defaultLevel。
levelAll := defaultLevel //defaultLevel为logging.INFO
var err error
if spec != "" { //如果spec不为空,则按既定格式读取
????fields := strings.Split(spec, ":") //按:分割
????for _, field := range fields {
????????split := strings.Split(field, "=") //按=分割
????????switch len(split) {
????????case 1: //只有level
????????????if levelAll, err = logging.LogLevel(field); err != nil { //levelAll赋值为logging.LogLevel枚举中定义的Level级别
????????????????levelAll = defaultLevel // 如果没有定义,则使用默认日志级别
????????????}
????????case 2: //针对module,module...=level,split[0]为模块集,split[1]为要设置的日志级别
????????????levelSingle, err := logging.LogLevel(split[1]) //levelSingle赋值为logging.LogLevel枚举中定义的Level级别
????????????modules := strings.Split(split[0], ",") //按,分割获取模块名
????????????for _, module := range modules {
????????????????logging.SetLevel(levelSingle, module) //本条规则中所有模块日志级别均设置为levelSingle
????????????}
????????default:
????????????//...
????????}
????}
}
//代码在common/flogging/logging.go
flogging(Fabric日志系统)更详细信息参考:Fabric 1.0源代码笔记 之 flogging(Fabric日志系统)
如下代码为初始化MSP,获取peer.mspConfigPath路径和peer.localMspId,分别表示MSP的本地路径(/etc/hyperledger/fabric/msp/)和Peer所关联的MSP ID,并初始化组织和身份信息。
var mspMgrConfigDir = config.GetPath("peer.mspConfigPath")
var mspID = viper.GetString("peer.localMspId")
err = common.InitCrypto(mspMgrConfigDir, mspID)
//代码在peer/main.go
/etc/hyperledger/fabric/msp/目录下包括:admincerts、cacerts、keystore、signcerts、tlscacerts。其中:
如下代码为common.InitCrypto(mspMgrConfigDir, mspID)的具体实现,peer.BCCSP为密码库相关配置,包括算法和文件路径等,格式如下:
BCCSP:
????Default: SW
????SW:
????????Hash: SHA2
????????Security: 256
????????FileKeyStore:
????????????KeyStore:
????????????
var bccspConfig *factory.FactoryOpts
err = viperutil.EnhancedExactUnmarshalKey("peer.BCCSP", &bccspConfig) //将peer.BCCSP配置信息加载至bccspConfig中
err = mspmgmt.LoadLocalMsp(mspMgrConfigDir, bccspConfig, localMSPID) //从指定目录中加载本地MSP
//代码在peer/common/common.go
factory.FactoryOpts定义为:
type FactoryOpts struct {
????ProviderName string `mapstructure:"default" json:"default" yaml:"Default"`
????SwOpts *SwOpts `mapstructure:"SW,omitempty" json:"SW,omitempty" yaml:"SwOpts"`
}
//FactoryOpts代码在bccsp/factory/nopkcs11.go,本目录下另有代码文件pkcs11.go,在-tags "nopkcs11"条件下二选一编译。
type SwOpts struct {
????// Default algorithms when not specified (Deprecated?)
????SecLevel int `mapstructure:"security" json:"security" yaml:"Security"`
????HashFamily string `mapstructure:"hash" json:"hash" yaml:"Hash"`
????// Keystore Options
????Ephemeral bool `mapstructure:"tempkeys,omitempty" json:"tempkeys,omitempty"`
????FileKeystore *FileKeystoreOpts `mapstructure:"filekeystore,omitempty" json:"filekeystore,omitempty" yaml:"FileKeyStore"`
????DummyKeystore *DummyKeystoreOpts `mapstructure:"dummykeystore,omitempty" json:"dummykeystore,omitempty"`
}
type FileKeystoreOpts struct {
????KeyStorePath string `mapstructure:"keystore" yaml:"KeyStore"`
}
//SwOpts和FileKeystoreOpts代码均在bccsp/factory/swfactory.go
如下代码为viperutil.EnhancedExactUnmarshalKey("peer.BCCSP", &bccspConfig)的具体实现,getKeysRecursively为递归读取peer.BCCSP配置信息。
mapstructure为第三方包:github.com/mitchellh/mapstructure,用于将map[string]interface{}转换为struct。
示例代码:https://godoc.org/github.com/mitchellh/mapstructure#example-Decode--WeaklyTypedInput
func EnhancedExactUnmarshalKey(baseKey string, output interface{}) error {
????m := make(map[string]interface{})
????m[baseKey] = nil
????leafKeys := getKeysRecursively("", viper.Get, m)
????config := &mapstructure.DecoderConfig{
????????Metadata: nil,
????????Result: output,
????????WeaklyTypedInput: true,
????}
????decoder, err := mapstructure.NewDecoder(config)
????return decoder.Decode(leafKeys[baseKey])
}
//代码在common/viperutil/config_util.go
如下代码为mspmgmt.LoadLocalMsp(mspMgrConfigDir, bccspConfig, localMSPID)的具体实现,从指定目录中加载本地MSP。
conf, err := msp.GetLocalMspConfig(dir, bccspConfig, mspID) //获取本地MSP配置,序列化后写入msp.MSPConfig,即conf
return GetLocalMSP().Setup(conf) //调取msp.NewBccspMsp()创建bccspmsp实例,调取bccspmsp.Setup(conf)解码conf.Config并设置bccspmsp
//代码在msp/mgmt/mgmt.go
如下代码为msp.GetLocalMspConfig(dir, bccspConfig, mspID)的具体实现。
SetupBCCSPKeystoreConfig()核心代码为bccspConfig.SwOpts.FileKeystore = &factory.FileKeystoreOpts{KeyStorePath: keystoreDir},目的是在FileKeystore或KeyStorePath为空时设置默认值。
signcertDir := filepath.Join(dir, signcerts) //signcerts为"signcerts",signcertDir即/etc/hyperledger/fabric/msp/signcerts/
keystoreDir := filepath.Join(dir, keystore) //keystore为"keystore",keystoreDir即/etc/hyperledger/fabric/msp/keystore/
bccspConfig = SetupBCCSPKeystoreConfig(bccspConfig, keystoreDir) //设置bccspConfig.SwOpts.Ephemeral = false和bccspConfig.SwOpts.FileKeystore = &factory.FileKeystoreOpts{KeyStorePath: keystoreDir}
????//bccspConfig.SwOpts.Ephemeral是否短暂的
err := factory.InitFactories(bccspConfig) //初始化bccsp factory,并创建bccsp实例
signcert, err := getPemMaterialFromDir(signcertDir) //读取X.509证书的PEM文件
sigid := &msp.SigningIdentityInfo{PublicSigner: signcert[0], PrivateSigner: nil} //构造SigningIdentityInfo
return getMspConfig(dir, ID, sigid) //分别读取cacerts、admincerts、tlscacerts文件,以及config.yaml中组织信息,构造msp.FabricMSPConfig,序列化后用于构造msp.MSPConfig
//代码在msp/configbuilder.go
factory.InitFactories(bccspConfig)及BCCSP(区块链加密服务提供者)更详细内容,参考:Fabric 1.0源代码笔记 之 BCCSP(区块链加密服务提供者)
至此,peer/main.go结束,接下来将进入peer/node/start.go中serve(args)函数。
!
区块链教程Fabric1.0源代码分析Peer peer根命令入口及加载子命令二
标签:cacerts public result 入口 tps spi out util ecif
原文地址:http://blog.51cto.com/14041296/2313478