diff --git a/chain/sync_test.go b/chain/sync_test.go new file mode 100644 index 000000000..acd700fa7 --- /dev/null +++ b/chain/sync_test.go @@ -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) + } + +} diff --git a/node/node_test.go b/node/node_test.go index a9fda6378..f120d508e 100644 --- a/node/node_test.go +++ b/node/node_test.go @@ -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, ) diff --git a/node/opts_test.go b/node/opts_test.go deleted file mode 100644 index 60e024a84..000000000 --- a/node/opts_test.go +++ /dev/null @@ -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), - ) -} diff --git a/node/testopts.go b/node/testopts.go new file mode 100644 index 000000000..5ec7a66bc --- /dev/null +++ b/node/testopts.go @@ -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), + ) +}