2019-11-25 04:45:13 +00:00
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
2020-02-12 21:41:59 +00:00
|
|
|
"encoding/hex"
|
2020-02-06 19:56:35 +00:00
|
|
|
"encoding/json"
|
2019-12-08 18:35:44 +00:00
|
|
|
"fmt"
|
2020-02-14 20:33:41 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi/big"
|
2020-02-12 21:41:59 +00:00
|
|
|
"io/ioutil"
|
2019-11-25 04:45:13 +00:00
|
|
|
"os"
|
2019-12-08 20:06:40 +00:00
|
|
|
"path/filepath"
|
2019-11-25 04:45:13 +00:00
|
|
|
|
2020-02-10 19:16:36 +00:00
|
|
|
"github.com/filecoin-project/specs-actors/actors/abi"
|
|
|
|
|
2019-12-19 21:06:25 +00:00
|
|
|
sectorbuilder "github.com/filecoin-project/go-sectorbuilder"
|
2020-02-04 22:49:10 +00:00
|
|
|
|
2020-01-09 13:23:44 +00:00
|
|
|
"github.com/ipfs/go-datastore"
|
|
|
|
"github.com/ipfs/go-datastore/namespace"
|
2020-01-08 18:56:45 +00:00
|
|
|
badger "github.com/ipfs/go-ds-badger2"
|
2020-01-08 19:10:57 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2019-11-25 04:45:13 +00:00
|
|
|
"github.com/mitchellh/go-homedir"
|
2019-12-08 20:06:40 +00:00
|
|
|
"golang.org/x/xerrors"
|
2019-11-25 04:45:13 +00:00
|
|
|
"gopkg.in/urfave/cli.v2"
|
2019-11-26 13:52:59 +00:00
|
|
|
|
2019-12-19 20:13:17 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
2020-02-06 19:56:35 +00:00
|
|
|
|
2019-11-26 13:52:59 +00:00
|
|
|
"github.com/filecoin-project/lotus/build"
|
2020-02-12 21:41:59 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2019-11-27 14:18:51 +00:00
|
|
|
"github.com/filecoin-project/lotus/cmd/lotus-seed/seed"
|
2019-12-08 18:35:44 +00:00
|
|
|
"github.com/filecoin-project/lotus/genesis"
|
2019-11-25 04:45:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var log = logging.Logger("lotus-seed")
|
|
|
|
|
|
|
|
func main() {
|
|
|
|
logging.SetLogLevel("*", "INFO")
|
|
|
|
|
|
|
|
log.Info("Starting seed")
|
|
|
|
|
|
|
|
local := []*cli.Command{
|
|
|
|
preSealCmd,
|
2019-12-08 18:35:44 +00:00
|
|
|
aggregateManifestsCmd,
|
2019-12-08 20:06:40 +00:00
|
|
|
aggregateSectorDirsCmd,
|
2019-11-25 04:45:13 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
app := &cli.App{
|
|
|
|
Name: "lotus-seed",
|
|
|
|
Usage: "Seal sectors for genesis miner",
|
2019-12-11 22:00:39 +00:00
|
|
|
Version: build.UserVersion,
|
2019-11-25 04:45:13 +00:00
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "sectorbuilder-dir",
|
|
|
|
Value: "~/.genesis-sectors",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
|
|
|
|
Commands: local,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := app.Run(os.Args); err != nil {
|
|
|
|
log.Warn(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
var preSealCmd = &cli.Command{
|
|
|
|
Name: "pre-seal",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "miner-addr",
|
|
|
|
Value: "t0101",
|
|
|
|
Usage: "specify the future address of your miner",
|
|
|
|
},
|
|
|
|
&cli.Uint64Flag{
|
|
|
|
Name: "sector-size",
|
2020-02-10 19:16:36 +00:00
|
|
|
Value: uint64(build.SectorSizes[0]),
|
2019-11-25 04:45:13 +00:00
|
|
|
Usage: "specify size of sectors to pre-seal",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "ticket-preimage",
|
|
|
|
Value: "lotus is fire",
|
|
|
|
Usage: "set the ticket preimage for sealing randomness",
|
|
|
|
},
|
2019-11-30 23:17:50 +00:00
|
|
|
&cli.IntFlag{
|
2019-11-25 04:45:13 +00:00
|
|
|
Name: "num-sectors",
|
|
|
|
Value: 1,
|
|
|
|
Usage: "select number of sectors to pre-seal",
|
|
|
|
},
|
2019-12-06 17:55:46 +00:00
|
|
|
&cli.Uint64Flag{
|
2019-12-06 17:53:33 +00:00
|
|
|
Name: "sector-offset",
|
|
|
|
Value: 0,
|
2019-12-06 21:49:44 +00:00
|
|
|
Usage: "how many sector ids to skip when starting to seal",
|
2019-12-06 17:53:33 +00:00
|
|
|
},
|
2020-02-12 21:41:59 +00:00
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "key",
|
|
|
|
Value: "",
|
|
|
|
Usage: "(optional) Key to use for signing / owner/worker addresses",
|
|
|
|
},
|
2019-11-25 04:45:13 +00:00
|
|
|
},
|
|
|
|
Action: func(c *cli.Context) error {
|
|
|
|
sdir := c.String("sectorbuilder-dir")
|
|
|
|
sbroot, err := homedir.Expand(sdir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
maddr, err := address.NewFromString(c.String("miner-addr"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-12 21:41:59 +00:00
|
|
|
var k *types.KeyInfo
|
|
|
|
if c.String("key") != "" {
|
|
|
|
k = new(types.KeyInfo)
|
|
|
|
kh, err := ioutil.ReadFile(c.String("key"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
kb, err := hex.DecodeString(string(kh))
|
|
|
|
if err := json.Unmarshal(kb, k); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
gm, key, err := seed.PreSeal(maddr, abi.SectorSize(c.Uint64("sector-size")), abi.SectorNumber(c.Uint64("sector-offset")), c.Int("num-sectors"), sbroot, []byte(c.String("ticket-preimage")), k)
|
2019-11-29 03:38:18 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-12 21:41:59 +00:00
|
|
|
return seed.WriteGenesisMiner(maddr, sbroot, gm, key)
|
2019-11-25 04:45:13 +00:00
|
|
|
},
|
|
|
|
}
|
2019-12-08 18:35:44 +00:00
|
|
|
|
|
|
|
var aggregateManifestsCmd = &cli.Command{
|
|
|
|
Name: "aggregate-manifests",
|
|
|
|
Usage: "aggregate a set of preseal manifests into a single file",
|
|
|
|
Action: func(cctx *cli.Context) error {
|
2020-02-12 21:41:59 +00:00
|
|
|
var inputs []map[string]genesis.Miner
|
2019-12-08 18:35:44 +00:00
|
|
|
for _, infi := range cctx.Args().Slice() {
|
|
|
|
fi, err := os.Open(infi)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer fi.Close()
|
2020-02-12 21:41:59 +00:00
|
|
|
var val map[string]genesis.Miner
|
2019-12-08 18:35:44 +00:00
|
|
|
if err := json.NewDecoder(fi).Decode(&val); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
inputs = append(inputs, val)
|
|
|
|
}
|
|
|
|
|
2020-02-12 21:41:59 +00:00
|
|
|
output := make(map[string]genesis.Miner)
|
2019-12-08 18:35:44 +00:00
|
|
|
for _, in := range inputs {
|
|
|
|
for maddr, val := range in {
|
|
|
|
if gm, ok := output[maddr]; ok {
|
|
|
|
output[maddr] = mergeGenMiners(gm, val)
|
|
|
|
} else {
|
|
|
|
output[maddr] = val
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
blob, err := json.MarshalIndent(output, "", " ")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
fmt.Println(string(blob))
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2019-12-08 20:06:40 +00:00
|
|
|
var aggregateSectorDirsCmd = &cli.Command{
|
|
|
|
Name: "aggregate-sector-dirs",
|
|
|
|
Usage: "aggregate a set of preseal manifests into a single file",
|
|
|
|
Flags: []cli.Flag{
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "miner",
|
|
|
|
Usage: "Specify address of miner to aggregate sectorbuilders for",
|
|
|
|
},
|
|
|
|
&cli.StringFlag{
|
|
|
|
Name: "dest",
|
|
|
|
Usage: "specify directory to create aggregate sector store in",
|
|
|
|
},
|
|
|
|
&cli.Uint64Flag{
|
|
|
|
Name: "sector-size",
|
|
|
|
Usage: "specify size of sectors to aggregate",
|
|
|
|
Value: 32 * 1024 * 1024 * 1024,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
Action: func(cctx *cli.Context) error {
|
|
|
|
if cctx.String("miner") == "" {
|
|
|
|
return fmt.Errorf("must specify miner address with --miner")
|
|
|
|
}
|
|
|
|
if cctx.String("dest") == "" {
|
|
|
|
return fmt.Errorf("must specify dest directory with --dest")
|
|
|
|
}
|
|
|
|
|
|
|
|
maddr, err := address.NewFromString(cctx.String("miner"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
destdir, err := homedir.Expand(cctx.String("dest"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-09 22:05:09 +00:00
|
|
|
if err := os.MkdirAll(destdir, 0755); err != nil {
|
2019-12-08 20:06:40 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
agmds, err := badger.NewDatastore(filepath.Join(destdir, "badger"), nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer agmds.Close()
|
|
|
|
|
2020-02-10 19:16:36 +00:00
|
|
|
ssize := abi.SectorSize(cctx.Uint64("sector-size"))
|
2019-12-08 20:06:40 +00:00
|
|
|
|
|
|
|
agsb, err := sectorbuilder.New(§orbuilder.Config{
|
|
|
|
Miner: maddr,
|
|
|
|
SectorSize: ssize,
|
2020-01-28 23:08:02 +00:00
|
|
|
Paths: sectorbuilder.SimplePath(destdir),
|
2019-12-08 20:06:40 +00:00
|
|
|
WorkerThreads: 2,
|
2020-01-09 13:23:44 +00:00
|
|
|
}, namespace.Wrap(agmds, datastore.NewKey("/sectorbuilder")))
|
2019-12-08 20:06:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-12 21:41:59 +00:00
|
|
|
var aggrGenMiner genesis.Miner
|
2020-02-10 19:16:36 +00:00
|
|
|
var highestSectorID abi.SectorNumber
|
2019-12-08 20:06:40 +00:00
|
|
|
for _, dir := range cctx.Args().Slice() {
|
|
|
|
dir, err := homedir.Expand(dir)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("failed to expand %q: %w", dir, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
st, err := os.Stat(dir)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if !st.IsDir() {
|
|
|
|
return fmt.Errorf("%q was not a directory", dir)
|
|
|
|
}
|
|
|
|
|
|
|
|
fi, err := os.Open(filepath.Join(dir, "pre-seal-"+maddr.String()+".json"))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-12 21:41:59 +00:00
|
|
|
var genmm map[string]genesis.Miner
|
2019-12-08 21:14:16 +00:00
|
|
|
if err := json.NewDecoder(fi).Decode(&genmm); err != nil {
|
2019-12-08 20:06:40 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-08 21:14:16 +00:00
|
|
|
genm, ok := genmm[maddr.String()]
|
|
|
|
if !ok {
|
|
|
|
return xerrors.Errorf("input data did not have our miner in it (%s)", maddr)
|
|
|
|
}
|
|
|
|
|
2019-12-08 20:06:40 +00:00
|
|
|
if genm.SectorSize != ssize {
|
2019-12-08 21:14:16 +00:00
|
|
|
return xerrors.Errorf("sector size mismatch in %q (%d != %d)", dir)
|
2019-12-08 20:06:40 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, s := range genm.Sectors {
|
|
|
|
if s.SectorID > highestSectorID {
|
|
|
|
highestSectorID = s.SectorID
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-12-10 13:01:17 +00:00
|
|
|
aggrGenMiner = mergeGenMiners(aggrGenMiner, genm)
|
|
|
|
|
2019-12-08 21:24:41 +00:00
|
|
|
opts := badger.DefaultOptions
|
|
|
|
opts.ReadOnly = true
|
|
|
|
mds, err := badger.NewDatastore(filepath.Join(dir, "badger"), &opts)
|
2019-12-08 20:06:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer mds.Close()
|
|
|
|
|
|
|
|
sb, err := sectorbuilder.New(§orbuilder.Config{
|
|
|
|
Miner: maddr,
|
|
|
|
SectorSize: genm.SectorSize,
|
2020-01-28 23:08:02 +00:00
|
|
|
Paths: sectorbuilder.SimplePath(dir),
|
2019-12-08 20:06:40 +00:00
|
|
|
WorkerThreads: 2,
|
2020-01-09 13:23:44 +00:00
|
|
|
}, namespace.Wrap(mds, datastore.NewKey("/sectorbuilder")))
|
2019-12-08 20:06:40 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-10 17:32:50 +00:00
|
|
|
if err := agsb.ImportFrom(sb, false); err != nil {
|
2019-12-08 20:06:40 +00:00
|
|
|
return xerrors.Errorf("importing sectors from %q failed: %w", dir, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-11 01:10:50 +00:00
|
|
|
if err := agsb.SetLastSectorNum(highestSectorID); err != nil {
|
2019-12-08 20:06:40 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-02-12 21:41:59 +00:00
|
|
|
if err := seed.WriteGenesisMiner(maddr, destdir, &aggrGenMiner, nil); err != nil {
|
2019-12-10 13:01:17 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-12-08 20:06:40 +00:00
|
|
|
return nil
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2020-02-12 21:41:59 +00:00
|
|
|
func mergeGenMiners(a, b genesis.Miner) genesis.Miner {
|
2019-12-08 18:35:44 +00:00
|
|
|
if a.SectorSize != b.SectorSize {
|
|
|
|
panic("sector sizes mismatch")
|
|
|
|
}
|
|
|
|
|
2020-02-12 21:41:59 +00:00
|
|
|
return genesis.Miner{
|
2020-02-14 20:33:41 +00:00
|
|
|
Owner: a.Owner,
|
|
|
|
Worker: a.Worker,
|
|
|
|
PeerId: "",
|
|
|
|
MarketBalance: big.Zero(),
|
|
|
|
PowerBalance: big.Zero(),
|
|
|
|
SectorSize: a.SectorSize,
|
|
|
|
Sectors: append(a.Sectors, b.Sectors...),
|
2019-12-08 18:35:44 +00:00
|
|
|
}
|
|
|
|
}
|