add more doc

pull/102/head
Minh Doan 6 years ago committed by Minh Doan
parent 3b1bd7fdf3
commit 979f1a6e61
  1. 10
      syncing/downloader/client.go
  2. 2
      syncing/downloader/errors.go
  3. 2
      syncing/downloader/interface.go
  4. 6
      syncing/downloader/server.go
  5. 3
      syncing/downloader/server_test.go
  6. 7
      syncing/syncing.go

@ -10,14 +10,14 @@ import (
"google.golang.org/grpc" "google.golang.org/grpc"
) )
// Client ... // Client is the client model for downloader package.
type Client struct { type Client struct {
dlClient pb.DownloaderClient dlClient pb.DownloaderClient
opts []grpc.DialOption opts []grpc.DialOption
conn *grpc.ClientConn conn *grpc.ClientConn
} }
// ClientSetup ... // ClientSetup setups a Client given ip and port.
func ClientSetup(ip, port string) *Client { func ClientSetup(ip, port string) *Client {
client := Client{} client := Client{}
client.opts = append(client.opts, grpc.WithInsecure()) client.opts = append(client.opts, grpc.WithInsecure())
@ -32,12 +32,12 @@ func ClientSetup(ip, port string) *Client {
return &client return &client
} }
// Close ... // Close closes the Client.
func (client *Client) Close() { func (client *Client) Close() {
client.conn.Close() client.conn.Close()
} }
// GetBlockHashes ... // GetBlockHashes gets block hashes from all the peers by calling grpc request.
func (client *Client) GetBlockHashes() *pb.DownloaderResponse { func (client *Client) GetBlockHashes() *pb.DownloaderResponse {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel() defer cancel()
@ -49,7 +49,7 @@ func (client *Client) GetBlockHashes() *pb.DownloaderResponse {
return response return response
} }
// GetBlocks ... // GetBlocks gets blocks in serialization byte array by calling a grpc request.
func (client *Client) GetBlocks(hashes [][]byte) *pb.DownloaderResponse { func (client *Client) GetBlocks(hashes [][]byte) *pb.DownloaderResponse {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel() defer cancel()

@ -2,7 +2,7 @@ package downloader
import "errors" import "errors"
// Errors ... // Errors for downloader package.
var ( var (
ErrDownloaderWithNoNode = errors.New("no node attached") ErrDownloaderWithNoNode = errors.New("no node attached")
) )

@ -4,7 +4,7 @@ import (
pb "github.com/harmony-one/harmony/syncing/downloader/proto" pb "github.com/harmony-one/harmony/syncing/downloader/proto"
) )
// DownloadInterface ... // DownloadInterface is the interface for downloader package.
type DownloadInterface interface { type DownloadInterface interface {
// Syncing blockchain from other peers. // Syncing blockchain from other peers.
// The returned channel is the signal of syncing finish. // The returned channel is the signal of syncing finish.

@ -11,7 +11,7 @@ import (
pb "github.com/harmony-one/harmony/syncing/downloader/proto" pb "github.com/harmony-one/harmony/syncing/downloader/proto"
) )
// Server ... // Server is the Server struct for downloader package.
type Server struct { type Server struct {
downloadInterface DownloadInterface downloadInterface DownloadInterface
} }
@ -25,7 +25,7 @@ func (s *Server) Query(ctx context.Context, request *pb.DownloaderRequest) (*pb.
return response, nil return response, nil
} }
// Start ... // Start starts the Server on given ip and port.
func (s *Server) Start(ip, port string) (*grpc.Server, error) { func (s *Server) Start(ip, port string) (*grpc.Server, error) {
lis, err := net.Listen("tcp", fmt.Sprintf("%s:%s", ip, port)) lis, err := net.Listen("tcp", fmt.Sprintf("%s:%s", ip, port))
if err != nil { if err != nil {
@ -38,7 +38,7 @@ func (s *Server) Start(ip, port string) (*grpc.Server, error) {
return grpcServer, nil return grpcServer, nil
} }
// NewServer ... // NewServer creates new Server which implements DownloadInterface.
func NewServer(dlInterface DownloadInterface) *Server { func NewServer(dlInterface DownloadInterface) *Server {
s := &Server{downloadInterface: dlInterface} s := &Server{downloadInterface: dlInterface}
return s return s

@ -52,6 +52,7 @@ func (node *FakeNode) Init() {
node.bc = bc.CreateBlockchainWithMoreBlocks(addresses, ShardID) node.bc = bc.CreateBlockchainWithMoreBlocks(addresses, ShardID)
} }
// CalculateResponse is the implementation for DownloadInterface.
func (node *FakeNode) CalculateResponse(request *pb.DownloaderRequest) (*pb.DownloaderResponse, error) { func (node *FakeNode) CalculateResponse(request *pb.DownloaderRequest) (*pb.DownloaderResponse, error) {
response := &pb.DownloaderResponse{} response := &pb.DownloaderResponse{}
if request.Type == pb.DownloaderRequest_HEADER { if request.Type == pb.DownloaderRequest_HEADER {
@ -67,6 +68,7 @@ func (node *FakeNode) CalculateResponse(request *pb.DownloaderRequest) (*pb.Down
return response, nil return response, nil
} }
// TestGetBlockHashes tests GetBlockHashes function.
func TestGetBlockHashes(t *testing.T) { func TestGetBlockHashes(t *testing.T) {
fakeNode := &FakeNode{} fakeNode := &FakeNode{}
fakeNode.Init() fakeNode.Init()
@ -85,6 +87,7 @@ func TestGetBlockHashes(t *testing.T) {
} }
} }
// TestGetBlocks tests GetBlocks function.
func TestGetBlocks(t *testing.T) { func TestGetBlocks(t *testing.T) {
fakeNode := &FakeNode{} fakeNode := &FakeNode{}
fakeNode.Init() fakeNode.Init()

@ -44,7 +44,7 @@ type StateSync struct {
stateSyncTaskQueue *queue.Queue stateSyncTaskQueue *queue.Queue
} }
// GetBlockHashes ... // GetBlockHashes gets block hashes by calling grpc request to the corresponding peer.
func (peerConfig *SyncPeerConfig) GetBlockHashes() error { func (peerConfig *SyncPeerConfig) GetBlockHashes() error {
if peerConfig.client == nil { if peerConfig.client == nil {
return ErrSyncPeerConfigClientNotReady return ErrSyncPeerConfigClientNotReady
@ -58,7 +58,7 @@ func (peerConfig *SyncPeerConfig) GetBlockHashes() error {
return nil return nil
} }
// GetBlocks ... // GetBlocks gets blocks by calling grpc request to the corresponding peer.
func (peerConfig *SyncPeerConfig) GetBlocks(hashes [][]byte) ([][]byte, error) { func (peerConfig *SyncPeerConfig) GetBlocks(hashes [][]byte) ([][]byte, error) {
if peerConfig.client == nil { if peerConfig.client == nil {
return nil, ErrSyncPeerConfigClientNotReady return nil, ErrSyncPeerConfigClientNotReady
@ -78,7 +78,7 @@ func (ss *StateSync) ProcessStateSyncFromPeers(peers []p2p.Peer, bc *blockchain.
return done, nil return done, nil
} }
// CreateSyncConfig ... // CreateSyncConfig creates SyncConfig for StateSync object.
func (ss *StateSync) CreateSyncConfig(peers []p2p.Peer) { func (ss *StateSync) CreateSyncConfig(peers []p2p.Peer) {
ss.peerNumber = len(peers) ss.peerNumber = len(peers)
ss.syncConfig = &SyncConfig{ ss.syncConfig = &SyncConfig{
@ -92,6 +92,7 @@ func (ss *StateSync) CreateSyncConfig(peers []p2p.Peer) {
} }
} }
// makeConnectionToPeers makes grpc connection to all peers.
func (ss *StateSync) makeConnectionToPeers() { func (ss *StateSync) makeConnectionToPeers() {
var wg sync.WaitGroup var wg sync.WaitGroup
wg.Add(ss.peerNumber) wg.Add(ss.peerNumber)

Loading…
Cancel
Save