Only set genesis once
This commit is contained in:
parent
0569075442
commit
8d58c0a2fd
@ -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},
|
||||||
|
@ -76,7 +76,7 @@ var runCmd = &cli.Command{
|
|||||||
|
|
||||||
ah := &auth.Handler{
|
ah := &auth.Handler{
|
||||||
Verify: minerapi.AuthVerify,
|
Verify: minerapi.AuthVerify,
|
||||||
Next: rpcServer.ServeHTTP,
|
Next: rpcServer.ServeHTTP,
|
||||||
}
|
}
|
||||||
|
|
||||||
http.Handle("/rpc/v0", ah)
|
http.Handle("/rpc/v0", ah)
|
||||||
|
@ -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 {
|
||||||
|
@ -14,7 +14,7 @@ func serveRPC(a api.FullNode, addr string) error {
|
|||||||
|
|
||||||
ah := &auth.Handler{
|
ah := &auth.Handler{
|
||||||
Verify: a.AuthVerify,
|
Verify: a.AuthVerify,
|
||||||
Next: rpcServer.ServeHTTP,
|
Next: rpcServer.ServeHTTP,
|
||||||
}
|
}
|
||||||
|
|
||||||
http.Handle("/rpc/v0", ah)
|
http.Handle("/rpc/v0", ah)
|
||||||
|
@ -67,7 +67,7 @@ type client struct {
|
|||||||
namespace string
|
namespace string
|
||||||
|
|
||||||
requests chan clientRequest
|
requests chan clientRequest
|
||||||
idCtr int64
|
idCtr int64
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewMergeClient is like NewClient, but allows to specify multiple structs
|
// 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
|
ch.Send(val.Elem()) // todo: select on ctx is probably a good idea
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return func() reflect.Value { return retVal }, chCtor
|
return func() reflect.Value { return retVal }, chCtor
|
||||||
@ -192,12 +192,12 @@ type rpcFunc struct {
|
|||||||
ftyp reflect.Type
|
ftyp reflect.Type
|
||||||
name string
|
name string
|
||||||
|
|
||||||
nout int
|
nout int
|
||||||
valOut int
|
valOut int
|
||||||
errOut int
|
errOut int
|
||||||
|
|
||||||
hasCtx int
|
hasCtx int
|
||||||
retCh bool
|
retCh bool
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fn *rpcFunc) processResponse(resp clientResponse, rval reflect.Value) []reflect.Value {
|
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{
|
fun := &rpcFunc{
|
||||||
client: c,
|
client: c,
|
||||||
ftyp: ftyp,
|
ftyp: ftyp,
|
||||||
name: f.Name,
|
name: f.Name,
|
||||||
}
|
}
|
||||||
fun.valOut, fun.errOut, fun.nout = processFuncOut(ftyp)
|
fun.valOut, fun.errOut, fun.nout = processFuncOut(ftyp)
|
||||||
|
|
||||||
|
@ -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)
|
||||||
|
@ -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),
|
||||||
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
@ -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)
|
|
||||||
if err != nil {
|
func MakeGenesis(outFile string) func(bs blockstore.Blockstore, w *chain.Wallet) modules.Genesis {
|
||||||
return nil, err
|
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
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user