add RuntimeSubsystems API method; use it in lotus-miner info

This commit is contained in:
Anton Evangelatov 2021-07-28 12:10:17 +03:00 committed by Jennifer Wang
parent 40449f1ccb
commit e90e71456b
10 changed files with 351 additions and 412 deletions

View File

@ -166,8 +166,6 @@ type StorageMiner interface {
MarketPendingDeals(ctx context.Context) (PendingDealInfo, error) //perm:write
MarketPublishPendingDeals(ctx context.Context) error //perm:admin
// RuntimeSubsystems returns the subsystems that are enabled
// in this instance.
RuntimeSubsystems(ctx context.Context) (MinerSubsystems, error) //perm:read
DealsImportData(ctx context.Context, dealPropCid cid.Cid, file string) error //perm:admin

63
api/api_subsystems.go Normal file
View File

@ -0,0 +1,63 @@
package api
import (
"bytes"
"encoding/json"
)
type MinerSubsystems []MinerSubsystem
func (ms MinerSubsystems) Has(entry MinerSubsystem) bool {
for _, v := range ms {
if v == entry {
return true
}
}
return false
}
type MinerSubsystem int
const (
MarketsSubsystem MinerSubsystem = iota
MiningSubsystem
SealingSubsystem
SectorStorageSubsystem
)
func (ms MinerSubsystem) String() string {
return MinerSubsystemToString[ms]
}
var MinerSubsystemToString = map[MinerSubsystem]string{
MarketsSubsystem: "Markets",
MiningSubsystem: "Mining",
SealingSubsystem: "Sealing",
SectorStorageSubsystem: "SectorStorage",
}
var MinerSubsystemToID = map[string]MinerSubsystem{
"Markets": MarketsSubsystem,
"Mining": MiningSubsystem,
"Sealing": SealingSubsystem,
"SectorStorage": SectorStorageSubsystem,
}
func (ms MinerSubsystem) MarshalJSON() ([]byte, error) {
buffer := bytes.NewBufferString(`"`)
buffer.WriteString(MinerSubsystemToString[ms])
buffer.WriteString(`"`)
return buffer.Bytes(), nil
}
func (ms *MinerSubsystem) UnmarshalJSON(b []byte) error {
var j string
err := json.Unmarshal(b, &j)
if err != nil {
return err
}
// TODO: handle zero value
*ms = MinerSubsystemToID[j]
return nil
}

View File

@ -16,10 +16,10 @@ import (
"github.com/google/uuid"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-filestore"
"github.com/libp2p/go-libp2p-core/metrics"
metrics "github.com/libp2p/go-libp2p-core/metrics"
"github.com/libp2p/go-libp2p-core/network"
"github.com/libp2p/go-libp2p-core/peer"
"github.com/libp2p/go-libp2p-core/protocol"
protocol "github.com/libp2p/go-libp2p-core/protocol"
pubsub "github.com/libp2p/go-libp2p-pubsub"
"github.com/multiformats/go-multiaddr"
@ -265,12 +265,6 @@ func init() {
addExample(api.CheckStatusCode(0))
addExample(map[string]interface{}{"abc": 123})
addExample(api.MinerSubsystems{
api.SubsystemMining,
api.SubsystemSealing,
api.SubsystemSectorStorage,
api.SubsystemMarkets,
})
}
func GetAPIType(name, pkg string) (i interface{}, t reflect.Type, permStruct []reflect.Type) {

View File

@ -1,79 +0,0 @@
package api
import (
"encoding/json"
)
// MinerSubsystem represents a miner subsystem. Int and string values are not
// guaranteed to be stable over time is not
// guaranteed to be stable over time.
type MinerSubsystem int
const (
// SubsystemUnknown is a placeholder for the zero value. It should never
// be used.
SubsystemUnknown MinerSubsystem = iota
// SubsystemMarkets signifies the storage and retrieval
// deal-making subsystem.
SubsystemMarkets
// SubsystemMining signifies the mining subsystem.
SubsystemMining
// SubsystemSealing signifies the sealing subsystem.
SubsystemSealing
// SubsystemSectorStorage signifies the sector storage subsystem.
SubsystemSectorStorage
)
var MinerSubsystemToString = map[MinerSubsystem]string{
SubsystemUnknown: "Unknown",
SubsystemMarkets: "Markets",
SubsystemMining: "Mining",
SubsystemSealing: "Sealing",
SubsystemSectorStorage: "SectorStorage",
}
var MinerSubsystemToID = map[string]MinerSubsystem{
"Unknown": SubsystemUnknown,
"Markets": SubsystemMarkets,
"Mining": SubsystemMining,
"Sealing": SubsystemSealing,
"SectorStorage": SubsystemSectorStorage,
}
func (ms MinerSubsystem) MarshalJSON() ([]byte, error) {
return json.Marshal(MinerSubsystemToString[ms])
}
func (ms *MinerSubsystem) UnmarshalJSON(b []byte) error {
var j string
err := json.Unmarshal(b, &j)
if err != nil {
return err
}
s, ok := MinerSubsystemToID[j]
if !ok {
*ms = SubsystemUnknown
} else {
*ms = s
}
return nil
}
type MinerSubsystems []MinerSubsystem
func (ms MinerSubsystems) Has(entry MinerSubsystem) bool {
for _, v := range ms {
if v == entry {
return true
}
}
return false
}
func (ms MinerSubsystem) String() string {
s, ok := MinerSubsystemToString[ms]
if !ok {
return MinerSubsystemToString[SubsystemUnknown]
}
return s
}

Binary file not shown.

View File

@ -21,7 +21,6 @@ import (
"github.com/filecoin-project/go-fil-markets/storagemarket"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/go-state-types/big"
"github.com/filecoin-project/lotus/api/v0api"
sealing "github.com/filecoin-project/lotus/extern/storage-sealing"
"github.com/filecoin-project/lotus/api"
@ -50,13 +49,7 @@ var infoCmd = &cli.Command{
}
func infoCmdAct(cctx *cli.Context) error {
minerApi, closer, err := lcli.GetStorageMinerAPI(cctx)
if err != nil {
return err
}
defer closer()
marketsApi, closer, err := lcli.GetMarketsAPI(cctx)
nodeApi, closer, err := lcli.GetStorageMinerAPI(cctx)
if err != nil {
return err
}
@ -70,19 +63,12 @@ func infoCmdAct(cctx *cli.Context) error {
ctx := lcli.ReqContext(cctx)
subsystems, err := minerApi.RuntimeSubsystems(ctx)
subsystems, err := nodeApi.RuntimeSubsystems(ctx)
if err != nil {
return err
}
fmt.Println("Enabled subsystems (from miner API):", subsystems)
subsystems, err = marketsApi.RuntimeSubsystems(ctx)
if err != nil {
return err
}
fmt.Println("Enabled subsystems (from markets API):", subsystems)
fmt.Println("Enabled subsystems:", subsystems)
fmt.Print("Chain: ")
@ -116,20 +102,7 @@ func infoCmdAct(cctx *cli.Context) error {
fmt.Println()
err = handleMiningInfo(ctx, cctx, fullapi, minerApi)
if err != nil {
return err
}
err = handleMarketsInfo(ctx, marketsApi)
if err != nil {
return err
}
return nil
}
func handleMiningInfo(ctx context.Context, cctx *cli.Context, fullapi v0api.FullNode, nodeApi api.StorageMiner) error {
if subsystems.Has(api.SectorStorageSubsystem) {
maddr, err := getActorAddress(ctx, cctx)
if err != nil {
return err
@ -318,11 +291,9 @@ func handleMiningInfo(ctx context.Context, cctx *cli.Context, fullapi v0api.Full
// TODO: grab actr state / info
// * Sealed sectors (count / bytes)
// * Power
return nil
}
func handleMarketsInfo(ctx context.Context, nodeApi api.StorageMiner) error {
if subsystems.Has(api.MarketsSubsystem) {
deals, err := nodeApi.MarketListIncompleteDeals(ctx)
if err != nil {
return err
@ -386,7 +357,6 @@ func handleMarketsInfo(ctx context.Context, nodeApi api.StorageMiner) error {
return sorted[i].status > sorted[j].status
})
fmt.Println()
fmt.Printf("Storage Deals: %d, %s\n", total.count, types.SizeStr(types.NewInt(total.bytes)))
tw := tabwriter.NewWriter(os.Stdout, 1, 1, 1, ' ', 0)
@ -413,6 +383,7 @@ func handleMarketsInfo(ctx context.Context, nodeApi api.StorageMiner) error {
fmt.Printf("Retrieval Deals (complete): %d, %s\n", retrComplete.count, types.SizeStr(types.NewInt(retrComplete.bytes)))
fmt.Println()
}
return nil
}

View File

@ -1528,23 +1528,13 @@ Response: `{}`
### RuntimeSubsystems
RuntimeSubsystems returns the subsystems that are enabled
in this instance.
Perms: read
Inputs: `null`
Response:
```json
[
"Mining",
"Sealing",
"SectorStorage",
"Markets"
]
```
Response: `null`
## Sealing

View File

@ -72,7 +72,7 @@ func ConfigStorageMiner(c interface{}) Option {
return Options(
ConfigCommon(&cfg.Common, enableLibp2pNode),
Override(new(api.MinerSubsystems), modules.ExtractEnabledMinerSubsystems(cfg.Subsystems)),
Override(new([]api.MinerSubsystem), modules.AddMinerSubsystems(cfg.Subsystems)),
Override(new(stores.LocalStorage), From(new(repo.LockedRepo))),
Override(new(*stores.Local), modules.LocalStorage),
Override(new(*stores.Remote), modules.RemoteStorage),
@ -216,6 +216,7 @@ func StorageMiner(out *api.StorageMiner, subsystemsCfg config.MinerSubsystemConf
func(s *Settings) error {
resAPI := &impl.StorageMinerAPI{}
s.invokes[ExtractApiKey] = fx.Populate(resAPI)
*out = resAPI
return nil

View File

@ -23,8 +23,8 @@ import (
"github.com/filecoin-project/go-address"
datatransfer "github.com/filecoin-project/go-data-transfer"
"github.com/filecoin-project/go-fil-markets/piecestore"
"github.com/filecoin-project/go-fil-markets/retrievalmarket"
"github.com/filecoin-project/go-fil-markets/storagemarket"
retrievalmarket "github.com/filecoin-project/go-fil-markets/retrievalmarket"
storagemarket "github.com/filecoin-project/go-fil-markets/storagemarket"
"github.com/filecoin-project/go-state-types/abi"
sectorstorage "github.com/filecoin-project/lotus/extern/sector-storage"
@ -48,11 +48,11 @@ import (
type StorageMinerAPI struct {
fx.In
Subsystems api.MinerSubsystems
api.Common
api.Net
EnabledSubsystems api.MinerSubsystems
Full api.FullNode
LocalStore *stores.Local
RemoteStore *stores.Remote
@ -706,7 +706,7 @@ func (sm *StorageMinerAPI) ComputeProof(ctx context.Context, ssi []builtin.Secto
}
func (sm *StorageMinerAPI) RuntimeSubsystems(context.Context) (res api.MinerSubsystems, err error) {
return sm.EnabledSubsystems, nil
return sm.Subsystems, nil
}
var _ api.StorageMiner = &StorageMinerAPI{}

View File

@ -1008,18 +1008,19 @@ func mutateCfg(r repo.LockedRepo, mutator func(*config.StorageMiner)) error {
return multierr.Combine(typeErr, setConfigErr)
}
func ExtractEnabledMinerSubsystems(cfg config.MinerSubsystemConfig) (res api.MinerSubsystems) {
func AddMinerSubsystems(cfg config.MinerSubsystemConfig) (res api.MinerSubsystems) {
if cfg.EnableMining {
res = append(res, api.SubsystemMining)
res = append(res, api.MiningSubsystem)
}
if cfg.EnableSealing {
res = append(res, api.SubsystemSealing)
res = append(res, api.SealingSubsystem)
}
if cfg.EnableSectorStorage {
res = append(res, api.SubsystemSectorStorage)
res = append(res, api.SectorStorageSubsystem)
}
if cfg.EnableMarkets {
res = append(res, api.SubsystemMarkets)
res = append(res, api.MarketsSubsystem)
}
return res
return
}