2021-05-19 16:14:14 +00:00
|
|
|
package itests
|
2020-10-07 09:26:15 +00:00
|
|
|
|
|
|
|
import (
|
2020-10-07 16:14:12 +00:00
|
|
|
"bytes"
|
2020-10-07 09:26:15 +00:00
|
|
|
"context"
|
|
|
|
"fmt"
|
2021-05-21 15:39:43 +00:00
|
|
|
"math"
|
2020-10-07 09:26:15 +00:00
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2021-05-21 15:39:43 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/stmgr"
|
2021-05-19 16:14:14 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2020-10-07 09:26:15 +00:00
|
|
|
"golang.org/x/xerrors"
|
2021-03-27 14:35:46 +00:00
|
|
|
|
2020-10-07 09:26:15 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"github.com/filecoin-project/go-jsonrpc"
|
|
|
|
"github.com/filecoin-project/go-state-types/abi"
|
2021-05-20 10:56:43 +00:00
|
|
|
"github.com/ipfs/go-cid"
|
2021-05-19 16:14:14 +00:00
|
|
|
|
2020-10-08 09:24:31 +00:00
|
|
|
"github.com/filecoin-project/lotus/api"
|
|
|
|
"github.com/filecoin-project/lotus/api/client"
|
2020-10-07 09:26:15 +00:00
|
|
|
"github.com/filecoin-project/lotus/chain/actors/policy"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2021-05-19 16:14:14 +00:00
|
|
|
"github.com/filecoin-project/lotus/cli"
|
|
|
|
"github.com/filecoin-project/lotus/gateway"
|
|
|
|
"github.com/filecoin-project/lotus/itests/kit"
|
2020-10-08 09:24:31 +00:00
|
|
|
"github.com/filecoin-project/lotus/node"
|
2021-05-19 16:14:14 +00:00
|
|
|
|
|
|
|
init2 "github.com/filecoin-project/specs-actors/v2/actors/builtin/init"
|
|
|
|
multisig2 "github.com/filecoin-project/specs-actors/v2/actors/builtin/multisig"
|
2020-10-07 09:26:15 +00:00
|
|
|
)
|
|
|
|
|
2021-05-21 15:39:43 +00:00
|
|
|
const (
|
|
|
|
maxLookbackCap = time.Duration(math.MaxInt64)
|
|
|
|
maxStateWaitLookbackLimit = stmgr.LookbackNoLimit
|
|
|
|
)
|
|
|
|
|
2020-10-07 09:26:15 +00:00
|
|
|
func init() {
|
|
|
|
policy.SetSupportedProofTypes(abi.RegisteredSealProof_StackedDrg2KiBV1)
|
|
|
|
policy.SetConsensusMinerMinPower(abi.NewStoragePower(2048))
|
|
|
|
policy.SetMinVerifiedDealSize(abi.NewStoragePower(256))
|
|
|
|
}
|
|
|
|
|
2021-05-21 15:39:43 +00:00
|
|
|
// TestGatewayWalletMsig tests that API calls to wallet and msig can be made on a lite
|
2020-10-09 11:41:09 +00:00
|
|
|
// node that is connected through a gateway to a full API node
|
2021-05-21 15:39:43 +00:00
|
|
|
func TestGatewayWalletMsig(t *testing.T) {
|
2020-10-07 09:26:15 +00:00
|
|
|
_ = os.Setenv("BELLMAN_NO_GPU", "1")
|
2021-05-19 16:14:14 +00:00
|
|
|
kit.QuietMiningLogs()
|
2020-10-07 09:26:15 +00:00
|
|
|
|
|
|
|
blocktime := 5 * time.Millisecond
|
|
|
|
ctx := context.Background()
|
2021-05-21 15:39:43 +00:00
|
|
|
nodes := startNodes(ctx, t, blocktime, maxLookbackCap, maxStateWaitLookbackLimit)
|
2020-10-09 11:41:09 +00:00
|
|
|
defer nodes.closer()
|
|
|
|
|
|
|
|
lite := nodes.lite
|
|
|
|
full := nodes.full
|
2020-10-07 09:26:15 +00:00
|
|
|
|
|
|
|
// The full node starts with a wallet
|
|
|
|
fullWalletAddr, err := full.WalletDefaultAddress(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Check the full node's wallet balance from the lite node
|
|
|
|
balance, err := lite.WalletBalance(ctx, fullWalletAddr)
|
|
|
|
require.NoError(t, err)
|
|
|
|
fmt.Println(balance)
|
|
|
|
|
|
|
|
// Create a wallet on the lite node
|
2020-10-11 18:12:01 +00:00
|
|
|
liteWalletAddr, err := lite.WalletNew(ctx, types.KTSecp256k1)
|
2020-10-07 09:26:15 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Send some funds from the full node to the lite node
|
2020-10-15 10:15:21 +00:00
|
|
|
err = sendFunds(ctx, full, fullWalletAddr, liteWalletAddr, types.NewInt(1e18))
|
2020-10-07 09:26:15 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Send some funds from the lite node back to the full node
|
2020-10-15 10:15:21 +00:00
|
|
|
err = sendFunds(ctx, lite, liteWalletAddr, fullWalletAddr, types.NewInt(100))
|
2020-10-07 09:26:15 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-10-07 11:59:32 +00:00
|
|
|
// Sign some data with the lite node wallet address
|
2020-10-07 09:26:15 +00:00
|
|
|
data := []byte("hello")
|
|
|
|
sig, err := lite.WalletSign(ctx, liteWalletAddr, data)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-10-07 11:59:32 +00:00
|
|
|
// Verify the signature
|
2020-10-07 09:26:15 +00:00
|
|
|
ok, err := lite.WalletVerify(ctx, liteWalletAddr, data, sig)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, ok)
|
2020-10-07 16:14:12 +00:00
|
|
|
|
|
|
|
// Create some wallets on the lite node to use for testing multisig
|
|
|
|
var walletAddrs []address.Address
|
|
|
|
for i := 0; i < 4; i++ {
|
2020-10-11 18:12:01 +00:00
|
|
|
addr, err := lite.WalletNew(ctx, types.KTSecp256k1)
|
2020-10-07 16:14:12 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
walletAddrs = append(walletAddrs, addr)
|
|
|
|
|
2020-10-15 10:15:21 +00:00
|
|
|
err = sendFunds(ctx, lite, liteWalletAddr, addr, types.NewInt(1e15))
|
2020-10-07 16:14:12 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create an msig with three of the addresses and threshold of two sigs
|
|
|
|
msigAddrs := walletAddrs[:3]
|
|
|
|
amt := types.NewInt(1000)
|
2021-03-27 14:35:46 +00:00
|
|
|
proto, err := lite.MsigCreate(ctx, 2, msigAddrs, abi.ChainEpoch(50), amt, liteWalletAddr, types.NewInt(0))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
doSend := func(proto *api.MessagePrototype) (cid.Cid, error) {
|
|
|
|
if proto.ValidNonce {
|
|
|
|
sm, err := lite.WalletSignMessage(ctx, proto.Message.From, &proto.Message)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return lite.MpoolPush(ctx, sm)
|
|
|
|
}
|
|
|
|
|
|
|
|
sm, err := lite.MpoolPushMessage(ctx, &proto.Message, nil)
|
|
|
|
if err != nil {
|
|
|
|
return cid.Undef, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return sm.Cid(), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
addProposal, err := doSend(proto)
|
2020-10-07 16:14:12 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-04-05 19:34:03 +00:00
|
|
|
res, err := lite.StateWaitMsg(ctx, addProposal, 1, api.LookbackNoLimit, true)
|
2020-10-07 16:14:12 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, 0, res.Receipt.ExitCode)
|
|
|
|
|
2020-10-08 01:09:33 +00:00
|
|
|
var execReturn init2.ExecReturn
|
2020-10-07 16:14:12 +00:00
|
|
|
err = execReturn.UnmarshalCBOR(bytes.NewReader(res.Receipt.Return))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Get available balance of msig: should be greater than zero and less
|
|
|
|
// than initial amount
|
|
|
|
msig := execReturn.IDAddress
|
|
|
|
msigBalance, err := lite.MsigGetAvailableBalance(ctx, msig, types.EmptyTSK)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Greater(t, msigBalance.Int64(), int64(0))
|
|
|
|
require.Less(t, msigBalance.Int64(), amt.Int64())
|
|
|
|
|
|
|
|
// Propose to add a new address to the msig
|
2021-03-27 14:35:46 +00:00
|
|
|
proto, err = lite.MsigAddPropose(ctx, msig, walletAddrs[0], walletAddrs[3], false)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
addProposal, err = doSend(proto)
|
2020-10-07 16:14:12 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-04-05 19:34:03 +00:00
|
|
|
res, err = lite.StateWaitMsg(ctx, addProposal, 1, api.LookbackNoLimit, true)
|
2020-10-07 16:14:12 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, 0, res.Receipt.ExitCode)
|
|
|
|
|
2020-10-08 01:09:33 +00:00
|
|
|
var proposeReturn multisig2.ProposeReturn
|
2020-10-07 16:14:12 +00:00
|
|
|
err = proposeReturn.UnmarshalCBOR(bytes.NewReader(res.Receipt.Return))
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Approve proposal (proposer is first (implicit) signer, approver is
|
|
|
|
// second signer
|
|
|
|
txnID := uint64(proposeReturn.TxnID)
|
2021-03-27 14:35:46 +00:00
|
|
|
proto, err = lite.MsigAddApprove(ctx, msig, walletAddrs[1], txnID, walletAddrs[0], walletAddrs[3], false)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
approval1, err := doSend(proto)
|
2020-10-07 16:14:12 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-04-05 19:34:03 +00:00
|
|
|
res, err = lite.StateWaitMsg(ctx, approval1, 1, api.LookbackNoLimit, true)
|
2020-10-07 16:14:12 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.EqualValues(t, 0, res.Receipt.ExitCode)
|
|
|
|
|
2020-10-08 01:09:33 +00:00
|
|
|
var approveReturn multisig2.ApproveReturn
|
2020-10-07 16:14:12 +00:00
|
|
|
err = approveReturn.UnmarshalCBOR(bytes.NewReader(res.Receipt.Return))
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, approveReturn.Applied)
|
2020-10-07 09:26:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-21 15:39:43 +00:00
|
|
|
// TestGatewayMsigCLI tests that msig CLI calls can be made
|
2020-10-15 10:15:21 +00:00
|
|
|
// on a lite node that is connected through a gateway to a full API node
|
2021-05-21 15:39:43 +00:00
|
|
|
func TestGatewayMsigCLI(t *testing.T) {
|
2020-10-15 10:15:21 +00:00
|
|
|
_ = os.Setenv("BELLMAN_NO_GPU", "1")
|
2021-05-19 16:14:14 +00:00
|
|
|
kit.QuietMiningLogs()
|
2020-10-15 10:15:21 +00:00
|
|
|
|
|
|
|
blocktime := 5 * time.Millisecond
|
|
|
|
ctx := context.Background()
|
2021-05-21 15:39:43 +00:00
|
|
|
nodes := startNodesWithFunds(ctx, t, blocktime, maxLookbackCap, maxStateWaitLookbackLimit)
|
2020-10-09 11:41:09 +00:00
|
|
|
defer nodes.closer()
|
|
|
|
|
|
|
|
lite := nodes.lite
|
2021-05-19 16:14:14 +00:00
|
|
|
runMultisigTests(t, lite)
|
2020-10-15 10:15:21 +00:00
|
|
|
}
|
|
|
|
|
2021-05-21 15:39:43 +00:00
|
|
|
func TestGatewayDealFlow(t *testing.T) {
|
2020-10-20 15:08:25 +00:00
|
|
|
_ = os.Setenv("BELLMAN_NO_GPU", "1")
|
2021-05-19 16:14:14 +00:00
|
|
|
kit.QuietMiningLogs()
|
2020-10-07 09:26:15 +00:00
|
|
|
|
2020-10-20 15:08:25 +00:00
|
|
|
blocktime := 5 * time.Millisecond
|
|
|
|
ctx := context.Background()
|
2021-05-21 15:39:43 +00:00
|
|
|
nodes := startNodesWithFunds(ctx, t, blocktime, maxLookbackCap, maxStateWaitLookbackLimit)
|
2020-10-20 15:08:25 +00:00
|
|
|
defer nodes.closer()
|
2020-10-07 09:26:15 +00:00
|
|
|
|
2020-12-01 13:00:28 +00:00
|
|
|
// 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)
|
2021-05-20 15:12:42 +00:00
|
|
|
|
|
|
|
dh := kit.NewDealHarness(t, nodes.lite, nodes.miner)
|
|
|
|
dh.MakeFullDeal(ctx, 6, false, false, dealStartEpoch)
|
2020-10-07 09:26:15 +00:00
|
|
|
}
|
|
|
|
|
2021-05-21 15:39:43 +00:00
|
|
|
func TestGatewayCLIDealFlow(t *testing.T) {
|
2020-10-09 11:41:09 +00:00
|
|
|
_ = os.Setenv("BELLMAN_NO_GPU", "1")
|
2021-05-19 16:14:14 +00:00
|
|
|
kit.QuietMiningLogs()
|
2020-10-09 11:41:09 +00:00
|
|
|
|
|
|
|
blocktime := 5 * time.Millisecond
|
|
|
|
ctx := context.Background()
|
2021-05-21 15:39:43 +00:00
|
|
|
nodes := startNodesWithFunds(ctx, t, blocktime, maxLookbackCap, maxStateWaitLookbackLimit)
|
2020-10-09 11:41:09 +00:00
|
|
|
defer nodes.closer()
|
|
|
|
|
2021-05-19 16:14:14 +00:00
|
|
|
kit.RunClientTest(t, cli.Commands, nodes.lite)
|
2020-10-20 15:08:25 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type testNodes struct {
|
2021-05-19 16:14:14 +00:00
|
|
|
lite kit.TestFullNode
|
|
|
|
full kit.TestFullNode
|
|
|
|
miner kit.TestMiner
|
2020-10-20 15:08:25 +00:00
|
|
|
closer jsonrpc.ClientCloser
|
|
|
|
}
|
|
|
|
|
|
|
|
func startNodesWithFunds(
|
|
|
|
ctx context.Context,
|
|
|
|
t *testing.T,
|
|
|
|
blocktime time.Duration,
|
|
|
|
lookbackCap time.Duration,
|
|
|
|
stateWaitLookbackLimit abi.ChainEpoch,
|
|
|
|
) *testNodes {
|
|
|
|
nodes := startNodes(ctx, t, blocktime, lookbackCap, stateWaitLookbackLimit)
|
2020-10-09 11:41:09 +00:00
|
|
|
|
|
|
|
// The full node starts with a wallet
|
2020-10-20 15:08:25 +00:00
|
|
|
fullWalletAddr, err := nodes.full.WalletDefaultAddress(ctx)
|
2020-10-09 11:41:09 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Create a wallet on the lite node
|
2020-10-20 15:08:25 +00:00
|
|
|
liteWalletAddr, err := nodes.lite.WalletNew(ctx, types.KTSecp256k1)
|
2020-10-09 11:41:09 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Send some funds from the full node to the lite node
|
2020-10-20 15:08:25 +00:00
|
|
|
err = sendFunds(ctx, nodes.full, fullWalletAddr, liteWalletAddr, types.NewInt(1e18))
|
2020-10-09 11:41:09 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-10-20 15:08:25 +00:00
|
|
|
return nodes
|
2020-10-09 11:41:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func startNodes(
|
|
|
|
ctx context.Context,
|
|
|
|
t *testing.T,
|
|
|
|
blocktime time.Duration,
|
|
|
|
lookbackCap time.Duration,
|
|
|
|
stateWaitLookbackLimit abi.ChainEpoch,
|
|
|
|
) *testNodes {
|
2020-10-07 09:26:15 +00:00
|
|
|
var closer jsonrpc.ClientCloser
|
|
|
|
|
2020-10-07 11:59:32 +00:00
|
|
|
// Create one miner and two full nodes.
|
|
|
|
// - Put a gateway server in front of full node 1
|
|
|
|
// - Start full node 2 in lite mode
|
|
|
|
// - Connect lite node -> gateway server -> full node
|
2020-10-07 09:26:15 +00:00
|
|
|
opts := append(
|
|
|
|
// Full node
|
2021-05-19 16:14:14 +00:00
|
|
|
kit.OneFull,
|
2020-10-07 09:26:15 +00:00
|
|
|
// Lite node
|
2021-05-19 16:14:14 +00:00
|
|
|
kit.FullNodeOpts{
|
2020-10-08 09:24:31 +00:00
|
|
|
Lite: true,
|
2021-05-19 16:14:14 +00:00
|
|
|
Opts: func(nodes []kit.TestFullNode) node.Option {
|
2020-10-08 09:24:31 +00:00
|
|
|
fullNode := nodes[0]
|
|
|
|
|
|
|
|
// Create a gateway server in front of the full node
|
2021-05-23 17:57:04 +00:00
|
|
|
gwapi := gateway.NewNode(fullNode, lookbackCap, stateWaitLookbackLimit)
|
|
|
|
handler, err := gateway.Handler(gwapi)
|
2020-10-08 09:24:31 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-05-23 17:57:04 +00:00
|
|
|
srv, _ := kit.CreateRPCServer(t, handler)
|
|
|
|
|
2020-10-08 09:24:31 +00:00
|
|
|
// Create a gateway client API that connects to the gateway server
|
2021-03-23 12:42:56 +00:00
|
|
|
var gapi api.Gateway
|
2021-06-09 23:01:41 +00:00
|
|
|
gapi, closer, err = client.NewGatewayRPCV1(ctx, "ws://"+srv.Listener.Addr().String()+"/rpc/v1", nil)
|
2020-10-08 09:24:31 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// Provide the gateway API to dependency injection
|
2021-03-23 12:42:56 +00:00
|
|
|
return node.Override(new(api.Gateway), gapi)
|
2020-10-08 09:24:31 +00:00
|
|
|
},
|
2020-10-07 09:26:15 +00:00
|
|
|
},
|
|
|
|
)
|
2021-05-20 11:17:41 +00:00
|
|
|
n, sn := kit.RPCMockMinerBuilder(t, opts, kit.OneMiner)
|
2020-10-07 09:26:15 +00:00
|
|
|
|
|
|
|
full := n[0]
|
|
|
|
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)
|
|
|
|
|
2020-10-09 11:41:09 +00:00
|
|
|
// 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)
|
|
|
|
|
2020-10-07 09:26:15 +00:00
|
|
|
// Start mining blocks
|
2021-05-19 16:14:14 +00:00
|
|
|
bm := kit.NewBlockMiner(t, miner)
|
|
|
|
bm.MineBlocks(ctx, blocktime)
|
2020-11-12 15:23:46 +00:00
|
|
|
t.Cleanup(bm.Stop)
|
2020-10-07 09:26:15 +00:00
|
|
|
|
2020-10-09 11:41:09 +00:00
|
|
|
return &testNodes{lite: lite, full: full, miner: miner, closer: closer}
|
2020-10-07 09:26:15 +00:00
|
|
|
}
|
2020-10-20 15:08:25 +00:00
|
|
|
|
2021-05-19 16:14:14 +00:00
|
|
|
func sendFunds(ctx context.Context, fromNode kit.TestFullNode, fromAddr address.Address, toAddr address.Address, amt types.BigInt) error {
|
2020-10-20 15:08:25 +00:00
|
|
|
msg := &types.Message{
|
|
|
|
From: fromAddr,
|
|
|
|
To: toAddr,
|
|
|
|
Value: amt,
|
|
|
|
}
|
|
|
|
|
|
|
|
sm, err := fromNode.MpoolPushMessage(ctx, msg, nil)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2021-05-07 14:38:40 +00:00
|
|
|
res, err := fromNode.StateWaitMsg(ctx, sm.Cid(), 3, api.LookbackNoLimit, true)
|
2020-10-20 15:08:25 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
if res.Receipt.ExitCode != 0 {
|
|
|
|
return xerrors.Errorf("send funds failed with exit code %d", res.Receipt.ExitCode)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|