Skip to content

Commit

Permalink
Merge pull request #38 from TRON-US/BTFS-1118-beta
Browse files Browse the repository at this point in the history
Btfs-1118
  • Loading branch information
taiyangc authored Dec 2, 2019
2 parents e02c5fa + b643c84 commit 844f6d3
Show file tree
Hide file tree
Showing 8 changed files with 615 additions and 1,110 deletions.
1,516 changes: 456 additions & 1,060 deletions protos/hub/hub.pb.go

Large diffs are not rendered by default.

53 changes: 19 additions & 34 deletions protos/hub/hub.proto
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,8 @@ option java_package = "io.btfs.hub";
import "github.com/tron-us/protobuf/gogoproto/gogo.proto";
import "google/protobuf/timestamp.proto";

service Settings {
service HubQuery {
rpc GetSettings(SettingsReq) returns (SettingsResp);
}

service Hosts {
rpc GetHosts(HostsReq) returns (HostsResp);
}

Expand Down Expand Up @@ -80,11 +77,11 @@ message HostsData {

message Host {
string node_id = 1;
google.protobuf.Timestamp time_create = 2 [
google.protobuf.Timestamp create_timestamp = 2 [
(gogoproto.nullable) = false,
(gogoproto.stdtime) = true
];
google.protobuf.Timestamp time_update = 3 [
google.protobuf.Timestamp update_timestamp = 3 [
(gogoproto.nullable) = false,
(gogoproto.stdtime) = true
];
Expand All @@ -96,37 +93,25 @@ message Host {
float uptime = 9;
int64 age = 10;
float reputation = 11;
float upload_avg = 12;
float upload_var = 13;
float download_avg = 14;
float download_var = 15;
Location location = 16;
StorageInfo storage_info = 17;
BandwidthInfo bandwidth_info = 18;
CollateralInfo collateral_info = 19;
float upload_average = 12;
float upload_variance = 13;
float download_average = 14;
float download_variance = 15;
Location location = 16 [(gogoproto.nullable) = false];
float storage_volume_cap = 17;
float storage_volume_left = 18;
uint64 storage_time_min = 19;
float storage_price_ask = 20;
float storage_price_est = 21;
double bandwidth_limit = 22;
float bandwidth_price_ask = 23;
float bandwidth_price_est = 24;
uint64 collateral_stake = 25;
uint64 collateral_lost = 26;
uint64 collateral_burn = 27;
}

message Location {
double lat = 1;
double lon = 2;
}

message StorageInfo {
float storage_cap = 1;
float storage_left = 2;
int64 storage_min_time = 3;
float storage_ask_price = 4;
float storage_price_est = 5;
}

message BandwidthInfo {
float bandwidth_limit = 1;
float bandwidth_ask_price = 2;
float bandwidth_price_est = 3;
}

message CollateralInfo {
float collateral_stake = 1;
float collateral_lost = 2;
float collateral_burn = 3;
}
79 changes: 66 additions & 13 deletions utils/grpc_helper.go → utils/grpc/client.go
Original file line number Diff line number Diff line change
@@ -1,38 +1,88 @@
package utils
package grpc

import (
"context"
"crypto/tls"
"errors"
"fmt"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"net/url"
"regexp"
"strconv"
"strings"
"time"

"github.com/tron-us/go-btfs-common/protos/escrow"
"github.com/tron-us/go-btfs-common/protos/guard"
"github.com/tron-us/go-btfs-common/protos/hub"
"github.com/tron-us/go-btfs-common/protos/status"

"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
"google.golang.org/grpc/credentials"
)

const (
defaultSchema = "http"
defaultSchema = "http"
defaultTimeout = 30 * time.Second
)

var (
domainRegexp = regexp.MustCompile(`^(localhost)|([a-zA-Z0-9-]{1,63}\.)+([a-zA-Z]{1,63})$`)
ipv4Regexp = regexp.MustCompile(`^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$`)
)

type parsedURL struct {
schema string
host string
port int
func (g *ClientBuilder) doWithContext(ctx context.Context, f interface{}) error {
newCtx, cancel := context.WithTimeout(ctx, g.timeout)
if cancel != nil {
defer cancel()
}
conn, err := newGRPCConn(newCtx, g.addr)
if conn != nil {
defer conn.Close()
}
if err != nil {
return err
}
if conn == nil || conn.GetState() != connectivity.Ready {
return errors.New("failed to get connection")
}
switch v := f.(type) {
case func(context.Context, status.StatusClient) error:
return v(ctx, status.NewStatusClient(conn))
case func(context.Context, hub.HubQueryClient) error:
return v(ctx, hub.NewHubQueryClient(conn))
case func(context.Context, guard.GuardServiceClient) error:
return v(ctx, guard.NewGuardServiceClient(conn))
case func(context.Context, escrow.EscrowServiceClient) error:
return v(ctx, escrow.NewEscrowServiceClient(conn))
default:
return fmt.Errorf("illegal function: %T", f)
}
}

func NewGRPCConn(ctx context.Context, address string, opts ...grpc.DialOption) (*grpc.ClientConn, error) {
u, err := parse(address)
type ClientBuilder struct {
addr string
timeout time.Duration
}

func (b *ClientBuilder) Timeout(to time.Duration) *ClientBuilder {
b.timeout = to
return b
}

func builder(address string) ClientBuilder {
return ClientBuilder{
addr: address,
timeout: defaultTimeout,
}
}

func newGRPCConn(ctx context.Context, addr string) (*grpc.ClientConn, error) {
u, err := parse(addr)
if err != nil {
return nil, err
}
opts := []grpc.DialOption{grpc.WithBlock()}
if u.schema == "http" {
opts = append(opts, grpc.WithInsecure())
} else if u.schema == "https" {
Expand Down Expand Up @@ -88,15 +138,18 @@ func checkHost(host string) error {
if host == "" {
return errors.New("empty host")
}

host = strings.ToLower(host)
if domainRegexp.MatchString(host) {
return nil
}

if ipv4Regexp.MatchString(host) {
return nil
}

return fmt.Errorf("invalid host: %v", host)
}

type parsedURL struct {
schema string
host string
port int
}
5 changes: 2 additions & 3 deletions utils/grpc_helper_test.go → utils/grpc/client_test.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package utils
package grpc

import (
"context"
"google.golang.org/grpc"
"google.golang.org/grpc/connectivity"
"testing"
"time"
Expand All @@ -20,7 +19,7 @@ func TestNewGRPCConn(t *testing.T) {
}
for _, tt := range tests {
ctx, cancelFunc := context.WithTimeout(context.Background(), 2*time.Second)
conn, err := NewGRPCConn(ctx, tt.in, grpc.WithBlock())
conn, err := newGRPCConn(ctx, tt.in)
if cancelFunc != nil {
defer cancelFunc()
}
Expand Down
18 changes: 18 additions & 0 deletions utils/grpc/escrow.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package grpc

import (
"context"
"github.com/tron-us/go-btfs-common/protos/escrow"
)

func EscrowClient(addr string) *EscrowClientBuilder {
return &EscrowClientBuilder{builder(addr)}
}

type EscrowClientBuilder struct {
ClientBuilder
}

func (g *EscrowClientBuilder) WithContext(ctx context.Context, f func(ctx context.Context, client escrow.EscrowServiceClient) error) error {
return g.doWithContext(ctx, f)
}
18 changes: 18 additions & 0 deletions utils/grpc/guard.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package grpc

import (
"context"
"github.com/tron-us/go-btfs-common/protos/guard"
)

func GuardClient(addr string) *GuardClientBuilder {
return &GuardClientBuilder{builder(addr)}
}

type GuardClientBuilder struct {
ClientBuilder
}

func (g *GuardClientBuilder) WithContext(ctx context.Context, f func(ctx context.Context, client guard.GuardServiceClient) error) error {
return g.doWithContext(ctx, f)
}
18 changes: 18 additions & 0 deletions utils/grpc/hub.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package grpc

import (
"context"
"github.com/tron-us/go-btfs-common/protos/hub"
)

func HubQueryClient(addr string) *HubQueryClientBuilder {
return &HubQueryClientBuilder{builder(addr)}
}

type HubQueryClientBuilder struct {
ClientBuilder
}

func (g *HubQueryClientBuilder) WithContext(ctx context.Context, f func(ctx context.Context, client hub.HubQueryClient) error) error {
return g.doWithContext(ctx, f)
}
18 changes: 18 additions & 0 deletions utils/grpc/status.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package grpc

import (
"context"
"github.com/tron-us/go-btfs-common/protos/status"
)

func StatusClient(addr string) *StatusClientBuilder {
return &StatusClientBuilder{builder(addr)}
}

type StatusClientBuilder struct {
ClientBuilder
}

func (g *StatusClientBuilder) WithContext(ctx context.Context, f func(ctx context.Context, client status.StatusClient) error) error {
return g.doWithContext(ctx, f)
}

0 comments on commit 844f6d3

Please sign in to comment.