lotus/node/impl/storminer.go

220 lines
5.2 KiB
Go
Raw Normal View History

package impl
2019-07-24 00:58:31 +00:00
import (
2019-07-27 01:54:03 +00:00
"context"
"encoding/json"
2019-12-09 17:08:32 +00:00
"github.com/filecoin-project/lotus/api/apistruct"
"io"
"mime"
"net/http"
"os"
"github.com/gorilla/mux"
files "github.com/ipfs/go-ipfs-files"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/chain/address"
"github.com/filecoin-project/lotus/lib/sectorbuilder"
"github.com/filecoin-project/lotus/lib/tarutil"
2019-11-25 04:45:13 +00:00
"github.com/filecoin-project/lotus/miner"
"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
SectorBuilderConfig *sectorbuilder.Config
2019-08-10 01:54:45 +00:00
SectorBuilder *sectorbuilder.SectorBuilder
2019-08-26 10:04:57 +00:00
SectorBlocks *sectorblocks.SectorBlocks
2019-11-25 04:45:13 +00:00
Miner *storage.Miner
BlockMiner *miner.Miner
Full api.FullNode
2019-07-24 00:58:31 +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) {
w.WriteHeader(401)
json.NewEncoder(w).Encode(struct{ Error string }{"unauthorized: missing write permission"})
return
}
mux := mux.NewRouter()
mux.HandleFunc("/remote/{type}/{sname}", sm.remoteGetSector).Methods("GET")
mux.HandleFunc("/remote/{type}/{sname}", sm.remotePutSector).Methods("PUT")
2019-11-21 18:38:43 +00:00
log.Infof("SERVEGETREMOTE %s", r.URL)
mux.ServeHTTP(w, r)
}
func (sm *StorageMinerAPI) remoteGetSector(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
2019-11-30 13:22:50 +00:00
path, err := sm.SectorBuilder.GetPath(vars["type"], vars["sname"])
if err != nil {
log.Error(err)
w.WriteHeader(500)
return
}
stat, err := os.Stat(path)
if err != nil {
log.Error(err)
w.WriteHeader(500)
return
}
var rd io.Reader
if stat.IsDir() {
rd, err = tarutil.TarDirectory(path)
w.Header().Set("Content-Type", "application/x-tar")
} else {
rd, err = os.OpenFile(path, os.O_RDONLY, 0644)
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
}
w.WriteHeader(200)
2019-11-30 13:22:50 +00:00
if _, err := io.Copy(w, rd); err != nil {
log.Error(err)
return
}
}
func (sm *StorageMinerAPI) remotePutSector(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r)
2019-11-30 13:22:50 +00:00
path, err := sm.SectorBuilder.GetPath(vars["type"], vars["sname"])
if err != nil {
log.Error(err)
w.WriteHeader(500)
return
}
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 {
log.Error(err)
2019-11-30 13:22:50 +00:00
w.WriteHeader(500)
return
}
if err := os.RemoveAll(path); err != nil {
log.Error(err)
w.WriteHeader(500)
return
}
2019-11-30 13:22:50 +00:00
switch mediatype {
case "application/x-tar":
if err := tarutil.ExtractTar(r.Body, 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
}
default:
if err := files.WriteTo(files.NewReaderFile(r.Body), 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 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
}
func (sm *StorageMinerAPI) ActorAddress(context.Context) (address.Address, error) {
return sm.SectorBuilderConfig.Miner, nil
2019-08-10 01:54:45 +00:00
}
func (sm *StorageMinerAPI) ActorSectorSize(ctx context.Context, addr address.Address) (uint64, error) {
return sm.Full.StateMinerSectorSize(ctx, addr, nil)
}
func (sm *StorageMinerAPI) PledgeSector(ctx context.Context) error {
return sm.Miner.PledgeSector()
2019-07-27 01:54:03 +00:00
}
2019-11-08 18:15:13 +00:00
func (sm *StorageMinerAPI) SectorsStatus(ctx context.Context, sid uint64) (api.SectorInfo, error) {
info, err := sm.Miner.GetSectorInfo(sid)
if err != nil {
return api.SectorInfo{}, err
}
deals := make([]uint64, len(info.Pieces))
for i, piece := range info.Pieces {
deals[i] = piece.DealID
}
return api.SectorInfo{
SectorID: sid,
State: info.State,
CommD: info.CommD,
CommR: info.CommR,
Proof: info.Proof,
Deals: deals,
Ticket: info.Ticket.SB(),
Seed: info.Seed.SB(),
Retries: info.Nonce,
2019-12-04 00:44:29 +00:00
LastErr: info.LastErr,
2019-11-08 18:15:13 +00:00
}, nil
}
// List all staged sectors
func (sm *StorageMinerAPI) SectorsList(context.Context) ([]uint64, error) {
2019-11-08 18:15:13 +00:00
sectors, err := sm.Miner.ListSectors()
if err != nil {
return nil, err
}
out := make([]uint64, len(sectors))
for i, sector := range sectors {
out[i] = sector.SectorID
}
return out, nil
}
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 {
out[k.String()] = v
}
return out, nil
}
func (sm *StorageMinerAPI) SectorsUpdate(ctx context.Context, id uint64, state api.SectorState) error {
return sm.Miner.UpdateSectorState(ctx, id, storage.NonceIncrement, state)
}
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 {
return sm.SectorBuilder.TaskDone(ctx, task, res)
2019-11-21 00:52:59 +00:00
}
2019-07-24 00:58:31 +00:00
var _ api.StorageMiner = &StorageMinerAPI{}