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 {
return err
}
fts := &FullTipSet{
Blocks: []*FullBlock{
{Header: b},

View File

@ -76,7 +76,7 @@ var runCmd = &cli.Command{
ah := &auth.Handler{
Verify: minerapi.AuthVerify,
Next: rpcServer.ServeHTTP,
Next: rpcServer.ServeHTTP,
}
http.Handle("/rpc/v0", ah)

View File

@ -6,6 +6,9 @@ import (
"context"
"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"
"gopkg.in/urfave/cli.v2"
@ -13,6 +16,10 @@ import (
"github.com/filecoin-project/go-lotus/node/repo"
)
const (
makeGenFlag = "lotus-make-random-genesis"
)
// DaemonCmd is the `go-lotus daemon` command
var DaemonCmd = &cli.Command{
Name: "daemon",
@ -22,6 +29,15 @@ var DaemonCmd = &cli.Command{
Name: "api",
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 {
ctx := context.Background()
@ -34,6 +50,14 @@ var DaemonCmd = &cli.Command{
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
err = node.New(ctx,
node.FullAPI(&api),
@ -41,6 +65,8 @@ var DaemonCmd = &cli.Command{
node.Online(),
node.Repo(r),
genesis,
node.Override(node.SetApiEndpointKey, func(lr repo.LockedRepo) error {
apima, err := multiaddr.NewMultiaddr("/ip4/127.0.0.1/tcp/" + cctx.String("api"))
if err != nil {

View File

@ -14,7 +14,7 @@ func serveRPC(a api.FullNode, addr string) error {
ah := &auth.Handler{
Verify: a.AuthVerify,
Next: rpcServer.ServeHTTP,
Next: rpcServer.ServeHTTP,
}
http.Handle("/rpc/v0", ah)

View File

@ -67,7 +67,7 @@ type client struct {
namespace string
requests chan clientRequest
idCtr int64
idCtr int64
}
// NewMergeClient is like NewClient, but allows to specify multiple structs
@ -143,7 +143,7 @@ func (c *client) makeOutChan(ctx context.Context, ftyp reflect.Type, valOut int)
}
ch.Send(val.Elem()) // todo: select on ctx is probably a good idea
}
}
}
return func() reflect.Value { return retVal }, chCtor
@ -192,12 +192,12 @@ type rpcFunc struct {
ftyp reflect.Type
name string
nout int
nout int
valOut int
errOut int
hasCtx int
retCh bool
retCh bool
}
func (fn *rpcFunc) processResponse(resp clientResponse, rval reflect.Value) []reflect.Value {
@ -290,8 +290,8 @@ func (c *client) makeRpcFunc(f reflect.StructField) (reflect.Value, error) {
fun := &rpcFunc{
client: c,
ftyp: ftyp,
name: f.Name,
ftyp: ftyp,
name: f.Name,
}
fun.valOut, fun.errOut, fun.nout = processFuncOut(ftyp)

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"))
}
c, err := upgrader.Upgrade(w, r, nil)
if err != nil {
log.Error(err)

View File

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

View File

@ -8,7 +8,6 @@ import (
"path/filepath"
"github.com/gbrlsnchs/jwt/v3"
"github.com/ipfs/go-bitswap"
"github.com/ipfs/go-bitswap/network"
"github.com/ipfs/go-blockservice"
@ -37,7 +36,7 @@ import (
var log = logging.Logger("modules")
type Genesis *chain.BlockHeader
type Genesis func() (*chain.BlockHeader, error)
// RecordValidator provides namesys compatible routing 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 {
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 {
@ -176,3 +188,22 @@ func ChainStore(lc fx.Lifecycle, bs blockstore.Blockstore, ds datastore.Batching
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
import (
"os"
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/node/modules"
)
func MakeGenesis(bs blockstore.Blockstore, w *chain.Wallet) (modules.Genesis, error) {
genb, err := chain.MakeGenesisBlock(bs, w)
if err != nil {
return nil, err
var glog = logging.Logger("genesis")
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 {
return nil, err
}
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
}
}
return genb.Genesis, nil
}