区块链教程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)

//获取怀疑脱机节点的只读通道

PresumedDead()

//关闭到某个节点的连接

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

未完待续欢迎继续关注区块链教程分享!

gossip 区块链_区块链教程Fabric1.0源代码分析流言算法Gossip服务端一兄弟连区块链教程-阿里云开发者社区...相关推荐

  1. 区块链教程Fabric1.0源代码分析流言算法Gossip服务端二

    区块链教程Fabric1.0源代码分析流言算法Gossip服务端二 Fabric 1.0源代码笔记 之 gossip(流言算法) #GossipServer(Gossip服务端) 5.2.commIm ...

  2. 区块链教程Fabric1.0源代码分析configtx#genesis-兄弟连

    区块链教程Fabric1.0源代码分析configtx#genesis,2018年下半年,区块链行业正逐渐褪去发展之初的浮躁.回归理性,表面上看相关人才需求与身价似乎正在回落.但事实上,正是初期泡沫的 ...

  3. 区块链教程Fabric1.0源代码分析Peer peer channel命令及子命令实现

    区块链教程Fabric1.0源代码分析Peer peer channel命令及子命令实现,2018年下半年,区块链行业正逐渐褪去发展之初的浮躁.回归理性,表面上看相关人才需求与身价似乎正在回落.但事实 ...

  4. 兄弟连区块链教程Fabric1.0源代码分析Peer peer根命令入口及加载子命令一

    区块链教程Fabric1.0源代码分析Peer peer根命令入口及加载子命令,2018年下半年,区块链行业正逐渐褪去发展之初的浮躁.回归理性,表面上看相关人才需求与身价似乎正在回落.但事实上,正是初 ...

  5. 区块链教程Fabric1.0源代码分析配置交易-生成通道配置二

    兄弟连区块链教程Fabric1.0源代码分析配置交易-生成通道配置二.Generator接口实现,即bootstrapper. type bootstrapper struct {channelGro ...

  6. 区块链教程Fabric1.0源代码分析scc(系统链码)

    区块链教程Fabric1.0源代码分析scc(系统链码),2018年下半年,区块链行业正逐渐褪去发展之初的浮躁.回归理性,表面上看相关人才需求与身价似乎正在回落.但事实上,正是初期泡沫的渐退,让人们更 ...

  7. 区块链教程Fabric1.0源代码分析Tx(Transaction 交易)一

    区块链教程Fabric1.0源代码分析Tx(Transaction 交易)一,2018年下半年,区块链行业正逐渐褪去发展之初的浮躁.回归理性,表面上看相关人才需求与身价似乎正在回落.但事实上,正是初期 ...

  8. 兄弟连区块链教程Fabric1.0源代码分析configupdate处理通道配置更新

    区块链教程Fabric1.0源代码分析configupdate处理通道配置更新,2018年下半年,区块链行业正逐渐褪去发展之初的浮躁.回归理性,表面上看相关人才需求与身价似乎正在回落.但事实上,正是初 ...

  9. 区块链教程Fabric1.0源代码分析Chaincode(链码)体系总结

    Fabric 1.0源代码笔记 之 Chaincode(链码) 1.Chaincode概述 Chaincode,即链码或智能合约,代码分布在protos/peer目录.core/chaincode和c ...

最新文章

  1. -mkdir 创建目录 Usage:hdfs dfs -mkdir [-p] < paths> 选项:-p 很像Unix mkdir -p,沿路径创建父目录。
  2. jQuery使用经验建议
  3. 狗窝里的小日子- 4 ...
  4. Controller计算值传到jsp页面,用session传值
  5. comsol积分函数_如何在 COMSOL 软件中合并解
  6. swift UI专项训练15 PcikerView老虎机视图
  7. windows下安装apache
  8. 什么是ARM开发板及其硬件特性介绍
  9. 【笔记】初读《SICP》:递归和迭代
  10. 三步走,帮你整理微信运营思路
  11. 1年19款,款款口碑爆棚,Cocos 插件大佬的真面目竟是?
  12. Windows共享上网的做法
  13. 9月Libra开发人员更新-路线图#1
  14. 基于蜉蝣优化算法的函数寻优算法
  15. 矩阵的等价,相似及合同
  16. 云台山风景区,来安化邂逅最美的景色
  17. MySQL零基础从入门到精通(函数篇)
  18. php人才系统 转让,PHP云人才系统 phpyun v3.2 正式版
  19. Go-一个写txt文件例子
  20. javaScript 正则表达式总结

热门文章

  1. 广播大学计算机考试,中央广播电视大学——学度第一学期期末考试计算机基础试题及参考答案...
  2. python读取rtf文件中指定的内容_使用Python读取RTF文件时遇到欧元符号问题
  3. 神经网络模型每次训练结果不一致
  4. 梯度下降算法原理及其计算过程
  5. 算法工程师的生存危机,我们怎么破?
  6. AP+AC旁挂式组网(简单易懂!新手必看!)
  7. java公钥加密私钥解密_公钥加密,私钥解密示例程序(JAVA)详解
  8. 【AD-Studio - 无法识别Module - 个人成功解决的方式】
  9. educoder基本SR锁存器+门控SR锁存器+与非门构成的门控SR锁存器
  10. oracle 自学笔记