Merge pull request #6483 from filecoin-project/refactor/itest-multisig

This commit is contained in:
raulk 2021-06-18 19:39:24 +01:00 committed by GitHub
commit 83de21e464
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
9 changed files with 84 additions and 91 deletions

View File

@ -17,5 +17,5 @@ func TestClient(t *testing.T) {
blockTime := 5 * time.Millisecond blockTime := 5 * time.Millisecond
client, _, ens := kit2.EnsembleMinimal(t, kit2.MockProofs(), kit2.ThroughRPC()) client, _, ens := kit2.EnsembleMinimal(t, kit2.MockProofs(), kit2.ThroughRPC())
ens.InterconnectAll().BeginMining(blockTime) ens.InterconnectAll().BeginMining(blockTime)
kit2.RunClientTest(t, cli.Commands, *client) kit2.RunClientTest(t, cli.Commands, client)
} }

View File

@ -5,11 +5,9 @@ import (
"context" "context"
"fmt" "fmt"
"math" "math"
"os"
"testing" "testing"
"time" "time"
"github.com/filecoin-project/lotus/chain/stmgr"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"golang.org/x/xerrors" "golang.org/x/xerrors"
@ -21,10 +19,11 @@ import (
"github.com/filecoin-project/lotus/api" "github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/api/client" "github.com/filecoin-project/lotus/api/client"
"github.com/filecoin-project/lotus/chain/actors/policy" "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/chain/types"
"github.com/filecoin-project/lotus/cli" "github.com/filecoin-project/lotus/cli"
"github.com/filecoin-project/lotus/gateway" "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" "github.com/filecoin-project/lotus/node"
init2 "github.com/filecoin-project/specs-actors/v2/actors/builtin/init" init2 "github.com/filecoin-project/specs-actors/v2/actors/builtin/init"
@ -45,8 +44,7 @@ func init() {
// TestGatewayWalletMsig tests that API calls to wallet and msig can be made on a lite // 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 // node that is connected through a gateway to a full API node
func TestGatewayWalletMsig(t *testing.T) { func TestGatewayWalletMsig(t *testing.T) {
_ = os.Setenv("BELLMAN_NO_GPU", "1") kit2.QuietMiningLogs()
kit.QuietMiningLogs()
blocktime := 5 * time.Millisecond blocktime := 5 * time.Millisecond
ctx := context.Background() ctx := context.Background()
@ -111,7 +109,6 @@ func TestGatewayWalletMsig(t *testing.T) {
if err != nil { if err != nil {
return cid.Undef, err return cid.Undef, err
} }
return lite.MpoolPush(ctx, sm) return lite.MpoolPush(ctx, sm)
} }
@ -179,8 +176,7 @@ func TestGatewayWalletMsig(t *testing.T) {
// TestGatewayMsigCLI tests that msig CLI calls can be made // TestGatewayMsigCLI tests that msig CLI calls can be made
// on a lite node that is connected through a gateway to a full API node // on a lite node that is connected through a gateway to a full API node
func TestGatewayMsigCLI(t *testing.T) { func TestGatewayMsigCLI(t *testing.T) {
_ = os.Setenv("BELLMAN_NO_GPU", "1") kit2.QuietMiningLogs()
kit.QuietMiningLogs()
blocktime := 5 * time.Millisecond blocktime := 5 * time.Millisecond
ctx := context.Background() ctx := context.Background()
@ -192,39 +188,40 @@ func TestGatewayMsigCLI(t *testing.T) {
} }
func TestGatewayDealFlow(t *testing.T) { func TestGatewayDealFlow(t *testing.T) {
_ = os.Setenv("BELLMAN_NO_GPU", "1") kit2.QuietMiningLogs()
kit.QuietMiningLogs()
blocktime := 5 * time.Millisecond blocktime := 5 * time.Millisecond
ctx := context.Background() ctx := context.Background()
nodes := startNodesWithFunds(ctx, t, blocktime, maxLookbackCap, maxStateWaitLookbackLimit) nodes := startNodesWithFunds(ctx, t, blocktime, maxLookbackCap, maxStateWaitLookbackLimit)
defer nodes.closer() defer nodes.closer()
time.Sleep(5 * time.Second)
// For these tests where the block time is artificially short, just use // 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 // a deal start epoch that is guaranteed to be far enough in the future
// so that the deal starts sealing in time // so that the deal starts sealing in time
dealStartEpoch := abi.ChainEpoch(2 << 12) dealStartEpoch := abi.ChainEpoch(2 << 12)
dh := kit.NewDealHarness(t, nodes.lite, nodes.miner) dh := kit2.NewDealHarness(t, nodes.lite, nodes.miner)
dh.MakeFullDeal(ctx, 6, false, false, dealStartEpoch) dealCid, res, _ := dh.MakeOnlineDeal(ctx, 6, false, dealStartEpoch)
dh.PerformRetrieval(ctx, dealCid, res.Root, false)
} }
func TestGatewayCLIDealFlow(t *testing.T) { func TestGatewayCLIDealFlow(t *testing.T) {
_ = os.Setenv("BELLMAN_NO_GPU", "1") kit2.QuietMiningLogs()
kit.QuietMiningLogs()
blocktime := 5 * time.Millisecond blocktime := 5 * time.Millisecond
ctx := context.Background() ctx := context.Background()
nodes := startNodesWithFunds(ctx, t, blocktime, maxLookbackCap, maxStateWaitLookbackLimit) nodes := startNodesWithFunds(ctx, t, blocktime, maxLookbackCap, maxStateWaitLookbackLimit)
defer nodes.closer() defer nodes.closer()
kit.RunClientTest(t, cli.Commands, nodes.lite) kit2.RunClientTest(t, cli.Commands, nodes.lite)
} }
type testNodes struct { type testNodes struct {
lite kit.TestFullNode lite *kit2.TestFullNode
full kit.TestFullNode full *kit2.TestFullNode
miner kit.TestMiner miner *kit2.TestMiner
closer jsonrpc.ClientCloser closer jsonrpc.ClientCloser
} }
@ -241,8 +238,8 @@ func startNodesWithFunds(
fullWalletAddr, err := nodes.full.WalletDefaultAddress(ctx) fullWalletAddr, err := nodes.full.WalletDefaultAddress(ctx)
require.NoError(t, err) require.NoError(t, err)
// Create a wallet on the lite node // Get the lite node default wallet address.
liteWalletAddr, err := nodes.lite.WalletNew(ctx, types.KTSecp256k1) liteWalletAddr, err := nodes.lite.WalletDefaultAddress(ctx)
require.NoError(t, err) require.NoError(t, err)
// Send some funds from the full node to the lite node // Send some funds from the full node to the lite node
@ -261,66 +258,46 @@ func startNodes(
) *testNodes { ) *testNodes {
var closer jsonrpc.ClientCloser var closer jsonrpc.ClientCloser
// Create one miner and two full nodes. var (
full *kit2.TestFullNode
miner *kit2.TestMiner
lite kit2.TestFullNode
)
// - Create one full node and one lite node
// - Put a gateway server in front of full node 1 // - Put a gateway server in front of full node 1
// - Start full node 2 in lite mode // - Start full node 2 in lite mode
// - Connect lite node -> gateway server -> full node // - Connect lite node -> gateway server -> full node
opts := append(
// Full node // create the full node and the miner.
kit.OneFull, var ens *kit2.Ensemble
// Lite node full, miner, ens = kit2.EnsembleMinimal(t, kit2.MockProofs())
kit.FullNodeOpts{ ens.InterconnectAll().BeginMining(blocktime)
Lite: true,
Opts: func(nodes []kit.TestFullNode) node.Option {
fullNode := nodes[0]
// Create a gateway server in front of the full node // Create a gateway server in front of the full node
gwapi := gateway.NewNode(fullNode, lookbackCap, stateWaitLookbackLimit) gwapi := gateway.NewNode(full, lookbackCap, stateWaitLookbackLimit)
handler, err := gateway.Handler(gwapi) handler, err := gateway.Handler(gwapi)
require.NoError(t, err) require.NoError(t, err)
srv, _ := kit.CreateRPCServer(t, handler) srv, _ := kit2.CreateRPCServer(t, handler)
// Create a gateway client API that connects to the gateway server // Create a gateway client API that connects to the gateway server
var gapi api.Gateway var gapi api.Gateway
gapi, closer, err = client.NewGatewayRPCV1(ctx, "ws://"+srv.Listener.Addr().String()+"/rpc/v1", nil) gapi, closer, err = client.NewGatewayRPCV1(ctx, "ws://"+srv.Listener.Addr().String()+"/rpc/v1", nil)
require.NoError(t, err) require.NoError(t, err)
// Provide the gateway API to dependency injection ens.FullNode(&lite,
return node.Override(new(api.Gateway), gapi) kit2.LiteNode(),
}, kit2.ThroughRPC(),
}, kit2.ConstructorOpts(
) node.Override(new(api.Gateway), gapi),
n, sn := kit.RPCMockMinerBuilder(t, opts, kit.OneMiner) ),
).Start().InterconnectAll()
full := n[0] return &testNodes{lite: &lite, full: full, miner: miner, closer: closer}
lite := n[1]
miner := sn[0]
// 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)
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{ msg := &types.Message{
From: fromAddr, From: fromAddr,
To: toAddr, To: toAddr,

View File

@ -21,7 +21,7 @@ import (
) )
// RunClientTest exercises some of the Client CLI commands // 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) ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
defer cancel() defer cancel()

View File

@ -145,13 +145,13 @@ func (n *Ensemble) FullNode(full *TestFullNode, opts ...NodeOpt) *Ensemble {
require.NoError(n.t, err) require.NoError(n.t, err)
} }
var key *wallet.Key key, err := wallet.GenerateKey(types.KTBLS)
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) 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{ genacc := genesis.Actor{
Type: genesis.TAccount, Type: genesis.TAccount,
Balance: options.balance, Balance: options.balance,
@ -290,10 +290,14 @@ 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. // Construct the full node.
stop, err := node.New(ctx, opts...) stop, err := node.New(ctx, opts...)
// fullOpts[i].Opts(fulls),
require.NoError(n.t, err) require.NoError(n.t, err)
addr, err := full.WalletImport(context.Background(), &full.DefaultKey.KeyInfo) addr, err := full.WalletImport(context.Background(), &full.DefaultKey.KeyInfo)

View File

@ -14,7 +14,7 @@ import (
// SendFunds sends funds from the default wallet of the specified sender node // SendFunds sends funds from the default wallet of the specified sender node
// to the recipient address. // 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) senderAddr, err := sender.WalletDefaultAddress(ctx)
require.NoError(t, err) require.NoError(t, err)

View File

@ -25,6 +25,7 @@ type nodeOpts struct {
rpc bool rpc bool
ownerKey *wallet.Key ownerKey *wallet.Key
extraNodeOpts []node.Option extraNodeOpts []node.Option
optBuilders []OptBuilder
} }
// DefaultNodeOpts are the default options that will be applied to test nodes. // DefaultNodeOpts are the default options that will be applied to test nodes.
@ -33,6 +34,10 @@ var DefaultNodeOpts = nodeOpts{
sectors: DefaultPresealsPerBootstrapMiner, 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. // NodeOpt is a functional option for test nodes.
type NodeOpt func(opts *nodeOpts) error type NodeOpt func(opts *nodeOpts) error
@ -89,3 +94,12 @@ func ConstructorOpts(extra ...node.Option) NodeOpt {
return nil 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
}
}

View File

@ -3,7 +3,6 @@ package itests
import ( import (
"context" "context"
"fmt" "fmt"
"os"
"regexp" "regexp"
"strings" "strings"
"testing" "testing"
@ -12,26 +11,25 @@ import (
"github.com/filecoin-project/go-address" "github.com/filecoin-project/go-address"
"github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/cli" "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" "github.com/stretchr/testify/require"
) )
// TestMultisig does a basic test to exercise the multisig CLI commands // TestMultisig does a basic test to exercise the multisig CLI commands
func TestMultisig(t *testing.T) { func TestMultisig(t *testing.T) {
_ = os.Setenv("BELLMAN_NO_GPU", "1") kit2.QuietMiningLogs()
kit.QuietMiningLogs()
blocktime := 5 * time.Millisecond blockTime := 5 * time.Millisecond
ctx := context.Background() client, _, ens := kit2.EnsembleMinimal(t, kit2.MockProofs(), kit2.ThroughRPC())
clientNode, _ := kit.StartOneNodeOneMiner(ctx, t, blocktime) 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 // Create mock CLI
ctx := context.Background() ctx := context.Background()
mockCLI := kit.NewMockCLI(ctx, t, cli.Commands) mockCLI := kit2.NewMockCLI(ctx, t, cli.Commands)
clientCLI := mockCLI.Client(clientNode.ListenAddr) clientCLI := mockCLI.Client(clientNode.ListenAddr)
// Create some wallets on the node to use for testing multisig // Create some wallets on the node to use for testing multisig
@ -42,7 +40,7 @@ func runMultisigTests(t *testing.T, clientNode kit.TestFullNode) {
walletAddrs = append(walletAddrs, addr) 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 // Create an msig with three of the addresses and threshold of two sigs

View File

@ -51,7 +51,7 @@ func TestPaymentChannelsAPI(t *testing.T) {
receiverAddr, err := paymentReceiver.WalletNew(ctx, types.KTSecp256k1) receiverAddr, err := paymentReceiver.WalletNew(ctx, types.KTSecp256k1)
require.NoError(t, err) 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 // setup the payment channel
createrAddr, err := paymentCreator.WalletDefaultAddress(ctx) createrAddr, err := paymentCreator.WalletDefaultAddress(ctx)

View File

@ -428,7 +428,7 @@ func startPaychCreatorReceiverMiner(ctx context.Context, t *testing.T, paymentCr
// Send some funds to the second node // Send some funds to the second node
receiverAddr, err := paymentReceiver.WalletDefaultAddress(ctx) receiverAddr, err := paymentReceiver.WalletDefaultAddress(ctx)
require.NoError(t, err) 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 // Get the first node's address
creatorAddr, err := paymentCreator.WalletDefaultAddress(ctx) creatorAddr, err := paymentCreator.WalletDefaultAddress(ctx)