兄弟连区块链培训Fabric1.0源代码分析(46)gRPC(Fabric中注册的gRPCService)
#Fabric1.0源代码笔记之-gRPC(Fabric中注册的gRPCService)
Peer节点中注册的gRPCService,包括:
*EventsService(事件服务):Chat
*AdminService(管理服务):GetStatus、StartServer、GetModuleLogLevel、SetModuleLogLevel、RevertLogLevels
*EndorserService(背书服务):ProcessProposal
*ChaincodeSupportService(链码支持服务):Register
*GossipService(Gossip服务):GossipStream、Ping
Orderer节点中注册的gRPCService,包括:
*AtomicBroadcastService(广播服务):Broadcast、Deliver
##1、Peer节点中注册的gRPCService
###1.1、EventsService(事件服务)
####1.1.1、EventsService客户端
```go
typeEventsClientinterface{
//eventchattingusingEvent
Chat(ctxcontext.Context,opts...grpc.CallOption)(Events_ChatClient,error)
}
typeeventsClientstruct{
cc*grpc.ClientConn
}
funcNewEventsClient(cc*grpc.ClientConn)EventsClient{
return&eventsClient{cc}
}
func(c*eventsClient)Chat(ctxcontext.Context,opts...grpc.CallOption)(Events_ChatClient,error){
stream,err:=grpc.NewClientStream(ctx,&_Events_serviceDesc.Streams[0],c.cc,"/protos.Events/Chat",opts...)
iferr!=nil{
returnnil,err
}
x:=&eventsChatClient{stream}
returnx,nil
}
//代码在protos/peer/events.pb.go
```
####1.1.2、EventsService服务端
```go
typeEventsServerinterface{
Chat(Events_ChatServer)error
}
funcRegisterEventsServer(s*grpc.Server,srvEventsServer){
s.RegisterService(&_Events_serviceDesc,srv)
}
func_Events_Chat_Handler(srvinterface{},streamgrpc.ServerStream)error{
returnsrv.(EventsServer).Chat(&eventsChatServer{stream})
}
var_Events_serviceDesc=grpc.ServiceDesc{
ServiceName:"protos.Events",
HandlerType:(*EventsServer)(nil),
Methods:[]grpc.MethodDesc{},
Streams:[]grpc.StreamDesc{
{
StreamName:"Chat",
Handler:_Events_Chat_Handler,
ServerStreams:true,
ClientStreams:true,
},
},
Metadata:"peer/events.proto",
}
//代码在protos/peer/events.pb.go
```
###1.2、AdminService(管理服务)
####1.2.1、AdminService客户端
```go
typeAdminClientinterface{
//Returntheservestatus.
GetStatus(ctxcontext.Context,in*google_protobuf.Empty,opts...grpc.CallOption)(*ServerStatus,error)
StartServer(ctxcontext.Context,in*google_protobuf.Empty,opts...grpc.CallOption)(*ServerStatus,error)
GetModuleLogLevel(ctxcontext.Context,in*LogLevelRequest,opts...grpc.CallOption)(*LogLevelResponse,error)
SetModuleLogLevel(ctxcontext.Context,in*LogLevelRequest,opts...grpc.CallOption)(*LogLevelResponse,error)
RevertLogLevels(ctxcontext.Context,in*google_protobuf.Empty,opts...grpc.CallOption)(*google_protobuf.Empty,error)
}
typeadminClientstruct{
cc*grpc.ClientConn
}
funcNewAdminClient(cc*grpc.ClientConn)AdminClient{
return&adminClient{cc}
}
func(c*adminClient)GetStatus(ctxcontext.Context,in*google_protobuf.Empty,opts...grpc.CallOption)(*ServerStatus,error){
out:=new(ServerStatus)
err:=grpc.Invoke(ctx,"/protos.Admin/GetStatus",in,out,c.cc,opts...)
iferr!=nil{
returnnil,err
}
returnout,nil
}
func(c*adminClient)StartServer(ctxcontext.Context,in*google_protobuf.Empty,opts...grpc.CallOption)(*ServerStatus,error){
out:=new(ServerStatus)
err:=grpc.Invoke(ctx,"/protos.Admin/StartServer",in,out,c.cc,opts...)
iferr!=nil{
returnnil,err
}
returnout,nil
}
func(c*adminClient)GetModuleLogLevel(ctxcontext.Context,in*LogLevelRequest,opts...grpc.CallOption)(*LogLevelResponse,error){
out:=new(LogLevelResponse)
err:=grpc.Invoke(ctx,"/protos.Admin/GetModuleLogLevel",in,out,c.cc,opts...)
iferr!=nil{
returnnil,err
}
returnout,nil
}
func(c*adminClient)SetModuleLogLevel(ctxcontext.Context,in*LogLevelRequest,opts...grpc.CallOption)(*LogLevelResponse,error){
out:=new(LogLevelResponse)
err:=grpc.Invoke(ctx,"/protos.Admin/SetModuleLogLevel",in,out,c.cc,opts...)
iferr!=nil{
returnnil,err
}
returnout,nil
}
func(c*adminClient)RevertLogLevels(ctxcontext.Context,in*google_protobuf.Empty,opts...grpc.CallOption)(*google_protobuf.Empty,error){
out:=new(google_protobuf.Empty)
err:=grpc.Invoke(ctx,"/protos.Admin/RevertLogLevels",in,out,c.cc,opts...)
iferr!=nil{
returnnil,err
}
returnout,nil
}
//代码在protos/peer/admin.pb.go
```
####1.2.2、AdminService服务端
```go
typeAdminServerinterface{
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)
}
funcRegisterAdminServer(s*grpc.Server,srvAdminServer){
s.RegisterService(&_Admin_serviceDesc,srv)
}
func_Admin_GetStatus_Handler(srvinterface{},ctxcontext.Context,decfunc(interface{})error,interceptorgrpc.UnaryServerInterceptor)(interface{},error){
in:=new(google_protobuf.Empty)
iferr:=dec(in);err!=nil{
returnnil,err
}
ifinterceptor==nil{
returnsrv.(AdminServer).GetStatus(ctx,in)
}
info:=&grpc.UnaryServerInfo{
Server:srv,
FullMethod:"/protos.Admin/GetStatus",
}
handler:=func(ctxcontext.Context,reqinterface{})(interface{},error){
returnsrv.(AdminServer).GetStatus(ctx,req.(*google_protobuf.Empty))
}
returninterceptor(ctx,in,info,handler)
}
func_Admin_StartServer_Handler(srvinterface{},ctxcontext.Context,decfunc(interface{})error,interceptorgrpc.UnaryServerInterceptor)(interface{},error){
in:=new(google_protobuf.Empty)
iferr:=dec(in);err!=nil{
returnnil,err
}
ifinterceptor==nil{
returnsrv.(AdminServer).StartServer(ctx,in)
}
info:=&grpc.UnaryServerInfo{
Server:srv,
FullMethod:"/protos.Admin/StartServer",
}
handler:=func(ctxcontext.Context,reqinterface{})(interface{},error){
returnsrv.(AdminServer).StartServer(ctx,req.(*google_protobuf.Empty))
}
returninterceptor(ctx,in,info,handler)
}
func_Admin_GetModuleLogLevel_Handler(srvinterface{},ctxcontext.Context,decfunc(interface{})error,interceptorgrpc.UnaryServerInterceptor)(interface{},error){
in:=new(LogLevelRequest)
iferr:=dec(in);err!=nil{
returnnil,err
}
ifinterceptor==nil{
returnsrv.(AdminServer).GetModuleLogLevel(ctx,in)
}
info:=&grpc.UnaryServerInfo{
Server:srv,
FullMethod:"/protos.Admin/GetModuleLogLevel",
}
handler:=func(ctxcontext.Context,reqinterface{})(interface{},error){
returnsrv.(AdminServer).GetModuleLogLevel(ctx,req.(*LogLevelRequest))
}
returninterceptor(ctx,in,info,handler)
}
func_Admin_SetModuleLogLevel_Handler(srvinterface{},ctxcontext.Context,decfunc(interface{})error,interceptorgrpc.UnaryServerInterceptor)(interface{},error){
in:=new(LogLevelRequest)
iferr:=dec(in);err!=nil{
returnnil,err
}
ifinterceptor==nil{
returnsrv.(AdminServer).SetModuleLogLevel(ctx,in)
}
info:=&grpc.UnaryServerInfo{
Server:srv,
FullMethod:"/protos.Admin/SetModuleLogLevel",
}
handler:=func(ctxcontext.Context,reqinterface{})(interface{},error){
returnsrv.(AdminServer).SetModuleLogLevel(ctx,req.(*LogLevelRequest))
}
returninterceptor(ctx,in,info,handler)
}
func_Admin_RevertLogLevels_Handler(srvinterface{},ctxcontext.Context,decfunc(interface{})error,interceptorgrpc.UnaryServerInterceptor)(interface{},error){
in:=new(google_protobuf.Empty)
iferr:=dec(in);err!=nil{
returnnil,err
}
ifinterceptor==nil{
returnsrv.(AdminServer).RevertLogLevels(ctx,in)
}
info:=&grpc.UnaryServerInfo{
Server:srv,
FullMethod:"/protos.Admin/RevertLogLevels",
}
handler:=func(ctxcontext.Context,reqinterface{})(interface{},error){
returnsrv.(AdminServer).RevertLogLevels(ctx,req.(*google_protobuf.Empty))
}
returninterceptor(ctx,in,info,handler)
}
var_Admin_serviceDesc=grpc.ServiceDesc{
ServiceName:"protos.Admin",
HandlerType:(*AdminServer)(nil),
Methods:[]grpc.MethodDesc{
{
MethodName:"GetStatus",
Handler:_Admin_GetStatus_Handler,
},
{
MethodName:"StartServer",
Handler:_Admin_StartServer_Handler,
},
{
MethodName:"GetModuleLogLevel",
Handler:_Admin_GetModuleLogLevel_Handler,
},
{
MethodName:"SetModuleLogLevel",
Handler:_Admin_SetModuleLogLevel_Handler,
},
{
MethodName:"RevertLogLevels",
Handler:_Admin_RevertLogLevels_Handler,
},
},
Streams:[]grpc.StreamDesc{},
Metadata:"peer/admin.proto",
}
//代码在protos/peer/admin.pb.go
```
###1.3、EndorserService(背书服务)
####1.3.1、EndorserService客户端
```go
typeEndorserClientinterface{
ProcessProposal(ctxcontext.Context,in*SignedProposal,opts...grpc.CallOption)(*ProposalResponse,error)
}
typeendorserClientstruct{
cc*grpc.ClientConn
}
funcNewEndorserClient(cc*grpc.ClientConn)EndorserClient{
return&endorserClient{cc}
}
func(c*endorserClient)ProcessProposal(ctxcontext.Context,in*SignedProposal,opts...grpc.CallOption)(*ProposalResponse,error){
out:=new(ProposalResponse)
err:=grpc.Invoke(ctx,"/protos.Endorser/ProcessProposal",in,out,c.cc,opts...)
iferr!=nil{
returnnil,err
}
returnout,nil
}
//代码在protos/peer/peer.pb.go
```
####1.3.2、EndorserService服务端
```go
typeEndorserServerinterface{
ProcessProposal(context.Context,*SignedProposal)(*ProposalResponse,error)
}
funcRegisterEndorserServer(s*grpc.Server,srvEndorserServer){
s.RegisterService(&_Endorser_serviceDesc,srv)
}
func_Endorser_ProcessProposal_Handler(srvinterface{},ctxcontext.Context,decfunc(interface{})error,interceptorgrpc.UnaryServerInterceptor)(interface{},error){
in:=new(SignedProposal)
iferr:=dec(in);err!=nil{
returnnil,err
}
ifinterceptor==nil{
returnsrv.(EndorserServer).ProcessProposal(ctx,in)
}
info:=&grpc.UnaryServerInfo{
Server:srv,
FullMethod:"/protos.Endorser/ProcessProposal",
}
handler:=func(ctxcontext.Context,reqinterface{})(interface{},error){
returnsrv.(EndorserServer).ProcessProposal(ctx,req.(*SignedProposal))
}
returninterceptor(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、ChaincodeSupportService(链码支持服务)
####1.4.1、ChaincodeSupportService客户端
```go
typeChaincodeSupportClientinterface{
Register(ctxcontext.Context,opts...grpc.CallOption)(ChaincodeSupport_RegisterClient,error)
}
typechaincodeSupportClientstruct{
cc*grpc.ClientConn
}
funcNewChaincodeSupportClient(cc*grpc.ClientConn)ChaincodeSupportClient{
return&chaincodeSupportClient{cc}
}
func(c*chaincodeSupportClient)Register(ctxcontext.Context,opts...grpc.CallOption)(ChaincodeSupport_RegisterClient,error){
stream,err:=grpc.NewClientStream(ctx,&_ChaincodeSupport_serviceDesc.Streams[0],c.cc,"/protos.ChaincodeSupport/Register",opts...)
iferr!=nil{
returnnil,err
}
x:=&chaincodeSupportRegisterClient{stream}
returnx,nil
}
//代码在protos/peer/peer.pb.go
```
####1.4.2、ChaincodeSupportService服务端
```go
typeChaincodeSupportServerinterface{
Register(ChaincodeSupport_RegisterServer)error
}
funcRegisterChaincodeSupportServer(s*grpc.Server,srvChaincodeSupportServer){
s.RegisterService(&_ChaincodeSupport_serviceDesc,srv)
}
func_ChaincodeSupport_Register_Handler(srvinterface{},streamgrpc.ServerStream)error{
returnsrv.(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、GossipService(Gossip服务)
####1.5.1、GossipService客户端
```go
typeGossipClientinterface{
//GossipStreamisthegRPCstreamusedforsendingandreceivingmessages
GossipStream(ctxcontext.Context,opts...grpc.CallOption)(Gossip_GossipStreamClient,error)
//Pingisusedtoprobearemotepeer'saliveness
Ping(ctxcontext.Context,in*Empty,opts...grpc.CallOption)(*Empty,error)
}
typegossipClientstruct{
cc*grpc.ClientConn
}
funcNewGossipClient(cc*grpc.ClientConn)GossipClient{
return&gossipClient{cc}
}
func(c*gossipClient)GossipStream(ctxcontext.Context,opts...grpc.CallOption)(Gossip_GossipStreamClient,error){
stream,err:=grpc.NewClientStream(ctx,&_Gossip_serviceDesc.Streams[0],c.cc,"/gossip.Gossip/GossipStream",opts...)
iferr!=nil{
returnnil,err
}
x:=&gossipGossipStreamClient{stream}
returnx,nil
}
typeGossip_GossipStreamClientinterface{
Send(*Envelope)error
Recv()(*Envelope,error)
grpc.ClientStream
}
typegossipGossipStreamClientstruct{
grpc.ClientStream
}
func(x*gossipGossipStreamClient)Send(m*Envelope)error{
returnx.ClientStream.SendMsg(m)
}
func(x*gossipGossipStreamClient)Recv()(*Envelope,error){
m:=new(Envelope)
iferr:=x.ClientStream.RecvMsg(m);err!=nil{
returnnil,err
}
returnm,nil
}
func(c*gossipClient)Ping(ctxcontext.Context,in*Empty,opts...grpc.CallOption)(*Empty,error){
out:=new(Empty)
err:=grpc.Invoke(ctx,"/gossip.Gossip/Ping",in,out,c.cc,opts...)
iferr!=nil{
returnnil,err
}
returnout,nil
}
//代码在protos/gossip/message.pb.go
```
####1.5.2、GossipServiced服务端
```go
typeGossipServerinterface{
//GossipStreamisthegRPCstreamusedforsendingandreceivingmessages
GossipStream(Gossip_GossipStreamServer)error
//Pingisusedtoprobearemotepeer'saliveness
Ping(context.Context,*Empty)(*Empty,error)
}
funcRegisterGossipServer(s*grpc.Server,srvGossipServer){
s.RegisterService(&_Gossip_serviceDesc,srv)
}
func_Gossip_GossipStream_Handler(srvinterface{},streamgrpc.ServerStream)error{
returnsrv.(GossipServer).GossipStream(&gossipGossipStreamServer{stream})
}
typeGossip_GossipStreamServerinterface{
Send(*Envelope)error
Recv()(*Envelope,error)
grpc.ServerStream
}
typegossipGossipStreamServerstruct{
grpc.ServerStream
}
func(x*gossipGossipStreamServer)Send(m*Envelope)error{
returnx.ServerStream.SendMsg(m)
}
func(x*gossipGossipStreamServer)Recv()(*Envelope,error){
m:=new(Envelope)
iferr:=x.ServerStream.RecvMsg(m);err!=nil{
returnnil,err
}
returnm,nil
}
func_Gossip_Ping_Handler(srvinterface{},ctxcontext.Context,decfunc(interface{})error,interceptorgrpc.UnaryServerInterceptor)(interface{},error){
in:=new(Empty)
iferr:=dec(in);err!=nil{
returnnil,err
}
ifinterceptor==nil{
returnsrv.(GossipServer).Ping(ctx,in)
}
info:=&grpc.UnaryServerInfo{
Server:srv,
FullMethod:"/gossip.Gossip/Ping",
}
handler:=func(ctxcontext.Context,reqinterface{})(interface{},error){
returnsrv.(GossipServer).Ping(ctx,req.(*Empty))
}
returninterceptor(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节点中注册的gRPCService
###2.1、AtomicBroadcastService(广播服务)
####2.1.1、AtomicBroadcastService客户端
```go
typeAtomicBroadcastClientinterface{
//broadcastreceivesareplyofAcknowledgementforeachcommon.Envelopeinorder,indicatingsuccessortypeoffailure
Broadcast(ctxcontext.Context,opts...grpc.CallOption)(AtomicBroadcast_BroadcastClient,error)
//deliverfirstrequiresanEnvelopeoftypeDELIVER_SEEK_INFOwithPayloaddataasamashaledSeekInfomessage,thenastreamofblockrepliesisreceived.
Deliver(ctxcontext.Context,opts...grpc.CallOption)(AtomicBroadcast_DeliverClient,error)
}
typeatomicBroadcastClientstruct{
cc*grpc.ClientConn
}
funcNewAtomicBroadcastClient(cc*grpc.ClientConn)AtomicBroadcastClient{
return&atomicBroadcastClient{cc}
}
func(c*atomicBroadcastClient)Broadcast(ctxcontext.Context,opts...grpc.CallOption)(AtomicBroadcast_BroadcastClient,error){
stream,err:=grpc.NewClientStream(ctx,&_AtomicBroadcast_serviceDesc.Streams[0],c.cc,"/orderer.AtomicBroadcast/Broadcast",opts...)
iferr!=nil{
returnnil,err
}
x:=&atomicBroadcastBroadcastClient{stream}
returnx,nil
}
func(c*atomicBroadcastClient)Deliver(ctxcontext.Context,opts...grpc.CallOption)(AtomicBroadcast_DeliverClient,error){
stream,err:=grpc.NewClientStream(ctx,&_AtomicBroadcast_serviceDesc.Streams[1],c.cc,"/orderer.AtomicBroadcast/Deliver",opts...)
iferr!=nil{
returnnil,err
}
x:=&atomicBroadcastDeliverClient{stream}
returnx,nil
}
//代码在protos/orderer/ab.pb.go
```
####2.1.2、AtomicBroadcastService服务端
```go
typeAtomicBroadcastServerinterface{
//broadcastreceivesareplyofAcknowledgementforeachcommon.Envelopeinorder,indicatingsuccessortypeoffailure
Broadcast(AtomicBroadcast_BroadcastServer)error
//deliverfirstrequiresanEnvelopeoftypeDELIVER_SEEK_INFOwithPayloaddataasamashaledSeekInfomessage,thenastreamofblockrepliesisreceived.
Deliver(AtomicBroadcast_DeliverServer)error
}
funcRegisterAtomicBroadcastServer(s*grpc.Server,srvAtomicBroadcastServer){
s.RegisterService(&_AtomicBroadcast_serviceDesc,srv)
}
func_AtomicBroadcast_Broadcast_Handler(srvinterface{},streamgrpc.ServerStream)error{
returnsrv.(AtomicBroadcastServer).Broadcast(&atomicBroadcastBroadcastServer{stream})
}
func_AtomicBroadcast_Deliver_Handler(srvinterface{},streamgrpc.ServerStream)error{
returnsrv.(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