commit
af14350fa1
@ -0,0 +1,76 @@ |
|||||||
|
package downloader |
||||||
|
|
||||||
|
import ( |
||||||
|
"context" |
||||||
|
"fmt" |
||||||
|
"log" |
||||||
|
"time" |
||||||
|
|
||||||
|
pb "github.com/harmony-one/harmony/syncing/downloader/proto" |
||||||
|
"google.golang.org/grpc" |
||||||
|
) |
||||||
|
|
||||||
|
// PrintResult ...
|
||||||
|
func PrintResult(client *Client) { |
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
||||||
|
defer cancel() |
||||||
|
request := &pb.DownloaderRequest{Type: pb.DownloaderRequest_HEADER} |
||||||
|
response, err := client.dlClient.Query(ctx, request) |
||||||
|
if err != nil { |
||||||
|
log.Fatalf("Error") |
||||||
|
} |
||||||
|
log.Println(response) |
||||||
|
} |
||||||
|
|
||||||
|
// Client ...
|
||||||
|
type Client struct { |
||||||
|
dlClient pb.DownloaderClient |
||||||
|
opts []grpc.DialOption |
||||||
|
conn *grpc.ClientConn |
||||||
|
} |
||||||
|
|
||||||
|
// ClientSetup ...
|
||||||
|
func ClientSetup(ip, port string) *Client { |
||||||
|
client := Client{} |
||||||
|
client.opts = append(client.opts, grpc.WithInsecure()) |
||||||
|
var err error |
||||||
|
client.conn, err = grpc.Dial(fmt.Sprintf("%s:%s", ip, port), client.opts...) |
||||||
|
if err != nil { |
||||||
|
log.Fatalf("fail to dial: %v", err) |
||||||
|
return nil |
||||||
|
} |
||||||
|
|
||||||
|
client.dlClient = pb.NewDownloaderClient(client.conn) |
||||||
|
return &client |
||||||
|
} |
||||||
|
|
||||||
|
// Close ...
|
||||||
|
func (client *Client) Close() { |
||||||
|
client.conn.Close() |
||||||
|
} |
||||||
|
|
||||||
|
// GetBlockHashes ...
|
||||||
|
func (client *Client) GetBlockHashes() *pb.DownloaderResponse { |
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
||||||
|
defer cancel() |
||||||
|
request := &pb.DownloaderRequest{Type: pb.DownloaderRequest_HEADER} |
||||||
|
response, err := client.dlClient.Query(ctx, request) |
||||||
|
if err != nil { |
||||||
|
log.Fatalf("Error") |
||||||
|
} |
||||||
|
return response |
||||||
|
} |
||||||
|
|
||||||
|
// GetBlocks ...
|
||||||
|
func (client *Client) GetBlocks(heights []int32) *pb.DownloaderResponse { |
||||||
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) |
||||||
|
defer cancel() |
||||||
|
request := &pb.DownloaderRequest{Type: pb.DownloaderRequest_BLOCK} |
||||||
|
request.Height = make([]int32, len(heights)) |
||||||
|
copy(request.Height, heights) |
||||||
|
response, err := client.dlClient.Query(ctx, request) |
||||||
|
if err != nil { |
||||||
|
log.Fatalf("Error") |
||||||
|
} |
||||||
|
return response |
||||||
|
} |
@ -0,0 +1,8 @@ |
|||||||
|
package downloader |
||||||
|
|
||||||
|
import "errors" |
||||||
|
|
||||||
|
// Errors ...
|
||||||
|
var ( |
||||||
|
ErrDownloaderWithNoNode = errors.New("no node attached") |
||||||
|
) |
@ -0,0 +1,12 @@ |
|||||||
|
package downloader |
||||||
|
|
||||||
|
import ( |
||||||
|
pb "github.com/harmony-one/harmony/syncing/downloader/proto" |
||||||
|
) |
||||||
|
|
||||||
|
// DownloadInterface ...
|
||||||
|
type DownloadInterface interface { |
||||||
|
// Syncing blockchain from other peers.
|
||||||
|
// The returned channel is the signal of syncing finish.
|
||||||
|
CalculateResponse(request *pb.DownloaderRequest) (*pb.DownloaderResponse, error) |
||||||
|
} |
@ -0,0 +1,240 @@ |
|||||||
|
// Code generated by protoc-gen-go. DO NOT EDIT.
|
||||||
|
// source: downloader.proto
|
||||||
|
|
||||||
|
package downloader |
||||||
|
|
||||||
|
import ( |
||||||
|
context "context" |
||||||
|
fmt "fmt" |
||||||
|
proto "github.com/golang/protobuf/proto" |
||||||
|
grpc "google.golang.org/grpc" |
||||||
|
math "math" |
||||||
|
) |
||||||
|
|
||||||
|
// Reference imports to suppress errors if they are not otherwise used.
|
||||||
|
var _ = proto.Marshal |
||||||
|
var _ = fmt.Errorf |
||||||
|
var _ = math.Inf |
||||||
|
|
||||||
|
// This is a compile-time assertion to ensure that this generated file
|
||||||
|
// is compatible with the proto package it is being compiled against.
|
||||||
|
// A compilation error at this line likely means your copy of the
|
||||||
|
// proto package needs to be updated.
|
||||||
|
const _ = proto.ProtoPackageIsVersion2 // please upgrade the proto package
|
||||||
|
|
||||||
|
type DownloaderRequest_RequestType int32 |
||||||
|
|
||||||
|
const ( |
||||||
|
DownloaderRequest_HEADER DownloaderRequest_RequestType = 0 |
||||||
|
DownloaderRequest_BLOCK DownloaderRequest_RequestType = 1 |
||||||
|
DownloaderRequest_UNKOWN DownloaderRequest_RequestType = 2 |
||||||
|
) |
||||||
|
|
||||||
|
var DownloaderRequest_RequestType_name = map[int32]string{ |
||||||
|
0: "HEADER", |
||||||
|
1: "BLOCK", |
||||||
|
2: "UNKOWN", |
||||||
|
} |
||||||
|
|
||||||
|
var DownloaderRequest_RequestType_value = map[string]int32{ |
||||||
|
"HEADER": 0, |
||||||
|
"BLOCK": 1, |
||||||
|
"UNKOWN": 2, |
||||||
|
} |
||||||
|
|
||||||
|
func (x DownloaderRequest_RequestType) String() string { |
||||||
|
return proto.EnumName(DownloaderRequest_RequestType_name, int32(x)) |
||||||
|
} |
||||||
|
|
||||||
|
func (DownloaderRequest_RequestType) EnumDescriptor() ([]byte, []int) { |
||||||
|
return fileDescriptor_6a99ec95c7ab1ff1, []int{0, 0} |
||||||
|
} |
||||||
|
|
||||||
|
// DownloaderRequest is the generic download request.
|
||||||
|
type DownloaderRequest struct { |
||||||
|
// Request type.
|
||||||
|
Type DownloaderRequest_RequestType `protobuf:"varint,1,opt,name=type,proto3,enum=downloader.DownloaderRequest_RequestType" json:"type,omitempty"` |
||||||
|
// The array of ids or heights of the blocks we want to download.
|
||||||
|
Height []int32 `protobuf:"varint,2,rep,packed,name=height,proto3" json:"height,omitempty"` |
||||||
|
XXX_NoUnkeyedLiteral struct{} `json:"-"` |
||||||
|
XXX_unrecognized []byte `json:"-"` |
||||||
|
XXX_sizecache int32 `json:"-"` |
||||||
|
} |
||||||
|
|
||||||
|
func (m *DownloaderRequest) Reset() { *m = DownloaderRequest{} } |
||||||
|
func (m *DownloaderRequest) String() string { return proto.CompactTextString(m) } |
||||||
|
func (*DownloaderRequest) ProtoMessage() {} |
||||||
|
func (*DownloaderRequest) Descriptor() ([]byte, []int) { |
||||||
|
return fileDescriptor_6a99ec95c7ab1ff1, []int{0} |
||||||
|
} |
||||||
|
|
||||||
|
func (m *DownloaderRequest) XXX_Unmarshal(b []byte) error { |
||||||
|
return xxx_messageInfo_DownloaderRequest.Unmarshal(m, b) |
||||||
|
} |
||||||
|
func (m *DownloaderRequest) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { |
||||||
|
return xxx_messageInfo_DownloaderRequest.Marshal(b, m, deterministic) |
||||||
|
} |
||||||
|
func (m *DownloaderRequest) XXX_Merge(src proto.Message) { |
||||||
|
xxx_messageInfo_DownloaderRequest.Merge(m, src) |
||||||
|
} |
||||||
|
func (m *DownloaderRequest) XXX_Size() int { |
||||||
|
return xxx_messageInfo_DownloaderRequest.Size(m) |
||||||
|
} |
||||||
|
func (m *DownloaderRequest) XXX_DiscardUnknown() { |
||||||
|
xxx_messageInfo_DownloaderRequest.DiscardUnknown(m) |
||||||
|
} |
||||||
|
|
||||||
|
var xxx_messageInfo_DownloaderRequest proto.InternalMessageInfo |
||||||
|
|
||||||
|
func (m *DownloaderRequest) GetType() DownloaderRequest_RequestType { |
||||||
|
if m != nil { |
||||||
|
return m.Type |
||||||
|
} |
||||||
|
return DownloaderRequest_HEADER |
||||||
|
} |
||||||
|
|
||||||
|
func (m *DownloaderRequest) GetHeight() []int32 { |
||||||
|
if m != nil { |
||||||
|
return m.Height |
||||||
|
} |
||||||
|
return nil |
||||||
|
} |
||||||
|
|
||||||
|
// DownloaderResponse is the generic response of DownloaderRequest.
|
||||||
|
type DownloaderResponse struct { |
||||||
|
// payload of Block.
|
||||||
|
Payload [][]byte `protobuf:"bytes,1,rep,name=payload,proto3" json:"payload,omitempty"` |
||||||
|
XXX_NoUnkeyedLiteral struct{} `json:"-"` |
||||||
|
XXX_unrecognized []byte `json:"-"` |
||||||
|
XXX_sizecache int32 `json:"-"` |
||||||
|
} |
||||||
|
|
||||||
|
func (m *DownloaderResponse) Reset() { *m = DownloaderResponse{} } |
||||||
|
func (m *DownloaderResponse) String() string { return proto.CompactTextString(m) } |
||||||
|
func (*DownloaderResponse) ProtoMessage() {} |
||||||
|
func (*DownloaderResponse) Descriptor() ([]byte, []int) { |
||||||
|
return fileDescriptor_6a99ec95c7ab1ff1, []int{1} |
||||||
|
} |
||||||
|
|
||||||
|
func (m *DownloaderResponse) XXX_Unmarshal(b []byte) error { |
||||||
|
return xxx_messageInfo_DownloaderResponse.Unmarshal(m, b) |
||||||
|
} |
||||||
|
func (m *DownloaderResponse) XXX_Marshal(b []byte, deterministic bool) ([]byte, error) { |
||||||
|
return xxx_messageInfo_DownloaderResponse.Marshal(b, m, deterministic) |
||||||
|
} |
||||||
|
func (m *DownloaderResponse) XXX_Merge(src proto.Message) { |
||||||
|
xxx_messageInfo_DownloaderResponse.Merge(m, src) |
||||||
|
} |
||||||
|
func (m *DownloaderResponse) XXX_Size() int { |
||||||
|
return xxx_messageInfo_DownloaderResponse.Size(m) |
||||||
|
} |
||||||
|
func (m *DownloaderResponse) XXX_DiscardUnknown() { |
||||||
|
xxx_messageInfo_DownloaderResponse.DiscardUnknown(m) |
||||||
|
} |
||||||
|
|
||||||
|
var xxx_messageInfo_DownloaderResponse proto.InternalMessageInfo |
||||||
|
|
||||||
|
func (m *DownloaderResponse) GetPayload() [][]byte { |
||||||
|
if m != nil { |
||||||
|
return m.Payload |
||||||
|
} |
||||||
|
return nil |
||||||
|
} |
||||||
|
|
||||||
|
func init() { |
||||||
|
proto.RegisterEnum("downloader.DownloaderRequest_RequestType", DownloaderRequest_RequestType_name, DownloaderRequest_RequestType_value) |
||||||
|
proto.RegisterType((*DownloaderRequest)(nil), "downloader.DownloaderRequest") |
||||||
|
proto.RegisterType((*DownloaderResponse)(nil), "downloader.DownloaderResponse") |
||||||
|
} |
||||||
|
|
||||||
|
func init() { proto.RegisterFile("downloader.proto", fileDescriptor_6a99ec95c7ab1ff1) } |
||||||
|
|
||||||
|
var fileDescriptor_6a99ec95c7ab1ff1 = []byte{ |
||||||
|
// 213 bytes of a gzipped FileDescriptorProto
|
||||||
|
0x1f, 0x8b, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0xff, 0xe2, 0x12, 0x48, 0xc9, 0x2f, 0xcf, |
||||||
|
0xcb, 0xc9, 0x4f, 0x4c, 0x49, 0x2d, 0xd2, 0x2b, 0x28, 0xca, 0x2f, 0xc9, 0x17, 0xe2, 0x42, 0x88, |
||||||
|
0x28, 0xcd, 0x61, 0xe4, 0x12, 0x74, 0x81, 0x73, 0x83, 0x52, 0x0b, 0x4b, 0x53, 0x8b, 0x4b, 0x84, |
||||||
|
0x6c, 0xb9, 0x58, 0x4a, 0x2a, 0x0b, 0x52, 0x25, 0x18, 0x15, 0x18, 0x35, 0xf8, 0x8c, 0x34, 0xf5, |
||||||
|
0x90, 0x8c, 0xc0, 0x50, 0xac, 0x07, 0xa5, 0x43, 0x2a, 0x0b, 0x52, 0x83, 0xc0, 0xda, 0x84, 0xc4, |
||||||
|
0xb8, 0xd8, 0x32, 0x52, 0x33, 0xd3, 0x33, 0x4a, 0x24, 0x98, 0x14, 0x98, 0x35, 0x58, 0x83, 0xa0, |
||||||
|
0x3c, 0x25, 0x03, 0x2e, 0x6e, 0x24, 0xc5, 0x42, 0x5c, 0x5c, 0x6c, 0x1e, 0xae, 0x8e, 0x2e, 0xae, |
||||||
|
0x41, 0x02, 0x0c, 0x42, 0x9c, 0x5c, 0xac, 0x4e, 0x3e, 0xfe, 0xce, 0xde, 0x02, 0x8c, 0x20, 0xe1, |
||||||
|
0x50, 0x3f, 0x6f, 0xff, 0x70, 0x3f, 0x01, 0x26, 0x25, 0x3d, 0x2e, 0x21, 0x64, 0x0b, 0x8b, 0x0b, |
||||||
|
0xf2, 0xf3, 0x8a, 0x53, 0x85, 0x24, 0xb8, 0xd8, 0x0b, 0x12, 0x2b, 0x41, 0x82, 0x12, 0x8c, 0x0a, |
||||||
|
0xcc, 0x1a, 0x3c, 0x41, 0x30, 0xae, 0x51, 0x18, 0x17, 0x17, 0x42, 0xbd, 0x90, 0x07, 0x17, 0x6b, |
||||||
|
0x60, 0x69, 0x6a, 0x51, 0xa5, 0x90, 0x2c, 0x5e, 0x1f, 0x48, 0xc9, 0xe1, 0x92, 0x86, 0xd8, 0xa7, |
||||||
|
0xc4, 0x90, 0xc4, 0x06, 0x0e, 0x39, 0x63, 0x40, 0x00, 0x00, 0x00, 0xff, 0xff, 0xdd, 0x6d, 0x18, |
||||||
|
0x54, 0x4d, 0x01, 0x00, 0x00, |
||||||
|
} |
||||||
|
|
||||||
|
// Reference imports to suppress errors if they are not otherwise used.
|
||||||
|
var _ context.Context |
||||||
|
var _ grpc.ClientConn |
||||||
|
|
||||||
|
// This is a compile-time assertion to ensure that this generated file
|
||||||
|
// is compatible with the grpc package it is being compiled against.
|
||||||
|
const _ = grpc.SupportPackageIsVersion4 |
||||||
|
|
||||||
|
// DownloaderClient is the client API for Downloader service.
|
||||||
|
//
|
||||||
|
// For semantics around ctx use and closing/ending streaming RPCs, please refer to https://godoc.org/google.golang.org/grpc#ClientConn.NewStream.
|
||||||
|
type DownloaderClient interface { |
||||||
|
Query(ctx context.Context, in *DownloaderRequest, opts ...grpc.CallOption) (*DownloaderResponse, error) |
||||||
|
} |
||||||
|
|
||||||
|
type downloaderClient struct { |
||||||
|
cc *grpc.ClientConn |
||||||
|
} |
||||||
|
|
||||||
|
func NewDownloaderClient(cc *grpc.ClientConn) DownloaderClient { |
||||||
|
return &downloaderClient{cc} |
||||||
|
} |
||||||
|
|
||||||
|
func (c *downloaderClient) Query(ctx context.Context, in *DownloaderRequest, opts ...grpc.CallOption) (*DownloaderResponse, error) { |
||||||
|
out := new(DownloaderResponse) |
||||||
|
err := c.cc.Invoke(ctx, "/downloader.Downloader/Query", in, out, opts...) |
||||||
|
if err != nil { |
||||||
|
return nil, err |
||||||
|
} |
||||||
|
return out, nil |
||||||
|
} |
||||||
|
|
||||||
|
// DownloaderServer is the server API for Downloader service.
|
||||||
|
type DownloaderServer interface { |
||||||
|
Query(context.Context, *DownloaderRequest) (*DownloaderResponse, error) |
||||||
|
} |
||||||
|
|
||||||
|
func RegisterDownloaderServer(s *grpc.Server, srv DownloaderServer) { |
||||||
|
s.RegisterService(&_Downloader_serviceDesc, srv) |
||||||
|
} |
||||||
|
|
||||||
|
func _Downloader_Query_Handler(srv interface{}, ctx context.Context, dec func(interface{}) error, interceptor grpc.UnaryServerInterceptor) (interface{}, error) { |
||||||
|
in := new(DownloaderRequest) |
||||||
|
if err := dec(in); err != nil { |
||||||
|
return nil, err |
||||||
|
} |
||||||
|
if interceptor == nil { |
||||||
|
return srv.(DownloaderServer).Query(ctx, in) |
||||||
|
} |
||||||
|
info := &grpc.UnaryServerInfo{ |
||||||
|
Server: srv, |
||||||
|
FullMethod: "/downloader.Downloader/Query", |
||||||
|
} |
||||||
|
handler := func(ctx context.Context, req interface{}) (interface{}, error) { |
||||||
|
return srv.(DownloaderServer).Query(ctx, req.(*DownloaderRequest)) |
||||||
|
} |
||||||
|
return interceptor(ctx, in, info, handler) |
||||||
|
} |
||||||
|
|
||||||
|
var _Downloader_serviceDesc = grpc.ServiceDesc{ |
||||||
|
ServiceName: "downloader.Downloader", |
||||||
|
HandlerType: (*DownloaderServer)(nil), |
||||||
|
Methods: []grpc.MethodDesc{ |
||||||
|
{ |
||||||
|
MethodName: "Query", |
||||||
|
Handler: _Downloader_Query_Handler, |
||||||
|
}, |
||||||
|
}, |
||||||
|
Streams: []grpc.StreamDesc{}, |
||||||
|
Metadata: "downloader.proto", |
||||||
|
} |
@ -0,0 +1,29 @@ |
|||||||
|
syntax = "proto3"; |
||||||
|
|
||||||
|
package downloader; |
||||||
|
|
||||||
|
// Downloader is the service used for downloading/sycning blocks. |
||||||
|
service Downloader { |
||||||
|
rpc Query(DownloaderRequest) returns (DownloaderResponse) {} |
||||||
|
} |
||||||
|
|
||||||
|
// DownloaderRequest is the generic download request. |
||||||
|
message DownloaderRequest { |
||||||
|
enum RequestType { |
||||||
|
HEADER = 0; |
||||||
|
BLOCK = 1; |
||||||
|
UNKOWN = 2; |
||||||
|
} |
||||||
|
|
||||||
|
// Request type. |
||||||
|
RequestType type = 1; |
||||||
|
|
||||||
|
// The array of ids or heights of the blocks we want to download. |
||||||
|
repeated int32 height = 2; |
||||||
|
} |
||||||
|
|
||||||
|
// DownloaderResponse is the generic response of DownloaderRequest. |
||||||
|
message DownloaderResponse { |
||||||
|
// payload of Block. |
||||||
|
repeated bytes payload = 1; |
||||||
|
} |
@ -0,0 +1,47 @@ |
|||||||
|
package downloader |
||||||
|
|
||||||
|
import ( |
||||||
|
"context" |
||||||
|
"fmt" |
||||||
|
"log" |
||||||
|
"net" |
||||||
|
|
||||||
|
"google.golang.org/grpc" |
||||||
|
|
||||||
|
pb "github.com/harmony-one/harmony/syncing/downloader/proto" |
||||||
|
) |
||||||
|
|
||||||
|
// Server ...
|
||||||
|
type Server struct { |
||||||
|
downloadInterface DownloadInterface |
||||||
|
} |
||||||
|
|
||||||
|
// Query returns the feature at the given point.
|
||||||
|
func (s *Server) Query(ctx context.Context, request *pb.DownloaderRequest) (*pb.DownloaderResponse, error) { |
||||||
|
response, err := s.downloadInterface.CalculateResponse(request) |
||||||
|
if err != nil { |
||||||
|
return nil, err |
||||||
|
} |
||||||
|
// response := pb.DownloaderResponse{}
|
||||||
|
// response.Payload = [][]byte{{0, 0, 2}}
|
||||||
|
return response, nil |
||||||
|
} |
||||||
|
|
||||||
|
// Start ...
|
||||||
|
func (s *Server) Start(ip, port string) (*grpc.Server, error) { |
||||||
|
lis, err := net.Listen("tcp", fmt.Sprintf("%s:%s", ip, port)) |
||||||
|
if err != nil { |
||||||
|
log.Fatalf("failed to listen: %v", err) |
||||||
|
} |
||||||
|
var opts []grpc.ServerOption |
||||||
|
grpcServer := grpc.NewServer(opts...) |
||||||
|
pb.RegisterDownloaderServer(grpcServer, s) |
||||||
|
go grpcServer.Serve(lis) |
||||||
|
return grpcServer, nil |
||||||
|
} |
||||||
|
|
||||||
|
// NewServer ...
|
||||||
|
func NewServer(dlInterface DownloadInterface) *Server { |
||||||
|
s := &Server{downloadInterface: dlInterface} |
||||||
|
return s |
||||||
|
} |
@ -0,0 +1,109 @@ |
|||||||
|
package downloader_test |
||||||
|
|
||||||
|
import ( |
||||||
|
"fmt" |
||||||
|
"reflect" |
||||||
|
"testing" |
||||||
|
|
||||||
|
bc "github.com/harmony-one/harmony/blockchain" |
||||||
|
"github.com/harmony-one/harmony/crypto/pki" |
||||||
|
"github.com/harmony-one/harmony/syncing/downloader" |
||||||
|
pb "github.com/harmony-one/harmony/syncing/downloader/proto" |
||||||
|
) |
||||||
|
|
||||||
|
const ( |
||||||
|
serverPort = "9997" |
||||||
|
serverIP = "127.0.0.1" |
||||||
|
clientPort = "9999" |
||||||
|
) |
||||||
|
|
||||||
|
var ( |
||||||
|
PriIntOne = 111 |
||||||
|
PriIntTwo = 222 |
||||||
|
TestAddressOne = pki.GetAddressFromInt(PriIntOne) |
||||||
|
TestAddressTwo = pki.GetAddressFromInt(PriIntTwo) |
||||||
|
ShardID = uint32(0) |
||||||
|
) |
||||||
|
|
||||||
|
type FakeNode struct { |
||||||
|
bc *bc.Blockchain |
||||||
|
} |
||||||
|
|
||||||
|
// GetBlockHashes used for state download.
|
||||||
|
func (node *FakeNode) GetBlockHashes() [][]byte { |
||||||
|
res := [][]byte{} |
||||||
|
for _, block := range node.bc.Blocks { |
||||||
|
res = append(res, block.Hash[:]) |
||||||
|
} |
||||||
|
return res |
||||||
|
} |
||||||
|
|
||||||
|
// GetBlocks used for state download.
|
||||||
|
func (node *FakeNode) GetBlocks() [][]byte { |
||||||
|
res := [][]byte{} |
||||||
|
for _, block := range node.bc.Blocks { |
||||||
|
res = append(res, block.Serialize()) |
||||||
|
} |
||||||
|
return res |
||||||
|
} |
||||||
|
|
||||||
|
// SetBlockchain is used for testing
|
||||||
|
func (node *FakeNode) Init() { |
||||||
|
addresses := [][20]byte{TestAddressOne, TestAddressTwo} |
||||||
|
node.bc = bc.CreateBlockchainWithMoreBlocks(addresses, ShardID) |
||||||
|
} |
||||||
|
|
||||||
|
func (node *FakeNode) CalculateResponse(request *pb.DownloaderRequest) (*pb.DownloaderResponse, error) { |
||||||
|
response := &pb.DownloaderResponse{} |
||||||
|
if request.Type == pb.DownloaderRequest_HEADER { |
||||||
|
for _, block := range node.bc.Blocks { |
||||||
|
response.Payload = append(response.Payload, block.Hash[:]) |
||||||
|
} |
||||||
|
} else { |
||||||
|
for _, id := range request.Height { |
||||||
|
response.Payload = append(response.Payload, node.bc.Blocks[id].Serialize()) |
||||||
|
} |
||||||
|
} |
||||||
|
return response, nil |
||||||
|
} |
||||||
|
|
||||||
|
func TestGetBlockHashes(t *testing.T) { |
||||||
|
fakeNode := &FakeNode{} |
||||||
|
fakeNode.Init() |
||||||
|
s := downloader.NewServer(fakeNode) |
||||||
|
grcpServer, err := s.Start(serverIP, serverPort) |
||||||
|
if err != nil { |
||||||
|
t.Error(err) |
||||||
|
} |
||||||
|
defer grcpServer.Stop() |
||||||
|
|
||||||
|
client := downloader.ClientSetup(serverIP, serverPort) |
||||||
|
defer client.Close() |
||||||
|
response := client.GetBlockHashes() |
||||||
|
if !reflect.DeepEqual(response.Payload, fakeNode.GetBlockHashes()) { |
||||||
|
t.Error("not equal") |
||||||
|
} |
||||||
|
} |
||||||
|
|
||||||
|
func TestGetBlocks(t *testing.T) { |
||||||
|
fakeNode := &FakeNode{} |
||||||
|
fakeNode.Init() |
||||||
|
s := downloader.NewServer(fakeNode) |
||||||
|
grcpServer, err := s.Start(serverIP, serverPort) |
||||||
|
if err != nil { |
||||||
|
t.Error(err) |
||||||
|
} |
||||||
|
defer grcpServer.Stop() |
||||||
|
|
||||||
|
client := downloader.ClientSetup(serverIP, serverPort) |
||||||
|
defer client.Close() |
||||||
|
response := client.GetBlockHashes() |
||||||
|
if !reflect.DeepEqual(response.Payload, fakeNode.GetBlockHashes()) { |
||||||
|
t.Error("not equal") |
||||||
|
} |
||||||
|
response = client.GetBlocks([]int32{0, 1}) |
||||||
|
fmt.Println(len(response.Payload)) |
||||||
|
if !reflect.DeepEqual(response.Payload, fakeNode.GetBlocks()) { |
||||||
|
t.Error("not equal") |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,8 @@ |
|||||||
|
package syncing |
||||||
|
|
||||||
|
import "errors" |
||||||
|
|
||||||
|
// Errors ...
|
||||||
|
var ( |
||||||
|
ErrSyncPeerConfigClientNotReady = errors.New("client is not ready") |
||||||
|
) |
Loading…
Reference in new issue