区块链教程Fabric1.0源代码分析gRPC(Fabric中注册的gRPC Service)一,2018年下半年,区块链行业正逐渐褪去发展之初的浮躁、回归理性,表面上看相关人才需求与身价似乎正在回落。但事实上,正是初期泡沫的渐退,让人们更多的关注点放在了区块链真正的技术之上。

Fabric 1.0源代码笔记 之 -gRPC(Fabric中注册的gRPC Service)

Peer节点中注册的gRPC Service,包括:

  • Events Service(事件服务):Chat
  • Admin Service(管理服务):GetStatus、StartServer、GetModuleLogLevel、SetModuleLogLevel、RevertLogLevels
  • Endorser Service(背书服务):ProcessProposal
  • ChaincodeSupport Service(链码支持服务):Register
  • Gossip Service(Gossip服务):GossipStream、Ping

Orderer节点中注册的gRPC Service,包括:

  • AtomicBroadcast Service(广播服务):Broadcast、Deliver

1、Peer节点中注册的gRPC Service

1.1、Events Service(事件服务)

1.1.1、Events Service客户端
type EventsClient interface {
    // event chatting using Event
    Chat(ctx context.Context, opts ...grpc.CallOption) (Events_ChatClient, error)
}

type eventsClient struct {
    cc *grpc.ClientConn
}

func NewEventsClient(cc *grpc.ClientConn) EventsClient {
    return &eventsClient{cc}
}

func (c *eventsClient) Chat(ctx context.Context, opts ...grpc.CallOption) (Events_ChatClient, error) {
    stream, err := grpc.NewClientStream(ctx, &_Events_serviceDesc.Streams[0], c.cc, "/protos.Events/Chat", opts...)
    if err != nil {
        return nil, err
    }
    x := &eventsChatClient{stream}
    return x, nil
}
//代码在protos/peer/events.pb.go
1.1.2、Events Service服务端
type EventsServer interface {
    Chat(Events_ChatServer) error
}

func RegisterEventsServer(s *grpc.Server, srv EventsServer) {
    s.RegisterService(&_Events_serviceDesc, srv)
}

func _Events_Chat_Handler(srv interface{}, stream grpc.ServerStream) error {
    return srv.(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、Admin Service(管理服务)

1.2.1、Admin Service客户端
type AdminClient interface {
    // Return the serve status.
    GetStatus(ctx context.Context, in *google_protobuf.Empty, opts ...grpc.CallOption) (*ServerStatus, error)
    StartServer(ctx context.Context, in *google_protobuf.Empty, opts ...grpc.CallOption) (*ServerStatus, error)
    GetModuleLogLevel(ctx context.Context, in *LogLevelRequest, opts ...grpc.CallOption) (*LogLevelResponse, error)
    SetModuleLogLevel(ctx context.Context, in *LogLevelRequest, opts ...grpc.CallOption) (*LogLevelResponse, error)
    RevertLogLevels(ctx context.Context, in *google_protobuf.Empty, opts ...grpc.CallOption) (*google_protobuf.Empty, error)
}

type adminClient struct {
    cc *grpc.ClientConn
}

func NewAdminClient(cc *grpc.ClientConn) AdminClient {
    return &adminClient{cc}
}

func (c *adminClient) GetStatus(ctx context.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...)
    if err != nil {
        return nil, err
    }
    return out, nil
}

func (c *adminClient) StartServer(ctx context.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...)
    if err != nil {
        return nil, err
    }
    return out, nil
}

func (c *adminClient) GetModuleLogLevel(ctx context.Context, in *LogLevelRequest, opts ...grpc.CallOption) (*LogLevelResponse, error) {
    out := new(LogLevelResponse)
    err := grpc.Invoke(ctx, "/protos.Admin/GetModuleLogLevel", in, out, c.cc, opts...)
    if err != nil {
        return nil, err
    }
    return out, nil
}

func (c *adminClient) SetModuleLogLevel(ctx context.Context, in *LogLevelRequest, opts ...grpc.CallOption) (*LogLevelResponse, error) {
    out := new(LogLevelResponse)
    err := grpc.Invoke(ctx, "/protos.Admin/SetModuleLogLevel", in, out, c.cc, opts...)
    if err != nil {
        return nil, err
    }
    return out, nil
}

func (c *adminClient) RevertLogLevels(ctx context.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...)
    if err != nil {
        return nil, err
    }
    return out, nil
}
//代码在protos/peer/admin.pb.go
1.2.2、Admin Service服务端
type AdminServer interface {
    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)
}

func RegisterAdminServer(s *grpc.Server, srv AdminServer) {
    s.RegisterService(&_Admin_serviceDesc, srv)
}

func _Admin_GetStatus_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
    in := new(google_protobuf.Empty)
    if err := dec(in); err != nil {
        return nil, err
    }
    if interceptor == nil {
        return srv.(AdminServer).GetStatus(ctx, in)
    }
    info := &grpc.UnaryServerInfo{
        Server:     srv,
        FullMethod: "/protos.Admin/GetStatus",
    }
    handler := func(ctx context.Context, req interface{}) (interface{}, error) {
        return srv.(AdminServer).GetStatus(ctx, req.(*google_protobuf.Empty))
    }
    return interceptor(ctx, in, info, handler)
}

func _Admin_StartServer_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
    in := new(google_protobuf.Empty)
    if err := dec(in); err != nil {
        return nil, err
    }
    if interceptor == nil {
        return srv.(AdminServer).StartServer(ctx, in)
    }
    info := &grpc.UnaryServerInfo{
        Server:     srv,
        FullMethod: "/protos.Admin/StartServer",
    }
    handler := func(ctx context.Context, req interface{}) (interface{}, error) {
        return srv.(AdminServer).StartServer(ctx, req.(*google_protobuf.Empty))
    }
    return interceptor(ctx, in, info, handler)
}

func _Admin_GetModuleLogLevel_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
    in := new(LogLevelRequest)
    if err := dec(in); err != nil {
        return nil, err
    }
    if interceptor == nil {
        return srv.(AdminServer).GetModuleLogLevel(ctx, in)
    }
    info := &grpc.UnaryServerInfo{
        Server:     srv,
        FullMethod: "/protos.Admin/GetModuleLogLevel",
    }
    handler := func(ctx context.Context, req interface{}) (interface{}, error) {
        return srv.(AdminServer).GetModuleLogLevel(ctx, req.(*LogLevelRequest))
    }
    return interceptor(ctx, in, info, handler)
}

func _Admin_SetModuleLogLevel_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
    in := new(LogLevelRequest)
    if err := dec(in); err != nil {
        return nil, err
    }
    if interceptor == nil {
        return srv.(AdminServer).SetModuleLogLevel(ctx, in)
    }
    info := &grpc.UnaryServerInfo{
        Server:     srv,
        FullMethod: "/protos.Admin/SetModuleLogLevel",
    }
    handler := func(ctx context.Context, req interface{}) (interface{}, error) {
        return srv.(AdminServer).SetModuleLogLevel(ctx, req.(*LogLevelRequest))
    }
    return interceptor(ctx, in, info, handler)
}

func _Admin_RevertLogLevels_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) {
    in := new(google_protobuf.Empty)
    if err := dec(in); err != nil {
        return nil, err
    }
    if interceptor == nil {
        return srv.(AdminServer).RevertLogLevels(ctx, in)
    }
    info := &grpc.UnaryServerInfo{
        Server:     srv,
        FullMethod: "/protos.Admin/RevertLogLevels",
    }
    handler := func(ctx context.Context, req interface{}) (interface{}, error) {
        return srv.(AdminServer).RevertLogLevels(ctx, req.(*google_protobuf.Empty))
    }
    return interceptor(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

未完待续感谢关注兄弟连区块链教程分享!