From 7a42ed2d85885b0a0c3c9f3494e4c1e0c358ad3b Mon Sep 17 00:00:00 2001 From: 0xEclair <38656355+0xEclair@users.noreply.github.com> Date: Tue, 16 Jul 2024 20:13:45 +0000 Subject: [PATCH 1/3] feat: block by hash --- proto/tendermint/rpc/grpc/types.pb.go | 296 +++++++++++++++++++++++--- proto/tendermint/rpc/grpc/types.proto | 9 + rpc/grpc/block.go | 21 ++ rpc/grpc/client_server.go | 1 + rpc/grpc/types.pb.go | 296 +++++++++++++++++++++++--- 5 files changed, 575 insertions(+), 48 deletions(-) create mode 100644 rpc/grpc/block.go diff --git a/proto/tendermint/rpc/grpc/types.pb.go b/proto/tendermint/rpc/grpc/types.pb.go index d61c39e9e1..3aef069b58 100644 --- a/proto/tendermint/rpc/grpc/types.pb.go +++ b/proto/tendermint/rpc/grpc/types.pb.go @@ -7,6 +7,7 @@ import ( context "context" fmt "fmt" types "github.com/cometbft/cometbft/abci/types" + types1 "github.com/cometbft/cometbft/proto/tendermint/types" proto "github.com/gogo/protobuf/proto" grpc "google.golang.org/grpc" codes "google.golang.org/grpc/codes" @@ -107,6 +108,50 @@ func (m *RequestBroadcastTx) GetTx() []byte { return nil } +type RequestBlockByHash struct { + Hash []byte `protobuf:"bytes,1,opt,name=hash,proto3" json:"hash,omitempty"` +} + +func (m *RequestBlockByHash) Reset() { *m = RequestBlockByHash{} } +func (m *RequestBlockByHash) String() string { return proto.CompactTextString(m) } +func (*RequestBlockByHash) ProtoMessage() {} +func (*RequestBlockByHash) Descriptor() ([]byte, []int) { + return fileDescriptor_0ffff5682c662b95, []int{2} +} +func (m *RequestBlockByHash) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *RequestBlockByHash) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_RequestBlockByHash.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *RequestBlockByHash) XXX_Merge(src proto.Message) { + xxx_messageInfo_RequestBlockByHash.Merge(m, src) +} +func (m *RequestBlockByHash) XXX_Size() int { + return m.Size() +} +func (m *RequestBlockByHash) XXX_DiscardUnknown() { + xxx_messageInfo_RequestBlockByHash.DiscardUnknown(m) +} + +var xxx_messageInfo_RequestBlockByHash proto.InternalMessageInfo + +func (m *RequestBlockByHash) GetHash() []byte { + if m != nil { + return m.Hash + } + return nil +} + type ResponsePing struct { } @@ -114,7 +159,7 @@ func (m *ResponsePing) Reset() { *m = ResponsePing{} } func (m *ResponsePing) String() string { return proto.CompactTextString(m) } func (*ResponsePing) ProtoMessage() {} func (*ResponsePing) Descriptor() ([]byte, []int) { - return fileDescriptor_0ffff5682c662b95, []int{2} + return fileDescriptor_0ffff5682c662b95, []int{3} } func (m *ResponsePing) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -152,7 +197,7 @@ func (m *ResponseBroadcastTx) Reset() { *m = ResponseBroadcastTx{} } func (m *ResponseBroadcastTx) String() string { return proto.CompactTextString(m) } func (*ResponseBroadcastTx) ProtoMessage() {} func (*ResponseBroadcastTx) Descriptor() ([]byte, []int) { - return fileDescriptor_0ffff5682c662b95, []int{3} + return fileDescriptor_0ffff5682c662b95, []int{4} } func (m *ResponseBroadcastTx) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -198,6 +243,7 @@ func (m *ResponseBroadcastTx) GetDeliverTx() *types.ResponseDeliverTx { func init() { proto.RegisterType((*RequestPing)(nil), "tendermint.rpc.grpc.RequestPing") proto.RegisterType((*RequestBroadcastTx)(nil), "tendermint.rpc.grpc.RequestBroadcastTx") + proto.RegisterType((*RequestBlockByHash)(nil), "tendermint.rpc.grpc.RequestBlockByHash") proto.RegisterType((*ResponsePing)(nil), "tendermint.rpc.grpc.ResponsePing") proto.RegisterType((*ResponseBroadcastTx)(nil), "tendermint.rpc.grpc.ResponseBroadcastTx") } @@ -205,28 +251,31 @@ func init() { func init() { proto.RegisterFile("tendermint/rpc/grpc/types.proto", fileDescriptor_0ffff5682c662b95) } var fileDescriptor_0ffff5682c662b95 = []byte{ - // 321 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x92, 0x3f, 0x4f, 0xc2, 0x40, - 0x18, 0xc6, 0x39, 0x62, 0xfc, 0xf3, 0x82, 0x0c, 0xc7, 0x62, 0x30, 0x39, 0xb1, 0x31, 0x91, 0xe9, - 0x48, 0x70, 0x64, 0x02, 0x1d, 0x34, 0x2e, 0xa4, 0x61, 0x72, 0x51, 0x7a, 0x7d, 0x85, 0x46, 0xe9, - 0xd5, 0xeb, 0x61, 0xea, 0xb7, 0x70, 0xf1, 0xe3, 0xb8, 0x3b, 0x32, 0x3a, 0x9a, 0xf6, 0x8b, 0x98, - 0x2b, 0xd4, 0x9e, 0x03, 0x2c, 0xcd, 0xd3, 0xe6, 0xf7, 0xbb, 0x3c, 0xef, 0xdb, 0x83, 0x13, 0x8d, - 0xa1, 0x8f, 0x6a, 0x1e, 0x84, 0xba, 0xab, 0x22, 0xd1, 0x9d, 0x9a, 0x87, 0x7e, 0x8b, 0x30, 0xe6, - 0x91, 0x92, 0x5a, 0xd2, 0x66, 0x09, 0x70, 0x15, 0x09, 0x6e, 0x80, 0xd6, 0xb1, 0x65, 0x4d, 0x3c, - 0x11, 0xd8, 0x86, 0x73, 0x08, 0x35, 0x17, 0x5f, 0x16, 0x18, 0xeb, 0x51, 0x10, 0x4e, 0x9d, 0x33, - 0xa0, 0xeb, 0xd7, 0xa1, 0x92, 0x13, 0x5f, 0x4c, 0x62, 0x3d, 0x4e, 0x68, 0x03, 0xaa, 0x3a, 0x39, - 0x22, 0x6d, 0xd2, 0xa9, 0xbb, 0x55, 0x9d, 0x38, 0x0d, 0xa8, 0xbb, 0x18, 0x47, 0x32, 0x8c, 0x31, - 0xb7, 0x3e, 0x08, 0x34, 0x8b, 0x0f, 0xb6, 0xd7, 0x87, 0x7d, 0x31, 0x43, 0xf1, 0x74, 0xbf, 0xb6, - 0x6b, 0xbd, 0x36, 0xb7, 0x1a, 0x9a, 0x32, 0xbc, 0xf0, 0x2e, 0x0d, 0x38, 0x4e, 0xdc, 0x3d, 0xb1, - 0x0a, 0x74, 0x00, 0xe0, 0xe3, 0x73, 0xf0, 0x8a, 0xca, 0xe8, 0xd5, 0x5c, 0x77, 0x36, 0xea, 0x57, - 0x2b, 0x74, 0x9c, 0xb8, 0x07, 0x7e, 0x11, 0x7b, 0x9f, 0x04, 0xea, 0x7f, 0x7d, 0x06, 0xa3, 0x1b, - 0x7a, 0x0b, 0x3b, 0xa6, 0x30, 0xfd, 0x57, 0xa3, 0x58, 0x14, 0xb7, 0x16, 0xd1, 0x3a, 0xdd, 0x40, - 0x94, 0x53, 0xd3, 0x07, 0xa8, 0xd9, 0xc3, 0x9e, 0x6f, 0x3b, 0xd3, 0x02, 0x5b, 0x9d, 0xad, 0x47, - 0x5b, 0xe4, 0xf0, 0xfa, 0x2b, 0x65, 0x64, 0x99, 0x32, 0xf2, 0x93, 0x32, 0xf2, 0x9e, 0xb1, 0xca, - 0x32, 0x63, 0x95, 0xef, 0x8c, 0x55, 0xee, 0xf8, 0x34, 0xd0, 0xb3, 0x85, 0xc7, 0x85, 0x9c, 0x77, - 0x85, 0x9c, 0xa3, 0xf6, 0x1e, 0x75, 0x19, 0x8a, 0xbb, 0xd1, 0x17, 0x52, 0xa1, 0x09, 0xde, 0x6e, - 0xfe, 0xb7, 0x2f, 0x7e, 0x03, 0x00, 0x00, 0xff, 0xff, 0x87, 0x53, 0x29, 0xbe, 0x42, 0x02, 0x00, - 0x00, + // 379 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x92, 0xbf, 0x4f, 0xf2, 0x40, + 0x18, 0xc7, 0x29, 0x21, 0xef, 0xcb, 0xfb, 0x94, 0x97, 0xe1, 0x18, 0x34, 0xd5, 0x54, 0x6c, 0x4c, + 0x64, 0xba, 0x26, 0x38, 0x32, 0x81, 0x0e, 0x18, 0x07, 0x49, 0xc3, 0xa4, 0x83, 0xb6, 0xd7, 0x93, + 0x36, 0x40, 0xaf, 0xf6, 0x0e, 0x53, 0xfe, 0x0b, 0x17, 0xff, 0x1c, 0x77, 0x47, 0x46, 0x47, 0x03, + 0xff, 0x88, 0xb9, 0x96, 0xda, 0x73, 0x80, 0xa5, 0xf9, 0xf6, 0x9e, 0xcf, 0xf7, 0xf9, 0x71, 0xf7, + 0xc0, 0x89, 0xa0, 0x91, 0x4f, 0x93, 0x79, 0x18, 0x09, 0x3b, 0x89, 0x89, 0x3d, 0x91, 0x1f, 0xb1, + 0x8c, 0x29, 0xc7, 0x71, 0xc2, 0x04, 0x43, 0xad, 0x12, 0xc0, 0x49, 0x4c, 0xb0, 0x04, 0x8c, 0x23, + 0xc5, 0xe5, 0x7a, 0x24, 0x54, 0x1d, 0xc6, 0xb1, 0x12, 0xcc, 0xce, 0x6d, 0x6f, 0xc6, 0xc8, 0x34, + 0x8f, 0x5a, 0xff, 0x41, 0x77, 0xe8, 0xf3, 0x82, 0x72, 0x31, 0x0a, 0xa3, 0x89, 0x75, 0x06, 0x68, + 0xfb, 0x3b, 0x48, 0x98, 0xeb, 0x13, 0x97, 0x8b, 0x71, 0x8a, 0x9a, 0x50, 0x15, 0xe9, 0xa1, 0xd6, + 0xd6, 0x3a, 0x0d, 0xa7, 0x2a, 0x52, 0xab, 0x53, 0x52, 0x32, 0xd5, 0x60, 0x39, 0x74, 0x79, 0x80, + 0x10, 0xd4, 0x02, 0x97, 0x07, 0x5b, 0x2e, 0xd3, 0x56, 0x13, 0x1a, 0x0e, 0xe5, 0x31, 0x8b, 0x38, + 0xcd, 0xf2, 0xbf, 0x69, 0xd0, 0x2a, 0x0e, 0xd4, 0x0a, 0x3d, 0xa8, 0x93, 0x80, 0x92, 0xe9, 0xc3, + 0xb6, 0x8e, 0xde, 0x6d, 0x63, 0x65, 0x52, 0x39, 0x14, 0x2e, 0x7c, 0x97, 0x12, 0x1c, 0xa7, 0xce, + 0x5f, 0x92, 0x0b, 0xd4, 0x07, 0xf0, 0xe9, 0x2c, 0x7c, 0xa1, 0x89, 0xb4, 0x57, 0x33, 0xbb, 0xb5, + 0xd3, 0x7e, 0x95, 0xa3, 0xe3, 0xd4, 0xf9, 0xe7, 0x17, 0xb2, 0xfb, 0xae, 0x41, 0xe3, 0xa7, 0x9f, + 0xfe, 0xe8, 0x1a, 0xdd, 0x40, 0x4d, 0x36, 0x8c, 0x7e, 0xb5, 0x51, 0x5c, 0x38, 0x56, 0xae, 0xcc, + 0x38, 0xdd, 0x41, 0x94, 0x53, 0xa3, 0x47, 0xd0, 0xd5, 0x61, 0xcf, 0xf7, 0xe5, 0x54, 0x40, 0xa3, + 0xb3, 0x37, 0xb5, 0x42, 0x76, 0xef, 0xa1, 0x9e, 0x3d, 0x85, 0x6c, 0xfd, 0x16, 0x74, 0xf5, 0x59, + 0xf6, 0x57, 0x2b, 0x41, 0xe3, 0x40, 0x05, 0xf3, 0x0d, 0xca, 0xc3, 0xc3, 0x8f, 0xb5, 0xa9, 0xad, + 0xd6, 0xa6, 0xf6, 0xb5, 0x36, 0xb5, 0xd7, 0x8d, 0x59, 0x59, 0x6d, 0xcc, 0xca, 0xe7, 0xc6, 0xac, + 0xdc, 0xe1, 0x49, 0x28, 0x82, 0x85, 0x87, 0x09, 0x9b, 0xdb, 0x84, 0xcd, 0xa9, 0xf0, 0x9e, 0x44, + 0x29, 0x8a, 0x05, 0xee, 0x11, 0x96, 0x50, 0x29, 0xbc, 0x3f, 0xd9, 0xd2, 0x5d, 0x7c, 0x07, 0x00, + 0x00, 0xff, 0xff, 0x9d, 0x82, 0xbc, 0xcd, 0xe7, 0x02, 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. @@ -345,6 +394,78 @@ var _BroadcastAPI_serviceDesc = grpc.ServiceDesc{ Metadata: "tendermint/rpc/grpc/types.proto", } +// BlockAPIClient is the client API for BlockAPI service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. +type BlockAPIClient interface { + BlockByHash(ctx context.Context, in *RequestBlockByHash, opts ...grpc.CallOption) (*types1.Block, error) +} + +type blockAPIClient struct { + cc *grpc.ClientConn +} + +func NewBlockAPIClient(cc *grpc.ClientConn) BlockAPIClient { + return &blockAPIClient{cc} +} + +func (c *blockAPIClient) BlockByHash(ctx context.Context, in *RequestBlockByHash, opts ...grpc.CallOption) (*types1.Block, error) { + out := new(types1.Block) + err := c.cc.Invoke(ctx, "/tendermint.rpc.grpc.BlockAPI/BlockByHash", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// BlockAPIServer is the server API for BlockAPI service. +type BlockAPIServer interface { + BlockByHash(context.Context, *RequestBlockByHash) (*types1.Block, error) +} + +// UnimplementedBlockAPIServer can be embedded to have forward compatible implementations. +type UnimplementedBlockAPIServer struct { +} + +func (*UnimplementedBlockAPIServer) BlockByHash(ctx context.Context, req *RequestBlockByHash) (*types1.Block, error) { + return nil, status.Errorf(codes.Unimplemented, "method BlockByHash not implemented") +} + +func RegisterBlockAPIServer(s *grpc.Server, srv BlockAPIServer) { + s.RegisterService(&_BlockAPI_serviceDesc, srv) +} + +func _BlockAPI_BlockByHash_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(RequestBlockByHash) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(BlockAPIServer).BlockByHash(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/tendermint.rpc.grpc.BlockAPI/BlockByHash", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(BlockAPIServer).BlockByHash(ctx, req.(*RequestBlockByHash)) + } + return interceptor(ctx, in, info, handler) +} + +var _BlockAPI_serviceDesc = grpc.ServiceDesc{ + ServiceName: "tendermint.rpc.grpc.BlockAPI", + HandlerType: (*BlockAPIServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "BlockByHash", + Handler: _BlockAPI_BlockByHash_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "tendermint/rpc/grpc/types.proto", +} + func (m *RequestPing) Marshal() (dAtA []byte, err error) { size := m.Size() dAtA = make([]byte, size) @@ -398,6 +519,36 @@ func (m *RequestBroadcastTx) MarshalToSizedBuffer(dAtA []byte) (int, error) { return len(dAtA) - i, nil } +func (m *RequestBlockByHash) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *RequestBlockByHash) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *RequestBlockByHash) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if len(m.Hash) > 0 { + i -= len(m.Hash) + copy(dAtA[i:], m.Hash) + i = encodeVarintTypes(dAtA, i, uint64(len(m.Hash))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + func (m *ResponsePing) Marshal() (dAtA []byte, err error) { size := m.Size() dAtA = make([]byte, size) @@ -501,6 +652,19 @@ func (m *RequestBroadcastTx) Size() (n int) { return n } +func (m *RequestBlockByHash) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.Hash) + if l > 0 { + n += 1 + l + sovTypes(uint64(l)) + } + return n +} + func (m *ResponsePing) Size() (n int) { if m == nil { return 0 @@ -667,6 +831,90 @@ func (m *RequestBroadcastTx) Unmarshal(dAtA []byte) error { } return nil } +func (m *RequestBlockByHash) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: RequestBlockByHash: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: RequestBlockByHash: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Hash", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + byteLen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return ErrInvalidLengthTypes + } + postIndex := iNdEx + byteLen + if postIndex < 0 { + return ErrInvalidLengthTypes + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Hash = append(m.Hash[:0], dAtA[iNdEx:postIndex]...) + if m.Hash == nil { + m.Hash = []byte{} + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipTypes(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthTypes + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} func (m *ResponsePing) Unmarshal(dAtA []byte) error { l := len(dAtA) iNdEx := 0 diff --git a/proto/tendermint/rpc/grpc/types.proto b/proto/tendermint/rpc/grpc/types.proto index ad79f04871..8611c038df 100644 --- a/proto/tendermint/rpc/grpc/types.proto +++ b/proto/tendermint/rpc/grpc/types.proto @@ -3,6 +3,7 @@ package tendermint.rpc.grpc; option go_package = "github.com/cometbft/cometbft/rpc/grpc;coregrpc"; import "tendermint/abci/types.proto"; +import "tendermint/types/block.proto"; //---------------------------------------- // Request types @@ -13,6 +14,10 @@ message RequestBroadcastTx { bytes tx = 1; } +message RequestBlockByHash { + bytes hash = 1; +} + //---------------------------------------- // Response types @@ -30,3 +35,7 @@ service BroadcastAPI { rpc Ping(RequestPing) returns (ResponsePing); rpc BroadcastTx(RequestBroadcastTx) returns (ResponseBroadcastTx); } + +service BlockAPI { + rpc BlockByHash(RequestBlockByHash) returns (tendermint.types.Block); +} diff --git a/rpc/grpc/block.go b/rpc/grpc/block.go new file mode 100644 index 0000000000..69afee3381 --- /dev/null +++ b/rpc/grpc/block.go @@ -0,0 +1,21 @@ +package coregrpc + +import ( + "context" + + cmtproto "github.com/cometbft/cometbft/proto/tendermint/types" + core "github.com/cometbft/cometbft/rpc/core" + rpctypes "github.com/cometbft/cometbft/rpc/jsonrpc/types" +) + +type blockAPI struct { +} + +func (b *blockAPI) BlockByHash(ctx context.Context, req *RequestBlockByHash) (*cmtproto.Block, error) { + res, err := core.BlockByHash(&rpctypes.Context{}, req.Hash) + if err != nil { + return nil, err + } + + return res.Block.ToProto() +} diff --git a/rpc/grpc/client_server.go b/rpc/grpc/client_server.go index 55a32bed7c..3f9eef52f8 100644 --- a/rpc/grpc/client_server.go +++ b/rpc/grpc/client_server.go @@ -21,6 +21,7 @@ type Config struct { func StartGRPCServer(ln net.Listener) error { grpcServer := grpc.NewServer() RegisterBroadcastAPIServer(grpcServer, &broadcastAPI{}) + RegisterBlockAPIServer(grpcServer, &blockAPI{}) return grpcServer.Serve(ln) } diff --git a/rpc/grpc/types.pb.go b/rpc/grpc/types.pb.go index d61c39e9e1..3aef069b58 100644 --- a/rpc/grpc/types.pb.go +++ b/rpc/grpc/types.pb.go @@ -7,6 +7,7 @@ import ( context "context" fmt "fmt" types "github.com/cometbft/cometbft/abci/types" + types1 "github.com/cometbft/cometbft/proto/tendermint/types" proto "github.com/gogo/protobuf/proto" grpc "google.golang.org/grpc" codes "google.golang.org/grpc/codes" @@ -107,6 +108,50 @@ func (m *RequestBroadcastTx) GetTx() []byte { return nil } +type RequestBlockByHash struct { + Hash []byte `protobuf:"bytes,1,opt,name=hash,proto3" json:"hash,omitempty"` +} + +func (m *RequestBlockByHash) Reset() { *m = RequestBlockByHash{} } +func (m *RequestBlockByHash) String() string { return proto.CompactTextString(m) } +func (*RequestBlockByHash) ProtoMessage() {} +func (*RequestBlockByHash) Descriptor() ([]byte, []int) { + return fileDescriptor_0ffff5682c662b95, []int{2} +} +func (m *RequestBlockByHash) XXX_Unmarshal(b []byte) error { + return m.Unmarshal(b) +} +func (m *RequestBlockByHash) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { + if deterministic { + return xxx_messageInfo_RequestBlockByHash.Marshal(b, m, deterministic) + } else { + b = b[:cap(b)] + n, err := m.MarshalToSizedBuffer(b) + if err != nil { + return nil, err + } + return b[:n], nil + } +} +func (m *RequestBlockByHash) XXX_Merge(src proto.Message) { + xxx_messageInfo_RequestBlockByHash.Merge(m, src) +} +func (m *RequestBlockByHash) XXX_Size() int { + return m.Size() +} +func (m *RequestBlockByHash) XXX_DiscardUnknown() { + xxx_messageInfo_RequestBlockByHash.DiscardUnknown(m) +} + +var xxx_messageInfo_RequestBlockByHash proto.InternalMessageInfo + +func (m *RequestBlockByHash) GetHash() []byte { + if m != nil { + return m.Hash + } + return nil +} + type ResponsePing struct { } @@ -114,7 +159,7 @@ func (m *ResponsePing) Reset() { *m = ResponsePing{} } func (m *ResponsePing) String() string { return proto.CompactTextString(m) } func (*ResponsePing) ProtoMessage() {} func (*ResponsePing) Descriptor() ([]byte, []int) { - return fileDescriptor_0ffff5682c662b95, []int{2} + return fileDescriptor_0ffff5682c662b95, []int{3} } func (m *ResponsePing) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -152,7 +197,7 @@ func (m *ResponseBroadcastTx) Reset() { *m = ResponseBroadcastTx{} } func (m *ResponseBroadcastTx) String() string { return proto.CompactTextString(m) } func (*ResponseBroadcastTx) ProtoMessage() {} func (*ResponseBroadcastTx) Descriptor() ([]byte, []int) { - return fileDescriptor_0ffff5682c662b95, []int{3} + return fileDescriptor_0ffff5682c662b95, []int{4} } func (m *ResponseBroadcastTx) XXX_Unmarshal(b []byte) error { return m.Unmarshal(b) @@ -198,6 +243,7 @@ func (m *ResponseBroadcastTx) GetDeliverTx() *types.ResponseDeliverTx { func init() { proto.RegisterType((*RequestPing)(nil), "tendermint.rpc.grpc.RequestPing") proto.RegisterType((*RequestBroadcastTx)(nil), "tendermint.rpc.grpc.RequestBroadcastTx") + proto.RegisterType((*RequestBlockByHash)(nil), "tendermint.rpc.grpc.RequestBlockByHash") proto.RegisterType((*ResponsePing)(nil), "tendermint.rpc.grpc.ResponsePing") proto.RegisterType((*ResponseBroadcastTx)(nil), "tendermint.rpc.grpc.ResponseBroadcastTx") } @@ -205,28 +251,31 @@ func init() { func init() { proto.RegisterFile("tendermint/rpc/grpc/types.proto", fileDescriptor_0ffff5682c662b95) } var fileDescriptor_0ffff5682c662b95 = []byte{ - // 321 bytes of a gzipped FileDescriptorProto - 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x92, 0x3f, 0x4f, 0xc2, 0x40, - 0x18, 0xc6, 0x39, 0x62, 0xfc, 0xf3, 0x82, 0x0c, 0xc7, 0x62, 0x30, 0x39, 0xb1, 0x31, 0x91, 0xe9, - 0x48, 0x70, 0x64, 0x02, 0x1d, 0x34, 0x2e, 0xa4, 0x61, 0x72, 0x51, 0x7a, 0x7d, 0x85, 0x46, 0xe9, - 0xd5, 0xeb, 0x61, 0xea, 0xb7, 0x70, 0xf1, 0xe3, 0xb8, 0x3b, 0x32, 0x3a, 0x9a, 0xf6, 0x8b, 0x98, - 0x2b, 0xd4, 0x9e, 0x03, 0x2c, 0xcd, 0xd3, 0xe6, 0xf7, 0xbb, 0x3c, 0xef, 0xdb, 0x83, 0x13, 0x8d, - 0xa1,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x64, 0x99, 0x32, 0xf2, 0x93, 0x32, 0xf2, 0x9e, 0xb1, 0xca, - 0x32, 0x63, 0x95, 0xef, 0x8c, 0x55, 0xee, 0xf8, 0x34, 0xd0, 0xb3, 0x85, 0xc7, 0x85, 0x9c, 0x77, - 0x85, 0x9c, 0xa3, 0xf6, 0x1e, 0x75, 0x19, 0x8a, 0xbb, 0xd1, 0x17, 0x52, 0xa1, 0x09, 0xde, 0x6e, - 0xfe, 0xb7, 0x2f, 0x7e, 0x03, 0x00, 0x00, 0xff, 0xff, 0x87, 0x53, 0x29, 0xbe, 0x42, 0x02, 0x00, - 0x00, + // 379 bytes of a gzipped FileDescriptorProto + 0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x92, 0xbf, 0x4f, 0xf2, 0x40, + 0x18, 0xc7, 0x29, 0x21, 0xef, 0xcb, 0xfb, 0x94, 0x97, 0xe1, 0x18, 0x34, 0xd5, 0x54, 0x6c, 0x4c, + 0x64, 0xba, 0x26, 0x38, 0x32, 0x81, 0x0e, 0x18, 0x07, 0x49, 0xc3, 0xa4, 0x83, 0xb6, 0xd7, 0x93, + 0x36, 0x40, 0xaf, 0xf6, 0x0e, 0x53, 0xfe, 0x0b, 0x17, 0xff, 0x1c, 0x77, 0x47, 0x46, 0x47, 0x03, + 0xff, 0x88, 0xb9, 0x96, 0xda, 0x73, 0x80, 0xa5, 0xf9, 0xf6, 0x9e, 0xcf, 0xf7, 0xf9, 0x71, 0xf7, + 0xc0, 0x89, 0xa0, 0x91, 0x4f, 0x93, 0x79, 0x18, 0x09, 0x3b, 0x89, 0x89, 0x3d, 0x91, 0x1f, 0xb1, + 0x8c, 0x29, 0xc7, 0x71, 0xc2, 0x04, 0x43, 0xad, 0x12, 0xc0, 0x49, 0x4c, 0xb0, 0x04, 0x8c, 0x23, + 0xc5, 0xe5, 0x7a, 0x24, 0x54, 0x1d, 0xc6, 0xb1, 0x12, 0xcc, 0xce, 0x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57, 0x2b, 0x41, 0xe3, 0x40, 0x05, 0xf3, 0x0d, 0xca, 0xc3, 0xc3, 0x8f, 0xb5, 0xa9, 0xad, + 0xd6, 0xa6, 0xf6, 0xb5, 0x36, 0xb5, 0xd7, 0x8d, 0x59, 0x59, 0x6d, 0xcc, 0xca, 0xe7, 0xc6, 0xac, + 0xdc, 0xe1, 0x49, 0x28, 0x82, 0x85, 0x87, 0x09, 0x9b, 0xdb, 0x84, 0xcd, 0xa9, 0xf0, 0x9e, 0x44, + 0x29, 0x8a, 0x05, 0xee, 0x11, 0x96, 0x50, 0x29, 0xbc, 0x3f, 0xd9, 0xd2, 0x5d, 0x7c, 0x07, 0x00, + 0x00, 0xff, 0xff, 0x9d, 0x82, 0xbc, 0xcd, 0xe7, 0x02, 0x00, 0x00, } // Reference imports to suppress errors if they are not otherwise used. @@ -345,6 +394,78 @@ var _BroadcastAPI_serviceDesc = grpc.ServiceDesc{ Metadata: "tendermint/rpc/grpc/types.proto", } +// BlockAPIClient is the client API for BlockAPI service. +// +// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream. +type BlockAPIClient interface { + BlockByHash(ctx context.Context, in *RequestBlockByHash, opts ...grpc.CallOption) (*types1.Block, error) +} + +type blockAPIClient struct { + cc *grpc.ClientConn +} + +func NewBlockAPIClient(cc *grpc.ClientConn) BlockAPIClient { + return &blockAPIClient{cc} +} + +func (c *blockAPIClient) BlockByHash(ctx context.Context, in *RequestBlockByHash, opts ...grpc.CallOption) (*types1.Block, error) { + out := new(types1.Block) + err := c.cc.Invoke(ctx, "/tendermint.rpc.grpc.BlockAPI/BlockByHash", in, out, opts...) + if err != nil { + return nil, err + } + return out, nil +} + +// BlockAPIServer is the server API for BlockAPI service. +type BlockAPIServer interface { + BlockByHash(context.Context, *RequestBlockByHash) (*types1.Block, error) +} + +// UnimplementedBlockAPIServer can be embedded to have forward compatible implementations. +type UnimplementedBlockAPIServer struct { +} + +func (*UnimplementedBlockAPIServer) BlockByHash(ctx context.Context, req *RequestBlockByHash) (*types1.Block, error) { + return nil, status.Errorf(codes.Unimplemented, "method BlockByHash not implemented") +} + +func RegisterBlockAPIServer(s *grpc.Server, srv BlockAPIServer) { + s.RegisterService(&_BlockAPI_serviceDesc, srv) +} + +func _BlockAPI_BlockByHash_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { + in := new(RequestBlockByHash) + if err := dec(in); err != nil { + return nil, err + } + if interceptor == nil { + return srv.(BlockAPIServer).BlockByHash(ctx, in) + } + info := &grpc.UnaryServerInfo{ + Server: srv, + FullMethod: "/tendermint.rpc.grpc.BlockAPI/BlockByHash", + } + handler := func(ctx context.Context, req interface{}) (interface{}, error) { + return srv.(BlockAPIServer).BlockByHash(ctx, req.(*RequestBlockByHash)) + } + return interceptor(ctx, in, info, handler) +} + +var _BlockAPI_serviceDesc = grpc.ServiceDesc{ + ServiceName: "tendermint.rpc.grpc.BlockAPI", + HandlerType: (*BlockAPIServer)(nil), + Methods: []grpc.MethodDesc{ + { + MethodName: "BlockByHash", + Handler: _BlockAPI_BlockByHash_Handler, + }, + }, + Streams: []grpc.StreamDesc{}, + Metadata: "tendermint/rpc/grpc/types.proto", +} + func (m *RequestPing) Marshal() (dAtA []byte, err error) { size := m.Size() dAtA = make([]byte, size) @@ -398,6 +519,36 @@ func (m *RequestBroadcastTx) MarshalToSizedBuffer(dAtA []byte) (int, error) { return len(dAtA) - i, nil } +func (m *RequestBlockByHash) Marshal() (dAtA []byte, err error) { + size := m.Size() + dAtA = make([]byte, size) + n, err := m.MarshalToSizedBuffer(dAtA[:size]) + if err != nil { + return nil, err + } + return dAtA[:n], nil +} + +func (m *RequestBlockByHash) MarshalTo(dAtA []byte) (int, error) { + size := m.Size() + return m.MarshalToSizedBuffer(dAtA[:size]) +} + +func (m *RequestBlockByHash) MarshalToSizedBuffer(dAtA []byte) (int, error) { + i := len(dAtA) + _ = i + var l int + _ = l + if len(m.Hash) > 0 { + i -= len(m.Hash) + copy(dAtA[i:], m.Hash) + i = encodeVarintTypes(dAtA, i, uint64(len(m.Hash))) + i-- + dAtA[i] = 0xa + } + return len(dAtA) - i, nil +} + func (m *ResponsePing) Marshal() (dAtA []byte, err error) { size := m.Size() dAtA = make([]byte, size) @@ -501,6 +652,19 @@ func (m *RequestBroadcastTx) Size() (n int) { return n } +func (m *RequestBlockByHash) Size() (n int) { + if m == nil { + return 0 + } + var l int + _ = l + l = len(m.Hash) + if l > 0 { + n += 1 + l + sovTypes(uint64(l)) + } + return n +} + func (m *ResponsePing) Size() (n int) { if m == nil { return 0 @@ -667,6 +831,90 @@ func (m *RequestBroadcastTx) Unmarshal(dAtA []byte) error { } return nil } +func (m *RequestBlockByHash) Unmarshal(dAtA []byte) error { + l := len(dAtA) + iNdEx := 0 + for iNdEx < l { + preIndex := iNdEx + var wire uint64 + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + wire |= uint64(b&0x7F) << shift + if b < 0x80 { + break + } + } + fieldNum := int32(wire >> 3) + wireType := int(wire & 0x7) + if wireType == 4 { + return fmt.Errorf("proto: RequestBlockByHash: wiretype end group for non-group") + } + if fieldNum <= 0 { + return fmt.Errorf("proto: RequestBlockByHash: illegal tag %d (wire type %d)", fieldNum, wire) + } + switch fieldNum { + case 1: + if wireType != 2 { + return fmt.Errorf("proto: wrong wireType = %d for field Hash", wireType) + } + var byteLen int + for shift := uint(0); ; shift += 7 { + if shift >= 64 { + return ErrIntOverflowTypes + } + if iNdEx >= l { + return io.ErrUnexpectedEOF + } + b := dAtA[iNdEx] + iNdEx++ + byteLen |= int(b&0x7F) << shift + if b < 0x80 { + break + } + } + if byteLen < 0 { + return ErrInvalidLengthTypes + } + postIndex := iNdEx + byteLen + if postIndex < 0 { + return ErrInvalidLengthTypes + } + if postIndex > l { + return io.ErrUnexpectedEOF + } + m.Hash = append(m.Hash[:0], dAtA[iNdEx:postIndex]...) + if m.Hash == nil { + m.Hash = []byte{} + } + iNdEx = postIndex + default: + iNdEx = preIndex + skippy, err := skipTypes(dAtA[iNdEx:]) + if err != nil { + return err + } + if (skippy < 0) || (iNdEx+skippy) < 0 { + return ErrInvalidLengthTypes + } + if (iNdEx + skippy) > l { + return io.ErrUnexpectedEOF + } + iNdEx += skippy + } + } + + if iNdEx > l { + return io.ErrUnexpectedEOF + } + return nil +} func (m *ResponsePing) Unmarshal(dAtA []byte) error { l := len(dAtA) iNdEx := 0 From 8f778eb53bb19edc49abffca21b74b9cc514f0be Mon Sep 17 00:00:00 2001 From: 0xEclair <38656355+0xEclair@users.noreply.github.com> Date: Wed, 17 Jul 2024 05:05:15 +0800 Subject: [PATCH 2/3] add new block client --- rpc/grpc/client_server.go | 18 ++++++++++++++++-- rpc/test/helpers.go | 2 +- 2 files changed, 17 insertions(+), 3 deletions(-) diff --git a/rpc/grpc/client_server.go b/rpc/grpc/client_server.go index 3f9eef52f8..5101b0cbb2 100644 --- a/rpc/grpc/client_server.go +++ b/rpc/grpc/client_server.go @@ -25,14 +25,28 @@ func StartGRPCServer(ln net.Listener) error { return grpcServer.Serve(ln) } +type Client interface { + BroadcastAPIClient + BlockAPIClient +} + +type client struct { + BroadcastAPIClient + BlockAPIClient +} + // StartGRPCClient dials the gRPC server using protoAddr and returns a new // BroadcastAPIClient. -func StartGRPCClient(protoAddr string) BroadcastAPIClient { +func StartGRPCClient(protoAddr string) Client { conn, err := grpc.Dial(protoAddr, grpc.WithTransportCredentials(insecure.NewCredentials()), grpc.WithContextDialer(dialerFunc)) if err != nil { panic(err) } - return NewBroadcastAPIClient(conn) + + return &client{ + BroadcastAPIClient: NewBroadcastAPIClient(conn), + BlockAPIClient: NewBlockAPIClient(conn), + } } func dialerFunc(ctx context.Context, addr string) (net.Conn, error) { diff --git a/rpc/test/helpers.go b/rpc/test/helpers.go index 22dcb9bfa1..042ea8e1d9 100644 --- a/rpc/test/helpers.go +++ b/rpc/test/helpers.go @@ -112,7 +112,7 @@ func GetConfig(forceCreate ...bool) *cfg.Config { return globalConfig } -func GetGRPCClient() core_grpc.BroadcastAPIClient { +func GetGRPCClient() core_grpc.Client { grpcAddr := globalConfig.RPC.GRPCListenAddress return core_grpc.StartGRPCClient(grpcAddr) } From 59cf93f6819400db247e8b45ee518ee81c662c2c Mon Sep 17 00:00:00 2001 From: 0xEclair <38656355+0xEclair@users.noreply.github.com> Date: Wed, 17 Jul 2024 05:13:07 +0800 Subject: [PATCH 3/3] add test case --- rpc/grpc/grpc_test.go | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/rpc/grpc/grpc_test.go b/rpc/grpc/grpc_test.go index ba53cf2327..b57c7610f3 100644 --- a/rpc/grpc/grpc_test.go +++ b/rpc/grpc/grpc_test.go @@ -33,3 +33,12 @@ func TestBroadcastTx(t *testing.T) { require.EqualValues(t, 0, res.CheckTx.Code) require.EqualValues(t, 0, res.DeliverTx.Code) } + +func TestBlockByHash(t *testing.T) { + res, err := rpctest.GetGRPCClient().BlockByHash( + context.Background(), + &core_grpc.RequestBlockByHash{Hash: []byte("this is a block hash")}, + ) + require.Error(t, err) + require.Nil(t, res) +}