2020-02-08 00:18:14 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-06-18 20:15:18 +00:00
|
|
|
"bufio"
|
2020-03-04 02:49:00 +00:00
|
|
|
"encoding/json"
|
2020-02-08 00:18:14 +00:00
|
|
|
"fmt"
|
2020-06-17 00:18:54 +00:00
|
|
|
"os"
|
2020-06-18 20:15:18 +00:00
|
|
|
"path/filepath"
|
2020-06-17 00:18:54 +00:00
|
|
|
"text/tabwriter"
|
2020-06-17 16:20:43 +00:00
|
|
|
"time"
|
2020-02-08 00:18:14 +00:00
|
|
|
|
2020-06-16 22:38:48 +00:00
|
|
|
"github.com/docker/go-units"
|
2020-03-04 02:49:00 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2020-06-18 20:15:18 +00:00
|
|
|
"github.com/ipfs/go-cidutil/cidenc"
|
|
|
|
"github.com/multiformats/go-multibase"
|
2020-06-02 18:12:53 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2020-06-17 15:42:30 +00:00
|
|
|
"golang.org/x/xerrors"
|
2020-06-16 22:38:48 +00:00
|
|
|
|
2020-06-17 00:18:54 +00:00
|
|
|
"github.com/filecoin-project/go-fil-markets/storagemarket"
|
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
|
|
|
|
2020-06-17 16:20:43 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
2020-06-16 22:38:48 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
lcli "github.com/filecoin-project/lotus/cli"
|
2020-02-08 00:18:14 +00:00
|
|
|
)
|
|
|
|
|
2020-06-18 20:15:18 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2020-06-26 17:33:06 +00:00
|
|
|
var storageDealSelectionCmd = &cli.Command{
|
|
|
|
Name: "selection",
|
|
|
|
Usage: "Configure acceptance criteria for storage deal proposals",
|
|
|
|
Subcommands: []*cli.Command{
|
|
|
|
storageDealSelectionShowCmd,
|
|
|
|
storageDealSelectionResetCmd,
|
|
|
|
storageDealSelectionRejectCmd,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var storageDealSelectionShowCmd = &cli.Command{
|
|
|
|
Name: "list",
|
|
|
|
Usage: "List storage deal proposal selection criteria",
|
2020-06-11 19:20:11 +00:00
|
|
|
Action: func(cctx *cli.Context) error {
|
2020-06-26 17:33:06 +00:00
|
|
|
smapi, closer, err := lcli.GetStorageMinerAPI(cctx)
|
2020-06-11 19:59:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
|
2020-06-26 17:33:06 +00:00
|
|
|
isAcceptingStorageDeals, err := smapi.DealsAcceptingStorageDeals(lcli.DaemonContext(cctx))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Printf("offline storage deals: %t\n", isAcceptingStorageDeals)
|
|
|
|
fmt.Printf("online storage deals: %t\n", isAcceptingStorageDeals)
|
|
|
|
|
|
|
|
return nil
|
2020-06-11 19:20:11 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-06-26 17:33:06 +00:00
|
|
|
var storageDealSelectionResetCmd = &cli.Command{
|
|
|
|
Name: "reset",
|
|
|
|
Usage: "Reset storage deal proposal selection criteria to default values",
|
2020-06-11 19:20:11 +00:00
|
|
|
Action: func(cctx *cli.Context) error {
|
2020-06-26 17:33:06 +00:00
|
|
|
smapi, closer, err := lcli.GetStorageMinerAPI(cctx)
|
2020-06-11 19:59:50 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
|
2020-06-26 17:33:06 +00:00
|
|
|
err = smapi.DealsSetAcceptingStorageDeals(lcli.DaemonContext(cctx), true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var storageDealSelectionRejectCmd = &cli.Command{
|
|
|
|
Name: "reject",
|
|
|
|
Usage: "Configure criteria which necessitate automatic rejection",
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
smapi, closer, err := lcli.GetStorageMinerAPI(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
err = smapi.DealsSetAcceptingStorageDeals(lcli.DaemonContext(cctx), false)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2020-06-11 19:20:11 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-06-16 22:38:48 +00:00
|
|
|
var setAskCmd = &cli.Command{
|
|
|
|
Name: "set-ask",
|
|
|
|
Usage: "Configure the miner's ask",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.Uint64Flag{
|
|
|
|
Name: "price",
|
|
|
|
Usage: "Set the price of the ask (specified as FIL / GiB / Epoch) to `PRICE`",
|
|
|
|
Required: true,
|
|
|
|
},
|
2020-06-17 16:20:43 +00:00
|
|
|
&cli.StringFlag{
|
2020-06-16 22:38:48 +00:00
|
|
|
Name: "duration",
|
2020-06-17 16:20:43 +00:00
|
|
|
Usage: "Set duration of ask (a quantity of time after which the ask expires) `DURATION`",
|
2020-06-17 17:20:42 +00:00
|
|
|
DefaultText: "720h0m0s",
|
|
|
|
Value: "720h0m0s",
|
2020-06-16 22:38:48 +00:00
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "min-piece-size",
|
2020-06-17 15:42:30 +00:00
|
|
|
Usage: "Set minimum piece size (w/bit-padding, in bytes) in ask to `SIZE`",
|
|
|
|
DefaultText: "256B",
|
|
|
|
Value: "256B",
|
2020-06-16 22:38:48 +00:00
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "max-piece-size",
|
2020-06-17 15:42:30 +00:00
|
|
|
Usage: "Set maximum piece size (w/bit-padding, in bytes) in ask to `SIZE`",
|
|
|
|
DefaultText: "miner sector size",
|
2020-06-16 22:38:48 +00:00
|
|
|
},
|
|
|
|
},
|
2020-02-08 00:18:14 +00:00
|
|
|
Action: func(cctx *cli.Context) error {
|
2020-06-16 22:38:48 +00:00
|
|
|
ctx := lcli.DaemonContext(cctx)
|
|
|
|
|
2020-02-08 00:18:14 +00:00
|
|
|
api, closer, err := lcli.GetStorageMinerAPI(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
|
2020-06-16 22:38:48 +00:00
|
|
|
pri := types.NewInt(cctx.Uint64("price"))
|
2020-06-17 16:20:43 +00:00
|
|
|
|
|
|
|
dur, err := time.ParseDuration(cctx.String("duration"))
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("cannot parse duration: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
qty := dur.Seconds() / build.BlockDelay
|
2020-02-08 00:18:14 +00:00
|
|
|
|
2020-06-16 22:38:48 +00:00
|
|
|
min, err := units.RAMInBytes(cctx.String("min-piece-size"))
|
|
|
|
if err != nil {
|
2020-06-17 16:20:43 +00:00
|
|
|
return xerrors.Errorf("cannot parse min-piece-size to quantity of bytes: %w", err)
|
2020-02-08 00:18:14 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 15:42:30 +00:00
|
|
|
if min < 256 {
|
|
|
|
return xerrors.New("minimum piece size (w/bit-padding) is 256B")
|
2020-06-17 00:45:11 +00:00
|
|
|
}
|
|
|
|
|
2020-06-16 22:38:48 +00:00
|
|
|
max, err := units.RAMInBytes(cctx.String("max-piece-size"))
|
2020-02-08 00:18:14 +00:00
|
|
|
if err != nil {
|
2020-06-17 16:20:43 +00:00
|
|
|
return xerrors.Errorf("cannot parse max-piece-size to quantity of bytes: %w", err)
|
2020-02-08 00:18:14 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 00:45:11 +00:00
|
|
|
maddr, err := api.ActorAddress(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-16 22:38:48 +00:00
|
|
|
|
2020-06-17 00:45:11 +00:00
|
|
|
ssize, err := api.ActorSectorSize(ctx, maddr)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-17 15:42:30 +00:00
|
|
|
smax := int64(ssize)
|
2020-06-17 00:45:11 +00:00
|
|
|
|
|
|
|
if max == 0 {
|
|
|
|
max = smax
|
|
|
|
}
|
2020-06-16 22:38:48 +00:00
|
|
|
|
2020-06-17 00:45:11 +00:00
|
|
|
if max > smax {
|
2020-06-17 15:57:18 +00:00
|
|
|
return xerrors.Errorf("max piece size (w/bit-padding) %s cannot exceed miner sector size %s", types.SizeStr(types.NewInt(uint64(max))), types.SizeStr(types.NewInt(uint64(smax))))
|
2020-06-16 22:38:48 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 16:20:43 +00:00
|
|
|
return api.MarketSetAsk(ctx, pri, abi.ChainEpoch(qty), abi.PaddedPieceSize(min), abi.PaddedPieceSize(max))
|
2020-02-08 00:18:14 +00:00
|
|
|
},
|
|
|
|
}
|
2020-03-04 02:49:00 +00:00
|
|
|
|
2020-06-17 00:18:54 +00:00
|
|
|
var getAskCmd = &cli.Command{
|
|
|
|
Name: "get-ask",
|
|
|
|
Usage: "Print the miner's ask",
|
|
|
|
Flags: []cli.Flag{},
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
ctx := lcli.DaemonContext(cctx)
|
|
|
|
|
2020-06-17 17:56:42 +00:00
|
|
|
fnapi, closer, err := lcli.GetFullNodeAPI(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
smapi, closer, err := lcli.GetStorageMinerAPI(cctx)
|
2020-06-17 00:18:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
|
2020-06-17 17:56:42 +00:00
|
|
|
sask, err := smapi.MarketGetAsk(ctx)
|
2020-06-17 00:18:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
var ask *storagemarket.StorageAsk
|
|
|
|
if sask != nil && sask.Ask != nil {
|
|
|
|
ask = sask.Ask
|
|
|
|
}
|
|
|
|
|
|
|
|
w := tabwriter.NewWriter(os.Stdout, 2, 4, 2, ' ', 0)
|
2020-06-17 17:20:42 +00:00
|
|
|
fmt.Fprintf(w, "Price per GiB / Epoch\tMin. Piece Size (w/bit-padding)\tMax. Piece Size (w/bit-padding)\tExpiry (Epoch)\tExpiry (Appx. Rem. Time)\tSeq. No.\n")
|
2020-06-17 00:18:54 +00:00
|
|
|
if ask == nil {
|
|
|
|
fmt.Fprintf(w, "<miner does not have an ask>\n")
|
2020-06-17 17:20:42 +00:00
|
|
|
|
|
|
|
return w.Flush()
|
2020-06-17 00:18:54 +00:00
|
|
|
}
|
|
|
|
|
2020-06-17 17:56:42 +00:00
|
|
|
head, err := fnapi.ChainHead(ctx)
|
2020-06-17 17:20:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-17 17:56:42 +00:00
|
|
|
dlt := ask.Expiry - head.Height()
|
2020-06-17 17:30:16 +00:00
|
|
|
rem := "<expired>"
|
|
|
|
if dlt > 0 {
|
|
|
|
rem = (time.Second * time.Duration(dlt*build.BlockDelay)).String()
|
|
|
|
}
|
2020-06-17 17:20:42 +00:00
|
|
|
|
|
|
|
fmt.Fprintf(w, "%s\t%s\t%s\t%d\t%s\t%d\n", ask.Price, types.SizeStr(types.NewInt(uint64(ask.MinPieceSize))), types.SizeStr(types.NewInt(uint64(ask.MaxPieceSize))), ask.Expiry, rem, ask.SeqNo)
|
|
|
|
|
2020-06-17 00:18:54 +00:00
|
|
|
return w.Flush()
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-06-23 16:30:34 +00:00
|
|
|
var storageDealsCmd = &cli.Command{
|
|
|
|
Name: "storage-deals",
|
2020-06-23 17:17:31 +00:00
|
|
|
Usage: "Manage storage deals and related configuration",
|
2020-03-04 02:49:00 +00:00
|
|
|
Subcommands: []*cli.Command{
|
|
|
|
dealsImportDataCmd,
|
|
|
|
dealsListCmd,
|
2020-06-26 17:33:06 +00:00
|
|
|
storageDealSelectionCmd,
|
2020-06-16 22:38:48 +00:00
|
|
|
setAskCmd,
|
2020-06-17 00:18:54 +00:00
|
|
|
getAskCmd,
|
2020-06-18 22:42:24 +00:00
|
|
|
setBlocklistCmd,
|
|
|
|
getBlocklistCmd,
|
|
|
|
resetBlocklistCmd,
|
2020-03-04 02:49:00 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var dealsImportDataCmd = &cli.Command{
|
2020-03-18 05:08:14 +00:00
|
|
|
Name: "import-data",
|
|
|
|
Usage: "Manually import data for a deal",
|
|
|
|
ArgsUsage: "<proposal CID> <file>",
|
2020-03-04 02:49:00 +00:00
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
api, closer, err := lcli.GetStorageMinerAPI(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
ctx := lcli.DaemonContext(cctx)
|
|
|
|
|
2020-03-31 18:18:13 +00:00
|
|
|
if cctx.Args().Len() < 2 {
|
2020-03-04 02:49:00 +00:00
|
|
|
return fmt.Errorf("must specify proposal CID and file path")
|
|
|
|
}
|
|
|
|
|
|
|
|
propCid, err := cid.Decode(cctx.Args().Get(0))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fpath := cctx.Args().Get(1)
|
|
|
|
|
|
|
|
return api.DealsImportData(ctx, propCid, fpath)
|
|
|
|
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
var dealsListCmd = &cli.Command{
|
|
|
|
Name: "list",
|
|
|
|
Usage: "List all deals for this miner",
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
api, closer, err := lcli.GetStorageMinerAPI(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
|
|
|
|
ctx := lcli.DaemonContext(cctx)
|
|
|
|
|
2020-03-31 18:54:37 +00:00
|
|
|
deals, err := api.MarketListIncompleteDeals(ctx)
|
2020-03-04 02:49:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.MarshalIndent(deals, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(string(data))
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2020-06-18 20:15:18 +00:00
|
|
|
|
2020-06-18 22:42:24 +00:00
|
|
|
var getBlocklistCmd = &cli.Command{
|
|
|
|
Name: "get-blocklist",
|
|
|
|
Usage: "List the contents of the storage miner's piece CID blocklist",
|
2020-06-18 20:15:18 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&CidBaseFlag,
|
|
|
|
},
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
api, closer, err := lcli.GetStorageMinerAPI(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
|
2020-06-18 22:42:24 +00:00
|
|
|
blocklist, err := api.DealsPieceCidBlocklist(lcli.DaemonContext(cctx))
|
2020-06-18 20:15:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
encoder, err := GetCidEncoder(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-18 22:42:24 +00:00
|
|
|
for idx := range blocklist {
|
|
|
|
fmt.Println(encoder.Encode(blocklist[idx]))
|
2020-06-18 20:15:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-06-18 22:42:24 +00:00
|
|
|
var setBlocklistCmd = &cli.Command{
|
|
|
|
Name: "set-blocklist",
|
|
|
|
Usage: "Set the storage miner's list of blocklisted piece CIDs",
|
2020-06-18 20:36:48 +00:00
|
|
|
ArgsUsage: "[<path-of-file-containing-newline-delimited-piece-CIDs> (optional, will read from stdin if omitted)]",
|
2020-06-18 20:15:18 +00:00
|
|
|
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)
|
|
|
|
}
|
2020-06-19 16:19:46 +00:00
|
|
|
defer file.Close() //nolint:errcheck
|
2020-06-18 20:15:18 +00:00
|
|
|
|
|
|
|
scanner = bufio.NewScanner(file)
|
|
|
|
}
|
|
|
|
|
2020-06-18 22:42:24 +00:00
|
|
|
var blocklist []cid.Cid
|
2020-06-18 20:15:18 +00:00
|
|
|
for scanner.Scan() {
|
|
|
|
decoded, err := cid.Decode(scanner.Text())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-18 22:42:24 +00:00
|
|
|
blocklist = append(blocklist, decoded)
|
2020-06-18 20:15:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
err = scanner.Err()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-06-18 22:42:24 +00:00
|
|
|
return api.DealsSetPieceCidBlocklist(lcli.DaemonContext(cctx), blocklist)
|
2020-06-18 20:15:18 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-06-18 22:42:24 +00:00
|
|
|
var resetBlocklistCmd = &cli.Command{
|
|
|
|
Name: "reset-blocklist",
|
|
|
|
Usage: "Remove all entries from the storage miner's piece CID blocklist",
|
2020-06-18 20:15:18 +00:00
|
|
|
Flags: []cli.Flag{},
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
api, closer, err := lcli.GetStorageMinerAPI(cctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
|
|
|
|
2020-06-18 22:42:24 +00:00
|
|
|
return api.DealsSetPieceCidBlocklist(lcli.DaemonContext(cctx), []cid.Cid{})
|
2020-06-18 20:15:18 +00:00
|
|
|
},
|
|
|
|
}
|