add commands for manipulating storage deal CID blacklist

This commit is contained in:
laser 2020-06-18 13:15:18 -07:00
parent 997a46a90f
commit b0edf924b4
8 changed files with 225 additions and 25 deletions

View File

@ -56,6 +56,8 @@ type StorageMiner interface {
DealsImportData(ctx context.Context, dealPropCid cid.Cid, file string) error
DealsList(ctx context.Context) ([]storagemarket.StorageDeal, error)
DealsSetAcceptingStorageDeals(context.Context, bool) error
DealsBlacklist(context.Context) ([]cid.Cid, error)
DealsSetBlacklist(context.Context, []cid.Cid) error
StorageAddLocal(ctx context.Context, path string) error
}

View File

@ -226,6 +226,8 @@ type StorageMinerStruct struct {
DealsImportData func(ctx context.Context, dealPropCid cid.Cid, file string) error `perm:"write"`
DealsList func(ctx context.Context) ([]storagemarket.StorageDeal, error) `perm:"read"`
DealsSetAcceptingStorageDeals func(context.Context, bool) error `perm:"admin"`
DealsBlacklist func(context.Context) ([]cid.Cid, error) `perm:"admin"`
DealsSetBlacklist func(context.Context, []cid.Cid) error `perm:"read"`
StorageAddLocal func(ctx context.Context, path string) error `perm:"admin"`
}
@ -872,6 +874,14 @@ func (c *StorageMinerStruct) DealsSetAcceptingStorageDeals(ctx context.Context,
return c.Internal.DealsSetAcceptingStorageDeals(ctx, b)
}
func (c *StorageMinerStruct) DealsBlacklist(ctx context.Context) ([]cid.Cid, error) {
return c.Internal.DealsBlacklist(ctx)
}
func (c *StorageMinerStruct) DealsSetBlacklist(ctx context.Context, cids []cid.Cid) error {
return c.Internal.DealsSetBlacklist(ctx, cids)
}
func (c *StorageMinerStruct) StorageAddLocal(ctx context.Context, path string) error {
return c.Internal.StorageAddLocal(ctx, path)
}

View File

@ -1,14 +1,18 @@
package main
import (
"bufio"
"encoding/json"
"fmt"
"os"
"path/filepath"
"text/tabwriter"
"time"
"github.com/docker/go-units"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-cidutil/cidenc"
"github.com/multiformats/go-multibase"
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"
@ -20,6 +24,32 @@ import (
lcli "github.com/filecoin-project/lotus/cli"
)
var CidBaseFlag = cli.StringFlag{
Name: "cid-base",
Hidden: true,
Value: "base32",
Usage: "Multibase encoding used for version 1 CIDs in output.",
DefaultText: "base32",
}
// GetCidEncoder returns an encoder using the `cid-base` flag if provided, or
// the default (Base32) encoder if not.
func GetCidEncoder(cctx *cli.Context) (cidenc.Encoder, error) {
val := cctx.String("cid-base")
e := cidenc.Encoder{Base: multibase.MustNewEncoder(multibase.Base32)}
if val != "" {
var err error
e.Base, err = multibase.EncoderByName(val)
if err != nil {
return e, err
}
}
return e, nil
}
var enableCmd = &cli.Command{
Name: "enable",
Usage: "Configure the miner to consider storage deal proposals",
@ -199,6 +229,9 @@ var dealsCmd = &cli.Command{
disableCmd,
setAskCmd,
getAskCmd,
setBlacklistCmd,
getBlacklistCmd,
resetBlacklistCmd,
},
}
@ -257,3 +290,96 @@ var dealsListCmd = &cli.Command{
return nil
},
}
var getBlacklistCmd = &cli.Command{
Name: "get-blacklist",
Usage: "List the CIDs in the storage miner's blacklist",
Flags: []cli.Flag{
&CidBaseFlag,
},
Action: func(cctx *cli.Context) error {
api, closer, err := lcli.GetStorageMinerAPI(cctx)
if err != nil {
return err
}
defer closer()
blacklist, err := api.DealsBlacklist(lcli.DaemonContext(cctx))
if err != nil {
return err
}
encoder, err := GetCidEncoder(cctx)
if err != nil {
return err
}
for idx := range blacklist {
fmt.Println(encoder.Encode(blacklist[idx]))
}
return nil
},
}
var setBlacklistCmd = &cli.Command{
Name: "set-blacklist",
Usage: "Set the storage miner's list of blacklisted CIDs",
ArgsUsage: "[<path-of-file-containing-newline-delimited-CIDs> (optional, will read from stdin if omitted)]",
Flags: []cli.Flag{},
Action: func(cctx *cli.Context) error {
api, closer, err := lcli.GetStorageMinerAPI(cctx)
if err != nil {
return err
}
defer closer()
scanner := bufio.NewScanner(os.Stdin)
if cctx.Args().Present() && cctx.Args().First() != "-" {
absPath, err := filepath.Abs(cctx.Args().First())
if err != nil {
return err
}
file, err := os.Open(absPath)
if err != nil {
log.Fatal(err)
}
defer file.Close()
scanner = bufio.NewScanner(file)
}
var blacklist []cid.Cid
for scanner.Scan() {
decoded, err := cid.Decode(scanner.Text())
if err != nil {
return err
}
blacklist = append(blacklist, decoded)
}
err = scanner.Err()
if err != nil {
return err
}
return api.DealsSetBlacklist(lcli.DaemonContext(cctx), blacklist)
},
}
var resetBlacklistCmd = &cli.Command{
Name: "reset-blacklist",
Usage: "Remove all entries from the storage miner's blacklist",
Flags: []cli.Flag{},
Action: func(cctx *cli.Context) error {
api, closer, err := lcli.GetStorageMinerAPI(cctx)
if err != nil {
return err
}
defer closer()
return api.DealsSetBlacklist(lcli.DaemonContext(cctx), []cid.Cid{})
},
}

View File

@ -314,6 +314,8 @@ func Online() Option {
Override(new(dtypes.AcceptingStorageDealsConfigFunc), modules.NewAcceptingStorageDealsConfigFunc),
Override(new(dtypes.SetAcceptingStorageDealsConfigFunc), modules.NewSetAcceptingStorageDealsConfigFunc),
Override(new(dtypes.StorageDealCidBlacklistConfigFunc), modules.NewStorageDealCidBlacklistConfigFunc),
Override(new(dtypes.SetStorageDealCidBlacklistConfigFunc), modules.NewSetStorageDealCidBlacklistConfigFunc),
),
)
}

View File

@ -4,6 +4,8 @@ import (
"encoding"
"time"
"github.com/ipfs/go-cid"
sectorstorage "github.com/filecoin-project/sector-storage"
)
@ -33,6 +35,7 @@ type StorageMiner struct {
type DealmakingConfig struct {
AcceptingStorageDeals bool
Blacklist []cid.Cid
}
// API contains configs for API endpoint
@ -121,6 +124,7 @@ func DefaultStorageMiner() *StorageMiner {
Dealmaking: DealmakingConfig{
AcceptingStorageDeals: true,
Blacklist: []cid.Cid{},
},
}
cfg.Common.API.ListenAddress = "/ip4/127.0.0.1/tcp/2345/http"

View File

@ -44,6 +44,8 @@ type StorageMinerAPI struct {
*stores.Index
SetAcceptingStorageDealsConfigFunc dtypes.SetAcceptingStorageDealsConfigFunc
StorageDealCidBlacklistConfigFunc dtypes.StorageDealCidBlacklistConfigFunc
SetStorageDealCidBlacklistConfigFunc dtypes.SetStorageDealCidBlacklistConfigFunc
}
func (sm *StorageMinerAPI) ServeRemote(w http.ResponseWriter, r *http.Request) {
@ -232,6 +234,14 @@ func (sm *StorageMinerAPI) DealsImportData(ctx context.Context, deal cid.Cid, fn
return sm.StorageProvider.ImportDataForDeal(ctx, deal, fi)
}
func (sm *StorageMinerAPI) DealsBlacklist(ctx context.Context) ([]cid.Cid, error) {
return sm.StorageDealCidBlacklistConfigFunc()
}
func (sm *StorageMinerAPI) DealsSetBlacklist(ctx context.Context, cids []cid.Cid) error {
return sm.SetStorageDealCidBlacklistConfigFunc(cids)
}
func (sm *StorageMinerAPI) StorageAddLocal(ctx context.Context, path string) error {
if sm.StorageMgr == nil {
return xerrors.Errorf("no storage manager")

View File

@ -1,6 +1,8 @@
package dtypes
import (
"github.com/ipfs/go-cid"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/specs-actors/actors/abi"
)
@ -15,3 +17,12 @@ type AcceptingStorageDealsConfigFunc func() (bool, error)
// SetAcceptingStorageDealsFunc is a function which is used to disable or enable
// storage deal acceptance.
type SetAcceptingStorageDealsConfigFunc func(bool) error
// StorageDealCidBlacklistConfigFunc is a function which reads from miner config
// to obtain a list of CIDs for which the storage miner will not accept storage
// proposals.
type StorageDealCidBlacklistConfigFunc func() ([]cid.Cid, error)
// SetStorageDealCidBlacklistConfigFunc is a function which is used to set a
// list of CIDs for which the storage miner will reject deal proposals.
type SetStorageDealCidBlacklistConfigFunc func([]cid.Cid) error

View File

@ -8,6 +8,7 @@ import (
"github.com/ipfs/go-bitswap"
"github.com/ipfs/go-bitswap/network"
"github.com/ipfs/go-blockservice"
"github.com/ipfs/go-cid"
"github.com/ipfs/go-datastore"
"github.com/ipfs/go-datastore/namespace"
graphsync "github.com/ipfs/go-graphsync/impl"
@ -380,23 +381,58 @@ func StorageAuth(ctx helpers.MetricsCtx, ca lapi.Common) (sectorstorage.StorageA
}
func NewAcceptingStorageDealsConfigFunc(r repo.LockedRepo) (dtypes.AcceptingStorageDealsConfigFunc, error) {
return func() (bool, error) {
raw, err := r.Config()
if err != nil {
return false, err
}
cfg, ok := raw.(*config.StorageMiner)
if !ok {
return false, xerrors.New("expected address of config.StorageMiner")
}
return cfg.Dealmaking.AcceptingStorageDeals, nil
return func() (out bool, err error) {
err = readCfg(r, func(cfg *config.StorageMiner) {
out = cfg.Dealmaking.AcceptingStorageDeals
})
return
}, nil
}
func NewSetAcceptingStorageDealsConfigFunc(r repo.LockedRepo) (dtypes.SetAcceptingStorageDealsConfigFunc, error) {
return func(b bool) error {
return func(b bool) (err error) {
err = mutateCfg(r, func(cfg *config.StorageMiner) {
cfg.Dealmaking.AcceptingStorageDeals = b
})
return
}, nil
}
func NewStorageDealCidBlacklistConfigFunc(r repo.LockedRepo) (dtypes.StorageDealCidBlacklistConfigFunc, error) {
return func() (out []cid.Cid, err error) {
err = readCfg(r, func(cfg *config.StorageMiner) {
out = cfg.Dealmaking.Blacklist
})
return
}, nil
}
func NewSetStorageDealCidBlacklistConfigFunc(r repo.LockedRepo) (dtypes.SetStorageDealCidBlacklistConfigFunc, error) {
return func(blacklist []cid.Cid) (err error) {
err = mutateCfg(r, func(cfg *config.StorageMiner) {
cfg.Dealmaking.Blacklist = blacklist
})
return
}, nil
}
func readCfg(r repo.LockedRepo, accessor func(*config.StorageMiner)) error {
raw, err := r.Config()
if err != nil {
return err
}
cfg, ok := raw.(*config.StorageMiner)
if !ok {
return xerrors.New("expected address of config.StorageMiner")
}
accessor(cfg)
return nil
}
func mutateCfg(r repo.LockedRepo, mutator func(*config.StorageMiner)) error {
var typeErr error
setConfigErr := r.SetConfig(func(raw interface{}) {
@ -406,9 +442,8 @@ func NewSetAcceptingStorageDealsConfigFunc(r repo.LockedRepo) (dtypes.SetAccepti
return
}
cfg.Dealmaking.AcceptingStorageDeals = b
mutator(cfg)
})
return multierr.Combine(typeErr, setConfigErr)
}, nil
}