2019-07-29 19:34:34 +00:00
|
|
|
package chain_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
2019-07-30 11:45:48 +00:00
|
|
|
"time"
|
2019-07-29 19:34:34 +00:00
|
|
|
|
2019-07-30 13:55:36 +00:00
|
|
|
logging "github.com/ipfs/go-log"
|
2019-07-29 19:34:34 +00:00
|
|
|
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
|
2019-07-30 11:45:48 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2019-07-29 19:34:34 +00:00
|
|
|
|
|
|
|
"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"
|
|
|
|
"github.com/filecoin-project/go-lotus/node/repo"
|
|
|
|
)
|
|
|
|
|
2019-07-30 13:55:36 +00:00
|
|
|
const source = 0
|
|
|
|
|
2019-07-30 11:45:48 +00:00
|
|
|
func repoWithChain(t *testing.T, h int) (repo.Repo, []byte) {
|
2019-07-29 19:34:34 +00:00
|
|
|
g, err := gen.NewGenerator()
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
for i := 0; i < h; i++ {
|
|
|
|
b, err := g.NextBlock()
|
2019-07-30 11:45:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, uint64(i+1), b.Header.Height, "wrong height")
|
2019-07-29 19:34:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
r, err := g.YieldRepo()
|
2019-07-30 11:45:48 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
genb, err := g.GenesisCar()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
return r, genb
|
2019-07-29 19:34:34 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 13:20:40 +00:00
|
|
|
type syncTestUtil struct {
|
|
|
|
t *testing.T
|
2019-07-29 19:34:34 +00:00
|
|
|
|
2019-07-30 13:20:40 +00:00
|
|
|
ctx context.Context
|
|
|
|
mn mocknet.Mocknet
|
2019-07-29 19:34:34 +00:00
|
|
|
|
2019-07-30 13:20:40 +00:00
|
|
|
genesis []byte
|
|
|
|
|
|
|
|
nds []api.FullNode
|
|
|
|
}
|
|
|
|
|
2019-07-30 13:55:36 +00:00
|
|
|
func (tu *syncTestUtil) addSourceNode(gen int) {
|
2019-07-30 13:20:40 +00:00
|
|
|
if tu.genesis != nil {
|
|
|
|
tu.t.Fatal("source node already exists")
|
|
|
|
}
|
2019-07-29 19:34:34 +00:00
|
|
|
|
2019-07-30 13:20:40 +00:00
|
|
|
sourceRepo, genesis := repoWithChain(tu.t, gen)
|
|
|
|
var out api.FullNode
|
2019-07-30 11:45:48 +00:00
|
|
|
|
2019-07-30 13:20:40 +00:00
|
|
|
err := node.New(tu.ctx,
|
|
|
|
node.FullAPI(&out),
|
2019-07-29 19:34:34 +00:00
|
|
|
node.Online(),
|
2019-07-30 11:45:48 +00:00
|
|
|
node.Repo(sourceRepo),
|
2019-07-30 13:20:40 +00:00
|
|
|
node.MockHost(tu.mn),
|
2019-07-29 19:34:34 +00:00
|
|
|
|
2019-07-30 11:45:48 +00:00
|
|
|
node.Override(new(modules.Genesis), modules.LoadGenesis(genesis)),
|
2019-07-29 19:34:34 +00:00
|
|
|
)
|
2019-07-30 13:20:40 +00:00
|
|
|
require.NoError(tu.t, err)
|
2019-07-29 19:34:34 +00:00
|
|
|
|
2019-07-30 13:20:40 +00:00
|
|
|
tu.genesis = genesis
|
2019-07-30 13:55:36 +00:00
|
|
|
tu.nds = append(tu.nds, out) // always at 0
|
2019-07-30 13:20:40 +00:00
|
|
|
}
|
2019-07-30 11:45:48 +00:00
|
|
|
|
2019-07-30 13:20:40 +00:00
|
|
|
func (tu *syncTestUtil) addClientNode() int {
|
2019-07-30 13:55:36 +00:00
|
|
|
if tu.genesis == nil {
|
|
|
|
tu.t.Fatal("source doesn't exists")
|
|
|
|
}
|
|
|
|
|
2019-07-30 13:20:40 +00:00
|
|
|
var out api.FullNode
|
2019-07-29 19:34:34 +00:00
|
|
|
|
2019-07-30 13:20:40 +00:00
|
|
|
err := node.New(tu.ctx,
|
|
|
|
node.FullAPI(&out),
|
2019-07-29 19:34:34 +00:00
|
|
|
node.Online(),
|
|
|
|
node.Repo(repo.NewMemory(nil)),
|
2019-07-30 13:20:40 +00:00
|
|
|
node.MockHost(tu.mn),
|
2019-07-29 19:34:34 +00:00
|
|
|
|
2019-07-30 13:20:40 +00:00
|
|
|
node.Override(new(modules.Genesis), modules.LoadGenesis(tu.genesis)),
|
2019-07-29 19:34:34 +00:00
|
|
|
)
|
2019-07-30 13:20:40 +00:00
|
|
|
require.NoError(tu.t, err)
|
|
|
|
|
|
|
|
tu.nds = append(tu.nds, out)
|
|
|
|
return len(tu.nds) - 1
|
|
|
|
}
|
|
|
|
|
2019-07-30 13:55:36 +00:00
|
|
|
func (tu *syncTestUtil) connect(from, to int) {
|
|
|
|
toPI, err := tu.nds[to].NetAddrsListen(tu.ctx)
|
|
|
|
require.NoError(tu.t, err)
|
2019-07-30 13:20:40 +00:00
|
|
|
|
2019-07-30 13:55:36 +00:00
|
|
|
err = tu.nds[from].NetConnect(tu.ctx, toPI)
|
|
|
|
require.NoError(tu.t, err)
|
|
|
|
}
|
2019-07-30 13:20:40 +00:00
|
|
|
|
2019-07-30 13:55:36 +00:00
|
|
|
func (tu *syncTestUtil) checkHeight(name string, n int, h int) {
|
|
|
|
b, err := tu.nds[n].ChainHead(tu.ctx)
|
|
|
|
require.NoError(tu.t, err)
|
2019-07-30 13:20:40 +00:00
|
|
|
|
2019-07-30 13:55:36 +00:00
|
|
|
require.Equal(tu.t, uint64(h), b.Height())
|
|
|
|
fmt.Printf("%s H: %d\n", name, b.Height())
|
|
|
|
}
|
2019-07-30 13:20:40 +00:00
|
|
|
|
2019-07-30 13:55:36 +00:00
|
|
|
func (tu *syncTestUtil) compareSourceState(with int) {
|
|
|
|
sourceAccounts, err := tu.nds[source].WalletList(tu.ctx)
|
|
|
|
require.NoError(tu.t, err)
|
2019-07-30 13:20:40 +00:00
|
|
|
|
2019-07-30 13:55:36 +00:00
|
|
|
for _, addr := range sourceAccounts {
|
|
|
|
sourceBalance, err := tu.nds[source].WalletBalance(tu.ctx, addr)
|
|
|
|
require.NoError(tu.t, err)
|
2019-07-30 11:45:48 +00:00
|
|
|
|
2019-07-30 13:55:36 +00:00
|
|
|
actBalance, err := tu.nds[with].WalletBalance(tu.ctx, addr)
|
|
|
|
require.NoError(tu.t, err)
|
2019-07-30 13:20:40 +00:00
|
|
|
|
2019-07-30 13:55:36 +00:00
|
|
|
require.Equal(tu.t, sourceBalance, actBalance)
|
|
|
|
}
|
|
|
|
}
|
2019-07-30 13:20:40 +00:00
|
|
|
|
2019-07-30 13:55:36 +00:00
|
|
|
func TestSyncSimple(t *testing.T) {
|
|
|
|
logging.SetLogLevel("*", "INFO")
|
2019-07-30 13:20:40 +00:00
|
|
|
|
2019-07-30 13:55:36 +00:00
|
|
|
H := 20
|
|
|
|
ctx := context.Background()
|
|
|
|
tu := &syncTestUtil{
|
|
|
|
t: t,
|
|
|
|
ctx: ctx,
|
|
|
|
mn: mocknet.New(ctx),
|
|
|
|
}
|
2019-07-30 11:45:48 +00:00
|
|
|
|
2019-07-30 13:55:36 +00:00
|
|
|
tu.addSourceNode(H)
|
|
|
|
tu.checkHeight("source", source, H)
|
2019-07-30 11:45:48 +00:00
|
|
|
|
2019-07-30 13:55:36 +00:00
|
|
|
// separate logs
|
|
|
|
fmt.Println("\x1b[31m///////////////////////////////////////////////////\x1b[39b")
|
2019-07-30 11:45:48 +00:00
|
|
|
|
2019-07-30 13:55:36 +00:00
|
|
|
client := tu.addClientNode()
|
|
|
|
tu.checkHeight("client", client, 0)
|
2019-07-30 11:45:48 +00:00
|
|
|
|
2019-07-30 13:55:36 +00:00
|
|
|
require.NoError(t, tu.mn.LinkAll())
|
|
|
|
tu.connect(1, 0)
|
2019-07-30 13:20:40 +00:00
|
|
|
time.Sleep(time.Second * 1)
|
2019-07-29 19:34:34 +00:00
|
|
|
|
2019-07-30 13:55:36 +00:00
|
|
|
tu.checkHeight("client", client, H)
|
|
|
|
|
|
|
|
tu.compareSourceState(client)
|
2019-07-29 19:34:34 +00:00
|
|
|
}
|