lotus/node/impl/storminer.go

263 lines
7.7 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"
"net/http"
"os"
"strconv"
"github.com/ipfs/go-cid"
"golang.org/x/xerrors"
2020-02-27 21:45:31 +00:00
"github.com/filecoin-project/go-address"
storagemarket "github.com/filecoin-project/go-fil-markets/storagemarket"
2020-05-20 18:23:51 +00:00
"github.com/filecoin-project/go-jsonrpc/auth"
sectorstorage "github.com/filecoin-project/sector-storage"
"github.com/filecoin-project/sector-storage/ffiwrapper"
"github.com/filecoin-project/sector-storage/stores"
2020-04-23 22:23:20 +00:00
"github.com/filecoin-project/sector-storage/storiface"
2020-03-03 22:19:22 +00:00
"github.com/filecoin-project/specs-actors/actors/abi"
sealing "github.com/filecoin-project/storage-fsm"
"github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/api/apistruct"
"github.com/filecoin-project/lotus/chain/types"
2019-11-25 04:45:13 +00:00
"github.com/filecoin-project/lotus/miner"
"github.com/filecoin-project/lotus/node/impl/common"
"github.com/filecoin-project/lotus/node/modules/dtypes"
"github.com/filecoin-project/lotus/storage"
"github.com/filecoin-project/lotus/storage/sectorblocks"
2019-07-24 00:58:31 +00:00
)
type StorageMinerAPI struct {
common.CommonAPI
2019-07-27 00:45:27 +00:00
2020-03-26 19:34:38 +00:00
ProofsConfig *ffiwrapper.Config
2020-03-05 19:21:06 +00:00
SectorBlocks *sectorblocks.SectorBlocks
StorageProvider storagemarket.StorageProvider
Miner *storage.Miner
BlockMiner *miner.Miner
Full api.FullNode
2020-03-23 11:40:02 +00:00
StorageMgr *sectorstorage.Manager `optional:"true"`
*stores.Index
SetAcceptingStorageDealsConfigFunc dtypes.SetAcceptingStorageDealsConfigFunc
SetAcceptingRetrievalDealsConfigFunc dtypes.SetAcceptingRetrievalDealsConfigFunc
2020-06-18 22:42:24 +00:00
StorageDealPieceCidBlocklistConfigFunc dtypes.StorageDealPieceCidBlocklistConfigFunc
SetStorageDealPieceCidBlocklistConfigFunc dtypes.SetStorageDealPieceCidBlocklistConfigFunc
2019-07-24 00:58:31 +00:00
}
func (sm *StorageMinerAPI) ServeRemote(w http.ResponseWriter, r *http.Request) {
2020-05-20 18:23:51 +00:00
if !auth.HasPerm(r.Context(), nil, apistruct.PermAdmin) {
w.WriteHeader(401)
_ = json.NewEncoder(w).Encode(struct{ Error string }{"unauthorized: missing write permission"})
return
}
2020-03-11 05:49:17 +00:00
sm.StorageMgr.ServeHTTP(w, r)
}
2020-03-05 19:21:06 +00:00
2020-04-23 22:23:20 +00:00
func (sm *StorageMinerAPI) WorkerStats(context.Context) (map[uint64]storiface.WorkerStats, error) {
2020-03-23 14:56:22 +00:00
return sm.StorageMgr.WorkerStats(), nil
}
2019-11-08 18:15:13 +00:00
func (sm *StorageMinerAPI) ActorAddress(context.Context) (address.Address, error) {
2020-03-17 20:19:52 +00:00
return sm.Miner.Address(), nil
2019-08-10 01:54:45 +00:00
}
2020-04-23 21:12:42 +00:00
func (sm *StorageMinerAPI) MiningBase(ctx context.Context) (*types.TipSet, error) {
mb, err := sm.BlockMiner.GetBestMiningCandidate(ctx)
if err != nil {
return nil, err
}
return mb.TipSet, nil
}
func (sm *StorageMinerAPI) ActorSectorSize(ctx context.Context, addr address.Address) (abi.SectorSize, error) {
mi, err := sm.Full.StateMinerInfo(ctx, addr, types.EmptyTSK)
if err != nil {
return 0, err
}
return mi.SectorSize, nil
}
func (sm *StorageMinerAPI) PledgeSector(ctx context.Context) error {
return sm.Miner.PledgeSector()
2019-07-27 01:54:03 +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
}
deals := make([]abi.DealID, len(info.Pieces))
2019-11-08 18:15:13 +00:00
for i, piece := range info.Pieces {
if piece.DealInfo == nil {
2020-02-23 00:47:47 +00:00
continue
}
deals[i] = piece.DealInfo.DealID
2019-11-08 18:15:13 +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: api.SectorState(info.State),
2019-11-08 18:15:13 +00:00
CommD: info.CommD,
CommR: info.CommR,
Proof: info.Proof,
Deals: deals,
Ticket: api.SealTicket{
Value: info.TicketValue,
Epoch: info.TicketEpoch,
},
Seed: api.SealSeed{
Value: info.SeedValue,
Epoch: info.SeedEpoch,
},
2020-06-04 13:54:36 +00:00
Retries: info.InvalidProofs,
2019-12-04 00:44:29 +00:00
LastErr: info.LastErr,
Log: log,
2019-11-08 18:15:13 +00:00
}, nil
}
// List all staged sectors
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
}
out := make([]abi.SectorNumber, len(sectors))
2019-11-08 18:15:13 +00:00
for i, sector := range sectors {
2020-04-06 22:31:33 +00:00
out[i] = sector.SectorNumber
2019-11-08 18:15:13 +00:00
}
return out, nil
}
2020-03-19 19:51:33 +00:00
func (sm *StorageMinerAPI) StorageLocal(ctx context.Context) (map[stores.ID]string, error) {
return sm.StorageMgr.StorageLocal(ctx)
}
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[strconv.FormatUint(k, 10)] = v
2019-08-26 10:04:57 +00:00
}
return out, nil
}
func (sm *StorageMinerAPI) StorageStat(ctx context.Context, id stores.ID) (stores.FsStat, error) {
return sm.StorageMgr.FsStat(ctx, id)
}
func (sm *StorageMinerAPI) SectorsUpdate(ctx context.Context, id abi.SectorNumber, state api.SectorState) error {
return sm.Miner.ForceSectorState(ctx, id, sealing.SectorState(state))
}
2020-03-05 19:21:06 +00:00
2020-06-22 17:35:14 +00:00
func (sm *StorageMinerAPI) SectorRemove(ctx context.Context, id abi.SectorNumber) error {
return sm.Miner.RemoveSector(ctx, id)
}
2020-03-11 01:57:52 +00:00
func (sm *StorageMinerAPI) WorkerConnect(ctx context.Context, url string) error {
w, err := connectRemoteWorker(ctx, sm, url)
2020-03-11 01:57:52 +00:00
if err != nil {
2020-03-18 23:23:28 +00:00
return xerrors.Errorf("connecting remote storage failed: %w", err)
2020-03-11 01:57:52 +00:00
}
2020-03-18 23:23:28 +00:00
log.Infof("Connected to a remote worker at %s", url)
2020-03-20 22:30:17 +00:00
return sm.StorageMgr.AddWorker(ctx, w)
2020-03-11 01:57:52 +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() //nolint:errcheck
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.ListLocalDeals()
}
func (sm *StorageMinerAPI) MarketSetAsk(ctx context.Context, price types.BigInt, duration abi.ChainEpoch, minPieceSize abi.PaddedPieceSize, maxPieceSize abi.PaddedPieceSize) error {
options := []storagemarket.StorageAskOption{
storagemarket.MinPieceSize(minPieceSize),
storagemarket.MaxPieceSize(maxPieceSize),
}
return sm.StorageProvider.SetAsk(price, duration, options...)
}
2020-06-17 00:18:54 +00:00
func (sm *StorageMinerAPI) MarketGetAsk(ctx context.Context) (*storagemarket.SignedStorageAsk, error) {
return sm.StorageProvider.GetAsk(), nil
}
func (sm *StorageMinerAPI) DealsList(ctx context.Context) ([]storagemarket.StorageDeal, error) {
return sm.StorageProvider.ListDeals(ctx)
}
2020-06-11 20:18:18 +00:00
func (sm *StorageMinerAPI) DealsSetAcceptingStorageDeals(ctx context.Context, b bool) error {
return sm.SetAcceptingStorageDealsConfigFunc(b)
}
func (sm *StorageMinerAPI) DealsSetAcceptingRetrievalDeals(ctx context.Context, b bool) error {
return sm.SetAcceptingRetrievalDealsConfigFunc(b)
}
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() //nolint:errcheck
return sm.StorageProvider.ImportDataForDeal(ctx, deal, fi)
}
2020-06-18 22:42:24 +00:00
func (sm *StorageMinerAPI) DealsPieceCidBlocklist(ctx context.Context) ([]cid.Cid, error) {
return sm.StorageDealPieceCidBlocklistConfigFunc()
}
2020-06-18 22:42:24 +00:00
func (sm *StorageMinerAPI) DealsSetPieceCidBlocklist(ctx context.Context, cids []cid.Cid) error {
return sm.SetStorageDealPieceCidBlocklistConfigFunc(cids)
}
func (sm *StorageMinerAPI) StorageAddLocal(ctx context.Context, path string) error {
if sm.StorageMgr == nil {
return xerrors.Errorf("no storage manager")
}
2020-03-19 15:10:19 +00:00
return sm.StorageMgr.AddLocalStorage(ctx, path)
}
2019-07-24 00:58:31 +00:00
var _ api.StorageMiner = &StorageMinerAPI{}