From d94a19ff65dbcb37a38ead4f527feaf2fea9d862 Mon Sep 17 00:00:00 2001 From: Dirk McCormick Date: Mon, 14 Jun 2021 12:07:50 +0200 Subject: [PATCH 1/4] refactor: cli test with kit2 --- itests/cli_test.go | 13 ++-- itests/kit2/client.go | 146 +++++++++++++++++++++++++++++++++++++++++ itests/kit2/mockcli.go | 141 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 293 insertions(+), 7 deletions(-) create mode 100644 itests/kit2/client.go create mode 100644 itests/kit2/mockcli.go diff --git a/itests/cli_test.go b/itests/cli_test.go index 10e2af15c..8436f189e 100644 --- a/itests/cli_test.go +++ b/itests/cli_test.go @@ -1,22 +1,21 @@ package itests import ( - "context" "os" "testing" "time" "github.com/filecoin-project/lotus/cli" - "github.com/filecoin-project/lotus/itests/kit" + "github.com/filecoin-project/lotus/itests/kit2" ) // TestClient does a basic test to exercise the client CLI commands. func TestClient(t *testing.T) { _ = os.Setenv("BELLMAN_NO_GPU", "1") - kit.QuietMiningLogs() + kit2.QuietMiningLogs() - blocktime := 5 * time.Millisecond - ctx := context.Background() - clientNode, _ := kit.StartOneNodeOneMiner(ctx, t, blocktime) - kit.RunClientTest(t, cli.Commands, clientNode) + blockTime := 5 * time.Millisecond + client, _, ens := kit2.EnsembleMinimal(t, kit2.MockProofs(), kit2.ThroughRPC()) + ens.InterconnectAll().BeginMining(blockTime) + kit2.RunClientTest(t, cli.Commands, *client) } diff --git a/itests/kit2/client.go b/itests/kit2/client.go new file mode 100644 index 000000000..247d20836 --- /dev/null +++ b/itests/kit2/client.go @@ -0,0 +1,146 @@ +package kit2 + +import ( + "context" + "fmt" + "io/ioutil" + "math/rand" + "os" + "path/filepath" + "regexp" + "strings" + "testing" + "time" + + "github.com/filecoin-project/lotus/api" + "github.com/filecoin-project/lotus/build" + "github.com/filecoin-project/lotus/chain/types" + "github.com/filecoin-project/specs-actors/v2/actors/builtin" + "github.com/stretchr/testify/require" + lcli "github.com/urfave/cli/v2" +) + +// RunClientTest exercises some of the Client CLI commands +func RunClientTest(t *testing.T, cmds []*lcli.Command, clientNode TestFullNode) { + ctx, cancel := context.WithTimeout(context.Background(), time.Minute) + defer cancel() + + // Create mock CLI + mockCLI := NewMockCLI(ctx, t, cmds) + clientCLI := mockCLI.Client(clientNode.ListenAddr) + + // Get the Miner address + addrs, err := clientNode.StateListMiners(ctx, types.EmptyTSK) + require.NoError(t, err) + require.Len(t, addrs, 1) + + minerAddr := addrs[0] + fmt.Println("Miner:", minerAddr) + + // client query-ask + out := clientCLI.RunCmd("client", "query-ask", minerAddr.String()) + require.Regexp(t, regexp.MustCompile("Ask:"), out) + + // Create a deal (non-interactive) + // client deal --start-epoch= 1000000attofil + res, _, _, err := CreateImportFile(ctx, clientNode, 1, 0) + + require.NoError(t, err) + startEpoch := fmt.Sprintf("--start-epoch=%d", 2<<12) + dataCid := res.Root + price := "1000000attofil" + duration := fmt.Sprintf("%d", build.MinDealDuration) + out = clientCLI.RunCmd("client", "deal", startEpoch, dataCid.String(), minerAddr.String(), price, duration) + fmt.Println("client deal", out) + + // Create a deal (interactive) + // client deal + // + // (in days) + // + // "no" (verified Client) + // "yes" (confirm deal) + res, _, _, err = CreateImportFile(ctx, clientNode, 2, 0) + require.NoError(t, err) + dataCid2 := res.Root + duration = fmt.Sprintf("%d", build.MinDealDuration/builtin.EpochsInDay) + cmd := []string{"client", "deal"} + interactiveCmds := []string{ + dataCid2.String(), + duration, + minerAddr.String(), + "no", + "yes", + } + out = clientCLI.RunInteractiveCmd(cmd, interactiveCmds) + fmt.Println("client deal:\n", out) + + // Wait for provider to start sealing deal + dealStatus := "" + for { + // client list-deals + out = clientCLI.RunCmd("client", "list-deals") + fmt.Println("list-deals:\n", out) + + lines := strings.Split(out, "\n") + require.GreaterOrEqual(t, len(lines), 2) + re := regexp.MustCompile(`\s+`) + parts := re.Split(lines[1], -1) + if len(parts) < 4 { + require.Fail(t, "bad list-deals output format") + } + dealStatus = parts[3] + fmt.Println(" Deal status:", dealStatus) + + st := CategorizeDealState(dealStatus) + require.NotEqual(t, TestDealStateFailed, st) + if st == TestDealStateComplete { + break + } + + time.Sleep(time.Second) + } + + // Retrieve the first file from the Miner + // client retrieve + tmpdir, err := ioutil.TempDir(os.TempDir(), "test-cli-Client") + require.NoError(t, err) + path := filepath.Join(tmpdir, "outfile.dat") + out = clientCLI.RunCmd("client", "retrieve", dataCid.String(), path) + fmt.Println("retrieve:\n", out) + require.Regexp(t, regexp.MustCompile("Success"), out) +} + +func CreateImportFile(ctx context.Context, client api.FullNode, rseed int, size int) (res *api.ImportRes, path string, data []byte, err error) { + data, path, err = createRandomFile(rseed, size) + if err != nil { + return nil, "", nil, err + } + + res, err = client.ClientImport(ctx, api.FileRef{Path: path}) + if err != nil { + return nil, "", nil, err + } + return res, path, data, nil +} + +func createRandomFile(rseed, size int) ([]byte, string, error) { + if size == 0 { + size = 1600 + } + data := make([]byte, size) + rand.New(rand.NewSource(int64(rseed))).Read(data) + + dir, err := ioutil.TempDir(os.TempDir(), "test-make-deal-") + if err != nil { + return nil, "", err + } + + path := filepath.Join(dir, "sourcefile.dat") + err = ioutil.WriteFile(path, data, 0644) + if err != nil { + return nil, "", err + } + + return data, path, nil +} diff --git a/itests/kit2/mockcli.go b/itests/kit2/mockcli.go new file mode 100644 index 000000000..592c97333 --- /dev/null +++ b/itests/kit2/mockcli.go @@ -0,0 +1,141 @@ +package kit2 + +import ( + "bytes" + "context" + "flag" + "strings" + "testing" + + "github.com/multiformats/go-multiaddr" + "github.com/stretchr/testify/require" + lcli "github.com/urfave/cli/v2" +) + +type MockCLI struct { + t *testing.T + cmds []*lcli.Command + cctx *lcli.Context + out *bytes.Buffer +} + +func NewMockCLI(ctx context.Context, t *testing.T, cmds []*lcli.Command) *MockCLI { + // Create a CLI App with an --api-url flag so that we can specify which node + // the command should be executed against + app := &lcli.App{ + Flags: []lcli.Flag{ + &lcli.StringFlag{ + Name: "api-url", + Hidden: true, + }, + }, + Commands: cmds, + } + + var out bytes.Buffer + app.Writer = &out + app.Setup() + + cctx := lcli.NewContext(app, &flag.FlagSet{}, nil) + cctx.Context = ctx + return &MockCLI{t: t, cmds: cmds, cctx: cctx, out: &out} +} + +func (c *MockCLI) Client(addr multiaddr.Multiaddr) *MockCLIClient { + return &MockCLIClient{t: c.t, cmds: c.cmds, addr: addr, cctx: c.cctx, out: c.out} +} + +// MockCLIClient runs commands against a particular node +type MockCLIClient struct { + t *testing.T + cmds []*lcli.Command + addr multiaddr.Multiaddr + cctx *lcli.Context + out *bytes.Buffer +} + +func (c *MockCLIClient) RunCmd(input ...string) string { + out, err := c.RunCmdRaw(input...) + require.NoError(c.t, err, "output:\n%s", out) + + return out +} + +// Given an input, find the corresponding command or sub-command. +// eg "paych add-funds" +func (c *MockCLIClient) cmdByNameSub(input []string) (*lcli.Command, []string) { + name := input[0] + for _, cmd := range c.cmds { + if cmd.Name == name { + return c.findSubcommand(cmd, input[1:]) + } + } + return nil, []string{} +} + +func (c *MockCLIClient) findSubcommand(cmd *lcli.Command, input []string) (*lcli.Command, []string) { + // If there are no sub-commands, return the current command + if len(cmd.Subcommands) == 0 { + return cmd, input + } + + // Check each sub-command for a match against the name + subName := input[0] + for _, subCmd := range cmd.Subcommands { + if subCmd.Name == subName { + // Found a match, recursively search for sub-commands + return c.findSubcommand(subCmd, input[1:]) + } + } + return nil, []string{} +} + +func (c *MockCLIClient) RunCmdRaw(input ...string) (string, error) { + cmd, input := c.cmdByNameSub(input) + if cmd == nil { + panic("Could not find command " + input[0] + " " + input[1]) + } + + // prepend --api-url= + apiFlag := "--api-url=" + c.addr.String() + input = append([]string{apiFlag}, input...) + + fs := c.flagSet(cmd) + err := fs.Parse(input) + require.NoError(c.t, err) + + err = cmd.Action(lcli.NewContext(c.cctx.App, fs, c.cctx)) + + // Get the output + str := strings.TrimSpace(c.out.String()) + c.out.Reset() + return str, err +} + +func (c *MockCLIClient) flagSet(cmd *lcli.Command) *flag.FlagSet { + // Apply app level flags (so we can process --api-url flag) + fs := &flag.FlagSet{} + for _, f := range c.cctx.App.Flags { + err := f.Apply(fs) + if err != nil { + c.t.Fatal(err) + } + } + // Apply command level flags + for _, f := range cmd.Flags { + err := f.Apply(fs) + if err != nil { + c.t.Fatal(err) + } + } + return fs +} + +func (c *MockCLIClient) RunInteractiveCmd(cmd []string, interactive []string) string { + c.toStdin(strings.Join(interactive, "\n") + "\n") + return c.RunCmd(cmd...) +} + +func (c *MockCLIClient) toStdin(s string) { + c.cctx.App.Metadata["stdin"] = bytes.NewBufferString(s) +} From 86cca7303d503e119e39c42cbb87a9392930dded Mon Sep 17 00:00:00 2001 From: Dirk McCormick Date: Mon, 14 Jun 2021 14:03:08 +0200 Subject: [PATCH 2/4] refactor: convert multisig tests to kit2 --- itests/multisig_test.go | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/itests/multisig_test.go b/itests/multisig_test.go index 4c513640d..cc2b38c30 100644 --- a/itests/multisig_test.go +++ b/itests/multisig_test.go @@ -12,26 +12,26 @@ import ( "github.com/filecoin-project/go-address" "github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/cli" - "github.com/filecoin-project/lotus/itests/kit" + "github.com/filecoin-project/lotus/itests/kit2" "github.com/stretchr/testify/require" ) // TestMultisig does a basic test to exercise the multisig CLI commands func TestMultisig(t *testing.T) { _ = os.Setenv("BELLMAN_NO_GPU", "1") - kit.QuietMiningLogs() + kit2.QuietMiningLogs() - blocktime := 5 * time.Millisecond - ctx := context.Background() - clientNode, _ := kit.StartOneNodeOneMiner(ctx, t, blocktime) + blockTime := 5 * time.Millisecond + client, _, ens := kit2.EnsembleMinimal(t, kit2.MockProofs()) + ens.InterconnectAll().BeginMining(blockTime) - runMultisigTests(t, clientNode) + runMultisigTests(t, *client) } -func runMultisigTests(t *testing.T, clientNode kit.TestFullNode) { +func runMultisigTests(t *testing.T, clientNode kit2.TestFullNode) { // Create mock CLI ctx := context.Background() - mockCLI := kit.NewMockCLI(ctx, t, cli.Commands) + mockCLI := kit2.NewMockCLI(ctx, t, cli.Commands) clientCLI := mockCLI.Client(clientNode.ListenAddr) // Create some wallets on the node to use for testing multisig @@ -42,7 +42,7 @@ func runMultisigTests(t *testing.T, clientNode kit.TestFullNode) { walletAddrs = append(walletAddrs, addr) - kit.SendFunds(ctx, t, clientNode, addr, types.NewInt(1e15)) + kit2.SendFunds(ctx, t, clientNode, addr, types.NewInt(1e15)) } // Create an msig with three of the addresses and threshold of two sigs From 16cad0e01a2a7e1d43c54214d7d8239bcb450a4f Mon Sep 17 00:00:00 2001 From: Dirk McCormick Date: Tue, 15 Jun 2021 11:46:32 +0200 Subject: [PATCH 3/4] refactor: convert gateway tests to kit2 --- itests/gateway_test.go | 102 +++++++++++++++++---------------------- itests/kit2/ensemble.go | 5 ++ itests/kit2/funds.go | 2 +- itests/kit2/node_opts.go | 14 ++++++ itests/multisig_test.go | 2 +- 5 files changed, 64 insertions(+), 61 deletions(-) diff --git a/itests/gateway_test.go b/itests/gateway_test.go index 7f1b70f2d..20b0add6a 100644 --- a/itests/gateway_test.go +++ b/itests/gateway_test.go @@ -9,7 +9,6 @@ import ( "testing" "time" - "github.com/filecoin-project/lotus/chain/stmgr" "github.com/stretchr/testify/require" "golang.org/x/xerrors" @@ -21,10 +20,11 @@ import ( "github.com/filecoin-project/lotus/api" "github.com/filecoin-project/lotus/api/client" "github.com/filecoin-project/lotus/chain/actors/policy" + "github.com/filecoin-project/lotus/chain/stmgr" "github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/cli" "github.com/filecoin-project/lotus/gateway" - "github.com/filecoin-project/lotus/itests/kit" + "github.com/filecoin-project/lotus/itests/kit2" "github.com/filecoin-project/lotus/node" init2 "github.com/filecoin-project/specs-actors/v2/actors/builtin/init" @@ -46,7 +46,7 @@ func init() { // node that is connected through a gateway to a full API node func TestGatewayWalletMsig(t *testing.T) { _ = os.Setenv("BELLMAN_NO_GPU", "1") - kit.QuietMiningLogs() + kit2.QuietMiningLogs() blocktime := 5 * time.Millisecond ctx := context.Background() @@ -180,7 +180,7 @@ func TestGatewayWalletMsig(t *testing.T) { // on a lite node that is connected through a gateway to a full API node func TestGatewayMsigCLI(t *testing.T) { _ = os.Setenv("BELLMAN_NO_GPU", "1") - kit.QuietMiningLogs() + kit2.QuietMiningLogs() blocktime := 5 * time.Millisecond ctx := context.Background() @@ -193,7 +193,7 @@ func TestGatewayMsigCLI(t *testing.T) { func TestGatewayDealFlow(t *testing.T) { _ = os.Setenv("BELLMAN_NO_GPU", "1") - kit.QuietMiningLogs() + kit2.QuietMiningLogs() blocktime := 5 * time.Millisecond ctx := context.Background() @@ -205,26 +205,27 @@ func TestGatewayDealFlow(t *testing.T) { // so that the deal starts sealing in time dealStartEpoch := abi.ChainEpoch(2 << 12) - dh := kit.NewDealHarness(t, nodes.lite, nodes.miner) - dh.MakeFullDeal(ctx, 6, false, false, dealStartEpoch) + dh := kit2.NewDealHarness(t, &nodes.lite, &nodes.miner) + dealCid, res, _ := dh.MakeOnlineDeal(ctx, 6, false, dealStartEpoch) + dh.PerformRetrieval(ctx, dealCid, res.Root, false) } func TestGatewayCLIDealFlow(t *testing.T) { _ = os.Setenv("BELLMAN_NO_GPU", "1") - kit.QuietMiningLogs() + kit2.QuietMiningLogs() blocktime := 5 * time.Millisecond ctx := context.Background() nodes := startNodesWithFunds(ctx, t, blocktime, maxLookbackCap, maxStateWaitLookbackLimit) defer nodes.closer() - kit.RunClientTest(t, cli.Commands, nodes.lite) + kit2.RunClientTest(t, cli.Commands, nodes.lite) } type testNodes struct { - lite kit.TestFullNode - full kit.TestFullNode - miner kit.TestMiner + lite kit2.TestFullNode + full kit2.TestFullNode + miner kit2.TestMiner closer jsonrpc.ClientCloser } @@ -261,66 +262,49 @@ func startNodes( ) *testNodes { var closer jsonrpc.ClientCloser - // Create one miner and two full nodes. + var ( + full kit2.TestFullNode + lite kit2.TestFullNode + miner kit2.TestMiner + ) + + // - Create one full node and one lite node // - Put a gateway server in front of full node 1 // - Start full node 2 in lite mode // - Connect lite node -> gateway server -> full node - opts := append( - // Full node - kit.OneFull, - // Lite node - kit.FullNodeOpts{ - Lite: true, - Opts: func(nodes []kit.TestFullNode) node.Option { - fullNode := nodes[0] - // Create a gateway server in front of the full node - gwapi := gateway.NewNode(fullNode, lookbackCap, stateWaitLookbackLimit) - handler, err := gateway.Handler(gwapi) - require.NoError(t, err) + var liteOptBuilder kit2.OptBuilder + liteOptBuilder = func(nodes []*kit2.TestFullNode) node.Option { + fullNode := nodes[0] - srv, _ := kit.CreateRPCServer(t, handler) + // Create a gateway server in front of the full node + gwapi := gateway.NewNode(fullNode, lookbackCap, stateWaitLookbackLimit) + handler, err := gateway.Handler(gwapi) + require.NoError(t, err) - // Create a gateway client API that connects to the gateway server - var gapi api.Gateway - gapi, closer, err = client.NewGatewayRPCV1(ctx, "ws://"+srv.Listener.Addr().String()+"/rpc/v1", nil) - require.NoError(t, err) + srv, _ := kit2.CreateRPCServer(t, handler) - // Provide the gateway API to dependency injection - return node.Override(new(api.Gateway), gapi) - }, - }, - ) - n, sn := kit.RPCMockMinerBuilder(t, opts, kit.OneMiner) + // Create a gateway client API that connects to the gateway server + var gapi api.Gateway + gapi, closer, err = client.NewGatewayRPCV1(ctx, "ws://"+srv.Listener.Addr().String()+"/rpc/v1", nil) + require.NoError(t, err) - full := n[0] - lite := n[1] - miner := sn[0] + // Provide the gateway API to dependency injection + return node.Override(new(api.Gateway), gapi) + } - // Get the listener address for the full node - fullAddr, err := full.NetAddrsListen(ctx) - require.NoError(t, err) - - // Connect the miner and the full node - err = miner.NetConnect(ctx, fullAddr) - require.NoError(t, err) - - // Connect the miner and the lite node (so that the lite node can send - // data to the miner) - liteAddr, err := lite.NetAddrsListen(ctx) - require.NoError(t, err) - err = miner.NetConnect(ctx, liteAddr) - require.NoError(t, err) - - // Start mining blocks - bm := kit.NewBlockMiner(t, miner) - bm.MineBlocks(ctx, blocktime) - t.Cleanup(bm.Stop) + kit2.NewEnsemble(t, kit2.MockProofs()). + FullNode(&full). + FullNode(&lite, kit2.LiteNode(), kit2.ThroughRPC(), kit2.AddOptBuilder(liteOptBuilder)). + Miner(&miner, &full). + Start(). + InterconnectAll(). + BeginMining(blocktime) return &testNodes{lite: lite, full: full, miner: miner, closer: closer} } -func sendFunds(ctx context.Context, fromNode kit.TestFullNode, fromAddr address.Address, toAddr address.Address, amt types.BigInt) error { +func sendFunds(ctx context.Context, fromNode kit2.TestFullNode, fromAddr address.Address, toAddr address.Address, amt types.BigInt) error { msg := &types.Message{ From: fromAddr, To: toAddr, diff --git a/itests/kit2/ensemble.go b/itests/kit2/ensemble.go index 5d12c83e1..d74649c26 100644 --- a/itests/kit2/ensemble.go +++ b/itests/kit2/ensemble.go @@ -279,6 +279,11 @@ func (n *Ensemble) Start() *Ensemble { ) } + // Call option builders, passing active nodes as the parameter + for _, bopt := range full.options.optBuilders { + opts = append(opts, bopt(n.active.fullnodes)) + } + // Construct the full node. stop, err := node.New(ctx, opts...) diff --git a/itests/kit2/funds.go b/itests/kit2/funds.go index da37ae2ba..fc4a6c294 100644 --- a/itests/kit2/funds.go +++ b/itests/kit2/funds.go @@ -30,5 +30,5 @@ func SendFunds(ctx context.Context, t *testing.T, sender TestFullNode, recipient res, err := sender.StateWaitMsg(ctx, sm.Cid(), 3, api.LookbackNoLimit, true) require.NoError(t, err) - require.Equal(t, 0, res.Receipt.ExitCode, "did not successfully send funds") + require.EqualValues(t, 0, res.Receipt.ExitCode, "did not successfully send funds") } diff --git a/itests/kit2/node_opts.go b/itests/kit2/node_opts.go index 59d5454df..bba78262a 100644 --- a/itests/kit2/node_opts.go +++ b/itests/kit2/node_opts.go @@ -23,6 +23,7 @@ type nodeOpts struct { rpc bool ownerKey *wallet.Key extraNodeOpts []node.Option + optBuilders []OptBuilder } // DefaultNodeOpts are the default options that will be applied to test nodes. @@ -31,6 +32,10 @@ var DefaultNodeOpts = nodeOpts{ sectors: DefaultPresealsPerBootstrapMiner, } +// OptBuilder is used to create an option after some other node is already +// active. Takes all active nodes as a parameter. +type OptBuilder func(activeNodes []*TestFullNode) node.Option + // NodeOpt is a functional option for test nodes. type NodeOpt func(opts *nodeOpts) error @@ -87,3 +92,12 @@ func ConstructorOpts(extra ...node.Option) NodeOpt { return nil } } + +// AddOptBuilder adds an OptionBuilder to a node. It is used to add Lotus node +// constructor options after some nodes are already active. +func AddOptBuilder(optBuilder OptBuilder) NodeOpt { + return func(opts *nodeOpts) error { + opts.optBuilders = append(opts.optBuilders, optBuilder) + return nil + } +} diff --git a/itests/multisig_test.go b/itests/multisig_test.go index cc2b38c30..f5df0be1a 100644 --- a/itests/multisig_test.go +++ b/itests/multisig_test.go @@ -22,7 +22,7 @@ func TestMultisig(t *testing.T) { kit2.QuietMiningLogs() blockTime := 5 * time.Millisecond - client, _, ens := kit2.EnsembleMinimal(t, kit2.MockProofs()) + client, _, ens := kit2.EnsembleMinimal(t, kit2.MockProofs(), kit2.ThroughRPC()) ens.InterconnectAll().BeginMining(blockTime) runMultisigTests(t, *client) From 3d8eb374bd05fed40e1845a25afa0440587b83a8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Kripalani?= Date: Fri, 18 Jun 2021 19:23:32 +0100 Subject: [PATCH 4/4] cleaner instantiation of lite and gateway nodes + general cleanup. --- itests/cli_test.go | 2 +- itests/gateway_test.go | 71 ++++++++++++++++++---------------------- itests/kit2/client.go | 2 +- itests/kit2/ensemble.go | 13 ++++---- itests/kit2/funds.go | 2 +- itests/multisig_test.go | 6 ++-- itests/paych_api_test.go | 2 +- itests/paych_cli_test.go | 2 +- 8 files changed, 45 insertions(+), 55 deletions(-) diff --git a/itests/cli_test.go b/itests/cli_test.go index 8436f189e..5c9cf0f69 100644 --- a/itests/cli_test.go +++ b/itests/cli_test.go @@ -17,5 +17,5 @@ func TestClient(t *testing.T) { blockTime := 5 * time.Millisecond client, _, ens := kit2.EnsembleMinimal(t, kit2.MockProofs(), kit2.ThroughRPC()) ens.InterconnectAll().BeginMining(blockTime) - kit2.RunClientTest(t, cli.Commands, *client) + kit2.RunClientTest(t, cli.Commands, client) } diff --git a/itests/gateway_test.go b/itests/gateway_test.go index 20b0add6a..36df41d54 100644 --- a/itests/gateway_test.go +++ b/itests/gateway_test.go @@ -5,7 +5,6 @@ import ( "context" "fmt" "math" - "os" "testing" "time" @@ -45,7 +44,6 @@ func init() { // TestGatewayWalletMsig tests that API calls to wallet and msig can be made on a lite // node that is connected through a gateway to a full API node func TestGatewayWalletMsig(t *testing.T) { - _ = os.Setenv("BELLMAN_NO_GPU", "1") kit2.QuietMiningLogs() blocktime := 5 * time.Millisecond @@ -111,7 +109,6 @@ func TestGatewayWalletMsig(t *testing.T) { if err != nil { return cid.Undef, err } - return lite.MpoolPush(ctx, sm) } @@ -179,7 +176,6 @@ func TestGatewayWalletMsig(t *testing.T) { // TestGatewayMsigCLI tests that msig CLI calls can be made // on a lite node that is connected through a gateway to a full API node func TestGatewayMsigCLI(t *testing.T) { - _ = os.Setenv("BELLMAN_NO_GPU", "1") kit2.QuietMiningLogs() blocktime := 5 * time.Millisecond @@ -192,7 +188,6 @@ func TestGatewayMsigCLI(t *testing.T) { } func TestGatewayDealFlow(t *testing.T) { - _ = os.Setenv("BELLMAN_NO_GPU", "1") kit2.QuietMiningLogs() blocktime := 5 * time.Millisecond @@ -200,18 +195,19 @@ func TestGatewayDealFlow(t *testing.T) { nodes := startNodesWithFunds(ctx, t, blocktime, maxLookbackCap, maxStateWaitLookbackLimit) defer nodes.closer() + time.Sleep(5 * time.Second) + // For these tests where the block time is artificially short, just use // a deal start epoch that is guaranteed to be far enough in the future // so that the deal starts sealing in time dealStartEpoch := abi.ChainEpoch(2 << 12) - dh := kit2.NewDealHarness(t, &nodes.lite, &nodes.miner) + dh := kit2.NewDealHarness(t, nodes.lite, nodes.miner) dealCid, res, _ := dh.MakeOnlineDeal(ctx, 6, false, dealStartEpoch) dh.PerformRetrieval(ctx, dealCid, res.Root, false) } func TestGatewayCLIDealFlow(t *testing.T) { - _ = os.Setenv("BELLMAN_NO_GPU", "1") kit2.QuietMiningLogs() blocktime := 5 * time.Millisecond @@ -223,9 +219,9 @@ func TestGatewayCLIDealFlow(t *testing.T) { } type testNodes struct { - lite kit2.TestFullNode - full kit2.TestFullNode - miner kit2.TestMiner + lite *kit2.TestFullNode + full *kit2.TestFullNode + miner *kit2.TestMiner closer jsonrpc.ClientCloser } @@ -242,8 +238,8 @@ func startNodesWithFunds( fullWalletAddr, err := nodes.full.WalletDefaultAddress(ctx) require.NoError(t, err) - // Create a wallet on the lite node - liteWalletAddr, err := nodes.lite.WalletNew(ctx, types.KTSecp256k1) + // Get the lite node default wallet address. + liteWalletAddr, err := nodes.lite.WalletDefaultAddress(ctx) require.NoError(t, err) // Send some funds from the full node to the lite node @@ -263,9 +259,9 @@ func startNodes( var closer jsonrpc.ClientCloser var ( - full kit2.TestFullNode + full *kit2.TestFullNode + miner *kit2.TestMiner lite kit2.TestFullNode - miner kit2.TestMiner ) // - Create one full node and one lite node @@ -273,38 +269,35 @@ func startNodes( // - Start full node 2 in lite mode // - Connect lite node -> gateway server -> full node - var liteOptBuilder kit2.OptBuilder - liteOptBuilder = func(nodes []*kit2.TestFullNode) node.Option { - fullNode := nodes[0] + // create the full node and the miner. + var ens *kit2.Ensemble + full, miner, ens = kit2.EnsembleMinimal(t, kit2.MockProofs()) + ens.InterconnectAll().BeginMining(blocktime) - // Create a gateway server in front of the full node - gwapi := gateway.NewNode(fullNode, lookbackCap, stateWaitLookbackLimit) - handler, err := gateway.Handler(gwapi) - require.NoError(t, err) + // Create a gateway server in front of the full node + gwapi := gateway.NewNode(full, lookbackCap, stateWaitLookbackLimit) + handler, err := gateway.Handler(gwapi) + require.NoError(t, err) - srv, _ := kit2.CreateRPCServer(t, handler) + srv, _ := kit2.CreateRPCServer(t, handler) - // Create a gateway client API that connects to the gateway server - var gapi api.Gateway - gapi, closer, err = client.NewGatewayRPCV1(ctx, "ws://"+srv.Listener.Addr().String()+"/rpc/v1", nil) - require.NoError(t, err) + // Create a gateway client API that connects to the gateway server + var gapi api.Gateway + gapi, closer, err = client.NewGatewayRPCV1(ctx, "ws://"+srv.Listener.Addr().String()+"/rpc/v1", nil) + require.NoError(t, err) - // Provide the gateway API to dependency injection - return node.Override(new(api.Gateway), gapi) - } + ens.FullNode(&lite, + kit2.LiteNode(), + kit2.ThroughRPC(), + kit2.ConstructorOpts( + node.Override(new(api.Gateway), gapi), + ), + ).Start().InterconnectAll() - kit2.NewEnsemble(t, kit2.MockProofs()). - FullNode(&full). - FullNode(&lite, kit2.LiteNode(), kit2.ThroughRPC(), kit2.AddOptBuilder(liteOptBuilder)). - Miner(&miner, &full). - Start(). - InterconnectAll(). - BeginMining(blocktime) - - return &testNodes{lite: lite, full: full, miner: miner, closer: closer} + return &testNodes{lite: &lite, full: full, miner: miner, closer: closer} } -func sendFunds(ctx context.Context, fromNode kit2.TestFullNode, fromAddr address.Address, toAddr address.Address, amt types.BigInt) error { +func sendFunds(ctx context.Context, fromNode *kit2.TestFullNode, fromAddr address.Address, toAddr address.Address, amt types.BigInt) error { msg := &types.Message{ From: fromAddr, To: toAddr, diff --git a/itests/kit2/client.go b/itests/kit2/client.go index 247d20836..78a7034fe 100644 --- a/itests/kit2/client.go +++ b/itests/kit2/client.go @@ -21,7 +21,7 @@ import ( ) // RunClientTest exercises some of the Client CLI commands -func RunClientTest(t *testing.T, cmds []*lcli.Command, clientNode TestFullNode) { +func RunClientTest(t *testing.T, cmds []*lcli.Command, clientNode *TestFullNode) { ctx, cancel := context.WithTimeout(context.Background(), time.Minute) defer cancel() diff --git a/itests/kit2/ensemble.go b/itests/kit2/ensemble.go index 67e6b8592..44580920f 100644 --- a/itests/kit2/ensemble.go +++ b/itests/kit2/ensemble.go @@ -145,13 +145,13 @@ func (n *Ensemble) FullNode(full *TestFullNode, opts ...NodeOpt) *Ensemble { require.NoError(n.t, err) } - var key *wallet.Key - if !n.bootstrapped && !options.balance.IsZero() { - // create a key+address, and assign it some FIL; this will be set as the default wallet. - var err error - key, err = wallet.GenerateKey(types.KTBLS) - require.NoError(n.t, err) + key, err := wallet.GenerateKey(types.KTBLS) + require.NoError(n.t, err) + if !n.bootstrapped && !options.balance.IsZero() { + // if we still haven't forged genesis, create a key+address, and assign + // it some FIL; this will be set as the default wallet when the node is + // started. genacc := genesis.Actor{ Type: genesis.TAccount, Balance: options.balance, @@ -298,7 +298,6 @@ func (n *Ensemble) Start() *Ensemble { // Construct the full node. stop, err := node.New(ctx, opts...) - // fullOpts[i].Opts(fulls), require.NoError(n.t, err) addr, err := full.WalletImport(context.Background(), &full.DefaultKey.KeyInfo) diff --git a/itests/kit2/funds.go b/itests/kit2/funds.go index fc4a6c294..b29963353 100644 --- a/itests/kit2/funds.go +++ b/itests/kit2/funds.go @@ -14,7 +14,7 @@ import ( // SendFunds sends funds from the default wallet of the specified sender node // to the recipient address. -func SendFunds(ctx context.Context, t *testing.T, sender TestFullNode, recipient address.Address, amount abi.TokenAmount) { +func SendFunds(ctx context.Context, t *testing.T, sender *TestFullNode, recipient address.Address, amount abi.TokenAmount) { senderAddr, err := sender.WalletDefaultAddress(ctx) require.NoError(t, err) diff --git a/itests/multisig_test.go b/itests/multisig_test.go index f5df0be1a..9b1f59673 100644 --- a/itests/multisig_test.go +++ b/itests/multisig_test.go @@ -3,7 +3,6 @@ package itests import ( "context" "fmt" - "os" "regexp" "strings" "testing" @@ -18,17 +17,16 @@ import ( // TestMultisig does a basic test to exercise the multisig CLI commands func TestMultisig(t *testing.T) { - _ = os.Setenv("BELLMAN_NO_GPU", "1") kit2.QuietMiningLogs() blockTime := 5 * time.Millisecond client, _, ens := kit2.EnsembleMinimal(t, kit2.MockProofs(), kit2.ThroughRPC()) ens.InterconnectAll().BeginMining(blockTime) - runMultisigTests(t, *client) + runMultisigTests(t, client) } -func runMultisigTests(t *testing.T, clientNode kit2.TestFullNode) { +func runMultisigTests(t *testing.T, clientNode *kit2.TestFullNode) { // Create mock CLI ctx := context.Background() mockCLI := kit2.NewMockCLI(ctx, t, cli.Commands) diff --git a/itests/paych_api_test.go b/itests/paych_api_test.go index 8fb4cde0c..86a156790 100644 --- a/itests/paych_api_test.go +++ b/itests/paych_api_test.go @@ -51,7 +51,7 @@ func TestPaymentChannelsAPI(t *testing.T) { receiverAddr, err := paymentReceiver.WalletNew(ctx, types.KTSecp256k1) require.NoError(t, err) - kit2.SendFunds(ctx, t, paymentCreator, receiverAddr, abi.NewTokenAmount(1e18)) + kit2.SendFunds(ctx, t, &paymentCreator, receiverAddr, abi.NewTokenAmount(1e18)) // setup the payment channel createrAddr, err := paymentCreator.WalletDefaultAddress(ctx) diff --git a/itests/paych_cli_test.go b/itests/paych_cli_test.go index 2450828d3..84ccee95a 100644 --- a/itests/paych_cli_test.go +++ b/itests/paych_cli_test.go @@ -428,7 +428,7 @@ func startPaychCreatorReceiverMiner(ctx context.Context, t *testing.T, paymentCr // Send some funds to the second node receiverAddr, err := paymentReceiver.WalletDefaultAddress(ctx) require.NoError(t, err) - kit2.SendFunds(ctx, t, *paymentCreator, receiverAddr, abi.NewTokenAmount(1e18)) + kit2.SendFunds(ctx, t, paymentCreator, receiverAddr, abi.NewTokenAmount(1e18)) // Get the first node's address creatorAddr, err := paymentCreator.WalletDefaultAddress(ctx)