chain: Test basinc sync on generated chain

This commit is contained in:
Łukasz Magiera 2019-07-30 13:45:48 +02:00 committed by whyrusleeping
parent a656aea7fe
commit 9ef5e1266e
3 changed files with 65 additions and 30 deletions

View File

@ -1,8 +1,14 @@
package gen package gen
import ( import (
"bytes"
"context" "context"
"github.com/ipfs/go-blockservice"
"github.com/ipfs/go-car"
offline "github.com/ipfs/go-ipfs-exchange-offline"
"github.com/ipfs/go-merkledag"
"github.com/filecoin-project/go-lotus/chain/address" "github.com/filecoin-project/go-lotus/chain/address"
"github.com/filecoin-project/go-lotus/chain/store" "github.com/filecoin-project/go-lotus/chain/store"
"github.com/filecoin-project/go-lotus/chain/types" "github.com/filecoin-project/go-lotus/chain/types"
@ -67,7 +73,7 @@ func NewGenerator() (*ChainGen, error) {
return nil, err return nil, err
} }
bs := mybs{blockstore.NewBlockstore(bds)} bs := mybs{blockstore.NewIdStore(blockstore.NewBlockstore(bds))}
ks, err := lr.KeyStore() ks, err := lr.KeyStore()
if err != nil { if err != nil {
@ -125,6 +131,20 @@ func (cg *ChainGen) Genesis() *types.BlockHeader {
return cg.genesis return cg.genesis
} }
func (cg *ChainGen) GenesisCar() ([]byte, error) {
offl := offline.Exchange(cg.bs)
blkserv := blockservice.New(cg.bs, offl)
dserv := merkledag.NewDAGService(blkserv)
out := new(bytes.Buffer)
if err := car.WriteCar(context.TODO(), dserv, []cid.Cid{cg.Genesis().Cid()}, out); err != nil {
return nil, err
}
return out.Bytes(), nil
}
func (cg *ChainGen) nextBlockProof() (address.Address, types.ElectionProof, []types.Ticket, error) { func (cg *ChainGen) nextBlockProof() (address.Address, types.ElectionProof, []types.Ticket, error) {
return cg.miner, []byte("cat in a box"), []types.Ticket{types.Ticket("im a ticket, promise")}, nil return cg.miner, []byte("cat in a box"), []types.Ticket{types.Ticket("im a ticket, promise")}, nil
} }

View File

@ -1,22 +1,22 @@
package chain_test package chain_test
import ( import (
"bytes"
"context" "context"
"fmt" "fmt"
"testing" "testing"
"time"
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock" mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
"github.com/stretchr/testify/require"
"github.com/filecoin-project/go-lotus/api" "github.com/filecoin-project/go-lotus/api"
"github.com/filecoin-project/go-lotus/chain/gen" "github.com/filecoin-project/go-lotus/chain/gen"
"github.com/filecoin-project/go-lotus/node" "github.com/filecoin-project/go-lotus/node"
"github.com/filecoin-project/go-lotus/node/modules" "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" "github.com/filecoin-project/go-lotus/node/repo"
) )
func repoWithChain(t *testing.T, h int) repo.Repo { func repoWithChain(t *testing.T, h int) (repo.Repo, []byte) {
g, err := gen.NewGenerator() g, err := gen.NewGenerator()
if err != nil { if err != nil {
t.Fatal(err) t.Fatal(err)
@ -24,47 +24,44 @@ func repoWithChain(t *testing.T, h int) repo.Repo {
for i := 0; i < h; i++ { for i := 0; i < h; i++ {
b, err := g.NextBlock() b, err := g.NextBlock()
if err != nil { require.NoError(t, err)
t.Fatalf("error at H:%d, %s", i, err) require.Equal(t, uint64(i+1), b.Header.Height, "wrong height")
}
if b.Header.Height != uint64(i+1) {
t.Fatal("wrong height")
}
} }
r, err := g.YieldRepo() r, err := g.YieldRepo()
if err != nil { require.NoError(t, err)
t.Fatal(err)
} genb, err := g.GenesisCar()
return r require.NoError(t, err)
return r, genb
} }
func TestSyncSimple(t *testing.T) { func TestSyncSimple(t *testing.T) {
ctx := context.Background() ctx := context.Background()
var genbuf bytes.Buffer
var source api.FullNode var source api.FullNode
var client api.FullNode var client api.FullNode
mn := mocknet.New(ctx) mn := mocknet.New(ctx)
sourceRepo, genesis := repoWithChain(t, 20)
err := node.New(ctx, err := node.New(ctx,
node.FullAPI(&source), node.FullAPI(&source),
node.Online(), node.Online(),
node.Repo(repoWithChain(t, 20)), node.Repo(sourceRepo),
node.MockHost(mn), node.MockHost(mn),
node.Override(new(modules.Genesis), modtest.MakeGenesisMem(&genbuf)), node.Override(new(modules.Genesis), modules.LoadGenesis(genesis)),
) )
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
b, err := source.ChainHead(ctx) b, err := source.ChainHead(ctx)
if err != nil { require.NoError(t, err)
t.Fatal(err)
} require.Equal(t, uint64(20), b.Height())
fmt.Println(b.Height()) fmt.Printf("source H: %d\n", b.Height())
err = node.New(ctx, err = node.New(ctx,
node.FullAPI(&client), node.FullAPI(&client),
@ -72,10 +69,27 @@ func TestSyncSimple(t *testing.T) {
node.Repo(repo.NewMemory(nil)), node.Repo(repo.NewMemory(nil)),
node.MockHost(mn), node.MockHost(mn),
node.Override(new(modules.Genesis), modules.LoadGenesis(genbuf.Bytes())), node.Override(new(modules.Genesis), modules.LoadGenesis(genesis)),
) )
if err != nil { require.NoError(t, err)
t.Fatal(err)
}
require.NoError(t, mn.LinkAll())
cb, err := client.ChainHead(ctx)
require.NoError(t, err)
require.Equal(t, uint64(0), cb.Height())
fmt.Printf("client H: %d\n", cb.Height())
sourcePI, err := source.NetAddrsListen(ctx)
require.NoError(t, err)
err = client.NetConnect(ctx, sourcePI)
require.NoError(t, err)
time.Sleep(time.Second)
cb, err = client.ChainHead(ctx)
require.NoError(t, err)
require.Equal(t, uint64(20), cb.Height())
fmt.Printf("client H: %d\n", cb.Height())
} }

View File

@ -335,6 +335,7 @@ func New(ctx context.Context, opts ...Option) error {
// on this context, and implement closing logic through lifecycles // on this context, and implement closing logic through lifecycles
// correctly // correctly
if err := app.Start(ctx); err != nil { if err := app.Start(ctx); err != nil {
// comment fx.NopLogger few lines above for easier debugging
return err return err
} }