Skip to content

Commit fe480ec

Browse files
authored
Deals and Wallet CLI (textileio#60)
* wip on cli Signed-off-by: Aaron Sutula <hi@asutula.com> * cleanup after merge Signed-off-by: Aaron Sutula <hi@asutula.com> * more cli implementation Signed-off-by: Aaron Sutula <hi@asutula.com> * Quick watch impl Signed-off-by: Aaron Sutula <hi@asutula.com> * little fix to watch Signed-off-by: Aaron Sutula <hi@asutula.com> * deal command and stub client Signed-off-by: Aaron Sutula <hi@asutula.com> * use parent commands, more spinner Signed-off-by: Aaron Sutula <hi@asutula.com> * config init command, tied wallet commands into viper Signed-off-by: Aaron Sutula <hi@asutula.com> * Clean up viper integration Signed-off-by: Aaron Sutula <hi@asutula.com> * Cleaned up viper bound flags, expose serverAddress as config and flag Signed-off-by: Aaron Sutula <hi@asutula.com> * Little cleanup after merge Signed-off-by: Aaron Sutula <hi@asutula.com> * Accept DailOptions to grpc client Signed-off-by: Aaron Sutula <hi@asutula.com> * PR feedback Signed-off-by: Aaron Sutula <hi@asutula.com> * remove unused helper code, use .fil-tools conf file name Signed-off-by: Aaron Sutula <hi@asutula.com> * move cli dir to fil-tools for better go install name Signed-off-by: Aaron Sutula <hi@asutula.com>
1 parent ca9565c commit fe480ec

24 files changed

Lines changed: 1168 additions & 42 deletions

api/client/client.go

Lines changed: 2 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
package client
22

33
import (
4-
ma "github.com/multiformats/go-multiaddr"
54
dealsPb "github.com/textileio/filecoin/deals/pb"
6-
"github.com/textileio/filecoin/util"
75
walletPb "github.com/textileio/filecoin/wallet/pb"
86
"google.golang.org/grpc"
97
)
@@ -16,13 +14,8 @@ type Client struct {
1614
}
1715

1816
// NewClient starts the client
19-
func NewClient(maddr ma.Multiaddr) (*Client, error) {
20-
addr, err := util.TCPAddrFromMultiAddr(maddr)
21-
if err != nil {
22-
return nil, err
23-
}
24-
// ToDo: Support secure connection
25-
conn, err := grpc.Dial(addr, grpc.WithInsecure())
17+
func NewClient(target string, opts ...grpc.DialOption) (*Client, error) {
18+
conn, err := grpc.Dial(target, opts...)
2619
if err != nil {
2720
return nil, err
2821
}

api/client/client_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func TestClient(t *testing.T) {
1111
done := setupServer(t)
1212
defer done()
1313

14-
client, err := NewClient(getHostMultiaddress(t))
14+
client, err := NewClient(grpcHostAddress)
1515
if err != nil {
1616
t.Fatalf("failed to create client: %v", err)
1717
}

api/client/test_utils.go

Lines changed: 6 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,24 @@ import (
44
"context"
55
"testing"
66

7-
ma "github.com/multiformats/go-multiaddr"
87
"github.com/textileio/filecoin/api/server"
98
"github.com/textileio/filecoin/tests"
10-
"github.com/textileio/filecoin/util"
119
"google.golang.org/grpc"
1210
)
1311

1412
var (
15-
grpcHostAddr = "/ip4/127.0.0.1/tcp/50051"
16-
ctx = context.Background()
13+
grpcHostNetwork = "tcp"
14+
grpcHostAddress = "127.0.0.1:50051"
15+
ctx = context.Background()
1716
)
1817

19-
func getHostMultiaddress(t *testing.T) ma.Multiaddr {
20-
grpcAddr, err := ma.NewMultiaddr(grpcHostAddr)
21-
checkErr(t, err)
22-
return grpcAddr
23-
}
24-
2518
func setupServer(t *testing.T) func() {
2619
lotusAddr, token := tests.ClientConfigMA()
2720
conf := server.Config{
2821
LotusAddress: lotusAddr,
2922
LotusAuthToken: token,
30-
GrpcHostAddress: getHostMultiaddress(t),
23+
GrpcHostNetwork: grpcHostNetwork,
24+
GrpcHostAddress: grpcHostAddress,
3125
}
3226
server, err := server.NewServer(conf)
3327
checkErr(t, err)
@@ -38,9 +32,7 @@ func setupServer(t *testing.T) func() {
3832
}
3933

4034
func setupConnection(t *testing.T) (*grpc.ClientConn, func()) {
41-
addr, err := util.TCPAddrFromMultiAddr(getHostMultiaddress(t))
42-
checkErr(t, err)
43-
conn, err := grpc.Dial(addr, grpc.WithInsecure())
35+
conn, err := grpc.Dial(grpcHostAddress, grpc.WithInsecure())
4436
checkErr(t, err)
4537
return conn, func() {
4638
conn.Close()

api/server/server.go

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ import (
2121
"github.com/textileio/filecoin/iplocation/ip2location"
2222
"github.com/textileio/filecoin/lotus"
2323
txndstr "github.com/textileio/filecoin/txndstransform"
24-
"github.com/textileio/filecoin/util"
2524
"github.com/textileio/filecoin/wallet"
2625
walletPb "github.com/textileio/filecoin/wallet/pb"
2726
"google.golang.org/grpc"
@@ -55,7 +54,8 @@ type Server struct {
5554
type Config struct {
5655
LotusAddress ma.Multiaddr
5756
LotusAuthToken string
58-
GrpcHostAddress ma.Multiaddr
57+
GrpcHostNetwork string
58+
GrpcHostAddress string
5959
RepoPath string
6060
}
6161

@@ -119,11 +119,7 @@ func NewServer(conf Config) (*Server, error) {
119119
closeLotus: cls,
120120
}
121121

122-
grpcAddr, err := util.TCPAddrFromMultiAddr(conf.GrpcHostAddress)
123-
if err != nil {
124-
return nil, fmt.Errorf("error when transforming multiaddr to tcpaddr: %s", err)
125-
}
126-
listener, err := net.Listen("tcp", grpcAddr)
122+
listener, err := net.Listen(conf.GrpcHostNetwork, conf.GrpcHostAddress)
127123
if err != nil {
128124
return nil, fmt.Errorf("error when listening to grpc: %s", err)
129125
}

api/stub/client.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
package stub
2+
3+
import (
4+
"google.golang.org/grpc"
5+
)
6+
7+
// Client provides the client api
8+
type Client struct {
9+
Deals *Deals
10+
Wallet *Wallet
11+
}
12+
13+
// NewClient starts the client
14+
func NewClient(target string, opts ...grpc.DialOption) (*Client, error) {
15+
client := &Client{
16+
Deals: &Deals{},
17+
Wallet: &Wallet{},
18+
}
19+
return client, nil
20+
}
21+
22+
// Close closes the client's grpc connection and cancels any active requests
23+
func (c *Client) Close() error {
24+
return nil
25+
}

api/stub/deals.go

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
package stub
2+
3+
import (
4+
"context"
5+
"io"
6+
"time"
7+
8+
cid "github.com/ipfs/go-cid"
9+
"github.com/textileio/filecoin/api/client"
10+
"github.com/textileio/filecoin/deals"
11+
"github.com/textileio/filecoin/index/ask"
12+
)
13+
14+
// Deals provides an API for managing deals and storing data
15+
type Deals struct {
16+
}
17+
18+
var cids = []cid.Cid{}
19+
var dealStates = []string{
20+
"DealUnknown",
21+
"DealAccepted",
22+
"DealStaged",
23+
"DealSealing",
24+
"DealComplete",
25+
}
26+
27+
func init() {
28+
cid1, _ := cid.Parse("QmY7Yh4UquoXHLPFo2XbhXkhBvFoPwmQUSa92pxnxjQuPA")
29+
cid2, _ := cid.Parse("QmY7Yh4UquoXHLPFo2XbhXkhBvFoPwmQUSa92pxnxjQuPB")
30+
cid3, _ := cid.Parse("QmY7Yh4UquoXHLPFo2XbhXkhBvFoPwmQUSa92pxnxjQuPC")
31+
cid4, _ := cid.Parse("QmY7Yh4UquoXHLPFo2XbhXkhBvFoPwmQUSa92pxnxjQuPD")
32+
cid5, _ := cid.Parse("QmY7Yh4UquoXHLPFo2XbhXkhBvFoPwmQUSa92pxnxjQuPE")
33+
cid6, _ := cid.Parse("QmY7Yh4UquoXHLPFo2XbhXkhBvFoPwmQUSa92pxnxjQuPF")
34+
cid7, _ := cid.Parse("QmY7Yh4UquoXHLPFo2XbhXkhBvFoPwmQUSa92pxnxjQuPG")
35+
cid8, _ := cid.Parse("QmY7Yh4UquoXHLPFo2XbhXkhBvFoPwmQUSa92pxnxjQuPH")
36+
cid9, _ := cid.Parse("QmY7Yh4UquoXHLPFo2XbhXkhBvFoPwmQUSa92pxnxjQuPI")
37+
cid10, _ := cid.Parse("QmY7Yh4UquoXHLPFo2XbhXkhBvFoPwmQUSa92pxnxjQuPJ")
38+
cids = append(cids, cid1, cid2, cid3, cid4, cid5, cid6, cid7, cid8, cid9, cid10)
39+
}
40+
41+
// AvailableAsks executes a query to retrieve active Asks
42+
func (d *Deals) AvailableAsks(ctx context.Context, query ask.Query) ([]ask.StorageAsk, error) {
43+
time.Sleep(time.Second * 3)
44+
var asks = []ask.StorageAsk{
45+
ask.StorageAsk{
46+
Miner: "asdfzxvc123",
47+
Price: 1245,
48+
MinPieceSize: 1024,
49+
Timestamp: 1123456,
50+
Expiry: 12345677,
51+
},
52+
ask.StorageAsk{
53+
Miner: "xcvbfgfhh657",
54+
Price: 3420,
55+
MinPieceSize: 2048,
56+
Timestamp: 89798,
57+
Expiry: 12345677,
58+
},
59+
ask.StorageAsk{
60+
Miner: "asdfzxvc123",
61+
Price: 1245,
62+
MinPieceSize: 1024,
63+
Timestamp: 1123456,
64+
Expiry: 12345677,
65+
},
66+
ask.StorageAsk{
67+
Miner: "asdfzxvc123",
68+
Price: 1245,
69+
MinPieceSize: 1024,
70+
Timestamp: 1123456,
71+
Expiry: 12345677,
72+
},
73+
}
74+
return asks, nil
75+
}
76+
77+
// Store creates a proposal deal for data using wallet addr to all miners indicated
78+
// by dealConfigs for duration epochs
79+
func (d *Deals) Store(ctx context.Context, addr string, data io.Reader, dealConfigs []deals.DealConfig, duration uint64) ([]cid.Cid, []deals.DealConfig, error) {
80+
time.Sleep(time.Second * 3)
81+
success := []cid.Cid{}
82+
failed := []deals.DealConfig{}
83+
for i, dealConfig := range dealConfigs {
84+
if i == 0 {
85+
failed = append(failed, dealConfig)
86+
} else {
87+
success = append(success, cids[i-1])
88+
}
89+
}
90+
return success, failed, nil
91+
}
92+
93+
// Watch returnas a channel with state changes of indicated proposals
94+
func (d *Deals) Watch(ctx context.Context, proposals []cid.Cid) (<-chan client.WatchEvent, error) {
95+
ch := make(chan client.WatchEvent)
96+
go driveChannel(ch, proposals)
97+
return ch, nil
98+
}
99+
100+
func driveChannel(ch chan client.WatchEvent, proposals []cid.Cid) {
101+
for _, dealState := range dealStates {
102+
for _, proposal := range proposals {
103+
time.Sleep(time.Millisecond * 1000)
104+
ch <- client.WatchEvent{
105+
Deal: deals.DealInfo{
106+
ProposalCid: proposal,
107+
StateName: dealState,
108+
},
109+
}
110+
}
111+
}
112+
close(ch)
113+
}

api/stub/wallet.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
package stub
2+
3+
import (
4+
"context"
5+
"time"
6+
)
7+
8+
// Wallet provides an API for managing filecoin wallets
9+
type Wallet struct {
10+
}
11+
12+
// NewWallet creates a new filecoin wallet [bls|secp256k1]
13+
func (w *Wallet) NewWallet(ctx context.Context, typ string) (string, error) {
14+
time.Sleep(time.Millisecond * 1000)
15+
return "t16yt7dydsey3rzhefn4tudpn22pmp5ty2rmqyx7y", nil
16+
}
17+
18+
// WalletBalance gets a filecoin wallet's balance
19+
func (w *Wallet) WalletBalance(ctx context.Context, address string) (int64, error) {
20+
time.Sleep(time.Millisecond * 1000)
21+
return 47839, nil
22+
}

0 commit comments

Comments
 (0)