写点什么

长安链源码分析启动(3)

作者:
  • 2022 年 8 月 29 日
    湖南
  • 本文字数:1911 字

    阅读完需:约 6 分钟

本文已参与「开源摘星计划」,欢迎正在阅读的你加入。活动链接:https://github.com/weopenprojects/WeOpen-Star

接下来重点分析下长安链的网络模块,核心代码我都会上注释

func (server *ChainMakerServer) initNet() error {	//定义网络类型  var netType protocol.NetType	var err error	// 加载网络配置	provider := localconf.ChainMakerConfig.NetConfig.Provider	log.Infof("load net provider: %s", provider)  //查看网络配置类型,有两种,分别是libp2p和liquid	switch strings.ToLower(provider) {	case "libp2p":		netType = protocol.Libp2p
case "liquid": netType = protocol.Liquid default: return errors.New("unsupported net provider") } //读取安全认证类型 authType := localconf.ChainMakerConfig.AuthType emptyAuthType := ""
//读取tls连接配置的私钥路径 keyPath := localconf.ChainMakerConfig.NetConfig.TLSConfig.PrivKeyFile if !filepath.IsAbs(keyPath) { keyPath, err = filepath.Abs(keyPath) if err != nil { return err } } log.Infof("load net tls key file path: %s", keyPath)
var certPath string var pubKeyMode bool //安全认证类型有5种,分别是PermissionedWithKey、Public、PermissionedWithCert //Identity、emptyAuthType switch strings.ToLower(authType) { case protocol.PermissionedWithKey, protocol.Public: pubKeyMode = true case protocol.PermissionedWithCert, protocol.Identity, emptyAuthType: pubKeyMode = false certPath = localconf.ChainMakerConfig.NetConfig.TLSConfig.CertFile if !filepath.IsAbs(certPath) { certPath, err = filepath.Abs(certPath) if err != nil { return err } } log.Infof("load net tls cert file path: %s", certPath) default: return errors.New("wrong auth type") } //通过网络工厂类创建一个网络, //传入参数主要是网络类型,监听地址、加密信息等等,细节暂时不研究 var netFactory net.NetFactory server.net, err = netFactory.NewNet( netType, net.WithReadySignalC(server.readyC), net.WithListenAddr(localconf.ChainMakerConfig.NetConfig.ListenAddr), net.WithCrypto(pubKeyMode, keyPath, certPath), net.WithPeerStreamPoolSize(localconf.ChainMakerConfig.NetConfig.PeerStreamPoolSize), net.WithMaxPeerCountAllowed(localconf.ChainMakerConfig.NetConfig.MaxPeerCountAllow), net.WithPeerEliminationStrategy(localconf.ChainMakerConfig.NetConfig.PeerEliminationStrategy), net.WithSeeds(localconf.ChainMakerConfig.NetConfig.Seeds...), net.WithBlackAddresses(localconf.ChainMakerConfig.NetConfig.BlackList.Addresses...), net.WithBlackNodeIds(localconf.ChainMakerConfig.NetConfig.BlackList.NodeIds...), net.WithMsgCompression(localconf.ChainMakerConfig.DebugConfig.UseNetMsgCompression), net.WithInsecurity(localconf.ChainMakerConfig.DebugConfig.IsNetInsecurity), ) if err != nil { errMsg := fmt.Sprintf("new net failed, %s", err.Error()) log.Error(errMsg) return errors.New(errMsg) }
// 读取私钥文件 file, err := ioutil.ReadFile(keyPath) if err != nil { return err } //从私钥文件中读取私钥 privateKey, err := asym.PrivateKeyFromPEM(file, nil) if err != nil { return err } //基于私钥创建一个nodeid nodeId, err := helper.CreateLibp2pPeerIdWithPrivateKey(privateKey) if err != nil { return err } //设置nodeid localconf.ChainMakerConfig.SetNodeId(nodeId)
// 加载TrustRoots文件,并设置到网络中 for _, chainTrustRoots := range localconf.ChainMakerConfig.NetConfig.CustomChainTrustRoots { roots := make([][]byte, 0, len(chainTrustRoots.TrustRoots)) for _, r := range chainTrustRoots.TrustRoots { rootBytes, err2 := ioutil.ReadFile(r.Root) if err2 != nil { log.Errorf("load custom chain trust roots failed, %s", err2.Error()) return err2 } roots = append(roots, rootBytes) } server.net.SetChainCustomTrustRoots(chainTrustRoots.ChainId, roots) log.Infof("set custom trust roots for chain[%s] success.", chainTrustRoots.ChainId) } return nil}
复制代码

整体核心脉络很清晰,至于细节,我们一层层看

用户头像

关注

还未添加个人签名 2018.05.04 加入

还未添加个人简介

评论

发布
暂无评论
长安链源码分析启动(3)_长安链_李_InfoQ写作社区