Go语言+区块链教程Fabric1.0源代码分析gRPC(Fabric中注册的gRPC Service)二。
### 1.3、Endorser Service(背书服务)
#### 1.3.1、Endorser Service客户端
```go
type EndorserClient interface {
ProcessProposal(ctx context.Context, in *SignedProposal, opts ...grpc.CallOption) (*ProposalResponse, error)
type endorserClient struct {
cc *grpc.ClientConn
func NewEndorserClient(cc *grpc.ClientConn) EndorserClient {
return &endorserClient{cc}
func (c *endorserClient) ProcessProposal(ctx context.Context, in *SignedProposal, opts ...grpc.CallOption) (*ProposalResponse, error) {
out := new(ProposalResponse)
err := grpc.Invoke(ctx, "/protos.Endorser/ProcessProposal", in, out, c.cc, opts...)
if err != nil {
return nil, err
return out, nil
//代码在protos/peer/peer.pb.go
#### 1.3.2、Endorser Service服务端
```go
type EndorserServer interface {
ProcessProposal(context.Context, *SignedProposal) (*ProposalResponse, error)
func RegisterEndorserServer(s *grpc.Server, srv EndorserServer) {
s.RegisterService(&_Endorser_serviceDesc, srv)
func _Endorser_ProcessProposal_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
in := new(SignedProposal)
if err := dec(in); err != nil {
return nil, err
if interceptor == nil {
return srv.(EndorserServer).ProcessProposal(ctx, in)
info := &grpc.UnaryServerInfo{
Server: srv,
FullMethod: "/protos.Endorser/ProcessProposal",
handler := func(ctx context.Context, req interface{}) (interface{}, error) {
return srv.(EndorserServer).ProcessProposal(ctx, req.(*SignedProposal))
return interceptor(ctx, in, info, handler)
var _Endorser_serviceDesc = grpc.ServiceDesc{
ServiceName: "protos.Endorser",
HandlerType: (*EndorserServer)(nil),
Methods: []grpc.MethodDesc{
MethodName: "ProcessProposal",
Handler: _Endorser_ProcessProposal_Handler,
Streams: []grpc.StreamDesc{},
Metadata: "peer/peer.proto",
//代码在protos/peer/peer.pb.go
### 1.4、ChaincodeSupport Service(链码支持服务)
#### 1.4.1、ChaincodeSupport Service客户端
```go
type ChaincodeSupportClient interface {
Register(ctx context.Context, opts ...grpc.CallOption) (ChaincodeSupport_RegisterClient, error)
type chaincodeSupportClient struct {
cc *grpc.ClientConn
func NewChaincodeSupportClient(cc *grpc.ClientConn) ChaincodeSupportClient {
return &chaincodeSupportClient{cc}
func (c *chaincodeSupportClient) Register(ctx context.Context, opts ...grpc.CallOption) (ChaincodeSupport_RegisterClient, error) {
stream, err := grpc.NewClientStream(ctx, &_ChaincodeSupport_serviceDesc.Streams[0], c.cc, "/protos.ChaincodeSupport/Register", opts...)
if err != nil {
return nil, err
x := &chaincodeSupportRegisterClient{stream}
return x, nil
//代码在protos/peer/peer.pb.go
#### 1.4.2、ChaincodeSupport Service服务端
```go
type ChaincodeSupportServer interface {
Register(ChaincodeSupport_RegisterServer) error
func RegisterChaincodeSupportServer(s *grpc.Server, srv ChaincodeSupportServer) {
s.RegisterService(&_ChaincodeSupport_serviceDesc, srv)
func _ChaincodeSupport_Register_Handler(srv interface{}, stream grpc.ServerStream) error {
return srv.(ChaincodeSupportServer).Register(&chaincodeSupportRegisterServer{stream})
var _ChaincodeSupport_serviceDesc = grpc.ServiceDesc{
ServiceName: "protos.ChaincodeSupport",
HandlerType: (*ChaincodeSupportServer)(nil),
Methods: []grpc.MethodDesc{},
Streams: []grpc.StreamDesc{
StreamName: "Register",
Handler: _ChaincodeSupport_Register_Handler,
ServerStreams: true,
ClientStreams: true,
Metadata: "peer/chaincode_shim.proto",
//代码在protos/peer/peer.pb.go
### 1.5、Gossip Service(Gossip服务)
#### 1.5.1、Gossip Service客户端
```go
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)
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
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
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
#### 1.5.2、Gossip Serviced服务端
```go
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})
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
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
## 2、Orderer节点中注册的gRPC Service
### 2.1、AtomicBroadcast Service(广播服务)
#### 2.1.1、AtomicBroadcast Service客户端
```go
type AtomicBroadcastClient interface {
// broadcast receives a reply of Acknowledgement for each common.Envelope in order, indicating success or type of failure
Broadcast(ctx context.Context, opts ...grpc.CallOption) (AtomicBroadcast_BroadcastClient, error)
// deliver first requires an Envelope of type DELIVER_SEEK_INFO with Payload data as a mashaled SeekInfo message, then a stream of block replies is received.
Deliver(ctx context.Context, opts ...grpc.CallOption) (AtomicBroadcast_DeliverClient, error)
type atomicBroadcastClient struct {
cc *grpc.ClientConn
func NewAtomicBroadcastClient(cc *grpc.ClientConn) AtomicBroadcastClient {
return &atomicBroadcastClient{cc}
func (c *atomicBroadcastClient) Broadcast(ctx context.Context, opts ...grpc.CallOption) (AtomicBroadcast_BroadcastClient, error) {
stream, err := grpc.NewClientStream(ctx, &_AtomicBroadcast_serviceDesc.Streams[0], c.cc, "/orderer.AtomicBroadcast/Broadcast", opts...)
if err != nil {
return nil, err
x := &atomicBroadcastBroadcastClient{stream}
return x, nil
func (c *atomicBroadcastClient) Deliver(ctx context.Context, opts ...grpc.CallOption) (AtomicBroadcast_DeliverClient, error) {
stream, err := grpc.NewClientStream(ctx, &_AtomicBroadcast_serviceDesc.Streams[1], c.cc, "/orderer.AtomicBroadcast/Deliver", opts...)
if err != nil {
return nil, err
x := &atomicBroadcastDeliverClient{stream}
return x, nil
//代码在protos/orderer/ab.pb.go
#### 2.1.2、AtomicBroadcast Service服务端
```go
type AtomicBroadcastServer interface {
// broadcast receives a reply of Acknowledgement for each common.Envelope in order, indicating success or type of failure
Broadcast(AtomicBroadcast_BroadcastServer) error
// deliver first requires an Envelope of type DELIVER_SEEK_INFO with Payload data as a mashaled SeekInfo message, then a stream of block replies is received.
Deliver(AtomicBroadcast_DeliverServer) error
func RegisterAtomicBroadcastServer(s *grpc.Server, srv AtomicBroadcastServer) {
s.RegisterService(&_AtomicBroadcast_serviceDesc, srv)
func _AtomicBroadcast_Broadcast_Handler(srv interface{}, stream grpc.ServerStream) error {
return srv.(AtomicBroadcastServer).Broadcast(&atomicBroadcastBroadcastServer{stream})
func _AtomicBroadcast_Deliver_Handler(srv interface{}, stream grpc.ServerStream) error {
return srv.(AtomicBroadcastServer).Deliver(&atomicBroadcastDeliverServer{stream})
var _AtomicBroadcast_serviceDesc = grpc.ServiceDesc{
ServiceName: "orderer.AtomicBroadcast",
HandlerType: (*AtomicBroadcastServer)(nil),
Methods: []grpc.MethodDesc{},
Streams: []grpc.StreamDesc{
StreamName: "Broadcast",
Handler: _AtomicBroadcast_Broadcast_Handler,
ServerStreams: true,
ClientStreams: true,
StreamName: "Deliver",
Handler: _AtomicBroadcast_Deliver_Handler,
ServerStreams: true,
ClientStreams: true,
Metadata: "orderer/ab.proto",
//代码在protos/orderer/ab.pb.go
感谢关注兄弟连区块链教程分享!