diff --git a/syncing/downloader/client.go b/syncing/downloader/client.go new file mode 100644 index 000000000..5b85898d2 --- /dev/null +++ b/syncing/downloader/client.go @@ -0,0 +1,48 @@ +package downloader + +import ( + "context" + "fmt" + "log" + "time" + + pb "github.com/harmony-one/harmony/syncing/downloader/proto" + "google.golang.org/grpc" +) + +// Client ... +type Client struct { + dlClient pb.DownloaderClient + opts []grpc.DialOption + conn *grpc.ClientConn +} + +// GetHeaders ... +func (client *Client) GetHeaders() { + ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) + defer cancel() + request := &pb.DownloaderRequest{Type: pb.DownloaderRequest_HEADER} + + result, err := client.dlClient.Query(ctx, request) + if err != nil { + log.Fatalf("%v.GetFeatures(_) = _, %v: ", client, err) + } + log.Println(result) +} + +// Close ... +func (client *Client) Close() { + client.conn.Close() +} + +// ClientSetUp ... +func ClientSetUp(ip, port string) *Client { + client := Client{} + 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) + } + client.dlClient = pb.NewDownloaderClient(client.conn) + return &client +} diff --git a/syncing/downloader/errors.go b/syncing/downloader/errors.go new file mode 100644 index 000000000..b5aacc029 --- /dev/null +++ b/syncing/downloader/errors.go @@ -0,0 +1,8 @@ +package downloader + +import "errors" + +// Errors ... +var ( + ErrDownloaderWithNoNode = errors.New("no node attached") +) diff --git a/syncing/downloader/server_test.go b/syncing/downloader/server_test.go new file mode 100644 index 000000000..8a4e13654 --- /dev/null +++ b/syncing/downloader/server_test.go @@ -0,0 +1,36 @@ +package downloader + +import ( + "testing" + + "github.com/harmony-one/harmony/blockchain" + "github.com/harmony-one/harmony/crypto/pki" + "github.com/harmony-one/harmony/node" +) + +const ( + serverPort = "9997" + serverIP = "127.0.0.1" + clientPort = "9999" +) + +var ( + PriIntOne = 111 + TestAddressOne = pki.GetAddressFromInt(PriIntOne) +) + +func setupServer() *Server { + bc := blockchain.CreateBlockchain(TestAddressOne, 0) + node := &node.Node{} + node.SetBlockchain(bc) + server := NewServer(node) + return server +} + +func TestGetBlockHashes(t *testing.T) { + server := setupServer() + server.Start(serverIP) + + // client := ClientSetUp(serverIP, serverPort) + // client.GetHeaders() +}