-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathclient.go
94 lines (73 loc) · 2.38 KB
/
client.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package node
import (
"context"
"github.com/ethereum/go-ethereum/common"
providers "github.com/openweb3/go-rpc-provider/provider_wrapper"
"github.com/sirupsen/logrus"
)
type Client struct {
url string
*providers.MiddlewarableProvider
}
func MustNewClient(url string, option ...providers.Option) *Client {
client, err := NewClient(url, option...)
if err != nil {
logrus.WithError(err).WithField("url", url).Fatal("Failed to connect to storage node")
}
return client
}
func NewClient(url string, option ...providers.Option) (*Client, error) {
var opt providers.Option
if len(option) > 0 {
opt = option[0]
}
provider, err := providers.NewProviderWithOption(url, opt)
if err != nil {
return nil, err
}
return &Client{
url: url,
MiddlewarableProvider: provider,
}, nil
}
func MustNewClients(urls []string, option ...providers.Option) []*Client {
var clients []*Client
for _, url := range urls {
client := MustNewClient(url, option...)
clients = append(clients, client)
}
return clients
}
func (c *Client) URL() string {
return c.url
}
// Ionian RPCs
func (c *Client) GetStatus() (status Status, err error) {
err = c.MiddlewarableProvider.CallContext(context.Background(), &status, "ionian_getStatus")
return
}
func (c *Client) GetFileInfo(root common.Hash) (file *FileInfo, err error) {
err = c.MiddlewarableProvider.CallContext(context.Background(), &file, "ionian_getFileInfo", root)
return
}
func (c *Client) UploadSegment(segment SegmentWithProof) (ret int, err error) {
err = c.MiddlewarableProvider.CallContext(context.Background(), &ret, "ionian_uploadSegment", segment)
return
}
func (c *Client) DownloadSegment(root common.Hash, startIndex, endIndex uint32) (data []byte, err error) {
err = c.MiddlewarableProvider.CallContext(context.Background(), &data, "ionian_downloadSegment", root, startIndex, endIndex)
return
}
// Admin RPCs
func (c *Client) Shutdown() (ret int, err error) {
err = c.MiddlewarableProvider.CallContext(context.Background(), &ret, "admin_shutdown")
return
}
func (c *Client) StartSyncFile(txSeq uint64) (ret int, err error) {
err = c.MiddlewarableProvider.CallContext(context.Background(), &ret, "admin_startSyncFile", txSeq)
return
}
func (c *Client) GetSyncStatus(txSeq uint64) (status string, err error) {
err = c.MiddlewarableProvider.CallContext(context.Background(), &status, "admin_getSyncStatus", txSeq)
return
}