2019-07-24 01:13:56 +00:00
|
|
|
package impl
|
2019-07-24 00:58:31 +00:00
|
|
|
|
|
|
|
import (
|
2019-07-27 01:54:03 +00:00
|
|
|
"context"
|
2019-11-21 14:10:51 +00:00
|
|
|
"encoding/json"
|
2019-12-05 04:43:54 +00:00
|
|
|
"io"
|
|
|
|
"mime"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
2020-01-24 20:19:52 +00:00
|
|
|
"strconv"
|
2019-12-06 00:27:32 +00:00
|
|
|
|
2020-02-27 21:45:31 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2020-02-09 06:06:32 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
2020-02-08 00:18:14 +00:00
|
|
|
|
2019-12-06 00:27:32 +00:00
|
|
|
"github.com/gorilla/mux"
|
2020-02-08 00:18:14 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2019-12-06 00:27:32 +00:00
|
|
|
files "github.com/ipfs/go-ipfs-files"
|
2020-02-08 00:18:14 +00:00
|
|
|
"golang.org/x/xerrors"
|
2019-12-05 04:43:54 +00:00
|
|
|
|
2019-12-19 20:13:17 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2020-02-08 00:18:14 +00:00
|
|
|
storagemarket "github.com/filecoin-project/go-fil-markets/storagemarket"
|
2020-01-07 16:18:35 +00:00
|
|
|
"github.com/filecoin-project/go-sectorbuilder"
|
2020-01-28 23:08:02 +00:00
|
|
|
"github.com/filecoin-project/go-sectorbuilder/fs"
|
2020-02-04 06:17:18 +00:00
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2020-01-28 23:08:02 +00:00
|
|
|
"github.com/filecoin-project/lotus/api/apistruct"
|
2019-12-06 00:27:32 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/tarutil"
|
2019-11-25 04:45:13 +00:00
|
|
|
"github.com/filecoin-project/lotus/miner"
|
2019-10-18 04:47:41 +00:00
|
|
|
"github.com/filecoin-project/lotus/storage"
|
|
|
|
"github.com/filecoin-project/lotus/storage/sectorblocks"
|
2019-07-24 00:58:31 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type StorageMinerAPI struct {
|
|
|
|
CommonAPI
|
2019-07-27 00:45:27 +00:00
|
|
|
|
2019-11-04 16:47:08 +00:00
|
|
|
SectorBuilderConfig *sectorbuilder.Config
|
2020-01-13 20:47:27 +00:00
|
|
|
SectorBuilder sectorbuilder.Interface
|
2019-08-26 10:04:57 +00:00
|
|
|
SectorBlocks *sectorblocks.SectorBlocks
|
2019-07-30 00:46:56 +00:00
|
|
|
|
2020-02-08 00:18:14 +00:00
|
|
|
StorageProvider storagemarket.StorageProvider
|
|
|
|
Miner *storage.Miner
|
|
|
|
BlockMiner *miner.Miner
|
|
|
|
Full api.FullNode
|
2019-07-24 00:58:31 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 14:10:51 +00:00
|
|
|
func (sm *StorageMinerAPI) ServeRemote(w http.ResponseWriter, r *http.Request) {
|
2019-12-09 17:08:32 +00:00
|
|
|
if !apistruct.HasPerm(r.Context(), apistruct.PermAdmin) {
|
2019-11-21 14:10:51 +00:00
|
|
|
w.WriteHeader(401)
|
|
|
|
json.NewEncoder(w).Encode(struct{ Error string }{"unauthorized: missing write permission"})
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
mux := mux.NewRouter()
|
|
|
|
|
2020-01-28 23:08:02 +00:00
|
|
|
mux.HandleFunc("/remote/{type}/{id}", sm.remoteGetSector).Methods("GET")
|
|
|
|
mux.HandleFunc("/remote/{type}/{id}", sm.remotePutSector).Methods("PUT")
|
2019-11-21 14:10:51 +00:00
|
|
|
|
2019-11-21 18:38:43 +00:00
|
|
|
log.Infof("SERVEGETREMOTE %s", r.URL)
|
|
|
|
|
2019-11-21 14:10:51 +00:00
|
|
|
mux.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sm *StorageMinerAPI) remoteGetSector(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
|
2020-01-28 23:08:02 +00:00
|
|
|
id, err := strconv.ParseUint(vars["id"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("parsing sector id: ", err)
|
|
|
|
w.WriteHeader(500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-09 06:06:32 +00:00
|
|
|
path, err := sm.SectorBuilder.SectorPath(fs.DataType(vars["type"]), abi.SectorNumber(id))
|
2019-11-30 13:22:50 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
w.WriteHeader(500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-28 23:08:02 +00:00
|
|
|
stat, err := os.Stat(string(path))
|
2019-11-21 14:10:51 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
w.WriteHeader(500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-12-04 14:58:28 +00:00
|
|
|
var rd io.Reader
|
|
|
|
if stat.IsDir() {
|
2020-01-28 23:08:02 +00:00
|
|
|
rd, err = tarutil.TarDirectory(string(path))
|
2019-12-04 14:58:28 +00:00
|
|
|
w.Header().Set("Content-Type", "application/x-tar")
|
|
|
|
} else {
|
2020-01-28 23:08:02 +00:00
|
|
|
rd, err = os.OpenFile(string(path), os.O_RDONLY, 0644)
|
2019-12-04 14:58:28 +00:00
|
|
|
w.Header().Set("Content-Type", "application/octet-stream")
|
|
|
|
}
|
2019-11-21 18:38:43 +00:00
|
|
|
if err != nil {
|
2019-11-30 13:22:50 +00:00
|
|
|
log.Error(err)
|
|
|
|
w.WriteHeader(500)
|
2019-11-21 18:38:43 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-21 14:10:51 +00:00
|
|
|
w.WriteHeader(200)
|
2019-11-30 13:22:50 +00:00
|
|
|
if _, err := io.Copy(w, rd); err != nil {
|
2019-11-21 14:10:51 +00:00
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sm *StorageMinerAPI) remotePutSector(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
|
2020-01-28 23:08:02 +00:00
|
|
|
id, err := strconv.ParseUint(vars["id"], 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
log.Error("parsing sector id: ", err)
|
|
|
|
w.WriteHeader(500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-03 03:48:56 +00:00
|
|
|
// This is going to get better with worker-to-worker transfers
|
2020-02-04 06:55:57 +00:00
|
|
|
|
2020-02-09 06:06:32 +00:00
|
|
|
path, err := sm.SectorBuilder.SectorPath(fs.DataType(vars["type"]), abi.SectorNumber(id))
|
2019-11-21 14:10:51 +00:00
|
|
|
if err != nil {
|
2020-02-04 06:55:57 +00:00
|
|
|
if err != fs.ErrNotFound {
|
|
|
|
log.Error(err)
|
|
|
|
w.WriteHeader(500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-09 06:06:32 +00:00
|
|
|
path, err = sm.SectorBuilder.AllocSectorPath(fs.DataType(vars["type"]), abi.SectorNumber(id), true)
|
2020-02-04 06:55:57 +00:00
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
w.WriteHeader(500)
|
|
|
|
return
|
|
|
|
}
|
2019-11-21 14:10:51 +00:00
|
|
|
}
|
|
|
|
|
2019-11-30 13:22:50 +00:00
|
|
|
mediatype, _, err := mime.ParseMediaType(r.Header.Get("Content-Type"))
|
2019-11-21 19:51:48 +00:00
|
|
|
if err != nil {
|
2019-11-21 14:10:51 +00:00
|
|
|
log.Error(err)
|
2019-11-30 13:22:50 +00:00
|
|
|
w.WriteHeader(500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-01-28 23:08:02 +00:00
|
|
|
if err := os.RemoveAll(string(path)); err != nil {
|
2019-12-04 14:58:28 +00:00
|
|
|
log.Error(err)
|
|
|
|
w.WriteHeader(500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2019-11-30 13:22:50 +00:00
|
|
|
switch mediatype {
|
2019-12-04 14:58:28 +00:00
|
|
|
case "application/x-tar":
|
2020-01-28 23:08:02 +00:00
|
|
|
if err := tarutil.ExtractTar(r.Body, string(path)); err != nil {
|
2019-12-04 16:53:32 +00:00
|
|
|
log.Error(err)
|
|
|
|
w.WriteHeader(500)
|
2019-11-30 13:22:50 +00:00
|
|
|
return
|
|
|
|
}
|
2019-12-04 14:58:28 +00:00
|
|
|
default:
|
2020-01-28 23:08:02 +00:00
|
|
|
if err := files.WriteTo(files.NewReaderFile(r.Body), string(path)); err != nil {
|
2019-11-30 13:22:50 +00:00
|
|
|
log.Error(err)
|
|
|
|
w.WriteHeader(500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
w.WriteHeader(200)
|
|
|
|
|
|
|
|
log.Infof("received %s sector (%s): %d bytes", vars["type"], vars["sname"], r.ContentLength)
|
2019-11-21 14:10:51 +00:00
|
|
|
}
|
|
|
|
|
2019-11-21 16:23:42 +00:00
|
|
|
func (sm *StorageMinerAPI) WorkerStats(context.Context) (sectorbuilder.WorkerStats, error) {
|
|
|
|
stat := sm.SectorBuilder.WorkerStats()
|
|
|
|
return stat, nil
|
2019-11-08 18:15:13 +00:00
|
|
|
}
|
|
|
|
|
2019-10-14 02:32:32 +00:00
|
|
|
func (sm *StorageMinerAPI) ActorAddress(context.Context) (address.Address, error) {
|
|
|
|
return sm.SectorBuilderConfig.Miner, nil
|
2019-08-10 01:54:45 +00:00
|
|
|
}
|
|
|
|
|
2020-02-09 06:06:32 +00:00
|
|
|
func (sm *StorageMinerAPI) ActorSectorSize(ctx context.Context, addr address.Address) (abi.SectorSize, error) {
|
2020-02-11 23:29:45 +00:00
|
|
|
return sm.Full.StateMinerSectorSize(ctx, addr, types.EmptyTSK)
|
2019-11-21 14:10:51 +00:00
|
|
|
}
|
|
|
|
|
2019-12-08 21:48:20 +00:00
|
|
|
func (sm *StorageMinerAPI) PledgeSector(ctx context.Context) error {
|
|
|
|
return sm.Miner.PledgeSector()
|
2019-07-27 01:54:03 +00:00
|
|
|
}
|
|
|
|
|
2020-02-09 06:06:32 +00:00
|
|
|
func (sm *StorageMinerAPI) SectorsStatus(ctx context.Context, sid abi.SectorNumber) (api.SectorInfo, error) {
|
2019-11-08 18:15:13 +00:00
|
|
|
info, err := sm.Miner.GetSectorInfo(sid)
|
|
|
|
if err != nil {
|
|
|
|
return api.SectorInfo{}, err
|
|
|
|
}
|
|
|
|
|
2020-02-09 06:06:32 +00:00
|
|
|
deals := make([]abi.DealID, len(info.Pieces))
|
2019-11-08 18:15:13 +00:00
|
|
|
for i, piece := range info.Pieces {
|
2020-02-23 00:47:47 +00:00
|
|
|
if piece.DealID == nil {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
deals[i] = *piece.DealID
|
2019-11-08 18:15:13 +00:00
|
|
|
}
|
|
|
|
|
2020-01-23 14:18:05 +00:00
|
|
|
log := make([]api.SectorLog, len(info.Log))
|
|
|
|
for i, l := range info.Log {
|
|
|
|
log[i] = api.SectorLog{
|
|
|
|
Kind: l.Kind,
|
|
|
|
Timestamp: l.Timestamp,
|
|
|
|
Trace: l.Trace,
|
|
|
|
Message: l.Message,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-08 18:15:13 +00:00
|
|
|
return api.SectorInfo{
|
|
|
|
SectorID: sid,
|
|
|
|
State: info.State,
|
|
|
|
CommD: info.CommD,
|
|
|
|
CommR: info.CommR,
|
|
|
|
Proof: info.Proof,
|
|
|
|
Deals: deals,
|
2020-02-27 21:45:31 +00:00
|
|
|
Ticket: info.Ticket,
|
|
|
|
Seed: info.Seed,
|
2019-12-09 16:40:15 +00:00
|
|
|
Retries: info.Nonce,
|
2019-12-04 00:44:29 +00:00
|
|
|
|
|
|
|
LastErr: info.LastErr,
|
2020-01-23 14:18:05 +00:00
|
|
|
Log: log,
|
2019-11-08 18:15:13 +00:00
|
|
|
}, nil
|
2019-07-27 21:08:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// List all staged sectors
|
2020-02-09 06:06:32 +00:00
|
|
|
func (sm *StorageMinerAPI) SectorsList(context.Context) ([]abi.SectorNumber, error) {
|
2019-11-08 18:15:13 +00:00
|
|
|
sectors, err := sm.Miner.ListSectors()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2020-02-09 06:06:32 +00:00
|
|
|
out := make([]abi.SectorNumber, len(sectors))
|
2019-11-08 18:15:13 +00:00
|
|
|
for i, sector := range sectors {
|
|
|
|
out[i] = sector.SectorID
|
|
|
|
}
|
|
|
|
return out, nil
|
2019-07-27 21:08:10 +00:00
|
|
|
}
|
|
|
|
|
2019-08-26 10:04:57 +00:00
|
|
|
func (sm *StorageMinerAPI) SectorsRefs(context.Context) (map[string][]api.SealedRef, error) {
|
|
|
|
// json can't handle cids as map keys
|
|
|
|
out := map[string][]api.SealedRef{}
|
|
|
|
|
|
|
|
refs, err := sm.SectorBlocks.List()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
for k, v := range refs {
|
2020-01-24 20:19:52 +00:00
|
|
|
out[strconv.FormatUint(k, 10)] = v
|
2019-08-26 10:04:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2020-02-09 06:06:32 +00:00
|
|
|
func (sm *StorageMinerAPI) SectorsUpdate(ctx context.Context, id abi.SectorNumber, state api.SectorState) error {
|
2020-01-10 02:11:00 +00:00
|
|
|
return sm.Miner.ForceSectorState(ctx, id, state)
|
2019-12-05 04:43:54 +00:00
|
|
|
}
|
|
|
|
|
2019-12-06 00:27:32 +00:00
|
|
|
func (sm *StorageMinerAPI) WorkerQueue(ctx context.Context, cfg sectorbuilder.WorkerCfg) (<-chan sectorbuilder.WorkerTask, error) {
|
|
|
|
return sm.SectorBuilder.AddWorker(ctx, cfg)
|
2019-11-21 00:52:59 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (sm *StorageMinerAPI) WorkerDone(ctx context.Context, task uint64, res sectorbuilder.SealRes) error {
|
2019-11-21 14:10:51 +00:00
|
|
|
return sm.SectorBuilder.TaskDone(ctx, task, res)
|
2019-11-21 00:52:59 +00:00
|
|
|
}
|
|
|
|
|
2020-02-08 00:18:14 +00:00
|
|
|
func (sm *StorageMinerAPI) MarketImportDealData(ctx context.Context, propCid cid.Cid, path string) error {
|
|
|
|
fi, err := os.Open(path)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to open file: %w", err)
|
|
|
|
}
|
|
|
|
defer fi.Close()
|
|
|
|
|
|
|
|
return sm.StorageProvider.ImportDataForDeal(ctx, propCid, fi)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sm *StorageMinerAPI) MarketListDeals(ctx context.Context) ([]storagemarket.StorageDeal, error) {
|
|
|
|
return sm.StorageProvider.ListDeals(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sm *StorageMinerAPI) MarketListIncompleteDeals(ctx context.Context) ([]storagemarket.MinerDeal, error) {
|
|
|
|
return sm.StorageProvider.ListIncompleteDeals()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sm *StorageMinerAPI) SetPrice(ctx context.Context, p types.BigInt) error {
|
|
|
|
return sm.StorageProvider.AddAsk(abi.TokenAmount(p), 60*60*24*100) // lasts for 100 days?
|
|
|
|
}
|
|
|
|
|
2020-03-04 02:49:00 +00:00
|
|
|
func (sm *StorageMinerAPI) DealsList(ctx context.Context) ([]storagemarket.StorageDeal, error) {
|
|
|
|
return sm.StorageProvider.ListDeals(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sm *StorageMinerAPI) DealsImportData(ctx context.Context, deal cid.Cid, fname string) error {
|
|
|
|
fi, err := os.Open(fname)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to open given file: %w", err)
|
|
|
|
}
|
|
|
|
defer fi.Close()
|
|
|
|
|
|
|
|
return sm.StorageProvider.ImportDataForDeal(ctx, deal, fi)
|
|
|
|
}
|
|
|
|
|
2019-07-24 00:58:31 +00:00
|
|
|
var _ api.StorageMiner = &StorageMinerAPI{}
|