chain: WIP Generated node sync test

This commit is contained in:
Łukasz Magiera 2019-07-29 21:34:34 +02:00 committed by whyrusleeping
parent 7f5cba1749
commit a656aea7fe
4 changed files with 102 additions and 22 deletions

81
chain/sync_test.go Normal file
View File

@ -0,0 +1,81 @@
package chain_test
import (
"bytes"
"context"
"fmt"
"testing"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
"github.com/filecoin-project/go-lotus/api"
"github.com/filecoin-project/go-lotus/chain/gen"
"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/node/repo"
)
func repoWithChain(t *testing.T, h int) repo.Repo {
g, err := gen.NewGenerator()
if err != nil {
t.Fatal(err)
}
for i := 0; i < h; i++ {
b, err := g.NextBlock()
if err != nil {
t.Fatalf("error at H:%d, %s", i, err)
}
if b.Header.Height != uint64(i+1) {
t.Fatal("wrong height")
}
}
r, err := g.YieldRepo()
if err != nil {
t.Fatal(err)
}
return r
}
func TestSyncSimple(t *testing.T) {
ctx := context.Background()
var genbuf bytes.Buffer
var source api.FullNode
var client api.FullNode
mn := mocknet.New(ctx)
err := node.New(ctx,
node.FullAPI(&source),
node.Online(),
node.Repo(repoWithChain(t, 20)),
node.MockHost(mn),
node.Override(new(modules.Genesis), modtest.MakeGenesisMem(&genbuf)),
)
if err != nil {
t.Fatal(err)
}
b, err := source.ChainHead(ctx)
if err != nil {
t.Fatal(err)
}
fmt.Println(b.Height())
err = node.New(ctx,
node.FullAPI(&client),
node.Online(),
node.Repo(repo.NewMemory(nil)),
node.MockHost(mn),
node.Override(new(modules.Genesis), modules.LoadGenesis(genbuf.Bytes())),
)
if err != nil {
t.Fatal(err)
}
}

View File

@ -40,7 +40,7 @@ func builder(t *testing.T, n int) []api.FullNode {
node.FullAPI(&out[i]),
node.Online(),
node.Repo(repo.NewMemory(nil)),
MockHost(mn),
node.MockHost(mn),
genesis,
)

View File

@ -1,21 +0,0 @@
package node_test
import (
"errors"
"github.com/filecoin-project/go-lotus/node"
"github.com/filecoin-project/go-lotus/node/modules/lp2p"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
)
func MockHost(mn mocknet.Mocknet) node.Option {
return node.Options(
node.ApplyIf(func(s *node.Settings) bool { return !s.Online },
node.Error(errors.New("MockHost must be specified after Online")),
),
node.Override(new(lp2p.RawHost), lp2p.MockHost),
node.Override(new(mocknet.Mocknet), mn),
)
}

20
node/testopts.go Normal file
View File

@ -0,0 +1,20 @@
package node
import (
"errors"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
"github.com/filecoin-project/go-lotus/node/modules/lp2p"
)
func MockHost(mn mocknet.Mocknet) Option {
return Options(
ApplyIf(func(s *Settings) bool { return !s.Online },
Error(errors.New("MockHost must be specified after Online")),
),
Override(new(lp2p.RawHost), lp2p.MockHost),
Override(new(mocknet.Mocknet), mn),
)
}