lotus/node/node_test.go

207 lines
5.0 KiB
Go
Raw Normal View History

2019-07-10 13:06:04 +00:00
package node_test
2019-07-09 16:27:07 +00:00
import (
2019-07-24 23:23:06 +00:00
"bytes"
2019-07-09 16:27:07 +00:00
"context"
2019-09-23 15:27:30 +00:00
"io/ioutil"
2019-07-09 16:36:40 +00:00
"net/http/httptest"
2019-09-23 15:27:30 +00:00
"os"
2019-07-09 16:27:07 +00:00
"testing"
2019-09-23 15:27:30 +00:00
"github.com/ipfs/go-datastore"
"github.com/libp2p/go-libp2p-core/peer"
2019-07-24 23:25:14 +00:00
mocknet "github.com/libp2p/go-libp2p/p2p/net/mock"
2019-09-23 15:27:30 +00:00
"github.com/stretchr/testify/require"
2019-07-10 17:28:49 +00:00
2019-07-09 16:27:07 +00:00
"github.com/filecoin-project/go-lotus/api"
2019-07-09 16:36:40 +00:00
"github.com/filecoin-project/go-lotus/api/client"
"github.com/filecoin-project/go-lotus/api/test"
2019-09-23 15:27:30 +00:00
"github.com/filecoin-project/go-lotus/chain/actors"
"github.com/filecoin-project/go-lotus/chain/address"
"github.com/filecoin-project/go-lotus/chain/types"
2019-07-09 16:36:40 +00:00
"github.com/filecoin-project/go-lotus/lib/jsonrpc"
2019-09-23 15:27:30 +00:00
"github.com/filecoin-project/go-lotus/lib/sectorbuilder"
"github.com/filecoin-project/go-lotus/miner"
"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"
2019-07-09 16:27:07 +00:00
)
2019-09-23 15:27:30 +00:00
func testStorageNode(ctx context.Context, t *testing.T, waddr address.Address, act address.Address, tnd test.TestNode) test.TestStorageNode {
r := repo.NewMemory(nil)
lr, err := r.Lock()
require.NoError(t, err)
p2pSk, err := lr.Libp2pIdentity()
require.NoError(t, err)
ds, err := lr.Datastore("/metadata")
require.NoError(t, err)
err = ds.Put(datastore.NewKey("miner-address"), act.Bytes())
require.NoError(t, err)
err = lr.Close()
require.NoError(t, err)
peerid, err := peer.IDFromPrivateKey(p2pSk)
require.NoError(t, err)
enc, err := actors.SerializeParams(&actors.UpdatePeerIDParams{PeerID: peerid})
require.NoError(t, err)
msg := &types.Message{
To: act,
From: waddr,
Method: actors.MAMethods.UpdatePeerID,
Params: enc,
Value: types.NewInt(0),
GasPrice: types.NewInt(0),
GasLimit: types.NewInt(1000000),
}
_, err = tnd.MpoolPushMessage(ctx, msg)
require.NoError(t, err)
// start node
secbpath, err := ioutil.TempDir(os.TempDir(), "lotust-stortest-sb-")
require.NoError(t, err)
var minerapi api.StorageMiner
// TODO: use stop
_, err = node.New(ctx,
node.StorageMiner(&minerapi),
node.Online(),
node.Repo(r),
node.Override(new(*sectorbuilder.SectorBuilderConfig), modules.SectorBuilderConfig(secbpath)),
node.Override(new(api.FullNode), tnd),
)
require.NoError(t, err)
/*// Bootstrap with full node
remoteAddrs, err := tnd.NetAddrsListen(ctx)
require.NoError(t, err)
err = minerapi.NetConnect(ctx, remoteAddrs)
require.NoError(t, err)*/
return test.TestStorageNode{minerapi}
}
func builder(t *testing.T, nFull int, storage []int) ([]test.TestNode, []test.TestStorageNode) {
2019-07-09 16:27:07 +00:00
ctx := context.Background()
mn := mocknet.New(ctx)
2019-09-23 15:27:30 +00:00
fulls := make([]test.TestNode, nFull)
storers := make([]test.TestStorageNode, len(storage))
2019-07-09 16:27:07 +00:00
2019-07-24 23:23:06 +00:00
var genbuf bytes.Buffer
2019-09-23 15:27:30 +00:00
for i := 0; i < nFull; i++ {
2019-07-24 23:23:06 +00:00
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()))
}
2019-09-23 15:27:30 +00:00
mineBlock := make(chan struct{})
2019-07-09 16:27:07 +00:00
var err error
2019-09-17 14:45:09 +00:00
// TODO: Don't ignore stop
_, err = node.New(ctx,
2019-09-23 15:27:30 +00:00
node.FullAPI(&fulls[i].FullNode),
2019-07-10 13:06:04 +00:00
node.Online(),
node.Repo(repo.NewMemory(nil)),
2019-07-29 19:34:34 +00:00
node.MockHost(mn),
2019-07-24 23:23:06 +00:00
2019-09-23 15:27:30 +00:00
node.Override(new(*miner.Miner), miner.NewTestMiner(mineBlock)),
2019-07-24 23:23:06 +00:00
genesis,
2019-07-09 16:27:07 +00:00
)
if err != nil {
t.Fatal(err)
}
2019-09-23 15:27:30 +00:00
fulls[i].MineOne = func(ctx context.Context) error {
select {
case mineBlock <- struct{}{}:
return nil
case <-ctx.Done():
return ctx.Err()
}
}
}
for i, full := range storage {
// TODO: support non-bootstrap miners
if i != 0 {
t.Fatal("only one storage node supported")
}
if full != 0 {
t.Fatal("storage nodes only supported on the first full node")
}
f := fulls[full]
wa, err := f.WalletDefaultAddress(ctx)
require.NoError(t, err)
genMiner, err := address.NewFromString("t0101")
require.NoError(t, err)
storers[i] = testStorageNode(ctx, t, wa, genMiner, f)
2019-07-09 16:27:07 +00:00
}
2019-07-09 17:03:36 +00:00
if err := mn.LinkAll(); err != nil {
t.Fatal(err)
}
2019-09-23 15:27:30 +00:00
return fulls, storers
2019-07-09 16:27:07 +00:00
}
func TestAPI(t *testing.T) {
test.TestApis(t, builder)
}
2019-07-09 16:36:40 +00:00
2019-09-23 15:27:30 +00:00
func rpcBuilder(t *testing.T, nFull int, storage []int) ([]test.TestNode, []test.TestStorageNode) {
fullApis, storaApis := builder(t, nFull, storage)
fulls := make([]test.TestNode, nFull)
storers := make([]test.TestStorageNode, len(storage))
for i, a := range fullApis {
rpcServer := jsonrpc.NewServer()
rpcServer.Register("Filecoin", a)
testServ := httptest.NewServer(rpcServer) // todo: close
2019-07-09 16:36:40 +00:00
2019-09-23 15:27:30 +00:00
var err error
fulls[i].FullNode, err = client.NewFullNodeRPC("ws://"+testServ.Listener.Addr().String(), nil)
if err != nil {
t.Fatal(err)
}
fulls[i].MineOne = a.MineOne
}
2019-07-09 16:36:40 +00:00
2019-09-23 15:27:30 +00:00
for i, a := range storaApis {
2019-07-09 16:36:40 +00:00
rpcServer := jsonrpc.NewServer()
rpcServer.Register("Filecoin", a)
testServ := httptest.NewServer(rpcServer) // todo: close
2019-07-15 16:28:00 +00:00
var err error
2019-09-23 15:27:30 +00:00
storers[i].StorageMiner, err = client.NewStorageMinerRPC("ws://"+testServ.Listener.Addr().String(), nil)
2019-07-15 16:28:00 +00:00
if err != nil {
t.Fatal(err)
}
2019-07-09 16:36:40 +00:00
}
2019-09-23 15:27:30 +00:00
return fulls, storers
2019-07-09 16:36:40 +00:00
}
func TestAPIRPC(t *testing.T) {
test.TestApis(t, rpcBuilder)
}