2020-10-01 14:22:54 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2021-01-26 10:25:34 +00:00
|
|
|
"context"
|
2020-10-01 14:51:30 +00:00
|
|
|
"encoding/json"
|
2020-10-01 14:22:54 +00:00
|
|
|
"os"
|
|
|
|
|
|
|
|
"github.com/docker/go-units"
|
|
|
|
"github.com/ipfs/go-datastore"
|
2022-08-25 18:20:41 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2020-10-01 15:18:34 +00:00
|
|
|
"github.com/mitchellh/go-homedir"
|
2020-10-01 14:22:54 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
|
|
|
"golang.org/x/xerrors"
|
2020-10-01 15:18:34 +00:00
|
|
|
"gopkg.in/cheggaaa/pb.v1"
|
2020-10-01 14:22:54 +00:00
|
|
|
|
|
|
|
"github.com/filecoin-project/go-address"
|
2021-07-29 15:10:04 +00:00
|
|
|
"github.com/filecoin-project/go-paramfetch"
|
2020-10-01 14:22:54 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/big"
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
2021-03-05 21:01:20 +00:00
|
|
|
lapi "github.com/filecoin-project/lotus/api"
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/filecoin-project/lotus/api/v0api"
|
2020-10-01 14:22:54 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
|
|
|
lcli "github.com/filecoin-project/lotus/cli"
|
|
|
|
"github.com/filecoin-project/lotus/lib/backupds"
|
2020-10-01 15:18:34 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/config"
|
2020-10-01 14:22:54 +00:00
|
|
|
"github.com/filecoin-project/lotus/node/repo"
|
2022-11-01 11:01:31 +00:00
|
|
|
"github.com/filecoin-project/lotus/storage/sealer/storiface"
|
2020-10-01 14:22:54 +00:00
|
|
|
)
|
|
|
|
|
2021-07-12 09:30:26 +00:00
|
|
|
var restoreCmd = &cli.Command{
|
|
|
|
Name: "restore",
|
|
|
|
Usage: "Initialize a lotus miner repo from a backup",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "nosync",
|
|
|
|
Usage: "don't check full-node sync status",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "config",
|
|
|
|
Usage: "config file (config.toml)",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "storage-config",
|
|
|
|
Usage: "storage paths config (storage.json)",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
ArgsUsage: "[backupFile]",
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
ctx := lcli.ReqContext(cctx)
|
|
|
|
log.Info("Initializing lotus miner using a backup")
|
|
|
|
|
2022-11-01 11:01:31 +00:00
|
|
|
var storageCfg *storiface.StorageConfig
|
2021-07-13 10:54:19 +00:00
|
|
|
if cctx.IsSet("storage-config") {
|
|
|
|
cf, err := homedir.Expand(cctx.String("storage-config"))
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("expanding storage config path: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-03-29 19:24:07 +00:00
|
|
|
cfb, err := os.ReadFile(cf)
|
2021-07-13 10:54:19 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("reading storage config: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-11-01 11:01:31 +00:00
|
|
|
storageCfg = &storiface.StorageConfig{}
|
2021-07-13 10:54:19 +00:00
|
|
|
err = json.Unmarshal(cfb, storageCfg)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("cannot unmarshal json for storage config: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-29 15:10:04 +00:00
|
|
|
repoPath := cctx.String(FlagMinerRepo)
|
|
|
|
|
2022-04-20 21:34:28 +00:00
|
|
|
if err := restore(ctx, cctx, repoPath, storageCfg, nil, func(api lapi.FullNode, maddr address.Address, peerid peer.ID, mi api.MinerInfo) error {
|
2021-07-12 09:30:26 +00:00
|
|
|
log.Info("Checking proof parameters")
|
|
|
|
|
|
|
|
if err := paramfetch.GetParams(ctx, build.ParametersJSON(), build.SrsJSON(), uint64(mi.SectorSize)); err != nil {
|
|
|
|
return xerrors.Errorf("fetching proof parameters: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("Configuring miner actor")
|
|
|
|
|
|
|
|
if err := configureStorageMiner(ctx, api, maddr, peerid, big.Zero()); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2022-11-01 11:01:31 +00:00
|
|
|
func restore(ctx context.Context, cctx *cli.Context, targetPath string, strConfig *storiface.StorageConfig, manageConfig func(*config.StorageMiner) error, after func(api lapi.FullNode, addr address.Address, peerid peer.ID, mi api.MinerInfo) error) error {
|
2022-09-14 18:33:29 +00:00
|
|
|
if cctx.NArg() != 1 {
|
2022-09-14 19:38:15 +00:00
|
|
|
return lcli.IncorrectNumArgs(cctx)
|
2021-05-20 11:25:54 +00:00
|
|
|
}
|
2020-10-01 14:22:54 +00:00
|
|
|
|
2021-05-20 11:25:54 +00:00
|
|
|
log.Info("Trying to connect to full node RPC")
|
2021-04-15 22:19:26 +00:00
|
|
|
|
2021-05-20 11:25:54 +00:00
|
|
|
api, closer, err := lcli.GetFullNodeAPIV1(cctx) // TODO: consider storing full node address in config
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer closer()
|
2020-10-01 14:22:54 +00:00
|
|
|
|
2021-05-20 11:25:54 +00:00
|
|
|
log.Info("Checking full node version")
|
2021-04-15 22:19:26 +00:00
|
|
|
|
2021-05-20 11:25:54 +00:00
|
|
|
v, err := api.Version(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-01 14:22:54 +00:00
|
|
|
|
2021-05-27 11:35:54 +00:00
|
|
|
if !v.APIVersion.EqMajorMinor(lapi.FullAPIVersion1) {
|
|
|
|
return xerrors.Errorf("Remote API version didn't match (expected %s, remote %s)", lapi.FullAPIVersion1, v.APIVersion)
|
2021-05-20 11:25:54 +00:00
|
|
|
}
|
2020-10-01 14:22:54 +00:00
|
|
|
|
2021-05-20 11:25:54 +00:00
|
|
|
if !cctx.Bool("nosync") {
|
|
|
|
if err := lcli.SyncWait(ctx, &v0api.WrapperV1Full{FullNode: api}, false); err != nil {
|
|
|
|
return xerrors.Errorf("sync wait: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bf, err := homedir.Expand(cctx.Args().First())
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("expand backup file path: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
st, err := os.Stat(bf)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("stat backup file (%s): %w", bf, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
f, err := os.Open(bf)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("opening backup file: %w", err)
|
|
|
|
}
|
|
|
|
defer f.Close() // nolint:errcheck
|
|
|
|
|
|
|
|
log.Info("Checking if repo exists")
|
|
|
|
|
2021-07-29 15:10:04 +00:00
|
|
|
r, err := repo.NewFS(targetPath)
|
2021-05-20 11:25:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
ok, err := r.Exists()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ok {
|
|
|
|
return xerrors.Errorf("repo at '%s' is already initialized", cctx.String(FlagMinerRepo))
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("Initializing repo")
|
|
|
|
|
|
|
|
if err := r.Init(repo.StorageMiner); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
lr, err := r.Lock(repo.StorageMiner)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer lr.Close() //nolint:errcheck
|
|
|
|
|
|
|
|
if cctx.IsSet("config") {
|
|
|
|
log.Info("Restoring config")
|
|
|
|
|
|
|
|
cf, err := homedir.Expand(cctx.String("config"))
|
2020-10-01 14:22:54 +00:00
|
|
|
if err != nil {
|
2021-05-20 11:25:54 +00:00
|
|
|
return xerrors.Errorf("expanding config path: %w", err)
|
2020-10-01 14:22:54 +00:00
|
|
|
}
|
|
|
|
|
2021-05-20 11:25:54 +00:00
|
|
|
_, err = os.Stat(cf)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("stat config file (%s): %w", cf, err)
|
2020-10-01 14:22:54 +00:00
|
|
|
}
|
|
|
|
|
2021-05-20 11:25:54 +00:00
|
|
|
var cerr error
|
|
|
|
err = lr.SetConfig(func(raw interface{}) {
|
|
|
|
rcfg, ok := raw.(*config.StorageMiner)
|
|
|
|
if !ok {
|
|
|
|
cerr = xerrors.New("expected miner config")
|
|
|
|
return
|
2020-10-01 14:22:54 +00:00
|
|
|
}
|
|
|
|
|
2023-03-20 16:19:14 +00:00
|
|
|
ff, err := config.FromFile(cf, config.SetDefault(func() (interface{}, error) { return rcfg, nil }))
|
2021-05-20 11:25:54 +00:00
|
|
|
if err != nil {
|
|
|
|
cerr = xerrors.Errorf("loading config: %w", err)
|
|
|
|
return
|
|
|
|
}
|
2020-10-01 15:18:34 +00:00
|
|
|
|
2021-05-20 11:25:54 +00:00
|
|
|
*rcfg = *ff.(*config.StorageMiner)
|
|
|
|
if manageConfig != nil {
|
|
|
|
cerr = manageConfig(rcfg)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
if cerr != nil {
|
|
|
|
return cerr
|
2020-10-01 15:18:34 +00:00
|
|
|
}
|
2020-10-01 14:22:54 +00:00
|
|
|
if err != nil {
|
2021-05-20 11:25:54 +00:00
|
|
|
return xerrors.Errorf("setting config: %w", err)
|
2020-10-01 14:22:54 +00:00
|
|
|
}
|
|
|
|
|
2021-05-20 11:25:54 +00:00
|
|
|
} else {
|
|
|
|
log.Warn("--config NOT SET, WILL USE DEFAULT VALUES")
|
|
|
|
}
|
2020-10-01 14:22:54 +00:00
|
|
|
|
2021-07-13 10:54:19 +00:00
|
|
|
if strConfig != nil {
|
2021-05-20 11:25:54 +00:00
|
|
|
log.Info("Restoring storage path config")
|
|
|
|
|
2022-11-01 11:01:31 +00:00
|
|
|
err = lr.SetStorage(func(scfg *storiface.StorageConfig) {
|
2021-07-13 10:54:19 +00:00
|
|
|
*scfg = *strConfig
|
2021-05-20 11:25:54 +00:00
|
|
|
})
|
2020-10-01 14:22:54 +00:00
|
|
|
if err != nil {
|
2021-05-20 11:25:54 +00:00
|
|
|
return xerrors.Errorf("setting storage config: %w", err)
|
2020-10-01 14:22:54 +00:00
|
|
|
}
|
2021-05-20 11:25:54 +00:00
|
|
|
} else {
|
|
|
|
log.Warn("--storage-config NOT SET. NO SECTOR PATHS WILL BE CONFIGURED")
|
2021-08-11 03:29:52 +00:00
|
|
|
// setting empty config to allow miner to be started
|
2022-11-01 11:01:31 +00:00
|
|
|
if err := lr.SetStorage(func(sc *storiface.StorageConfig) {
|
|
|
|
sc.StoragePaths = append(sc.StoragePaths, storiface.LocalPath{})
|
2021-08-11 03:29:52 +00:00
|
|
|
}); err != nil {
|
|
|
|
return xerrors.Errorf("set storage config: %w", err)
|
|
|
|
}
|
2021-05-20 11:25:54 +00:00
|
|
|
}
|
2020-10-01 14:22:54 +00:00
|
|
|
|
2021-05-20 11:25:54 +00:00
|
|
|
log.Info("Restoring metadata backup")
|
2020-10-01 14:45:02 +00:00
|
|
|
|
2021-12-17 09:42:09 +00:00
|
|
|
mds, err := lr.Datastore(ctx, "/metadata")
|
2021-05-20 11:25:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-10-01 14:45:02 +00:00
|
|
|
|
2021-05-20 11:25:54 +00:00
|
|
|
bar := pb.New64(st.Size())
|
|
|
|
br := bar.NewProxyReader(f)
|
|
|
|
bar.ShowTimeLeft = true
|
|
|
|
bar.ShowPercent = true
|
|
|
|
bar.ShowSpeed = true
|
|
|
|
bar.Units = pb.U_BYTES
|
2020-10-01 14:45:02 +00:00
|
|
|
|
2021-05-20 11:25:54 +00:00
|
|
|
bar.Start()
|
|
|
|
err = backupds.RestoreInto(br, mds)
|
|
|
|
bar.Finish()
|
2020-10-01 14:45:02 +00:00
|
|
|
|
2021-05-20 11:25:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("restoring metadata: %w", err)
|
|
|
|
}
|
2020-10-01 14:45:02 +00:00
|
|
|
|
2021-05-20 11:25:54 +00:00
|
|
|
log.Info("Checking actor metadata")
|
2020-10-01 14:51:30 +00:00
|
|
|
|
2021-12-17 09:42:09 +00:00
|
|
|
abytes, err := mds.Get(ctx, datastore.NewKey("miner-address"))
|
2021-05-20 11:25:54 +00:00
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("getting actor address from metadata datastore: %w", err)
|
|
|
|
}
|
2020-10-01 14:51:30 +00:00
|
|
|
|
2021-05-20 11:25:54 +00:00
|
|
|
maddr, err := address.NewFromBytes(abytes)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("parsing actor address: %w", err)
|
|
|
|
}
|
2020-10-01 14:22:54 +00:00
|
|
|
|
2021-05-20 11:25:54 +00:00
|
|
|
log.Info("ACTOR ADDRESS: ", maddr.String())
|
2020-10-01 14:22:54 +00:00
|
|
|
|
2021-05-20 11:25:54 +00:00
|
|
|
mi, err := api.StateMinerInfo(ctx, maddr, types.EmptyTSK)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("getting miner info: %w", err)
|
|
|
|
}
|
2020-10-01 14:22:54 +00:00
|
|
|
|
2021-05-20 11:25:54 +00:00
|
|
|
log.Info("SECTOR SIZE: ", units.BytesSize(float64(mi.SectorSize)))
|
2020-10-01 14:22:54 +00:00
|
|
|
|
2021-05-20 11:25:54 +00:00
|
|
|
wk, err := api.StateAccountKey(ctx, mi.Worker, types.EmptyTSK)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("resolving worker key: %w", err)
|
|
|
|
}
|
2020-10-01 14:22:54 +00:00
|
|
|
|
2021-05-20 11:25:54 +00:00
|
|
|
has, err := api.WalletHas(ctx, wk)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("checking worker address: %w", err)
|
|
|
|
}
|
2020-10-01 14:22:54 +00:00
|
|
|
|
2021-05-20 11:25:54 +00:00
|
|
|
if !has {
|
|
|
|
return xerrors.Errorf("worker address %s for miner actor %s not present in full node wallet", mi.Worker, maddr)
|
|
|
|
}
|
2020-10-01 14:22:54 +00:00
|
|
|
|
2021-05-20 11:25:54 +00:00
|
|
|
log.Info("Initializing libp2p identity")
|
2020-10-01 15:51:56 +00:00
|
|
|
|
2021-05-20 11:25:54 +00:00
|
|
|
p2pSk, err := makeHostKey(lr)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("make host key: %w", err)
|
|
|
|
}
|
2020-10-01 14:22:54 +00:00
|
|
|
|
2021-05-20 11:25:54 +00:00
|
|
|
peerid, err := peer.IDFromPrivateKey(p2pSk)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("peer ID from private key: %w", err)
|
|
|
|
}
|
2020-10-01 14:22:54 +00:00
|
|
|
|
2021-05-20 11:25:54 +00:00
|
|
|
return after(api, maddr, peerid, mi)
|
|
|
|
}
|