add block header download api to grpc

pull/1530/head
Dennis Won 5 years ago
parent 5a4badadf7
commit 518f98ee5e
  1. 19
      api/service/syncing/downloader/client.go
  2. 3
      api/service/syncing/downloader/gen.sh
  3. 74
      api/service/syncing/downloader/proto/downloader.pb.go
  4. 3
      api/service/syncing/downloader/proto/downloader.proto
  5. 17
      node/node_syncing.go

@ -44,7 +44,7 @@ func (client *Client) Close() {
func (client *Client) GetBlockHashes(startHash []byte, size uint32, ip, port string) *pb.DownloaderResponse {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
request := &pb.DownloaderRequest{Type: pb.DownloaderRequest_HEADER, BlockHash: startHash, Size: size}
request := &pb.DownloaderRequest{Type: pb.DownloaderRequest_BLOCKHASH, BlockHash: startHash, Size: size}
request.Ip = ip
request.Port = port
response, err := client.dlClient.Query(ctx, request)
@ -54,6 +54,23 @@ func (client *Client) GetBlockHashes(startHash []byte, size uint32, ip, port str
return response
}
// GetBlockHeaders gets block headers in serialization byte array by calling a grpc request.
func (client *Client) GetBlockHeaders(hashes [][]byte) *pb.DownloaderResponse {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
request := &pb.DownloaderRequest{Type: pb.DownloaderRequest_BLOCKHEADER}
request.Hashes = make([][]byte, len(hashes))
for i := range hashes {
request.Hashes[i] = make([]byte, len(hashes[i]))
copy(request.Hashes[i], hashes[i])
}
response, err := client.dlClient.Query(ctx, request)
if err != nil {
utils.Logger().Error().Err(err).Str("target", client.conn.Target()).Msg("[SYNC] downloader/client.go:GetBlockHeaders query failed")
}
return response
}
// GetBlocks gets blocks in serialization byte array by calling a grpc request.
func (client *Client) GetBlocks(hashes [][]byte) *pb.DownloaderResponse {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)

@ -1 +1,2 @@
protoc -I proto/ proto/downloader.proto --go_out=plugins=grpc:proto
SRC_DIR=$(dirname $0)
protoc -I ${SRC_DIR}/proto/ ${SRC_DIR}/proto/downloader.proto --go_out=plugins=grpc:${SRC_DIR}/proto

@ -8,6 +8,8 @@ import (
fmt "fmt"
proto "github.com/golang/protobuf/proto"
grpc "google.golang.org/grpc"
codes "google.golang.org/grpc/codes"
status "google.golang.org/grpc/status"
math "math"
)
@ -25,33 +27,36 @@ const _ = proto.ProtoPackageIsVersion3 // please upgrade the proto package
type DownloaderRequest_RequestType int32
const (
DownloaderRequest_HEADER DownloaderRequest_RequestType = 0
DownloaderRequest_BLOCKHASH DownloaderRequest_RequestType = 0
DownloaderRequest_BLOCK DownloaderRequest_RequestType = 1
DownloaderRequest_NEWBLOCK DownloaderRequest_RequestType = 2
DownloaderRequest_BLOCKHEIGHT DownloaderRequest_RequestType = 3
DownloaderRequest_REGISTER DownloaderRequest_RequestType = 4
DownloaderRequest_REGISTERTIMEOUT DownloaderRequest_RequestType = 5
DownloaderRequest_UNKNOWN DownloaderRequest_RequestType = 6
DownloaderRequest_BLOCKHEADER DownloaderRequest_RequestType = 7
)
var DownloaderRequest_RequestType_name = map[int32]string{
0: "HEADER",
0: "BLOCKHASH",
1: "BLOCK",
2: "NEWBLOCK",
3: "BLOCKHEIGHT",
4: "REGISTER",
5: "REGISTERTIMEOUT",
6: "UNKNOWN",
7: "BLOCKHEADER",
}
var DownloaderRequest_RequestType_value = map[string]int32{
"HEADER": 0,
"BLOCKHASH": 0,
"BLOCK": 1,
"NEWBLOCK": 2,
"BLOCKHEIGHT": 3,
"REGISTER": 4,
"REGISTERTIMEOUT": 5,
"UNKNOWN": 6,
"BLOCKHEADER": 7,
}
func (x DownloaderRequest_RequestType) String() string {
@ -135,7 +140,7 @@ func (m *DownloaderRequest) GetType() DownloaderRequest_RequestType {
if m != nil {
return m.Type
}
return DownloaderRequest_HEADER
return DownloaderRequest_BLOCKHASH
}
func (m *DownloaderRequest) GetHashes() [][]byte {
@ -248,32 +253,33 @@ func init() {
func init() { proto.RegisterFile("downloader.proto", fileDescriptor_6a99ec95c7ab1ff1) }
var fileDescriptor_6a99ec95c7ab1ff1 = []byte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 0x60, 0xf2, 0xe9, 0x5e,
0xd6, 0x2d, 0xbe, 0xda, 0x7b, 0x1d, 0x47, 0xaf, 0xf7, 0x6f, 0xe3, 0x8d, 0xae, 0x1c, 0x73, 0x95,
0x27, 0x7f, 0x02, 0x00, 0x00, 0xff, 0xff, 0x68, 0xc1, 0xb5, 0x25, 0xa9, 0x02, 0x00, 0x00,
// 410 bytes of a gzipped FileDescriptorProto
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0x7c, 0x52, 0xcd, 0x6e, 0xd3, 0x40,
0x10, 0xce, 0x3a, 0x8e, 0x93, 0x4c, 0xd2, 0x76, 0x19, 0x10, 0x5a, 0x55, 0x80, 0x2c, 0x9f, 0xcc,
0xc5, 0x87, 0xf6, 0xc4, 0x81, 0x43, 0x70, 0x4d, 0x6c, 0xb5, 0x38, 0x62, 0xed, 0x50, 0x71, 0x74,
0xe9, 0xaa, 0xb6, 0xa8, 0xea, 0xc5, 0xeb, 0x0a, 0x99, 0x17, 0xe0, 0x05, 0x79, 0x06, 0x9e, 0x03,
0x79, 0x9d, 0xd6, 0x96, 0x80, 0x9c, 0x76, 0xbf, 0x6f, 0x7e, 0x34, 0xf3, 0x7d, 0x03, 0xf4, 0xba,
0xfc, 0x7e, 0x77, 0x5b, 0x66, 0xd7, 0xa2, 0xf2, 0x64, 0x55, 0xd6, 0x25, 0x42, 0xcf, 0x38, 0xbf,
0x0d, 0x78, 0x72, 0xf6, 0x08, 0xb9, 0xf8, 0x76, 0x2f, 0x54, 0x8d, 0x6f, 0xc1, 0xac, 0x1b, 0x29,
0x18, 0xb1, 0x89, 0x7b, 0x78, 0xf2, 0xda, 0x1b, 0xb4, 0xf8, 0x2b, 0xd9, 0xdb, 0xbd, 0x69, 0x23,
0x05, 0xd7, 0x65, 0xf8, 0x1c, 0xac, 0x3c, 0x53, 0xb9, 0x50, 0xcc, 0xb0, 0xc7, 0xee, 0x92, 0xef,
0x10, 0x1e, 0x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, 0xb5, 0xf0, 0x26,
0x1f, 0x52, 0xce, 0x1b, 0x78, 0xf6, 0xaf, 0xfa, 0x76, 0xa3, 0x64, 0xeb, 0xfb, 0x41, 0x92, 0xd0,
0x11, 0xce, 0xc0, 0x7c, 0xbf, 0x8a, 0x2e, 0x28, 0x41, 0x00, 0x2b, 0x8a, 0x93, 0xcf, 0xb1, 0x4f,
0x8d, 0x93, 0x4f, 0x00, 0xfd, 0x34, 0x18, 0xc2, 0xe4, 0xe3, 0xbd, 0xa8, 0x1a, 0x7c, 0xb9, 0xf7,
0x64, 0x8e, 0x5f, 0xed, 0xdf, 0xc6, 0x19, 0x5d, 0x59, 0xfa, 0x54, 0x4f, 0xff, 0x04, 0x00, 0x00,
0xff, 0xff, 0x00, 0xaf, 0x33, 0x33, 0xbe, 0x02, 0x00, 0x00,
}
// Reference imports to suppress errors if they are not otherwise used.
@ -313,6 +319,14 @@ type DownloaderServer interface {
Query(context.Context, *DownloaderRequest) (*DownloaderResponse, error)
}
// UnimplementedDownloaderServer can be embedded to have forward compatible implementations.
type UnimplementedDownloaderServer struct {
}
func (*UnimplementedDownloaderServer) Query(ctx context.Context, req *DownloaderRequest) (*DownloaderResponse, error) {
return nil, status.Errorf(codes.Unimplemented, "method Query not implemented")
}
func RegisterDownloaderServer(s *grpc.Server, srv DownloaderServer) {
s.RegisterService(&_Downloader_serviceDesc, srv)
}

@ -10,13 +10,14 @@ service Downloader {
// DownloaderRequest is the generic download request.
message DownloaderRequest {
enum RequestType {
HEADER = 0;
BLOCKHASH = 0;
BLOCK = 1;
NEWBLOCK = 2;
BLOCKHEIGHT = 3;
REGISTER = 4;
REGISTERTIMEOUT = 5;
UNKNOWN = 6;
BLOCKHEADER = 7;
}
// Request type.

@ -310,7 +310,7 @@ func (node *Node) SendNewBlockToUnsync() {
func (node *Node) CalculateResponse(request *downloader_pb.DownloaderRequest, incomingPeer string) (*downloader_pb.DownloaderResponse, error) {
response := &downloader_pb.DownloaderResponse{}
switch request.Type {
case downloader_pb.DownloaderRequest_HEADER:
case downloader_pb.DownloaderRequest_BLOCKHASH:
if request.BlockHash == nil {
return response, fmt.Errorf("[SYNC] GetBlockHashes Request BlockHash is NIL")
}
@ -347,6 +347,21 @@ func (node *Node) CalculateResponse(request *downloader_pb.DownloaderRequest, in
response.Payload = append(response.Payload, blockHash[:])
}
case downloader_pb.DownloaderRequest_BLOCKHEADER:
for _, bytes := range request.Hashes {
var hash common.Hash
hash.SetBytes(bytes)
blockHeader := node.Blockchain().GetHeaderByHash(hash)
if blockHeader == nil {
continue
}
encodedBlockHeader, err := rlp.EncodeToBytes(blockHeader)
if err == nil {
response.Payload = append(response.Payload, encodedBlockHeader)
}
}
case downloader_pb.DownloaderRequest_BLOCK:
for _, bytes := range request.Hashes {
var hash common.Hash

Loading…
Cancel
Save