Clean up after failed storage miner init

This commit is contained in:
whyrusleeping 2019-10-18 14:13:36 +09:00
parent 47eb5b6d8f
commit 098e05c4d7

View File

@ -3,6 +3,8 @@ package main
import ( import (
"context" "context"
"crypto/rand" "crypto/rand"
"fmt"
"os"
"github.com/libp2p/go-libp2p-core/crypto" "github.com/libp2p/go-libp2p-core/crypto"
@ -58,7 +60,8 @@ var initCmd = &cli.Command{
log.Info("Checking if repo exists") log.Info("Checking if repo exists")
r, err := repo.NewFS(cctx.String(FlagStorageRepo)) repoPath := cctx.String(FlagStorageRepo)
r, err := repo.NewFS(repoPath)
if err != nil { if err != nil {
return err return err
} }
@ -97,51 +100,13 @@ var initCmd = &cli.Command{
return err return err
} }
lr, err := r.Lock() if err := storageMinerInit(ctx, cctx, api, r); err != nil {
if err != nil { fmt.Printf("ERROR: failed to initialize lotus-storage-miner: %s\n", err)
return err fmt.Println("Cleaning up after attempt...")
} if err := os.RemoveAll(repoPath); err != nil {
defer lr.Close() fmt.Println("ERROR: failed to clean up failed storage repo: ", err)
log.Info("Initializing libp2p identity")
p2pSk, err := makeHostKey(lr)
if err != nil {
return err
}
peerid, err := peer.IDFromPrivateKey(p2pSk)
if err != nil {
return err
}
var addr address.Address
if act := cctx.String("actor"); act != "" {
a, err := address.NewFromString(act)
if err != nil {
return err
} }
return fmt.Errorf("storage-miner init failed")
if err := configureStorageMiner(ctx, api, a, peerid, cctx.Bool("genesis-miner")); err != nil {
return xerrors.Errorf("failed to configure storage miner: %w", err)
}
addr = a
} else {
a, err := createStorageMiner(ctx, api, peerid, cctx)
if err != nil {
return err
}
addr = a
}
ds, err := lr.Datastore("/metadata")
if err != nil {
return err
}
if err := ds.Put(datastore.NewKey("miner-address"), addr.Bytes()); err != nil {
return err
} }
// TODO: Point to setting storage price, maybe do it interactively or something // TODO: Point to setting storage price, maybe do it interactively or something
@ -151,6 +116,59 @@ var initCmd = &cli.Command{
}, },
} }
func storageMinerInit(ctx context.Context, cctx *cli.Context, api api.FullNode, r repo.Repo) error {
lr, err := r.Lock()
if err != nil {
return err
}
defer lr.Close()
log.Info("Initializing libp2p identity")
p2pSk, err := makeHostKey(lr)
if err != nil {
return err
}
peerid, err := peer.IDFromPrivateKey(p2pSk)
if err != nil {
return err
}
var addr address.Address
if act := cctx.String("actor"); act != "" {
a, err := address.NewFromString(act)
if err != nil {
return err
}
if err := configureStorageMiner(ctx, api, a, peerid, cctx.Bool("genesis-miner")); err != nil {
return xerrors.Errorf("failed to configure storage miner: %w", err)
}
addr = a
} else {
a, err := createStorageMiner(ctx, api, peerid, cctx)
if err != nil {
return err
}
addr = a
}
log.Infof("Created new storage miner: %s", addr)
ds, err := lr.Datastore("/metadata")
if err != nil {
return err
}
if err := ds.Put(datastore.NewKey("miner-address"), addr.Bytes()); err != nil {
return err
}
return nil
}
func makeHostKey(lr repo.LockedRepo) (crypto.PrivKey, error) { func makeHostKey(lr repo.LockedRepo) (crypto.PrivKey, error) {
pk, _, err := crypto.GenerateEd25519Key(rand.Reader) pk, _, err := crypto.GenerateEd25519Key(rand.Reader)
if err != nil { if err != nil {