二维码

12.5 用户链码 - 数据结构 - 机器学习

1246 人阅读 | 时间:2021年01月15日 01:17
12.5 用户链码 - 数据结构 - 机器学习 #daohang ul li t,.reed .riqi,a.shangg,a.xiatt,a.shangg:hover,a.xiatt:hover,a.shang,a.xiat,a.shang:hover,a.xiat:hover,.reed-pinglun-anniu,span.now-page,#daohangs-around,#caidan-tubiao,#daohangs,#daohangs li,#btnPost{background-color:#D10B04;} .dinglanyou1 h3{border-bottom:3px solid #D10B04;} #dibuer{border-top:2px solid #D10B04;}.cebianlan .rongqi h3{border-bottom:1px solid #D10B04;} #edtSearch{border:1px solid #D10B04;} #daohang .zuo ul li{border-right:1px solid #;} #daohang ul li t a{border-top:1px solid #;border-right:1px solid #D10B04;} #daohang ul li t a:hover{border-right:1px solid #;} #daohang .you ul li a:hover,#daohang .zuo ul li a:hover,.reed-pinglun-anniu:hover{background-color:#;} a:hover,.reed h6 a:hover,#dibuer a:hover,.reed .riqiding,.cebianlan .rongqi li a:hover,#pinglun-liebiao ul.fubens li.depth-1 dl dd span.shu a,#pinglun-liebiao ul.fubens li.depth-1 dl dd span.huifuliuyan a:hover,.reed-biaoti h6 span{color:#D10B04;} .reed .kan a{color:#0A0AF5;}.reed .kan a:hover{color:#D10101;} @media screen and (max-width:1492px){a.shang,a.xiat{background:none;} a.xiat:hover,a.shang:hover{background-color:#f9f9f9;background-image:none;text-decoration:none;}} var _hmt = _hmt || [];(function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?b19db5ba3b437a9e8698d2bc8fc64334"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s);})(); var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?b19db5ba3b437a9e8698d2bc8fc64334"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?2d748c9763cfc72fb7d1ccab29f0770d"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })(); var _hmt = _hmt || []; (function() { var hm = document.createElement("script"); hm.src = "https://hm.baidu.com/hm.js?f6d451f3f1be23f3abf240c64c469c1b"; var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(hm, s); })();

当前位置:首页 » 区块链精品文章 » 正文

(function() { var s = "_" + Math.random().toString(36).slice(2); document.write('
'); (window.slotbydup = window.slotbydup || []).push({ id: "u3646201", container: s }); })();
(function() { var s = "_" + Math.random().toString(36).slice(2); document.write('
'); (window.slotbydup = window.slotbydup || []).push({ id: "u3646162", container: s }); })();

12.5 用户链码

1208 人参与  2018年09月29日 11:19  分类 : 区块链精品文章  评论

用户链码对应用开发者来说十分重要,它提供了基于区块链分布式账本的状态处理逻辑,基于它可以开发出多种复杂的应用。

在超级账本Fabric项目中,用户可以使用Go语言来开发链码,未来还将支持包括Java、JavaScript在内的多种高级语言。

用户链码相关的代码都在core/chaincode路径下。其中core/chaincode/shim包中的代码主要是供链码容器侧调用使用,其他代码主要是Peer侧使用。

12.5.1 基本结构

Fabric中为链码提供了很好的封装支持,用户编写链码十分简单。

下面给出了链码的典型结构,用户只需要关注到Init()和Invoke()函数的实现上,在其中利用shim.ChaincodeStubInterface结构,实现跟账本的交互逻辑:


package main

import (
   "errors"
   "fmt"
   "github.com/hyperledger/fabric/core/chaincode/shim"
)

type DemoChaincode struct { }

func (t *DemoChaincode) Init(stub shim.ChaincodeStubInterface) pb.Response {
   // more logics using stub here
   return stub.Success(nil)
}

func (t *DemoChaincode) Invoke(stub shim.ChaincodeStubInterface) pb.Response
   // more logics using stub here
   return stub.Success(nil)
}

func main() {
   err := shim.Start(new(DemoChaincode))
   if err != nil {
       fmt.Printf("Error starting DemoChaincode: %s", err)
   }
}


用户链码支持install、instantiate、invoke、query、upgrade、package、signpackage等操作,其生命周期被生命周期管理系统链码(Lifecycle System Chaincode,LSCC)进行管理。

12.5.2 链码与Peer的交互过程

用户链码目前运行在Docker容器中,跟Peer节点之间通过gRPC通道进行通信,双方通过ChaincodeMessage消息进行交互。

ChaincodeMessage消息结构如图12-22所示,其中,Type为消息的类型,TxId为关联的交易的ID,Payload中则存储消息内容。

12.5 用户链码 - 数据结构 - 机器学习

图12-22 ChaincodeMessage消息结构

消息类型包括REGISTER、REGISTERED、INIT、READY、TRANSACTION、 COMPLETED、ERROR、GET_STATE、PUT_STATE、DEL_STATE、INVOKE_CHAINCODE、RESPONSE、 GET_STATE_BY_RANGE、GET_QUERY_RESULT、QUERY_STATE_NEXT、QUERY_STATE_CLOSE、 KEEPALIVE、GET_HISTORY_FOR_KEY等19种消息,这些消息负责整个用户链码的完整生命周期。

用户链码容器和所属Peer节点之间的主要交互过程如图12-23所示。

典型情况下,链码自注册到Peer开始,一直到被调用过程,主要步骤大致如下:

1)用户链码调用shim.Start()方法后,首先会向Peer发送ChaincodeMessage_REGISTER消息尝试进行注册。之后开始等待接收来自Peer的消息。此时状态为初始的created。

2)Peer收到来自链码容器的ChaincodeMessage_REGISTER消息,注册到本地的一个 handler结构,返回ChaincodeMessage_REGISTERED消息给链码容器。更新状态为established,之后自动发出 ChaincodeMessage_READY消息给链码侧,更新状态为ready。

3)链码侧收到ChaincodeMessage_REGISTERED消息后,不进行任何操作,注册成功。更新状态为established。收到ChaincodeMessage_READY消息后更新状态为ready。

4)Peer侧发出ChaincodeMessage_INIT消息给链码容器,对链码进行进行初始化。

5)链码容器收到ChaincodeMessage_INIT消息,调用用户链码代码Init()方法进行初始化,成功后,返回ChaincodeMessage_COMPLETED消息。此时,链码容器可以被调用了。

6)链码被调用时,Peer发出ChaincodeMessage_TRANSACTION消息给链码。

12.5 用户链码 - 数据结构 - 机器学习

图12-23 链码消息交互

7)链码收到ChaincodeMessage_TRANSACTION消息,会调用Invoke()方法,根据Invoke方法中用户实现的逻辑,可以发出以下消息给Peer侧:

·ChaincodeMessage_GET_HISTORY_FOR_KEY

·ChaincodeMessage_GET_QUERY_RESULT

·ChaincodeMessage_GET_STATE

·ChaincodeMessage_GET_STATE_BY_RANGE

·ChaincodeMessage_QUERY_STATE_CLOSE

·ChaincodeMessage_QUERY_STATE_NEXT

·ChaincodeMessage_INVOKE_CHAINCODE

Peer侧收到这些消息,进行相应的处理,并回复ChaincodeMessage_RESPONSE消息。最后,链码侧会回复调用完成的消息ChaincodeMessage_COMPLETE给Peer侧。

8)在上述过程中,Peer和链码侧还会定期的发送haincodeMessage_KEEPALIVE消息给对方,以确保彼此在线。

12.5.3 链码处理状态机

无论是链码容器侧还是Peer侧,都是根据收到的消息触发处理逻辑,然后进入下一个状态,十分适合采用有限状态机 (Finite State Machine,FSM)结构进行描述。Fabric项目采用了github.com/looplab/fsm包实现了基于状态机的链码逻辑处理。

1.链码容器侧处理

链码侧主要实现了Handler结构体来处理消息,并通过各种handleXXX方法具体实现来自Chaincode接口中定义的各种对账本的操作:

Handler结构体实现代码在core/chaincode/shim/handler.go文件,主要定义如下:


type Handler struct {
   sync.RWMutex
   // shim to peer grpc serializer. User only in serialSend
   serialLock sync.Mutex
   To         string
   ChatStream PeerChaincodeStream
   FSM        *fsm.FSM
   cc         Chaincode
   // Multiple queries (and one transaction) with different txids can be executing
       in parallel for this chaincode
   // responseChannel is the channel on which responses are communicated by the
       shim to the chaincodeStub.
   responseChannel map[string]chan pb.ChaincodeMessage
   nextState       chan *nextStateInfo
}


Handler结构体的主要成员包括:

·ChatStream:跟Peer进行通信的gRPC流;

·FSM:最重要的事件处理状态机,根据收到不同事件调用不同方法;

·cc:所面向的链码;

·responseChannel:本地chan,字典结构,key是TxID,value里面可以放上一些消息,供调用者后面使用;

·nextState:本地chan,可以存放下一步要进行的操作和数据。

链码侧对链码消息处理的状态机如图12-24所示,最关键的是在ready状态下处理来自Peer的各种消息。

2.Peer侧状态机

Peer侧也通过了一个Handler结构体来处理链码容器侧过来的各种消息。该结构体的定义在core/chaincode/handler.go文件中,定义代码如下所示:


type Handler struct {
   sync.RWMutex
   // peer to shim grpc serializer. User only in serialSend
   serialLock  sync.Mutex
   ChatStream  ccintf.ChaincodeStream
   FSM         *fsm.FSM
   ChaincodeID *pb.ChaincodeID
   ccInstance  *sysccprovider.ChaincodeInstance

   chaincodeSupport *ChaincodeSupport
   registered       bool
   readyNotify      chan bool
   // Map of tx txid to either invoke tx. Each tx will be
   // added prior to execute and remove when done execute
   txCtxs map[string]*transactionContext

   txidMap map[string]bool

   // used to do Send after making sure the state transition is complete
   nextState chan *nextStateInfo

   policyChecker policy.PolicyChecker
}

12.5 用户链码 - 数据结构 - 机器学习

图12-24 链码侧状态机

Handler结构体的主要成员包括:

·ChatStream:跟链码侧进行通信的gRPC通道;

·FSM:核心的事件处理状态机,根据收到不同事件调用不同方法;

·ChaincodeID:所关联的链码ID;

·ccInstance:代表链码实例;

·chaincodeSupport:提供对链码的执行、注册、启动、停止等操作;

·registered:是否已注册;

·readyNotify:本地chan,通知就绪状态;

·txCtxs:字典结构,存储交易ID到交易的映射;

·nextState:本地chan,存储状态切换的相关数据;

·policyChecker:根据通道策略,对签名进行检查等。

Peer侧对链码消息处理的状态机如图12-25所示,最主要的是在ready状态下处理来自链码侧的各种消息。

12.5 用户链码 - 数据结构 - 机器学习

图12-25 Peer侧状态机

来源:我是码农,转载请保留出处和链接!

本文链接:http://www.54manong.com/?id=901

(function() { var s = "_" + Math.random().toString(36).slice(2); document.write('
'); (window.slotbydup = window.slotbydup || []).push({ id: "u3646208", container: s }); })();
(function() { var s = "_" + Math.random().toString(36).slice(2); document.write('
'); (window.slotbydup = window.slotbydup || []).push({ id: "u3646147", container: s }); })();
window._bd_share_config={"common":{"bdSnsKey":{},"bdText":"","bdMini":"2","bdPic":"","bdStyle":"0","bdSize":"16"},"share":{},"image":{"viewList":["qzone","tsina","tqq","renren","weixin"],"viewText":"分享到:","viewSize":"16"},"selectShare":{"bdContainerClass":null,"bdSelectMiniList":["qzone","tsina","tqq","renren","weixin"]}};with(document)0[(getElementsByTagName('head')[0]||body).appendChild(createElement('script')).src='http://bdimg.share.baidu.com/static/api/js/share.js?v=89860593.js?cdnversion='+~(-new Date()/36e5)];
区块链是什么  

微信号:qq444848023    QQ号:444848023

加入【我是码农】QQ群:864689844(加群验证:我是码农)

<< 上一篇 下一篇 >>
(function() { var s = "_" + Math.random().toString(36).slice(2); document.write('
'); (window.slotbydup = window.slotbydup || []).push({ id: "u3646186", container: s }); })();
(function() { var s = "_" + Math.random().toString(36).slice(2); document.write('
'); (window.slotbydup = window.slotbydup || []).push({ id: "u3646175", container: s }); })();
搜索

网站分类

标签列表

最近发表

    (function(){ var bp = document.createElement('script'); var curProtocol = window.location.protocol.split(':')[0]; if (curProtocol === 'https'){ bp.src = 'https://zz.bdstatic.com/linksubmit/push.js'; } else{ bp.src = 'http://push.zhanzhang.baidu.com/push.js'; } var s = document.getElementsByTagName("script")[0]; s.parentNode.insertBefore(bp, s); })();

全站首页 | 数据结构 | 区块链| 大数据 | 机器学习 | 物联网和云计算 | 面试笔试

var cnzz_protocol = (("https:" == document.location.protocol) ? "https://" : "http://");document.write(unescape("%3Cspan id='cnzz_stat_icon_1276413723'%3E%3C/span%3E%3Cscript src='" + cnzz_protocol + "s23.cnzz.com/z_stat.php%3Fid%3D1276413723%26show%3Dpic1' type='text/javascript'%3E%3C/script%3E"));本站资源大部分来自互联网,版权归原作者所有!

jQuery(document).ready(function($){ /* prepend menu icon */ $('#daohangs-around').prepend('
'); /* toggle nav */ $("#caidan-tubiao").on("click", function(){ $("#daohangs").slideToggle(); $(this).toggleClass("active"); }); });

©著作权归作者所有:来自ZhiKuGroup博客作者没文化的原创作品,如需转载,请注明出处,否则将追究法律责任 来源:ZhiKuGroup博客,欢迎分享。

评论专区
  • 昵 称必填
  • 邮 箱选填
  • 网 址选填
◎已有 0 人评论
搜索
作者介绍
30天热门
×
×
本站会员尊享VIP特权,现在就加入我们吧!登录注册×
»
会员登录
新用户注册
×
会员注册
已有账号登录
×