Generate single genesis in tests

This commit is contained in:
Łukasz Magiera 2019-07-25 01:23:06 +02:00
parent 8d58c0a2fd
commit f2abb33933
4 changed files with 48 additions and 8 deletions

View File

@ -4,6 +4,7 @@ package main
import (
"context"
"io/ioutil"
"github.com/filecoin-project/go-lotus/api"
"github.com/filecoin-project/go-lotus/node/modules"
@ -55,7 +56,12 @@ var DaemonCmd = &cli.Command{
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")))
genBytes, err := ioutil.ReadFile(cctx.String("genesis"))
if err != nil {
return err
}
genesis = node.Override(new(modules.Genesis), modules.LoadGenesis(genBytes))
}
var api api.FullNode

View File

@ -195,14 +195,9 @@ func ErrorGenesis() Genesis {
}
}
func LoadGenesis(f string) func() Genesis {
func LoadGenesis(genBytes []byte) 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,6 +1,7 @@
package testing
import (
"io"
"os"
blockstore "github.com/ipfs/go-ipfs-blockstore"
@ -12,6 +13,29 @@ import (
var glog = logging.Logger("genesis")
func MakeGenesisMem(out io.Writer) 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
}
genBytes, err := b.Genesis.Serialize()
if err != nil {
return nil, err
}
if _, err := out.Write(genBytes); err != nil {
return nil, err
}
return b.Genesis, nil
}
}
}
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) {

View File

@ -1,19 +1,23 @@
package node_test
import (
"bytes"
"context"
"net/http/httptest"
"testing"
"github.com/filecoin-project/go-lotus/node"
"github.com/filecoin-project/go-lotus/node/modules"
modtest "github.com/filecoin-project/go-lotus/node/modules/testing"
"github.com/filecoin-project/go-lotus/api"
"github.com/filecoin-project/go-lotus/api/client"
"github.com/filecoin-project/go-lotus/api/test"
"github.com/filecoin-project/go-lotus/lib/jsonrpc"
"github.com/filecoin-project/go-lotus/node/repo"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
"github.com/filecoin-project/go-lotus/node/repo"
)
func builder(t *testing.T, n int) []api.FullNode {
@ -22,13 +26,24 @@ func builder(t *testing.T, n int) []api.FullNode {
out := make([]api.FullNode, n)
var genbuf bytes.Buffer
for i := 0; i < n; i++ {
var genesis node.Option
if i == 0 {
genesis = node.Override(new(modules.Genesis), modtest.MakeGenesisMem(&genbuf))
} else {
genesis = node.Override(new(modules.Genesis), modules.LoadGenesis(genbuf.Bytes()))
}
var err error
err = node.New(ctx,
node.FullAPI(&out[i]),
node.Online(),
node.Repo(repo.NewMemory(nil)),
MockHost(mn),
genesis,
)
if err != nil {
t.Fatal(err)