区块链教程Fabric1.0源代码分析流言算法Gossip服务端一兄弟连区块链教程
区块链教程Fabric1.0源代码分析流言算法Gossip服务端一,2018年下半年,区块链行业正逐渐褪去发展之初的浮躁、回归理性,表面上看相关人才需求与身价似乎正在回落。但事实上,正是初期泡沫的渐退,让人们更多的关注点放在了区块链真正的技术之上。
Fabric 1.0源代码笔记 之 gossip(流言算法) #GossipServer(Gossip服务端)
1、GossipServer概述
GossipServer相关代码,分布在protos/gossip、gossip/comm目录下。目录结构如下:
- protos/gossip目录:
* message.pb.go,GossipClient接口定义及实现,GossipServer接口定义。 - gossip/comm目录:
* comm.go,Comm接口定义。
* conn.go,connFactory接口定义,以及connectionStore结构体及方法。
* comm_impl.go,commImpl结构体及方法(同时实现GossipServer接口/Comm接口/connFactory接口)。
* demux.go,ChannelDeMultiplexer结构体及方法。
2、GossipClient接口定义及实现
2.1、GossipClient接口定义
type GossipClient interface { // GossipStream is the gRPC stream used for sending and receiving messages GossipStream(ctx context.Context, opts ...grpc.CallOption) (Gossip_GossipStreamClient, error) // Ping is used to probe a remote peer's aliveness Ping(ctx context.Context, in *Empty, opts ...grpc.CallOption) (*Empty, error) } //代码在protos/gossip/message.pb.go
2.2、GossipClient接口实现
type gossipClient struct { cc *grpc.ClientConn } func NewGossipClient(cc *grpc.ClientConn) GossipClient { return &gossipClient{cc} } func (c *gossipClient) GossipStream(ctx context.Context, opts ...grpc.CallOption) (Gossip_GossipStreamClient, error) { stream, err := grpc.NewClientStream(ctx, &_Gossip_serviceDesc.Streams[0], c.cc, "/gossip.Gossip/GossipStream", opts...) if err != nil { return nil, err } x := &gossipGossipStreamClient{stream} return x, nil } func (c *gossipClient) Ping(ctx context.Context, in *Empty, opts ...grpc.CallOption) (*Empty, error) { out := new(Empty) err := grpc.Invoke(ctx, "/gossip.Gossip/Ping", in, out, c.cc, opts...) if err != nil { return nil, err } return out, nil } //代码在protos/gossip/message.pb.go
2.3、Gossip_GossipStreamClient接口定义及实现
type Gossip_GossipStreamClient interface { Send(*Envelope) error Recv() (*Envelope, error) grpc.ClientStream } type gossipGossipStreamClient struct { grpc.ClientStream } func (x *gossipGossipStreamClient) Send(m *Envelope) error { return x.ClientStream.SendMsg(m) } func (x *gossipGossipStreamClient) Recv() (*Envelope, error) { m := new(Envelope) if err := x.ClientStream.RecvMsg(m); err != nil { return nil, err } return m, nil } //代码在protos/gossip/message.pb.go
3、GossipServer接口定义
3.1、GossipServer接口定义
type GossipServer interface { // GossipStream is the gRPC stream used for sending and receiving messages GossipStream(Gossip_GossipStreamServer) error // Ping is used to probe a remote peer's aliveness Ping(context.Context, *Empty) (*Empty, error) } func RegisterGossipServer(s *grpc.Server, srv GossipServer) { s.RegisterService(&_Gossip_serviceDesc, srv) } func _Gossip_GossipStream_Handler(srv interface{}, stream grpc.ServerStream) error { return srv.(GossipServer).GossipStream(&gossipGossipStreamServer{stream}) } func _Gossip_Ping_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { in := new(Empty) if err := dec(in); err != nil { return nil, err } if interceptor == nil { return srv.(GossipServer).Ping(ctx, in) } info := &grpc.UnaryServerInfo{ Server: srv, FullMethod: "/gossip.Gossip/Ping", } handler := func(ctx context.Context, req interface{}) (interface{}, error) { return srv.(GossipServer).Ping(ctx, req.(*Empty)) } return interceptor(ctx, in, info, handler) } var _Gossip_serviceDesc = grpc.ServiceDesc{ ServiceName: "gossip.Gossip", HandlerType: (*GossipServer)(nil), Methods: []grpc.MethodDesc{ { MethodName: "Ping", Handler: _Gossip_Ping_Handler, }, }, Streams: []grpc.StreamDesc{ { StreamName: "GossipStream", Handler: _Gossip_GossipStream_Handler, ServerStreams: true, ClientStreams: true, }, }, Metadata: "gossip/message.proto", } //代码在protos/gossip/message.pb.go
3.2、Gossip_GossipStreamServer接口定义及实现
type Gossip_GossipStreamServer interface { Send(*Envelope) error Recv() (*Envelope, error) grpc.ServerStream } type gossipGossipStreamServer struct { grpc.ServerStream } func (x *gossipGossipStreamServer) Send(m *Envelope) error { return x.ServerStream.SendMsg(m) } func (x *gossipGossipStreamServer) Recv() (*Envelope, error) { m := new(Envelope) if err := x.ServerStream.RecvMsg(m); err != nil { return nil, err } return m, nil } //代码在protos/gossip/message.pb.go
4、Comm接口/connFactory接口定义
4.1、Comm接口定义
type Comm interface { //返回此实例的 PKI id GetPKIid() common.PKIidType //向节点发送消息 Send(msg *proto.SignedGossipMessage, peers ...*RemotePeer) //探测远程节点是否有响应 Probe(peer *RemotePeer) error //握手验证远程节点 Handshake(peer *RemotePeer) (api.PeerIdentityType, error) Accept(common.MessageAcceptor) <-chan proto.ReceivedMessage //获取怀疑脱机节点的只读通道 PresumedDead() <-chan common.PKIidType //关闭到某个节点的连接 CloseConn(peer *RemotePeer) //关闭 Stop() } //代码在gossip/comm/comm.go
4.2、connFactory接口定义
type connFactory interface { createConnection(endpoint string, pkiID common.PKIidType) (*connection, error) } //代码在gossip/comm/conn.go
5、commImpl结构体及方法(同时实现GossipServer接口/Comm接口/connFactory接口)
5.1、commImpl结构体定义
type commImpl struct { selfCertHash []byte peerIdentity api.PeerIdentityType idMapper identity.Mapper logger *logging.Logger opts []grpc.DialOption secureDialOpts func() []grpc.DialOption connStore *connectionStore PKIID []byte deadEndpoints chan common.PKIidType msgPublisher *ChannelDeMultiplexer lock *sync.RWMutex lsnr net.Listener gSrv *grpc.Server exitChan chan struct{} stopWG sync.WaitGroup subscriptions []chan proto.ReceivedMessage port int stopping int32 } //代码在gossip/comm/comm_impl.go
未完待续欢迎继续关注区块链教程分享!
低调大师中文资讯倾力打造互联网数据资讯、行业资源、电子商务、移动互联网、网络营销平台。
持续更新报道IT业界、互联网、市场资讯、驱动更新,是最及时权威的产业资讯及硬件资讯报道平台。
转载内容版权归作者及来源网站所有,本站原创内容转载请注明来源。
- 上一篇
MyBatis3.x Idea搭建
MyBatis 本是apache的一个开源项目iBatis, 2010年这个项目由apache software foundation 迁移到了google code,并且改名为MyBatis 。2013年11月迁移到Github。 MyBatis作为一款优秀的orm框架,经常与Hibernate拿来比较,身为目前较流行的MyBatis 避免了几乎所有的 JDBC 代码和手动设置参数以及获取结果集。接下来我们来看看如何搭建一个MyBaits项目。 因为idea并不支持mybatis的自动搭建,所以我们需要手动的导入一些jar包 需要准备的Jar包 mybatis-3.4.6.jar mybatis的主体mysql-connector-java-5.1.46.jar 链接数据的log4j-1.2.17 用于打印日志的 关于Mybatis的构建分为两种,我们先看XML方式 XML方式 1. Mybatis-configuration.xml mybatis-configuration.xml <?xml version="1.0" encoding="UTF-8" ?> <...
- 下一篇
搞定所有的跨域请求问题: jsonp & CORS
网上各种跨域教程,各种实践,各种问答,除了简单的 jsonp 以外,很多说 CORS 的都是行不通的,老是缺那么一两个关键的配置。本文只想解决问题,所有的代码经过亲自实践。 本文解决跨域中的 get、post、data、cookie 等这些问题。 本文只会说 get 请求和 post 请求,读者请把 post 请求理解成除 get 请求外的所有其他请求方式。 JSONP jsonp 的原理很简单,利用了【前端请求静态资源的时候不存在跨域问题】这个思路。 但是只支持 get,只支持 get,只支持 get。 注意一点,既然这个方法叫 jsonp,后端数据一定要使用 json 数据,不能随便的搞个字符串什么的,不然你会觉得结果莫名其妙的。 前端 jQuery 写法 $.ajax({ type: "get", url: baseUrl + "/jsonp/get", dataType: "jsonp", success: function(response) { $("#response").val(JSON.stringify(response)); } }); dataType: "js...
相关文章
文章评论
共有0条评论来说两句吧...
文章二维码
点击排行
推荐阅读
最新文章
- CentOS8,CentOS7,CentOS6编译安装Redis5.0.7
- CentOS7,CentOS8安装Elasticsearch6.8.6
- Docker使用Oracle官方镜像安装(12C,18C,19C)
- Jdk安装(Linux,MacOS,Windows),包含三大操作系统的最全安装
- Linux系统CentOS6、CentOS7手动修改IP地址
- CentOS7安装Docker,走上虚拟化容器引擎之路
- CentOS7编译安装Cmake3.16.3,解决mysql等软件编译问题
- SpringBoot2全家桶,快速入门学习开发网站教程
- CentOS7设置SWAP分区,小内存服务器的救世主
- Docker快速安装Oracle11G,搭建oracle11g学习环境