2019-06-25 11:42:17 +00:00
|
|
|
package client
|
|
|
|
|
|
|
|
import (
|
2020-08-22 03:22:40 +00:00
|
|
|
"context"
|
2019-07-23 18:49:09 +00:00
|
|
|
"net/http"
|
2020-08-14 14:06:53 +00:00
|
|
|
"net/url"
|
|
|
|
"path"
|
2020-08-14 21:24:41 +00:00
|
|
|
"time"
|
2019-07-23 18:49:09 +00:00
|
|
|
|
2020-05-20 17:43:22 +00:00
|
|
|
"github.com/filecoin-project/go-jsonrpc"
|
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2021-03-23 18:15:44 +00:00
|
|
|
"github.com/filecoin-project/lotus/api/v0api"
|
|
|
|
"github.com/filecoin-project/lotus/api/v1api"
|
2020-08-14 14:06:53 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/rpcenc"
|
2019-06-25 11:42:17 +00:00
|
|
|
)
|
|
|
|
|
2021-03-25 14:39:48 +00:00
|
|
|
// NewCommonRPCV0 creates a new http jsonrpc client.
|
2021-06-29 12:07:00 +00:00
|
|
|
func NewCommonRPCV0(ctx context.Context, addr string, requestHeader http.Header) (api.CommonNet, jsonrpc.ClientCloser, error) {
|
|
|
|
var res v0api.CommonNetStruct
|
2020-08-22 03:22:40 +00:00
|
|
|
closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin",
|
2021-06-29 12:07:00 +00:00
|
|
|
api.GetInternalStructs(&res), requestHeader)
|
2019-08-02 16:25:10 +00:00
|
|
|
|
2019-10-03 18:12:30 +00:00
|
|
|
return &res, closer, err
|
2019-08-02 16:25:10 +00:00
|
|
|
}
|
|
|
|
|
2021-03-25 14:39:48 +00:00
|
|
|
// NewFullNodeRPCV0 creates a new http jsonrpc client.
|
2021-04-01 12:17:22 +00:00
|
|
|
func NewFullNodeRPCV0(ctx context.Context, addr string, requestHeader http.Header) (v0api.FullNode, jsonrpc.ClientCloser, error) {
|
2021-03-25 14:39:48 +00:00
|
|
|
var res v0api.FullNodeStruct
|
2021-06-29 12:07:00 +00:00
|
|
|
|
2021-03-25 14:39:48 +00:00
|
|
|
closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin",
|
2021-06-29 12:07:00 +00:00
|
|
|
api.GetInternalStructs(&res), requestHeader)
|
2021-03-25 14:39:48 +00:00
|
|
|
|
|
|
|
return &res, closer, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewFullNodeRPCV1 creates a new http jsonrpc client.
|
|
|
|
func NewFullNodeRPCV1(ctx context.Context, addr string, requestHeader http.Header) (api.FullNode, jsonrpc.ClientCloser, error) {
|
2021-03-23 18:15:44 +00:00
|
|
|
var res v1api.FullNodeStruct
|
2020-08-22 03:22:40 +00:00
|
|
|
closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin",
|
2021-06-29 12:07:00 +00:00
|
|
|
api.GetInternalStructs(&res), requestHeader)
|
2019-07-24 00:40:19 +00:00
|
|
|
|
2019-10-03 18:12:30 +00:00
|
|
|
return &res, closer, err
|
2019-07-01 20:00:22 +00:00
|
|
|
}
|
2019-07-24 00:40:19 +00:00
|
|
|
|
2021-05-19 11:10:56 +00:00
|
|
|
func getPushUrl(addr string) (string, error) {
|
|
|
|
pushUrl, err := url.Parse(addr)
|
|
|
|
if err != nil {
|
|
|
|
return "", err
|
|
|
|
}
|
|
|
|
switch pushUrl.Scheme {
|
|
|
|
case "ws":
|
|
|
|
pushUrl.Scheme = "http"
|
|
|
|
case "wss":
|
|
|
|
pushUrl.Scheme = "https"
|
|
|
|
}
|
|
|
|
///rpc/v0 -> /rpc/streams/v0/push
|
|
|
|
|
|
|
|
pushUrl.Path = path.Join(pushUrl.Path, "../streams/v0/push")
|
|
|
|
return pushUrl.String(), nil
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:39:48 +00:00
|
|
|
// NewStorageMinerRPCV0 creates a new http jsonrpc client for miner
|
|
|
|
func NewStorageMinerRPCV0(ctx context.Context, addr string, requestHeader http.Header, opts ...jsonrpc.Option) (v0api.StorageMiner, jsonrpc.ClientCloser, error) {
|
2021-05-19 11:10:56 +00:00
|
|
|
pushUrl, err := getPushUrl(addr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-23 18:15:44 +00:00
|
|
|
var res v0api.StorageMinerStruct
|
2020-08-22 03:22:40 +00:00
|
|
|
closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin",
|
2021-06-29 12:07:00 +00:00
|
|
|
api.GetInternalStructs(&res), requestHeader,
|
2021-05-19 11:10:56 +00:00
|
|
|
append([]jsonrpc.Option{
|
|
|
|
rpcenc.ReaderParamEncoder(pushUrl),
|
2021-06-29 12:07:00 +00:00
|
|
|
}, opts...)...)
|
2019-07-24 00:40:19 +00:00
|
|
|
|
2019-10-03 18:12:30 +00:00
|
|
|
return &res, closer, err
|
2019-07-24 01:10:26 +00:00
|
|
|
}
|
2020-03-11 01:57:52 +00:00
|
|
|
|
2021-05-19 11:10:56 +00:00
|
|
|
func NewWorkerRPCV0(ctx context.Context, addr string, requestHeader http.Header) (v0api.Worker, jsonrpc.ClientCloser, error) {
|
|
|
|
pushUrl, err := getPushUrl(addr)
|
2020-08-14 14:06:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, nil, err
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:09:50 +00:00
|
|
|
var res api.WorkerStruct
|
2020-08-22 03:22:40 +00:00
|
|
|
closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin",
|
2021-06-29 12:07:00 +00:00
|
|
|
api.GetInternalStructs(&res),
|
2020-08-14 21:24:41 +00:00
|
|
|
requestHeader,
|
2021-05-19 11:10:56 +00:00
|
|
|
rpcenc.ReaderParamEncoder(pushUrl),
|
2020-08-14 21:24:41 +00:00
|
|
|
jsonrpc.WithNoReconnect(),
|
2020-08-17 15:34:56 +00:00
|
|
|
jsonrpc.WithTimeout(30*time.Second),
|
2020-03-24 18:00:08 +00:00
|
|
|
)
|
2020-03-11 01:57:52 +00:00
|
|
|
|
|
|
|
return &res, closer, err
|
|
|
|
}
|
2020-09-05 19:36:32 +00:00
|
|
|
|
2021-04-05 19:34:03 +00:00
|
|
|
// NewGatewayRPCV1 creates a new http jsonrpc client for a gateway node.
|
|
|
|
func NewGatewayRPCV1(ctx context.Context, addr string, requestHeader http.Header, opts ...jsonrpc.Option) (api.Gateway, jsonrpc.ClientCloser, error) {
|
2021-03-25 14:09:50 +00:00
|
|
|
var res api.GatewayStruct
|
2020-09-29 15:25:45 +00:00
|
|
|
closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin",
|
2021-06-29 12:07:00 +00:00
|
|
|
api.GetInternalStructs(&res),
|
2020-09-29 15:25:45 +00:00
|
|
|
requestHeader,
|
|
|
|
opts...,
|
|
|
|
)
|
|
|
|
|
|
|
|
return &res, closer, err
|
|
|
|
}
|
2020-10-09 18:36:42 +00:00
|
|
|
|
2021-04-20 16:42:12 +00:00
|
|
|
// NewGatewayRPCV0 creates a new http jsonrpc client for a gateway node.
|
|
|
|
func NewGatewayRPCV0(ctx context.Context, addr string, requestHeader http.Header, opts ...jsonrpc.Option) (v0api.Gateway, jsonrpc.ClientCloser, error) {
|
|
|
|
var res v0api.GatewayStruct
|
|
|
|
closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin",
|
2021-06-29 12:07:00 +00:00
|
|
|
api.GetInternalStructs(&res),
|
2021-04-20 16:42:12 +00:00
|
|
|
requestHeader,
|
|
|
|
opts...,
|
|
|
|
)
|
|
|
|
|
|
|
|
return &res, closer, err
|
|
|
|
}
|
|
|
|
|
2021-03-25 14:39:48 +00:00
|
|
|
func NewWalletRPCV0(ctx context.Context, addr string, requestHeader http.Header) (api.Wallet, jsonrpc.ClientCloser, error) {
|
2021-03-25 14:09:50 +00:00
|
|
|
var res api.WalletStruct
|
2020-09-05 19:36:32 +00:00
|
|
|
closer, err := jsonrpc.NewMergeClient(ctx, addr, "Filecoin",
|
2021-06-29 12:07:00 +00:00
|
|
|
api.GetInternalStructs(&res),
|
2020-09-05 19:36:32 +00:00
|
|
|
requestHeader,
|
|
|
|
)
|
|
|
|
|
|
|
|
return &res, closer, err
|
|
|
|
}
|