2020-03-27 20:08:06 +00:00
|
|
|
package impl
|
2020-03-11 01:57:52 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"net/http"
|
|
|
|
|
2020-03-27 20:08:06 +00:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2020-05-20 18:23:51 +00:00
|
|
|
"github.com/filecoin-project/go-jsonrpc"
|
|
|
|
"github.com/filecoin-project/go-jsonrpc/auth"
|
2020-09-07 03:49:10 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2020-03-11 01:57:52 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
|
|
"github.com/filecoin-project/lotus/api/client"
|
2022-06-14 18:03:38 +00:00
|
|
|
"github.com/filecoin-project/lotus/storage/sealer"
|
2020-03-11 01:57:52 +00:00
|
|
|
)
|
|
|
|
|
2020-03-27 20:08:06 +00:00
|
|
|
type remoteWorker struct {
|
2021-03-23 12:42:56 +00:00
|
|
|
api.Worker
|
2020-03-24 23:49:45 +00:00
|
|
|
closer jsonrpc.ClientCloser
|
2020-03-11 01:57:52 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 20:08:06 +00:00
|
|
|
func (r *remoteWorker) NewSector(ctx context.Context, sector abi.SectorID) error {
|
2020-03-17 20:19:52 +00:00
|
|
|
return xerrors.New("unsupported")
|
|
|
|
}
|
|
|
|
|
2020-03-27 20:08:06 +00:00
|
|
|
func connectRemoteWorker(ctx context.Context, fa api.Common, url string) (*remoteWorker, error) {
|
2020-05-20 18:23:51 +00:00
|
|
|
token, err := fa.AuthNew(ctx, []auth.Permission{"admin"})
|
2020-03-11 01:57:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("creating auth token for remote connection: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
headers := http.Header{}
|
|
|
|
headers.Add("Authorization", "Bearer "+string(token))
|
|
|
|
|
2021-03-25 14:39:48 +00:00
|
|
|
wapi, closer, err := client.NewWorkerRPCV0(context.TODO(), url, headers)
|
2020-03-11 01:57:52 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("creating jsonrpc client: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-06-07 14:57:24 +00:00
|
|
|
wver, err := wapi.Version(ctx)
|
|
|
|
if err != nil {
|
|
|
|
closer()
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !wver.EqMajorMinor(api.WorkerAPIVersion0) {
|
|
|
|
return nil, xerrors.Errorf("unsupported worker api version: %s (expected %s)", wver, api.WorkerAPIVersion0)
|
|
|
|
}
|
|
|
|
|
2020-03-27 20:08:06 +00:00
|
|
|
return &remoteWorker{wapi, closer}, nil
|
2020-03-24 23:49:45 +00:00
|
|
|
}
|
|
|
|
|
2020-03-27 20:08:06 +00:00
|
|
|
func (r *remoteWorker) Close() error {
|
2020-03-24 23:49:45 +00:00
|
|
|
r.closer()
|
|
|
|
return nil
|
2020-03-11 01:57:52 +00:00
|
|
|
}
|
|
|
|
|
2022-06-14 18:03:38 +00:00
|
|
|
var _ sealer.Worker = &remoteWorker{}
|