1. 程式人生 > >區塊鏈教程Fabric1.0原始碼gRPC(Fabric中註冊的gRPC Service)一

區塊鏈教程Fabric1.0原始碼gRPC(Fabric中註冊的gRPC Service)一

  區塊鏈教程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

未完待續感謝關注兄弟連區塊鏈教程分享!