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"
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"io"
|
|
|
|
"net/http"
|
2019-07-27 01:54:03 +00:00
|
|
|
|
2019-10-18 04:47:41 +00:00
|
|
|
"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/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
|
2019-08-10 01:54:45 +00:00
|
|
|
SectorBuilder *sectorbuilder.SectorBuilder
|
2019-08-26 10:04:57 +00:00
|
|
|
SectorBlocks *sectorblocks.SectorBlocks
|
2019-07-30 00:46:56 +00:00
|
|
|
|
|
|
|
Miner *storage.Miner
|
2019-11-02 14:13:21 +00:00
|
|
|
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) {
|
|
|
|
if !api.HasPerm(r.Context(), api.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")
|
|
|
|
|
|
|
|
mux.ServeHTTP(w, r)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sm *StorageMinerAPI) remoteGetSector(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
|
|
|
|
fr, err := sm.SectorBuilder.OpenRemoteRead(vars["type"], vars["sname"])
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
w.WriteHeader(500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer fr.Close()
|
|
|
|
|
|
|
|
w.WriteHeader(200)
|
|
|
|
if _, err := io.Copy(w, fr); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sm *StorageMinerAPI) remotePutSector(w http.ResponseWriter, r *http.Request) {
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
|
|
|
|
fr, err := sm.SectorBuilder.OpenRemoteWrite(vars["type"], vars["sname"])
|
|
|
|
if err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
w.WriteHeader(500)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
defer fr.Close()
|
|
|
|
|
|
|
|
w.WriteHeader(200)
|
|
|
|
if _, err := io.Copy(w, fr); err != nil {
|
|
|
|
log.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-11-08 18:15:13 +00:00
|
|
|
func (sm *StorageMinerAPI) WorkerStats(context.Context) (api.WorkerStats, error) {
|
|
|
|
free, reserved, total := sm.SectorBuilder.WorkerStats()
|
|
|
|
return api.WorkerStats{
|
|
|
|
Free: free,
|
|
|
|
Reserved: reserved,
|
|
|
|
Total: total,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-11-21 14:10:51 +00:00
|
|
|
func (sm *StorageMinerAPI) ActorSectorSize(ctx context.Context, addr address.Address) (uint64, error) {
|
|
|
|
return sm.Full.StateMinerSectorSize(ctx, addr, nil)
|
|
|
|
}
|
|
|
|
|
2019-10-29 17:52:07 +00:00
|
|
|
func (sm *StorageMinerAPI) StoreGarbageData(ctx context.Context) error {
|
2019-11-08 23:06:07 +00:00
|
|
|
return sm.Miner.StoreGarbageData()
|
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(),
|
|
|
|
}, nil
|
2019-07-27 21:08:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// List all staged sectors
|
2019-10-14 02:32:32 +00:00
|
|
|
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-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 {
|
|
|
|
out[k.String()] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return out, nil
|
|
|
|
}
|
|
|
|
|
2019-11-21 00:52:59 +00:00
|
|
|
func (sm *StorageMinerAPI) WorkerQueue(ctx context.Context) (<-chan sectorbuilder.WorkerTask, error) {
|
|
|
|
return sm.SectorBuilder.AddWorker(ctx)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2019-07-24 00:58:31 +00:00
|
|
|
var _ api.StorageMiner = &StorageMinerAPI{}
|