2019-07-19 09:24:11 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2019-08-16 21:50:00 +00:00
|
|
|
"context"
|
2019-10-03 00:02:06 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"github.com/libp2p/go-libp2p-core/crypto"
|
2019-08-16 21:50:00 +00:00
|
|
|
|
2019-07-30 00:46:56 +00:00
|
|
|
"github.com/ipfs/go-datastore"
|
2019-07-25 12:50:34 +00:00
|
|
|
"github.com/libp2p/go-libp2p-core/peer"
|
2019-07-19 09:24:11 +00:00
|
|
|
"golang.org/x/xerrors"
|
|
|
|
"gopkg.in/urfave/cli.v2"
|
|
|
|
|
2019-08-16 21:50:00 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/api"
|
2019-07-19 10:15:22 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/build"
|
2019-07-25 12:50:34 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/actors"
|
2019-07-28 05:35:32 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/address"
|
2019-07-25 12:50:34 +00:00
|
|
|
"github.com/filecoin-project/go-lotus/chain/types"
|
2019-07-19 09:24:11 +00:00
|
|
|
lcli "github.com/filecoin-project/go-lotus/cli"
|
|
|
|
"github.com/filecoin-project/go-lotus/node/repo"
|
|
|
|
)
|
|
|
|
|
|
|
|
var initCmd = &cli.Command{
|
|
|
|
Name: "init",
|
|
|
|
Usage: "Initialize a lotus storage miner repo",
|
2019-08-16 21:50:00 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "actor",
|
|
|
|
Usage: "specify the address of an already created miner actor",
|
|
|
|
},
|
2019-08-20 18:17:59 +00:00
|
|
|
&cli.BoolFlag{
|
2019-09-26 20:57:20 +00:00
|
|
|
Name: "genesis-miner",
|
|
|
|
Usage: "enable genesis mining (DON'T USE ON BOOTSTRAPPED NETWORK)",
|
2019-09-26 12:19:21 +00:00
|
|
|
Hidden: true,
|
2019-08-20 18:17:59 +00:00
|
|
|
},
|
2019-09-26 00:47:53 +00:00
|
|
|
&cli.BoolFlag{
|
|
|
|
Name: "create-worker-key",
|
|
|
|
Usage: "create separate worker key",
|
|
|
|
},
|
2019-09-26 12:19:21 +00:00
|
|
|
&cli.StringFlag{
|
2019-09-26 20:57:20 +00:00
|
|
|
Name: "worker",
|
2019-09-26 12:19:21 +00:00
|
|
|
Aliases: []string{"w"},
|
2019-09-26 20:57:20 +00:00
|
|
|
Usage: "worker key to use (overrides --create-worker-key)",
|
2019-09-26 12:19:21 +00:00
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
2019-09-26 20:57:20 +00:00
|
|
|
Name: "owner",
|
2019-09-26 12:19:21 +00:00
|
|
|
Aliases: []string{"o"},
|
2019-09-26 20:57:20 +00:00
|
|
|
Usage: "owner key to use",
|
2019-09-26 12:19:21 +00:00
|
|
|
},
|
2019-08-16 21:50:00 +00:00
|
|
|
},
|
2019-07-19 09:24:11 +00:00
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
log.Info("Initializing lotus storage miner")
|
|
|
|
|
2019-10-02 17:20:30 +00:00
|
|
|
log.Info("Checking proof parameters")
|
|
|
|
if err := build.GetParams(true); err != nil {
|
|
|
|
return xerrors.Errorf("fetching proof parameters: %w", err)
|
|
|
|
}
|
|
|
|
|
2019-10-02 20:29:40 +00:00
|
|
|
log.Info("Checking if repo exists")
|
|
|
|
|
2019-07-23 21:50:52 +00:00
|
|
|
r, err := repo.NewFS(cctx.String(FlagStorageRepo))
|
2019-07-19 09:24:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-23 21:54:54 +00:00
|
|
|
ok, err := r.Exists()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if ok {
|
2019-07-23 21:50:52 +00:00
|
|
|
return xerrors.Errorf("repo at '%s' is already initialized", cctx.String(FlagStorageRepo))
|
2019-07-19 09:24:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("Trying to connect to full node RPC")
|
|
|
|
|
2019-08-02 16:18:44 +00:00
|
|
|
api, err := lcli.GetFullNodeAPI(cctx) // TODO: consider storing full node address in config
|
2019-07-19 09:24:11 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
ctx := lcli.ReqContext(cctx)
|
|
|
|
|
|
|
|
log.Info("Checking full node version")
|
|
|
|
|
2019-07-19 10:15:22 +00:00
|
|
|
v, err := api.Version(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-23 21:55:19 +00:00
|
|
|
if v.APIVersion&build.MinorMask != build.APIVersion&build.MinorMask {
|
2019-07-19 10:15:22 +00:00
|
|
|
return xerrors.Errorf("Remote API version didn't match (local %x, remote %x)", build.APIVersion, v.APIVersion)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Info("Initializing repo")
|
|
|
|
|
2019-07-19 09:24:11 +00:00
|
|
|
if err := r.Init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-07-25 12:50:34 +00:00
|
|
|
lr, err := r.Lock()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer lr.Close()
|
|
|
|
|
|
|
|
log.Info("Initializing libp2p identity")
|
|
|
|
|
2019-10-03 00:02:06 +00:00
|
|
|
p2pSk, err := makeHostKey(lr)
|
2019-07-25 12:50:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
peerid, err := peer.IDFromPrivateKey(p2pSk)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-16 21:50:00 +00:00
|
|
|
var addr address.Address
|
|
|
|
if act := cctx.String("actor"); act != "" {
|
|
|
|
a, err := address.NewFromString(act)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-20 18:17:59 +00:00
|
|
|
if err := configureStorageMiner(ctx, api, a, peerid, cctx.Bool("genesis-miner")); err != nil {
|
2019-08-16 21:50:00 +00:00
|
|
|
return xerrors.Errorf("failed to configure storage miner: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = a
|
|
|
|
} else {
|
2019-09-26 12:19:21 +00:00
|
|
|
a, err := createStorageMiner(ctx, api, peerid, cctx)
|
2019-08-16 21:50:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
addr = a
|
2019-07-25 12:50:34 +00:00
|
|
|
}
|
|
|
|
|
2019-08-16 21:50:00 +00:00
|
|
|
ds, err := lr.Datastore("/metadata")
|
2019-07-25 12:50:34 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2019-08-16 21:50:00 +00:00
|
|
|
if err := ds.Put(datastore.NewKey("miner-address"), addr.Bytes()); err != nil {
|
2019-07-25 12:50:34 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-16 21:50:00 +00:00
|
|
|
// TODO: Point to setting storage price, maybe do it interactively or something
|
|
|
|
log.Info("Storage miner successfully created, you can now start it with 'lotus-storage-miner run'")
|
2019-07-25 12:50:34 +00:00
|
|
|
|
2019-08-16 21:50:00 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
2019-07-25 12:50:34 +00:00
|
|
|
|
2019-10-03 00:02:06 +00:00
|
|
|
func makeHostKey(lr repo.LockedRepo) (crypto.PrivKey, error) {
|
|
|
|
pk, _, err := crypto.GenerateEd25519Key(rand.Reader)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
ks, err := lr.KeyStore()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
kbytes, err := pk.Bytes()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := ks.Put("libp2p-host", types.KeyInfo{
|
|
|
|
Type: "libp2p-host",
|
|
|
|
PrivateKey: kbytes,
|
|
|
|
}); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return pk, nil
|
|
|
|
}
|
|
|
|
|
2019-08-20 18:17:59 +00:00
|
|
|
func configureStorageMiner(ctx context.Context, api api.FullNode, addr address.Address, peerid peer.ID, genmine bool) error {
|
|
|
|
if genmine {
|
|
|
|
log.Warn("Starting genesis mining. This shouldn't happen when connecting to the real network.")
|
|
|
|
// We may be one of genesis miners, start mining before trying to do any chain operations
|
|
|
|
// (otherwise our messages won't be mined)
|
|
|
|
if err := api.MinerRegister(ctx, addr); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
defer func() {
|
|
|
|
if err := api.MinerUnregister(ctx, addr); err != nil {
|
|
|
|
log.Errorf("failed to call api.MinerUnregister: %s", err)
|
|
|
|
}
|
|
|
|
}()
|
2019-08-20 17:19:24 +00:00
|
|
|
}
|
|
|
|
|
2019-08-16 21:50:00 +00:00
|
|
|
// This really just needs to be an api call at this point...
|
2019-09-06 06:26:02 +00:00
|
|
|
recp, err := api.StateCall(ctx, &types.Message{
|
2019-08-16 21:50:00 +00:00
|
|
|
To: addr,
|
|
|
|
From: addr,
|
|
|
|
Method: actors.MAMethods.GetWorkerAddr,
|
|
|
|
}, nil)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to get worker address: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if recp.ExitCode != 0 {
|
|
|
|
return xerrors.Errorf("getWorkerAddr returned exit code %d", recp.ExitCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
waddr, err := address.NewFromBytes(recp.Return)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("getWorkerAddr returned bad address: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
enc, err := actors.SerializeParams(&actors.UpdatePeerIDParams{PeerID: peerid})
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := &types.Message{
|
|
|
|
To: addr,
|
|
|
|
From: waddr,
|
|
|
|
Method: actors.MAMethods.UpdatePeerID,
|
|
|
|
Params: enc,
|
2019-08-16 22:10:34 +00:00
|
|
|
Value: types.NewInt(0),
|
2019-08-16 21:50:00 +00:00
|
|
|
GasPrice: types.NewInt(0),
|
2019-09-06 17:42:03 +00:00
|
|
|
GasLimit: types.NewInt(1000000),
|
2019-08-16 21:50:00 +00:00
|
|
|
}
|
|
|
|
|
2019-09-17 08:15:26 +00:00
|
|
|
smsg, err := api.MpoolPushMessage(ctx, msg)
|
2019-08-16 21:50:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-08-16 22:10:34 +00:00
|
|
|
log.Info("Waiting for message: ", smsg.Cid())
|
2019-08-16 21:50:00 +00:00
|
|
|
ret, err := api.ChainWaitMsg(ctx, smsg.Cid())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if ret.Receipt.ExitCode != 0 {
|
|
|
|
return xerrors.Errorf("update peer id message failed with exit code %d", ret.Receipt.ExitCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2019-07-25 12:50:34 +00:00
|
|
|
|
2019-09-26 12:19:21 +00:00
|
|
|
func createStorageMiner(ctx context.Context, api api.FullNode, peerid peer.ID, cctx *cli.Context) (addr address.Address, err error) {
|
2019-08-16 21:50:00 +00:00
|
|
|
log.Info("Creating StorageMarket.CreateStorageMiner message")
|
2019-07-25 12:50:34 +00:00
|
|
|
|
2019-09-26 12:19:21 +00:00
|
|
|
var owner address.Address
|
|
|
|
if cctx.String("owner") != "" {
|
|
|
|
owner, err = address.NewFromString(cctx.String("owner"))
|
|
|
|
} else {
|
|
|
|
owner, err = api.WalletDefaultAddress(ctx)
|
|
|
|
}
|
2019-08-16 21:50:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return address.Undef, err
|
|
|
|
}
|
2019-07-25 12:50:34 +00:00
|
|
|
|
2019-09-26 12:19:21 +00:00
|
|
|
worker := owner
|
|
|
|
if cctx.String("worker") != "" {
|
|
|
|
worker, err = address.NewFromString(cctx.String("worker"))
|
|
|
|
} else if cctx.Bool("create-worker-key") { // TODO: Do we need to force this if owner is Secpk?
|
|
|
|
worker, err = api.WalletNew(ctx, types.KTBLS)
|
|
|
|
}
|
|
|
|
// TODO: Transfer some initial funds to worker
|
|
|
|
if err != nil {
|
|
|
|
return address.Undef, err
|
2019-08-16 21:50:00 +00:00
|
|
|
}
|
2019-07-25 12:50:34 +00:00
|
|
|
|
2019-09-25 23:32:33 +00:00
|
|
|
collateral, err := api.StatePledgeCollateral(ctx, nil)
|
|
|
|
if err != nil {
|
|
|
|
return address.Undef, err
|
|
|
|
}
|
2019-07-25 12:50:34 +00:00
|
|
|
|
2019-09-10 23:03:17 +00:00
|
|
|
params, err := actors.SerializeParams(&actors.CreateStorageMinerParams{
|
2019-09-26 12:19:21 +00:00
|
|
|
Owner: owner,
|
|
|
|
Worker: worker,
|
2019-08-27 22:10:23 +00:00
|
|
|
SectorSize: types.NewInt(build.SectorSize),
|
2019-08-16 21:50:00 +00:00
|
|
|
PeerID: peerid,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return address.Undef, err
|
|
|
|
}
|
2019-07-25 12:50:34 +00:00
|
|
|
|
2019-09-17 08:15:26 +00:00
|
|
|
createStorageMinerMsg := &types.Message{
|
2019-08-19 18:06:39 +00:00
|
|
|
To: actors.StorageMarketAddress,
|
2019-09-26 12:19:21 +00:00
|
|
|
From: owner,
|
2019-08-16 21:50:00 +00:00
|
|
|
Value: collateral,
|
2019-07-25 12:50:34 +00:00
|
|
|
|
2019-08-16 21:50:00 +00:00
|
|
|
Method: actors.SMAMethods.CreateStorageMiner,
|
|
|
|
Params: params,
|
2019-08-19 18:06:39 +00:00
|
|
|
|
|
|
|
GasLimit: types.NewInt(10000),
|
|
|
|
GasPrice: types.NewInt(0),
|
2019-08-16 21:50:00 +00:00
|
|
|
}
|
2019-07-25 12:50:34 +00:00
|
|
|
|
2019-09-17 08:15:26 +00:00
|
|
|
signed, err := api.MpoolPushMessage(ctx, createStorageMinerMsg)
|
2019-08-16 21:50:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return address.Undef, err
|
|
|
|
}
|
|
|
|
|
2019-09-17 08:15:26 +00:00
|
|
|
log.Infof("Pushed StorageMarket.CreateStorageMiner, %s to Mpool", signed.Cid())
|
2019-08-16 21:50:00 +00:00
|
|
|
log.Infof("Waiting for confirmation")
|
|
|
|
|
|
|
|
mw, err := api.ChainWaitMsg(ctx, signed.Cid())
|
|
|
|
if err != nil {
|
|
|
|
return address.Undef, err
|
|
|
|
}
|
|
|
|
|
2019-09-26 12:19:21 +00:00
|
|
|
addr, err = address.NewFromBytes(mw.Receipt.Return)
|
2019-08-16 21:50:00 +00:00
|
|
|
if err != nil {
|
|
|
|
return address.Undef, err
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Infof("New storage miners address is: %s", addr)
|
|
|
|
return addr, nil
|
2019-07-19 09:24:11 +00:00
|
|
|
}
|