go谈话+区块链教程源代码领会peer peer nodestart

  伯仲连go谈话+区块链教程fabric1.0源代码领会peer peer node start吩咐实...

go谈话+区块链教程源代码领会peer peer nodestart

  伯仲连go谈话+区块链教程fabric1.0源代码领会peer peer node start吩咐实行,2021年 下星期,区块链行业正渐渐褪去兴盛之初的烦躁、回归理性,外表上看关系人才需要与身价犹如正在回落区块链伯仲。但究竟上,恰是前期泡沫的渐退,让人们更多的关心点放在了区块链真实的本领之上。

  # fabric 1.0源代码条记 之 peer #peer node start吩咐实行

  有个图2

  ## 1、peer node加载子吩咐start和status

币小哥资源讯息:

  ```go

  func cmd() *cobra.command {

      nodecmd.addcommand(startcmd()) //加载子吩咐start

      nodecmd.addcommand(statuscmd()) //加载子吩咐status

      return nodecmd

  var nodecmd = &cobra.command{

      use: nodefuncname,

      short: fmt.sprint(shortdes),

      long: fmt.sprint(longdes),

  //代码在peer/node/node.go

币小哥资源讯息:

  个中serve(args)为peer node start的实行代码,比拟搀杂,正文将中心解说区块链伯仲。

  另statuscmd()代码与startcmd()邻近,暂略区块链伯仲。

  ```go

  func startcmd() *cobra.command {

      flags := nodestartcmd.flags()

      flags.boolvarp(&chaincodedevmode, "peer-chaincodedev", "", false, "whether peer in chaincode development mode")

      flags.boolvarp(&peerdefaultchain, "peer-defaultchain", "", false, "whether to start peer with chain testchainid")

      flags.stringvarp(&orderingendpoint, "orderer", "o", "orderer:7050", "ordering service endpoint") //orderer

      return nodestartcmd

  var nodestartcmd = &cobra.command{

      use: "start",

      short: "starts the node.",

      long: `starts a node that interacts with the network.`,

      rune: func(cmd *cobra.command, args []string) error {

          return serve(args) //serve(args)为peer node start的实行代码

  //代码在peer/node/start.go

币小哥资源讯息:如次实质均为serve(args)的代码,即peer node start吩咐实行过程区块链伯仲。**

  ## 2、初始化ledger(账本)

币小哥资源讯息:

  ```go

  ledgermgmt.initialize()

  //代码在peer/node/start.go

币小哥资源讯息:

  ```go

  func initialize() {

      openedledgers = make(map[string]ledger.peerledger) //openedledgers为全部变量区块链伯仲,保存暂时运用的账本列表

      provider, err := kvledger.newprovider() //创造账本provider范例

      ledgerprovider = provider

  //代码在core/ledger/ledgermgmt/ledger_mgmt.go

币小哥资源讯息:

  ```go

  func newprovider() (ledger.peerledgerprovider, error) {

      idstore := openidstore(ledgerconfig.getledgerproviderpath()) //翻开idstore

      //创造并初始化blkstorage

      attrstoindex := []blkstorage.indexableattr{

          blkstorage.indexableattrblockhash,

          blkstorage.indexableattrblocknum,

          blkstorage.indexableattrtxid,

          blkstorage.indexableattrblocknumtrannum,

          blkstorage.indexableattrblocktxid,

          blkstorage.indexableattrtxvalidationcode,

      indexconfig := &blkstorage.indexconfig{attrstoindex: attrstoindex}

      blockstoreprovider := fsblkstorage.newprovider(

          fsblkstorage.newconf(ledgerconfig.getblockstorepath(), ledgerconfig.getmaxblockfilesize()),

          indexconfig)

      //创造并初始化statedb

      var vdbprovider statedb.versioneddbprovider

      if !ledgerconfig.iscouchdbenabled() {

          vdbprovider = stateleveldb.newversioneddbprovider()

      } else {

          vdbprovider, err = statecouchdb.newversioneddbprovider()

      //创造并初始化historydb

      var historydbprovider historydb.historydbprovider

      historydbprovider = historyleveldb.newhistorydbprovider()

      //结构provider

      provider := &provider{idstore, blockstoreprovider, vdbprovider, historydbprovider}

      provider.recoverunderconstructionledger()

      return provider, nil

  //代码在core/ledger/kvledger/kv_ledger_provider.go

币小哥资源讯息:[fabric 1.0源代码条记 之 ledger(账本)](../ledger/readme.md)

  ## 3、摆设及创造peerserver、创造eventhubserver(事变中压服务器)

  ```go

  //初始化全部变量localaddress和peerendpoint

  err := peer.cacheconfiguration()

  peerendpoint, err := peer.getpeerendpoint() //获得peerendpoint

  listenaddr := viper.getstring("peer.listenaddress") //peerserver监听地方

  secureconfig, err := peer.getsecureconfig() //获得peerserver安定摆设区块链伯仲,能否起用tls、公钥、私钥、根文凭

  //以监听地方和安定摆设区块链伯仲,创造peer grpc server

  peerserver, err := peer.createpeerserver(listenaddr, secureconfig)

  //创造eventhubserver(事变中压服务器)

  ehubgrpcserver, err := createeventhubserver(secureconfig)

  //代码在peer/node/start.go

币小哥资源讯息:

  ```go

  var lis net.listener

  var err error

  lis, err = net.listen("tcp", viper.getstring("peer.events.address")) //创造listen

  grpcserver, err := comm.newgrpcserverfromlistener(lis, secureconfig) //从listen创造grpcserver

  ehserver := producer.neweventsserver(

      uint(viper.getint("peer.events.buffersize")), //最大举行缓冲的动静数

      viper.getduration("peer.events.timeout")) //缓冲已满的情景下区块链伯仲,往缓冲中发送动静的超时

  pb.registereventsserver(grpcserver.server(), ehserver) //eventhubserver注册至grpcserver

  return grpcserver, nil

  //代码在peer/node/start.go

币小哥资源讯息:

  ```go

  func registereventsserver(s *grpc.server, srv eventsserver) {

      s.registerservice(&_events_servicedesc, srv)

  //代码在protos/peer/events.pb.go

币小哥资源讯息:fabric 1.0源代码条记 之 events(事变效劳)

  ## 4、创造并启用chaincode server区块链伯仲,并注册体例链码

币小哥资源讯息:

  ```go

  ccprovider.enableccinfocache() //ccinfocacheenabled = true

  //创造chaincode server

  //即使peer.chaincodelistenaddress区块链伯仲,没有设置大概设置和peerlistenaddress沟通,均径直运用peerserver

  //要不另行创造newgrpcserver用来chaincode server

  ccsrv, ccepfunc := createchaincodeserver(peerserver, listenaddr)

  //chaincode service注册到grpcserver区块链伯仲,并注册体例链码

  registerchaincodesupport(ccsrv.server(), ccepfunc)

  go ccsrv.start() //启用grpcserver

  //代码在peer/node/start.go

币小哥资源讯息:创造chaincode server区块链伯仲。

  ```go

  func createchaincodeserver(peerserver comm.grpcserver, peerlistenaddress string) (comm.grpcserver, ccendpointfunc) {

      //peer.chaincodelistenaddress区块链伯仲,链码容器贯穿时的监听地方

      cclistenaddress := viper.getstring("peer.chaincodelistenaddress")

      var srv comm.grpcserver

      var ccepfunc ccendpointfunc

      //即使peer.chaincodelistenaddress区块链伯仲,没有设置大概设置和peerlistenaddress沟通,均径直运用peerserver

      //要不另行创造newgrpcserver用来chaincode server

      if cclistenaddress == "" {

          ccepfunc = peer.getpeerendpoint

          srv = peerserver

      } else if cclistenaddress == peerlistenaddress {

          ccepfunc = peer.getpeerendpoint

          srv = peerserver

      } else {

          config, err := peer.getsecureconfig()

          srv, err = comm.newgrpcserver(cclistenaddress, config)

          ccepfunc = getchaincodeaddressendpoint

      return srv, ccepfunc

  //代码在peer/node/start.go

币小哥资源讯息:chaincode service注册到grpcserver区块链伯仲。

  ```go

  func registerchaincodesupport(grpcserver *grpc.server, ccepfunc ccendpointfunc) {

      userrunscc := chaincode.isdevmode() //能否开拓形式

      ccstartuptimeout := viper.getduration("chaincode.startuptimeout") //启用链码容器的超时

      if ccstartuptimeout < time.duration(5)*time.second { //起码5秒

          ccstartuptimeout = time.duration(5) * time.second

      } else {

      //结构chaincodesupport

      ccsrv := chaincode.newchaincodesupport(ccepfunc, userrunscc, ccstartuptimeout)

      scc.registersysccs() //注册体例链码

      pb.registerchaincodesupportserver(grpcserver, ccsrv) //service注册到grpcserver

  //代码在peer/node/start.go

币小哥资源讯息:结构chaincodesupport区块链伯仲。

  ```go

  var thechaincodesupport *chaincodesupport

  func newchaincodesupport(getccendpoint func() (*pb.peerendpoint, error), userrunscc bool, ccstartuptimeout time.duration) *chaincodesupport {

      //即/var/hyperledger/production/chaincodes

      ccprovider.setchaincodespath(config.getpath("peer.filesystempath") + string(filepath.separator) + "chaincodes")

      pnid := viper.getstring("peer.networkid") //搜集id

      pid := viper.getstring("peer.id") //节点id

      //结构chaincodesupport

      thechaincodesupport = &chaincodesupport{runningchaincodes: &runningchaincodes{chaincodemap: make(map[string]*chaincodertenv), launchstarted: make(map[string]bool)}, peernetworkid:

  pnid, peerid: pid}

      ccendpoint, err := getccendpoint() //此处传入ccepfunc

      if err != nil {

          thechaincodesupport.peeraddress = viper.getstring("chaincode.peeraddress")

      } else {

          thechaincodesupport.peeraddress = ccendpoint.address

      if thechaincodesupport.peeraddress == "" {

          thechaincodesupport.peeraddress = peeraddressdefault

      thechaincodesupport.userrunscc = userrunscc //能否开拓形式

      thechaincodesupport.ccstartuptimeout = ccstartuptimeout //启用链码容器的超时

      thechaincodesupport.peertls = viper.getbool("peer.tls.enabled") //能否起用tls

      if thechaincodesupport.peertls {

          thechaincodesupport.peertlscertfile = config.getpath("peer.tls.cert.file")

          thechaincodesupport.peertlskeyfile = config.getpath("peer.tls.key.file")

          thechaincodesupport.peertlssvrhostord = viper.getstring("peer.tls.serverhostoverride")

      kadef := 0

      // peer和链码之间的心跳超时区块链伯仲,小于或即是0表示着封闭

      if ka := viper.getstring("chaincode.keepalive"); ka == "" {

          thechaincodesupport.keepalive = time.duration(kadef) * time.second //0

      } else {

          t, terr := strconv.atoi(ka)

          if terr != nil {

              t = kadef //0

          } else if t <= 0 {

              t = kadef //0

          thechaincodesupport.keepalive = time.duration(t) * time.second //非0

      execto := time.duration(30) * time.second

      //invoke和initialize吩咐实行超时

      if eto := viper.getduration("chaincode.executetimeout"); eto <= time.duration(1)*time.second {

          //小于1秒时区块链伯仲,默许30秒

      } else {

          execto = eto

      thechaincodesupport.executetimeout = execto

      viper.setenvprefix("core")

      viper.automaticenv()

      replacer := strings.newreplacer(".", "_")

      viper.setenvkeyreplacer(replacer)

      thechaincodesupport.chaincodeloglevel = getloglevelfromviper("level")

      thechaincodesupport.shimloglevel = getloglevelfromviper("shim")

      thechaincodesupport.logformat = viper.getstring("chaincode.logging.format")

      return thechaincodesupport

  //代码在core/chaincode/chaincode_support.go

币小哥资源讯息:注册体例链码区块链伯仲。

  ```go

  func registersysccs() {

      //cscc、lscc、escc、vscc、qscc

      for _, syscc := range systemchaincodes {

          registersyscc(syscc)

  代码在core/scc/importsysccs.go

  ## 5、注册admin server和endorser server

币小哥资源讯息:

  ```go

  //s.registerservice(&_admin_servicedesc, srv)

  //var _admin_servicedesc = grpc.servicedesc{...}

  //core.newadminserver()结构serveradmin构造体区块链伯仲,serveradmin构造体实行type adminserver interface接口

  pb.registeradminserver(peerserver.server(), core.newadminserver())

  //结构构造体endorser区块链伯仲,endorser构造体实行type endorserserver interface接口

  serverendorser := endorser.newendorserserver()

  //s.registerservice(&_endorser_servicedesc, srv)

  //var _endorser_servicedesc = grpc.servicedesc{...}

  pb.registerendorserserver(peerserver.server(), serverendorser)

  //代码在peer/node/start.go

币小哥资源讯息:

  ```go

  type adminserver interface {

      getstatus(context.context, *google_protobuf.empty) (*serverstatus, error)

      startserver(context.context, *google_protobuf.empty) (*serverstatus, error)

      getmoduleloglevel(context.context, *loglevelrequest) (*loglevelresponse, error)

      setmoduleloglevel(context.context, *loglevelrequest) (*loglevelresponse, error)

      revertloglevels(context.context, *google_protobuf.empty) (*google_protobuf.empty, error)

  //代码在protos/peer/admin.pb.go

币小哥资源讯息:

  ```go

  type endorserserver interface {

      processproposal(context.context, *signedproposal) (*proposalresponse, error)

  //代码在protos/peer/peer.pb.go

  ## 6、初始化gossip效劳

  ```go

  bootstrap := viper.getstringslice("peer.gossip.bootstrap") //启用节点后gossip贯穿的初始节点

  serializedidentity, err := mgmt.getlocalsigningidentityorpanic().serialize() //获得出面身份

  messagecryptoservice := peergossip.newmcs( //结构mspmessagecryptoservice(动静加密效劳)

      peer.newchannelpolicymanagergetter(), //结构type channelpolicymanagergetter struct{}

      localmsp.newsigner(), //结构type mspsigner struct {}

      mgmt.newdeserializersmanager()) //结构type mspdeserializersmanager struct{}

  secadv := peergossip.newsecurityadvisor(mgmt.newdeserializersmanager()) //结构mspsecurityadvisor(安定参谋)

  securedialopts := func() []grpc.dialoption {

      var dialopts []grpc.dialoption

      dialopts = append(dialopts, grpc.withdefaultcalloptions(grpc.maxcallrecvmsgsize(comm.maxrecvmsgsize()), //maxrecvmsgsize

          grpc.maxcallsendmsgsize(comm.maxsendmsgsize()))) //maxsendmsgsize

      dialopts = append(dialopts, comm.clientkeepaliveoptions()...) //clientkeepaliveoptions

      if comm.tlsenabled() {

          tlscert := peerserver.servercertificate()

          dialopts = append(dialopts, grpc.withtransportcredentials(comm.getcasupport().getpeercredentials(tlscert)))

      } else {

          dialopts = append(dialopts, grpc.withinsecure())

      return dialopts

  err = service.initgossipservice(serializedidentity, peerendpoint.address, peerserver.server(),

      messagecryptoservice, secadv, securedialopts, bootstrap...) //结构gossipserviceimpl

  defer service.getgossipservice().stop()

  //代码在peer/node/start.go

币小哥资源讯息:[fabric 1.0源代码条记 之 gossip(谎言算法)](../gossip/readme.md)

  ## 7、初始化、安置并实行体例链码(scc)

币小哥资源讯息:

  ```go

  initsysccs() //初始化体例链码区块链伯仲,挪用scc.deploysysccs("")

  peer.initialize(func(cid string) { //初始化一切链

      scc.deploysysccs(cid) //按chain id安置并运转体例链码

  //代码在peer/node/start.go

币小哥资源讯息:

  ```go

  func initialize(init func(string)) {

      chaininitializer = init

      var cb *common.block

      var ledger ledger.peerledger

      ledgermgmt.initialize()

      ledgerids, err := ledgermgmt.getledgerids()

      for _, cid := range ledgerids {

          ledger, err = ledgermgmt.openledger(cid)

          cb, err = getcurrconfigblockfromledger(ledger) //获得最新的摆设块

          err = createchain(cid, ledger, cb)

          initchain(cid) //即挪用chaininitializer(cid)区块链伯仲,即scc.deploysysccs(cid)

  //代码在core/peer/peer.go

币小哥资源讯息:

  ```go

  func createchain(cid string, ledger ledger.peerledger, cb *common.block) error {

      envelopeconfig, err := utils.extractenvelope(cb, 0) //获得摆设买卖

      configtxinitializer := configtx.newinitializer() //结构initializer

      gossipeventer := service.getgossipservice().newconfigeventer() //获得gossipserviceinstance区块链伯仲,并结构configeventer

      gossipcallbackwrapper := func(cm configtxapi.manager) {

          ac, ok := configtxinitializer.applicationconfig()

          gossipeventer.processconfigupdate(&chainsupport{

              manager: cm,

              application: ac,

          //考证疑惑节点身份区块链伯仲,并封闭失效链接

          service.getgossipservice().suspectpeers(func(identity api.peeridentitytype) bool {

              return true

      trustedrootscallbackwrapper := func(cm configtxapi.manager) {

          updatetrustedroots(cm)

      configtxmanager, err := configtx.newmanagerimpl(

          envelopeconfig,

          configtxinitializer,

          []func(cm configtxapi.manager){gossipcallbackwrapper, trustedrootscallbackwrapper},

      mspmgmt.xxxsetmspmanager(cid, configtxmanager.mspmanager())

      ac, ok := configtxinitializer.applicationconfig()

      cs := &chainsupport{

          manager: configtxmanager,

          application: ac, // todo, refactor as this is accessible through manager

          ledger: ledger,

      c := committer.newledgercommitterreactive(ledger, txvalidator.newtxvalidator(cs), func(block *common.block) error {

          chainid, err := utils.getchainidfromblock(block)

          if err != nil {

              return err

          return setcurrconfigblock(block, chainid)

      ordereraddresses := configtxmanager.channelconfig().ordereraddresses()

      service.getgossipservice().initializechannel(cs.chainid(), c, ordereraddresses)

      chains.lock()

      defer chains.unlock()

      chains.list[cid] = &chain{

          cs: cs,

          cb: cb,

          committer: c,

      return nil

  //代码在core/peer/peer.go

币小哥资源讯息:[fabric 1.0源代码条记 之 scc(体例链码)](../scc/readme.md)

  ## 8、启用peerserver和ehubgrpcserver区块链伯仲,并监察和控制体例旗号,以及启用go自带的profiling扶助举行调节和测试

币小哥资源讯息:

  ```go

  serve := make(chan error)

  sigs := make(chan os.signal, 1)

  signal.notify(sigs, syscall.sigint, syscall.sigterm)

  go func() {

      sig := <-sigs //接受体例旗号

      serve <- nil

  go func() {

      var grpcerr error

      grpcerr = peerserver.start() //启用peerserver

      serve <- grpcerr

  err := writepid(config.getpath("peer.filesystempath")+"/peer.pid", os.getpid()) //写入pid

  go ehubgrpcserver.start() //启用ehubgrpcserver

  if viper.getbool("peer.profile.enabled") {

      go func() { //启用go自带的profiling扶助举行调节和测试

          profilelistenaddress := viper.getstring("peer.profile.listenaddress")

          profileerr := )

  return <-serve //等候serve

  //代码在peer/node/start.go

  ## 9、按摆设文献从新革新模块日记级别

  ```go

  overridelogmodules := []string{"msp", "gossip", "ledger", "cauthdsl", "policies", "grpc"}

  for _, module := range overridelogmodules {

      err = common.setloglevelfromviper(module)

  flogging.setpeerstartupmodulesmap()

  //代码在peer/node/start.go

本文转载自互联网,如有侵权,联系删除

相关推荐

挖矿会形成什么感化?

挖矿会形成什么感化?

你说的挖矿,此刻仍旧有了两种道理了挖矿。一种是挖矿,金矿银矿,铁矿之类。第二种即是互联网络金融派生出来的假造钱币,如比特币。。底下我...

okex官网 2021.11.29 5 1

评论列表
  • DeFi智能合约是没有时间限制的,零点更新一天三次

    2021-11-04 12:48:06 回复该评论
  • 一手转U一手给现金

    2021-11-04 12:48:06 回复该评论
  • 特斯拉公布的是q1卖了一部分比特币,毕竟一家公司要正常运营还是需要法币,又不是全抛

    2021-11-04 19:36:13 回复该评论
  • 币圈这几天流量飙升啊

    2021-11-04 19:36:13 回复该评论
  • 币安链上的那些千万亿的币有人买吗

    2021-11-04 19:36:13 回复该评论
  • 只要亚马逊支持狗狗币支付,那么下一步就是贝佐斯开交易所了

    2021-11-04 19:36:13 回复该评论