feat: miner cli: sealing data-cid command

This commit is contained in:
Łukasz Magiera 2022-05-24 15:07:53 +02:00
parent 4b1bfa9964
commit bacaccc378
2 changed files with 83 additions and 0 deletions

View File

@ -11,11 +11,15 @@ import (
"text/tabwriter"
"time"
"github.com/dustin/go-humanize"
"github.com/fatih/color"
"github.com/google/uuid"
"github.com/mitchellh/go-homedir"
"github.com/urfave/cli/v2"
"golang.org/x/xerrors"
"github.com/filecoin-project/go-padreader"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/lotus/extern/sector-storage/sealtasks"
"github.com/filecoin-project/lotus/extern/sector-storage/storiface"
@ -31,6 +35,7 @@ var sealingCmd = &cli.Command{
workersCmd(true),
sealingSchedDiagCmd,
sealingAbortCmd,
sealingDataCidCmd,
},
}
@ -349,3 +354,67 @@ var sealingAbortCmd = &cli.Command{
return nodeApi.SealingAbort(ctx, job.ID)
},
}
var sealingDataCidCmd = &cli.Command{
Name: "data-cid",
Usage: "Compute data CID using workers",
ArgsUsage: "[file] <padded piece size>",
Action: func(cctx *cli.Context) error {
if cctx.Args().Len() < 1 || cctx.Args().Len() > 2 {
return xerrors.Errorf("expected 1 or 2 arguments")
}
nodeApi, closer, err := lcli.GetStorageMinerAPI(cctx)
if err != nil {
return err
}
defer closer()
ctx := lcli.ReqContext(cctx)
p, err := homedir.Expand(cctx.Args().First())
if err != nil {
return xerrors.Errorf("expanding path: %w", err)
}
f, err := os.OpenFile(p, os.O_RDONLY, 0)
if err != nil {
return xerrors.Errorf("opening source file: %w", err)
}
st, err := f.Stat()
if err != nil {
return xerrors.Errorf("stat: %w", err)
}
var psize abi.PaddedPieceSize
if cctx.Args().Len() == 2 {
rps, err := humanize.ParseBytes(cctx.Args().Get(1))
if err != nil {
return xerrors.Errorf("parsing piece size: %w", err)
}
psize = abi.PaddedPieceSize(rps)
if err := psize.Validate(); err != nil {
return xerrors.Errorf("checking piece size: %w", err)
}
if st.Size() > int64(psize.Unpadded()) {
return xerrors.Errorf("file larger than the piece")
}
} else {
psize = padreader.PaddedSize(uint64(st.Size())).Padded()
}
ir, err := padreader.NewInflator(f, uint64(st.Size()), psize.Unpadded())
if err != nil {
return err
}
pc, err := nodeApi.ComputeDataCid(ctx, psize.Unpadded(), ir)
if err != nil {
return xerrors.Errorf("computing data CID: %w", err)
}
fmt.Println(pc.PieceCID, " ", pc.Size)
return nil
},
}

View File

@ -2331,6 +2331,7 @@ COMMANDS:
workers list workers
sched-diag Dump internal scheduler state
abort Abort a running job
data-cid Compute data CID
help, h Shows a list of commands or help for one command
OPTIONS:
@ -2393,3 +2394,16 @@ OPTIONS:
--help, -h show help (default: false)
```
### lotus-miner sealing data-cid
```
NAME:
lotus-miner sealing data-cid - Compute data CID
USAGE:
lotus-miner sealing data-cid [command options] [file] <padded piece size>
OPTIONS:
--help, -h show help (default: false)
```