lotus/cmd/lotus-seed/genesis.go

144 lines
3.5 KiB
Go
Raw Normal View History

2020-02-21 20:56:30 +00:00
package main
import (
"encoding/json"
"io/ioutil"
"github.com/google/uuid"
"github.com/mitchellh/go-homedir"
"github.com/urfave/cli/v2"
2020-06-05 22:59:01 +00:00
"golang.org/x/xerrors"
2020-02-21 20:56:30 +00:00
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/specs-actors/actors/abi/big"
"github.com/filecoin-project/lotus/build"
2020-02-21 20:56:30 +00:00
genesis2 "github.com/filecoin-project/lotus/chain/gen/genesis"
"github.com/filecoin-project/lotus/genesis"
)
var genesisCmd = &cli.Command{
Name: "genesis",
Description: "manipulate lotus genesis template",
Subcommands: []*cli.Command{
genesisNewCmd,
genesisAddMinerCmd,
},
}
var genesisNewCmd = &cli.Command{
Name: "new",
Description: "create new genesis template",
Flags: []cli.Flag{
&cli.StringFlag{
Name: "network-name",
},
},
Action: func(cctx *cli.Context) error {
if !cctx.Args().Present() {
return xerrors.New("seed genesis new [genesis.json]")
}
out := genesis.Template{
2020-02-23 00:47:47 +00:00
Accounts: []genesis.Actor{},
Miners: []genesis.Miner{},
2020-02-21 20:56:30 +00:00
NetworkName: cctx.String("network-name"),
}
if out.NetworkName == "" {
out.NetworkName = "localnet-" + uuid.New().String()
}
2020-02-23 00:47:47 +00:00
genb, err := json.MarshalIndent(&out, "", " ")
2020-02-21 20:56:30 +00:00
if err != nil {
return err
}
genf, err := homedir.Expand(cctx.Args().First())
if err != nil {
return err
}
if err := ioutil.WriteFile(genf, genb, 0644); err != nil {
return err
}
return nil
},
}
var genesisAddMinerCmd = &cli.Command{
Name: "add-miner",
Description: "add genesis miner",
2020-02-23 00:47:47 +00:00
Flags: []cli.Flag{},
2020-02-21 20:56:30 +00:00
Action: func(cctx *cli.Context) error {
if cctx.Args().Len() != 2 {
return xerrors.New("seed genesis add-miner [genesis.json] [preseal.json]")
}
genf, err := homedir.Expand(cctx.Args().First())
if err != nil {
return err
}
var template genesis.Template
genb, err := ioutil.ReadFile(genf)
if err != nil {
return xerrors.Errorf("read genesis template: %w", err)
}
if err := json.Unmarshal(genb, &template); err != nil {
return xerrors.Errorf("unmarshal genesis template: %w", err)
}
minf, err := homedir.Expand(cctx.Args().Get(1))
if err != nil {
return xerrors.Errorf("expand preseal file path: %w", err)
}
miners := map[string]genesis.Miner{}
minb, err := ioutil.ReadFile(minf)
if err != nil {
return xerrors.Errorf("read preseal file: %w", err)
}
if err := json.Unmarshal(minb, &miners); err != nil {
return xerrors.Errorf("unmarshal miner info: %w", err)
}
for mn, miner := range miners {
log.Infof("Adding miner %s to genesis template", mn)
{
id := uint64(genesis2.MinerStart) + uint64(len(template.Miners))
maddr, err := address.NewFromString(mn)
if err != nil {
return xerrors.Errorf("parsing miner address: %w", err)
}
mid, err := address.IDFromAddress(maddr)
if err != nil {
return xerrors.Errorf("getting miner id from address: %w", err)
}
if mid != id {
return xerrors.Errorf("tried to set miner t0%d as t0%d", mid, id)
}
}
template.Miners = append(template.Miners, miner)
log.Infof("Giving %s some initial balance", miner.Owner)
template.Accounts = append(template.Accounts, genesis.Actor{
Type: genesis.TAccount,
Balance: big.Mul(big.NewInt(50_000_000), big.NewInt(build.FilecoinPrecision)),
2020-02-21 20:56:30 +00:00
Meta: (&genesis.AccountMeta{Owner: miner.Owner}).ActorMeta(),
})
}
2020-02-23 00:47:47 +00:00
genb, err = json.MarshalIndent(&template, "", " ")
2020-02-21 20:56:30 +00:00
if err != nil {
return err
}
if err := ioutil.WriteFile(genf, genb, 0644); err != nil {
return err
}
return nil
},
}