2021-05-20 11:10:14 +00:00
|
|
|
package modules
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2024-02-01 19:44:29 +00:00
|
|
|
"strings"
|
2021-05-20 11:10:14 +00:00
|
|
|
|
|
|
|
"go.uber.org/fx"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2024-02-01 19:44:29 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
|
|
|
|
2021-05-20 11:10:14 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
|
|
"github.com/filecoin-project/lotus/api/client"
|
2024-02-01 19:44:29 +00:00
|
|
|
"github.com/filecoin-project/lotus/api/v1api"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2021-05-20 11:10:14 +00:00
|
|
|
cliutil "github.com/filecoin-project/lotus/cli/util"
|
2024-03-15 21:38:13 +00:00
|
|
|
"github.com/filecoin-project/lotus/curiosrc/market"
|
|
|
|
"github.com/filecoin-project/lotus/curiosrc/market/fakelm"
|
2024-02-01 19:44:29 +00:00
|
|
|
"github.com/filecoin-project/lotus/lib/harmony/harmonydb"
|
|
|
|
"github.com/filecoin-project/lotus/node/config"
|
2021-05-20 11:10:14 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/modules/helpers"
|
2024-02-01 19:44:29 +00:00
|
|
|
"github.com/filecoin-project/lotus/storage/paths"
|
2024-02-11 13:08:54 +00:00
|
|
|
"github.com/filecoin-project/lotus/storage/sealer/storiface"
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/filecoin-project/lotus/storage/sectorblocks"
|
2021-05-20 11:10:14 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type MinerSealingService api.StorageMiner
|
|
|
|
type MinerStorageService api.StorageMiner
|
|
|
|
|
|
|
|
var _ sectorblocks.SectorBuilder = *new(MinerSealingService)
|
|
|
|
|
2024-02-02 17:08:54 +00:00
|
|
|
func harmonyApiInfoToConf(apiInfo string) (config.HarmonyDB, error) {
|
2024-02-01 19:44:29 +00:00
|
|
|
hc := config.HarmonyDB{}
|
|
|
|
|
2024-02-02 17:21:50 +00:00
|
|
|
// apiInfo - harmony:layer:maddr:user:pass:dbname:host:port
|
2024-02-01 19:44:29 +00:00
|
|
|
|
|
|
|
parts := strings.Split(apiInfo, ":")
|
|
|
|
|
2024-02-02 17:21:50 +00:00
|
|
|
if len(parts) != 8 {
|
2024-02-02 17:08:54 +00:00
|
|
|
return config.HarmonyDB{}, xerrors.Errorf("invalid harmonydb info '%s'", apiInfo)
|
2024-02-01 19:44:29 +00:00
|
|
|
}
|
|
|
|
|
2024-02-02 17:21:50 +00:00
|
|
|
hc.Username = parts[3]
|
|
|
|
hc.Password = parts[4]
|
|
|
|
hc.Database = parts[5]
|
|
|
|
hc.Hosts = []string{parts[6]}
|
|
|
|
hc.Port = parts[7]
|
2024-02-01 19:44:29 +00:00
|
|
|
|
2024-02-02 17:08:54 +00:00
|
|
|
return hc, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func connectHarmony(apiInfo string, fapi v1api.FullNode, mctx helpers.MetricsCtx, lc fx.Lifecycle) (api.StorageMiner, error) {
|
|
|
|
log.Info("Connecting to harmonydb")
|
|
|
|
|
|
|
|
hc, err := harmonyApiInfoToConf(apiInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2024-02-01 19:44:29 +00:00
|
|
|
db, err := harmonydb.NewFromConfig(hc)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("connecting to harmonydb: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-02-02 17:08:54 +00:00
|
|
|
parts := strings.Split(apiInfo, ":")
|
2024-02-02 17:21:50 +00:00
|
|
|
maddr, err := address.NewFromString(parts[2])
|
2024-02-02 17:08:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("parsing miner address: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-03-15 21:38:13 +00:00
|
|
|
pin := market.NewPieceIngester(db, fapi)
|
2024-02-01 19:44:29 +00:00
|
|
|
|
|
|
|
si := paths.NewDBIndex(nil, db)
|
|
|
|
|
|
|
|
mid, err := address.IDFromAddress(maddr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("getting miner id: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
mi, err := fapi.StateMinerInfo(mctx, maddr, types.EmptyTSK)
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("getting miner info: %w", err)
|
|
|
|
}
|
|
|
|
|
2024-02-11 12:40:25 +00:00
|
|
|
lp := fakelm.NewLMRPCProvider(si, fapi, maddr, abi.ActorID(mid), mi.SectorSize, pin, db, parts[1])
|
2024-02-01 19:44:29 +00:00
|
|
|
|
|
|
|
ast := api.StorageMinerStruct{}
|
|
|
|
|
2024-02-02 17:21:50 +00:00
|
|
|
ast.CommonStruct.Internal.AuthNew = lp.AuthNew
|
|
|
|
|
2024-02-01 19:44:29 +00:00
|
|
|
ast.Internal.ActorAddress = lp.ActorAddress
|
|
|
|
ast.Internal.WorkerJobs = lp.WorkerJobs
|
|
|
|
ast.Internal.SectorsStatus = lp.SectorsStatus
|
|
|
|
ast.Internal.SectorsList = lp.SectorsList
|
|
|
|
ast.Internal.SectorsSummary = lp.SectorsSummary
|
|
|
|
ast.Internal.SectorsListInStates = lp.SectorsListInStates
|
|
|
|
ast.Internal.StorageRedeclareLocal = lp.StorageRedeclareLocal
|
|
|
|
ast.Internal.ComputeDataCid = lp.ComputeDataCid
|
2024-02-02 14:38:25 +00:00
|
|
|
ast.Internal.SectorAddPieceToAny = func(p0 context.Context, p1 abi.UnpaddedPieceSize, p2 storiface.Data, p3 api.PieceDealInfo) (api.SectorOffset, error) {
|
|
|
|
panic("implement me")
|
|
|
|
}
|
2024-02-01 19:44:29 +00:00
|
|
|
|
|
|
|
ast.Internal.StorageList = si.StorageList
|
|
|
|
ast.Internal.StorageDetach = si.StorageDetach
|
|
|
|
ast.Internal.StorageReportHealth = si.StorageReportHealth
|
|
|
|
ast.Internal.StorageDeclareSector = si.StorageDeclareSector
|
|
|
|
ast.Internal.StorageDropSector = si.StorageDropSector
|
|
|
|
ast.Internal.StorageFindSector = si.StorageFindSector
|
|
|
|
ast.Internal.StorageInfo = si.StorageInfo
|
|
|
|
ast.Internal.StorageBestAlloc = si.StorageBestAlloc
|
|
|
|
ast.Internal.StorageLock = si.StorageLock
|
|
|
|
ast.Internal.StorageTryLock = si.StorageTryLock
|
|
|
|
ast.Internal.StorageGetLocks = si.StorageGetLocks
|
|
|
|
|
|
|
|
return &ast, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func connectMinerService(apiInfo string) func(mctx helpers.MetricsCtx, lc fx.Lifecycle, fapi v1api.FullNode) (api.StorageMiner, error) {
|
|
|
|
return func(mctx helpers.MetricsCtx, lc fx.Lifecycle, fapi v1api.FullNode) (api.StorageMiner, error) {
|
2024-02-02 14:38:25 +00:00
|
|
|
if strings.HasPrefix(apiInfo, "harmony:") {
|
2024-02-01 19:44:29 +00:00
|
|
|
return connectHarmony(apiInfo, fapi, mctx, lc)
|
|
|
|
}
|
|
|
|
|
2021-05-20 11:10:14 +00:00
|
|
|
ctx := helpers.LifecycleCtx(mctx, lc)
|
|
|
|
info := cliutil.ParseApiInfo(apiInfo)
|
|
|
|
addr, err := info.DialArgs("v0")
|
|
|
|
if err != nil {
|
|
|
|
return nil, xerrors.Errorf("could not get DialArgs: %w", err)
|
|
|
|
}
|
|
|
|
|
2021-05-26 10:47:21 +00:00
|
|
|
log.Infof("Checking (svc) api version of %s", addr)
|
2021-05-20 11:10:14 +00:00
|
|
|
|
|
|
|
mapi, closer, err := client.NewStorageMinerRPCV0(ctx, addr, info.AuthHeader())
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
lc.Append(fx.Hook{
|
|
|
|
OnStart: func(ctx context.Context) error {
|
|
|
|
v, err := mapi.Version(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("checking version: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if !v.APIVersion.EqMajorMinor(api.MinerAPIVersion0) {
|
|
|
|
return xerrors.Errorf("remote service API version didn't match (expected %s, remote %s)", api.MinerAPIVersion0, v.APIVersion)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
OnStop: func(context.Context) error {
|
|
|
|
closer()
|
|
|
|
return nil
|
|
|
|
}})
|
|
|
|
|
|
|
|
return mapi, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-01 19:44:29 +00:00
|
|
|
func ConnectSealingService(apiInfo string) func(mctx helpers.MetricsCtx, lc fx.Lifecycle, fapi v1api.FullNode) (MinerSealingService, error) {
|
|
|
|
return func(mctx helpers.MetricsCtx, lc fx.Lifecycle, fapi v1api.FullNode) (MinerSealingService, error) {
|
2021-05-26 10:47:21 +00:00
|
|
|
log.Info("Connecting sealing service to miner")
|
2024-02-01 19:44:29 +00:00
|
|
|
return connectMinerService(apiInfo)(mctx, lc, fapi)
|
2021-05-20 11:10:14 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-01 19:44:29 +00:00
|
|
|
func ConnectStorageService(apiInfo string) func(mctx helpers.MetricsCtx, lc fx.Lifecycle, fapi v1api.FullNode) (MinerStorageService, error) {
|
|
|
|
return func(mctx helpers.MetricsCtx, lc fx.Lifecycle, fapi v1api.FullNode) (MinerStorageService, error) {
|
2021-05-26 10:47:21 +00:00
|
|
|
log.Info("Connecting storage service to miner")
|
2024-02-01 19:44:29 +00:00
|
|
|
return connectMinerService(apiInfo)(mctx, lc, fapi)
|
2021-05-20 11:10:14 +00:00
|
|
|
}
|
|
|
|
}
|