Only set genesis once

This commit is contained in:
Łukasz Magiera 2019-07-25 00:49:37 +02:00
parent 0569075442
commit 8d58c0a2fd
9 changed files with 105 additions and 19 deletions

View File

@ -315,7 +315,6 @@ func (cs *ChainStore) SetGenesis(b *BlockHeader) error {
if err != nil { if err != nil {
return err return err
} }
fts := &FullTipSet{ fts := &FullTipSet{
Blocks: []*FullBlock{ Blocks: []*FullBlock{
{Header: b}, {Header: b},

View File

@ -6,6 +6,9 @@ import (
"context" "context"
"github.com/filecoin-project/go-lotus/api" "github.com/filecoin-project/go-lotus/api"
"github.com/filecoin-project/go-lotus/node/modules"
"github.com/filecoin-project/go-lotus/node/modules/testing"
"github.com/multiformats/go-multiaddr" "github.com/multiformats/go-multiaddr"
"gopkg.in/urfave/cli.v2" "gopkg.in/urfave/cli.v2"
@ -13,6 +16,10 @@ import (
"github.com/filecoin-project/go-lotus/node/repo" "github.com/filecoin-project/go-lotus/node/repo"
) )
const (
makeGenFlag = "lotus-make-random-genesis"
)
// DaemonCmd is the `go-lotus daemon` command // DaemonCmd is the `go-lotus daemon` command
var DaemonCmd = &cli.Command{ var DaemonCmd = &cli.Command{
Name: "daemon", Name: "daemon",
@ -22,6 +29,15 @@ var DaemonCmd = &cli.Command{
Name: "api", Name: "api",
Value: "1234", Value: "1234",
}, },
&cli.StringFlag{
Name: makeGenFlag,
Value: "",
Hidden: true,
},
&cli.StringFlag{
Name: "genesis",
Usage: "genesis file to use for first node run",
},
}, },
Action: func(cctx *cli.Context) error { Action: func(cctx *cli.Context) error {
ctx := context.Background() ctx := context.Background()
@ -34,6 +50,14 @@ var DaemonCmd = &cli.Command{
return err return err
} }
genesis := node.Options()
if cctx.String(makeGenFlag) != "" {
genesis = node.Override(new(modules.Genesis), testing.MakeGenesis(cctx.String(makeGenFlag)))
}
if cctx.String("genesis") != "" {
genesis = node.Override(new(modules.Genesis), modules.LoadGenesis(cctx.String("genesis")))
}
var api api.FullNode var api api.FullNode
err = node.New(ctx, err = node.New(ctx,
node.FullAPI(&api), node.FullAPI(&api),
@ -41,6 +65,8 @@ var DaemonCmd = &cli.Command{
node.Online(), node.Online(),
node.Repo(r), node.Repo(r),
genesis,
node.Override(node.SetApiEndpointKey, func(lr repo.LockedRepo) error { node.Override(node.SetApiEndpointKey, func(lr repo.LockedRepo) error {
apima, err := multiaddr.NewMultiaddr("/ip4/127.0.0.1/tcp/" + cctx.String("api")) apima, err := multiaddr.NewMultiaddr("/ip4/127.0.0.1/tcp/" + cctx.String("api"))
if err != nil { if err != nil {

View File

@ -42,7 +42,6 @@ func (s *RPCServer) handleWS(ctx context.Context, w http.ResponseWriter, r *http
w.Header().Set("Sec-WebSocket-Protocol", r.Header.Get("Sec-WebSocket-Protocol")) w.Header().Set("Sec-WebSocket-Protocol", r.Header.Get("Sec-WebSocket-Protocol"))
} }
c, err := upgrader.Upgrade(w, r, nil) c, err := upgrader.Upgrade(w, r, nil)
if err != nil { if err != nil {
log.Error(err) log.Error(err)

View File

@ -196,7 +196,7 @@ func Online() Option {
Override(new(*chain.Wallet), chain.NewWallet), Override(new(*chain.Wallet), chain.NewWallet),
Override(new(*chain.MessagePool), chain.NewMessagePool), Override(new(*chain.MessagePool), chain.NewMessagePool),
Override(new(modules.Genesis), testing.MakeGenesis), Override(new(modules.Genesis), modules.ErrorGenesis),
Override(SetGenesisKey, modules.SetGenesis), Override(SetGenesisKey, modules.SetGenesis),
Override(new(*hello.Service), hello.NewHelloService), Override(new(*hello.Service), hello.NewHelloService),

View File

@ -8,7 +8,6 @@ import (
"path/filepath" "path/filepath"
"github.com/gbrlsnchs/jwt/v3" "github.com/gbrlsnchs/jwt/v3"
"github.com/ipfs/go-bitswap" "github.com/ipfs/go-bitswap"
"github.com/ipfs/go-bitswap/network" "github.com/ipfs/go-bitswap/network"
"github.com/ipfs/go-blockservice" "github.com/ipfs/go-blockservice"
@ -37,7 +36,7 @@ import (
var log = logging.Logger("modules") var log = logging.Logger("modules")
type Genesis *chain.BlockHeader type Genesis func() (*chain.BlockHeader, error)
// RecordValidator provides namesys compatible routing record validator // RecordValidator provides namesys compatible routing record validator
func RecordValidator(ps peerstore.Peerstore) record.Validator { func RecordValidator(ps peerstore.Peerstore) record.Validator {
@ -58,7 +57,20 @@ func Bitswap(mctx helpers.MetricsCtx, lc fx.Lifecycle, host host.Host, rt routin
} }
func SetGenesis(cs *chain.ChainStore, g Genesis) error { func SetGenesis(cs *chain.ChainStore, g Genesis) error {
return cs.SetGenesis(g) _, err := cs.GetGenesis()
if err == nil {
return nil // already set, noop
}
if err != datastore.ErrNotFound {
return err
}
genesis, err := g()
if err != nil {
return err
}
return cs.SetGenesis(genesis)
} }
func LockedRepo(lr repo.LockedRepo) func(lc fx.Lifecycle) repo.LockedRepo { func LockedRepo(lr repo.LockedRepo) func(lc fx.Lifecycle) repo.LockedRepo {
@ -176,3 +188,22 @@ func ChainStore(lc fx.Lifecycle, bs blockstore.Blockstore, ds datastore.Batching
return chain return chain
} }
func ErrorGenesis() Genesis {
return func() (header *chain.BlockHeader, e error) {
return nil, xerrors.New("No genesis block provided")
}
}
func LoadGenesis(f string) func() Genesis {
return func() Genesis {
return func() (header *chain.BlockHeader, e error) {
genBytes, err := ioutil.ReadFile(f)
if err != nil {
return &chain.BlockHeader{}, err
}
return chain.DecodeBlock(genBytes)
}
}
}

View File

@ -1,16 +1,47 @@
package testing package testing
import ( import (
"os"
blockstore "github.com/ipfs/go-ipfs-blockstore" blockstore "github.com/ipfs/go-ipfs-blockstore"
logging "github.com/ipfs/go-log"
"github.com/filecoin-project/go-lotus/chain" "github.com/filecoin-project/go-lotus/chain"
"github.com/filecoin-project/go-lotus/node/modules" "github.com/filecoin-project/go-lotus/node/modules"
) )
func MakeGenesis(bs blockstore.Blockstore, w *chain.Wallet) (modules.Genesis, error) { var glog = logging.Logger("genesis")
genb, err := chain.MakeGenesisBlock(bs, w)
func MakeGenesis(outFile string) func(bs blockstore.Blockstore, w *chain.Wallet) modules.Genesis {
return func(bs blockstore.Blockstore, w *chain.Wallet) modules.Genesis {
return func() (*chain.BlockHeader, error) {
glog.Warn("Generating new random genesis block, note that this SHOULD NOT happen unless you are setting up new network")
b, err := chain.MakeGenesisBlock(bs, w)
if err != nil { if err != nil {
return nil, err return nil, err
} }
return genb.Genesis, nil
f, err := os.OpenFile(outFile, os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
return nil, err
}
genBytes, err := b.Genesis.Serialize()
if err != nil {
return nil, err
}
if _, err := f.Write(genBytes); err != nil {
return nil, err
}
glog.Warnf("WRITING GENESIS FILE AT %s", f.Name())
if err := f.Close(); err != nil {
return nil, err
}
return b.Genesis, nil
}
}
} }