You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
144 lines
5.1 KiB
144 lines
5.1 KiB
6 years ago
|
package downloader
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"fmt"
|
||
|
"time"
|
||
|
|
||
4 years ago
|
pb "github.com/harmony-one/harmony/api/service/legacysync/downloader/proto"
|
||
6 years ago
|
"github.com/harmony-one/harmony/internal/utils"
|
||
|
"google.golang.org/grpc"
|
||
6 years ago
|
)
|
||
|
|
||
6 years ago
|
// Client is the client model for downloader package.
|
||
6 years ago
|
type Client struct {
|
||
|
dlClient pb.DownloaderClient
|
||
|
opts []grpc.DialOption
|
||
|
conn *grpc.ClientConn
|
||
|
}
|
||
|
|
||
6 years ago
|
// ClientSetup setups a Client given ip and port.
|
||
6 years ago
|
func ClientSetup(ip, port string) *Client {
|
||
|
client := Client{}
|
||
|
client.opts = append(client.opts, grpc.WithInsecure())
|
||
4 years ago
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||
|
defer cancel()
|
||
|
|
||
6 years ago
|
var err error
|
||
4 years ago
|
client.conn, err = grpc.DialContext(ctx, fmt.Sprintf(ip+":"+port), client.opts...)
|
||
6 years ago
|
if err != nil {
|
||
6 years ago
|
utils.Logger().Error().Err(err).Str("ip", ip).Msg("[SYNC] client.go:ClientSetup fail to dial")
|
||
6 years ago
|
return nil
|
||
6 years ago
|
}
|
||
5 years ago
|
utils.Logger().Debug().Str("ip", ip).Msg("[SYNC] grpc connect successfully")
|
||
6 years ago
|
client.dlClient = pb.NewDownloaderClient(client.conn)
|
||
|
return &client
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
// Close closes the Client.
|
||
6 years ago
|
func (client *Client) Close() {
|
||
6 years ago
|
err := client.conn.Close()
|
||
|
if err != nil {
|
||
6 years ago
|
utils.Logger().Info().Msg("[SYNC] unable to close connection")
|
||
6 years ago
|
}
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
// GetBlockHashes gets block hashes from all the peers by calling grpc request.
|
||
5 years ago
|
func (client *Client) GetBlockHashes(startHash []byte, size uint32, ip, port string) *pb.DownloaderResponse {
|
||
6 years ago
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||
|
defer cancel()
|
||
5 years ago
|
request := &pb.DownloaderRequest{Type: pb.DownloaderRequest_BLOCKHASH, BlockHash: startHash, Size: size}
|
||
5 years ago
|
request.Ip = ip
|
||
|
request.Port = port
|
||
6 years ago
|
response, err := client.dlClient.Query(ctx, request)
|
||
6 years ago
|
if err != nil {
|
||
5 years ago
|
utils.Logger().Error().Err(err).Str("target", client.conn.Target()).Msg("[SYNC] GetBlockHashes query failed")
|
||
6 years ago
|
}
|
||
6 years ago
|
return response
|
||
6 years ago
|
}
|
||
6 years ago
|
|
||
5 years ago
|
// 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
|
||
|
}
|
||
|
|
||
6 years ago
|
// GetBlocks gets blocks in serialization byte array by calling a grpc request.
|
||
6 years ago
|
func (client *Client) GetBlocks(hashes [][]byte) *pb.DownloaderResponse {
|
||
6 years ago
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||
|
defer cancel()
|
||
|
request := &pb.DownloaderRequest{Type: pb.DownloaderRequest_BLOCK}
|
||
6 years ago
|
request.Hashes = make([][]byte, len(hashes))
|
||
|
for i := range hashes {
|
||
|
request.Hashes[i] = make([]byte, len(hashes[i]))
|
||
|
copy(request.Hashes[i], hashes[i])
|
||
|
}
|
||
6 years ago
|
response, err := client.dlClient.Query(ctx, request)
|
||
|
if err != nil {
|
||
5 years ago
|
utils.Logger().Error().Err(err).Str("target", client.conn.Target()).Msg("[SYNC] downloader/client.go:GetBlocks query failed")
|
||
6 years ago
|
}
|
||
|
return response
|
||
|
}
|
||
|
|
||
|
// Register will register node's ip/port information to peers receive newly created blocks in future
|
||
|
// hash is the bytes of "ip:port" string representation
|
||
6 years ago
|
func (client *Client) Register(hash []byte, ip, port string) *pb.DownloaderResponse {
|
||
5 years ago
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||
6 years ago
|
defer cancel()
|
||
|
request := &pb.DownloaderRequest{Type: pb.DownloaderRequest_REGISTER}
|
||
|
request.PeerHash = make([]byte, len(hash))
|
||
|
copy(request.PeerHash, hash)
|
||
6 years ago
|
request.Ip = ip
|
||
|
request.Port = port
|
||
6 years ago
|
response, err := client.dlClient.Query(ctx, request)
|
||
6 years ago
|
if err != nil || response == nil {
|
||
5 years ago
|
utils.Logger().Error().Err(err).Str("target", client.conn.Target()).Interface("response", response).Msg("[SYNC] client.go:Register failed")
|
||
6 years ago
|
}
|
||
|
return response
|
||
|
}
|
||
|
|
||
6 years ago
|
// PushNewBlock will send the lastest verified block to registered nodes
|
||
5 years ago
|
func (client *Client) PushNewBlock(selfPeerHash [20]byte, blockHash []byte, timeout bool) (*pb.DownloaderResponse, error) {
|
||
6 years ago
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
|
||
|
defer cancel()
|
||
|
|
||
|
request := &pb.DownloaderRequest{Type: pb.DownloaderRequest_NEWBLOCK}
|
||
|
request.BlockHash = make([]byte, len(blockHash))
|
||
|
copy(request.BlockHash, blockHash)
|
||
6 years ago
|
request.PeerHash = make([]byte, len(selfPeerHash))
|
||
|
copy(request.PeerHash, selfPeerHash[:])
|
||
6 years ago
|
|
||
|
if timeout {
|
||
|
request.Type = pb.DownloaderRequest_REGISTERTIMEOUT
|
||
|
}
|
||
|
|
||
|
response, err := client.dlClient.Query(ctx, request)
|
||
|
if err != nil {
|
||
5 years ago
|
utils.Logger().Error().Err(err).Str("target", client.conn.Target()).Msg("[SYNC] unable to send new block to unsync node")
|
||
6 years ago
|
}
|
||
5 years ago
|
return response, err
|
||
6 years ago
|
}
|
||
6 years ago
|
|
||
|
// GetBlockChainHeight gets the blockheight from peer
|
||
5 years ago
|
func (client *Client) GetBlockChainHeight() (*pb.DownloaderResponse, error) {
|
||
6 years ago
|
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Second)
|
||
6 years ago
|
defer cancel()
|
||
|
request := &pb.DownloaderRequest{Type: pb.DownloaderRequest_BLOCKHEIGHT}
|
||
|
response, err := client.dlClient.Query(ctx, request)
|
||
|
if err != nil {
|
||
5 years ago
|
return nil, err
|
||
6 years ago
|
}
|
||
5 years ago
|
return response, nil
|
||
6 years ago
|
}
|