lotus/cmd/lotus-seed/main.go

185 lines
4.0 KiB
Go
Raw Normal View History

2019-11-25 04:45:13 +00:00
package main
import (
"encoding/hex"
2020-02-06 19:56:35 +00:00
"encoding/json"
"fmt"
"io/ioutil"
2019-11-25 04:45:13 +00:00
"os"
logging "github.com/ipfs/go-log/v2"
2019-11-25 04:45:13 +00:00
"github.com/mitchellh/go-homedir"
"gopkg.in/urfave/cli.v2"
2019-11-26 13:52:59 +00:00
"github.com/filecoin-project/go-address"
2020-02-21 20:56:30 +00:00
"github.com/filecoin-project/specs-actors/actors/abi"
"github.com/filecoin-project/specs-actors/actors/abi/big"
2020-02-06 19:56:35 +00:00
2020-02-29 02:02:40 +00:00
lapi "github.com/filecoin-project/lotus/api"
2019-11-26 13:52:59 +00:00
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/types"
2019-11-27 14:18:51 +00:00
"github.com/filecoin-project/lotus/cmd/lotus-seed/seed"
"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")
local := []*cli.Command{
2020-02-21 20:56:30 +00:00
genesisCmd,
2019-11-25 04:45:13 +00:00
preSealCmd,
aggregateManifestsCmd,
2019-11-25 04:45:13 +00:00
}
app := &cli.App{
Name: "lotus-seed",
Usage: "Seal sectors for genesis miner",
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)
2020-02-21 20:56:30 +00:00
os.Exit(1)
2019-11-25 04:45:13 +00:00
}
}
var preSealCmd = &cli.Command{
Name: "pre-seal",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "miner-addr",
2020-02-21 20:56:30 +00:00
Value: "t01000",
2019-11-25 04:45:13 +00:00
Usage: "specify the future address of your miner",
},
&cli.Uint64Flag{
Name: "sector-size",
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",
},
&cli.Uint64Flag{
Name: "sector-offset",
Value: 0,
Usage: "how many sector ids to skip when starting to seal",
},
&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
}
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
}
}
2020-02-29 02:02:40 +00:00
rp, _, err := lapi.ProofTypeFromSectorSize(abi.SectorSize(c.Uint64("sector-size")))
2020-02-27 00:42:39 +00:00
if err != nil {
return err
}
gm, key, err := seed.PreSeal(maddr, rp, 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
}
return seed.WriteGenesisMiner(maddr, sbroot, gm, key)
2019-11-25 04:45:13 +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 {
var inputs []map[string]genesis.Miner
for _, infi := range cctx.Args().Slice() {
fi, err := os.Open(infi)
if err != nil {
return err
}
defer fi.Close()
var val map[string]genesis.Miner
if err := json.NewDecoder(fi).Decode(&val); err != nil {
return err
}
inputs = append(inputs, val)
}
output := make(map[string]genesis.Miner)
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
},
}
func mergeGenMiners(a, b genesis.Miner) genesis.Miner {
if a.SectorSize != b.SectorSize {
panic("sector sizes mismatch")
}
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...),
}
}