diff --git a/chain/chain.go b/chain/chain.go index 5757219ea..61ed7dea7 100644 --- a/chain/chain.go +++ b/chain/chain.go @@ -315,7 +315,6 @@ func (cs *ChainStore) SetGenesis(b *BlockHeader) error { if err != nil { return err } - fts := &FullTipSet{ Blocks: []*FullBlock{ {Header: b}, diff --git a/cmd/lotus-storage-miner/run.go b/cmd/lotus-storage-miner/run.go index 1cc209532..42a26eb23 100644 --- a/cmd/lotus-storage-miner/run.go +++ b/cmd/lotus-storage-miner/run.go @@ -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) diff --git a/cmd/lotus/daemon.go b/cmd/lotus/daemon.go index b8a88bc33..6921a8bac 100644 --- a/cmd/lotus/daemon.go +++ b/cmd/lotus/daemon.go @@ -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 { diff --git a/cmd/lotus/rpc.go b/cmd/lotus/rpc.go index a0be6ecbe..68ac13c87 100644 --- a/cmd/lotus/rpc.go +++ b/cmd/lotus/rpc.go @@ -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) diff --git a/lib/jsonrpc/client.go b/lib/jsonrpc/client.go index 448418c67..9d1053222 100644 --- a/lib/jsonrpc/client.go +++ b/lib/jsonrpc/client.go @@ -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) diff --git a/lib/jsonrpc/server.go b/lib/jsonrpc/server.go index 18d2da48f..4468c53c4 100644 --- a/lib/jsonrpc/server.go +++ b/lib/jsonrpc/server.go @@ -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) diff --git a/node/builder.go b/node/builder.go index 2bef9ab70..ce11ff085 100644 --- a/node/builder.go +++ b/node/builder.go @@ -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), diff --git a/node/modules/core.go b/node/modules/core.go index 388c6df19..e3fe93f5d 100644 --- a/node/modules/core.go +++ b/node/modules/core.go @@ -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) + } + } +} diff --git a/node/modules/testing/genesis.go b/node/modules/testing/genesis.go index 094302286..cf610b71f 100644 --- a/node/modules/testing/genesis.go +++ b/node/modules/testing/genesis.go @@ -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 }