2022-08-29 14:25:30 +00:00
|
|
|
// stm: #integration
|
2021-05-18 20:32:10 +00:00
|
|
|
package itests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2022-06-14 15:00:51 +00:00
|
|
|
logging "github.com/ipfs/go-log/v2"
|
2022-08-25 18:20:41 +00:00
|
|
|
"github.com/libp2p/go-libp2p/core/peer"
|
2022-06-14 15:00:51 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-07-18 16:36:51 +00:00
|
|
|
"golang.org/x/xerrors"
|
2022-06-14 15:00:51 +00:00
|
|
|
|
2021-05-18 20:32:10 +00:00
|
|
|
"github.com/filecoin-project/go-address"
|
|
|
|
"github.com/filecoin-project/go-state-types/big"
|
2021-05-25 23:04:13 +00:00
|
|
|
"github.com/filecoin-project/go-state-types/exitcode"
|
2021-08-20 14:53:24 +00:00
|
|
|
|
2021-05-18 20:32:10 +00:00
|
|
|
lapi "github.com/filecoin-project/lotus/api"
|
|
|
|
"github.com/filecoin-project/lotus/build"
|
|
|
|
"github.com/filecoin-project/lotus/chain/types"
|
2021-06-18 18:45:29 +00:00
|
|
|
"github.com/filecoin-project/lotus/itests/kit"
|
2021-05-18 20:32:10 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestAPI(t *testing.T) {
|
2021-12-13 12:41:04 +00:00
|
|
|
//stm: @CHAIN_SYNCER_LOAD_GENESIS_001, @CHAIN_SYNCER_FETCH_TIPSET_001,
|
|
|
|
//stm: @CHAIN_SYNCER_START_001, @CHAIN_SYNCER_SYNC_001, @BLOCKCHAIN_BEACON_VALIDATE_BLOCK_VALUES_01
|
|
|
|
//stm: @CHAIN_SYNCER_COLLECT_CHAIN_001, @CHAIN_SYNCER_COLLECT_HEADERS_001, @CHAIN_SYNCER_VALIDATE_TIPSET_001
|
|
|
|
//stm: @CHAIN_SYNCER_NEW_PEER_HEAD_001, @CHAIN_SYNCER_VALIDATE_MESSAGE_META_001, @CHAIN_SYNCER_STOP_001
|
2021-12-10 15:08:25 +00:00
|
|
|
|
|
|
|
//stm: @CHAIN_STATE_MINER_INFO_001
|
2021-05-21 12:39:09 +00:00
|
|
|
t.Run("direct", func(t *testing.T) {
|
2021-05-25 23:04:13 +00:00
|
|
|
runAPITest(t)
|
2021-05-21 12:39:09 +00:00
|
|
|
})
|
|
|
|
t.Run("rpc", func(t *testing.T) {
|
2021-06-18 18:45:29 +00:00
|
|
|
runAPITest(t, kit.ThroughRPC())
|
2021-05-21 12:39:09 +00:00
|
|
|
})
|
2021-05-18 20:32:10 +00:00
|
|
|
}
|
|
|
|
|
2021-05-21 12:39:09 +00:00
|
|
|
type apiSuite struct {
|
2021-06-13 22:43:22 +00:00
|
|
|
opts []interface{}
|
2021-05-18 20:32:10 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// runAPITest is the entry point to API test suite
|
2021-06-13 22:43:22 +00:00
|
|
|
func runAPITest(t *testing.T, opts ...interface{}) {
|
2021-05-25 23:04:13 +00:00
|
|
|
ts := apiSuite{opts: opts}
|
2021-05-18 20:32:10 +00:00
|
|
|
|
|
|
|
t.Run("version", ts.testVersion)
|
|
|
|
t.Run("id", ts.testID)
|
|
|
|
t.Run("testConnectTwo", ts.testConnectTwo)
|
|
|
|
t.Run("testMining", ts.testMining)
|
|
|
|
t.Run("testMiningReal", ts.testMiningReal)
|
2021-08-02 21:12:00 +00:00
|
|
|
t.Run("testSlowNotify", ts.testSlowNotify)
|
2021-05-18 20:32:10 +00:00
|
|
|
t.Run("testSearchMsg", ts.testSearchMsg)
|
2022-07-18 16:36:51 +00:00
|
|
|
t.Run("testOutOfGasError", ts.testOutOfGasError)
|
|
|
|
t.Run("testLookupNotFoundError", ts.testLookupNotFoundError)
|
2021-05-18 20:32:10 +00:00
|
|
|
t.Run("testNonGenesisMiner", ts.testNonGenesisMiner)
|
|
|
|
}
|
|
|
|
|
2021-05-20 11:17:41 +00:00
|
|
|
func (ts *apiSuite) testVersion(t *testing.T) {
|
2021-05-18 20:32:10 +00:00
|
|
|
lapi.RunningNodeType = lapi.NodeFull
|
|
|
|
t.Cleanup(func() {
|
|
|
|
lapi.RunningNodeType = lapi.NodeUnknown
|
|
|
|
})
|
|
|
|
|
2021-06-18 18:45:29 +00:00
|
|
|
full, _, _ := kit.EnsembleMinimal(t, ts.opts...)
|
2021-05-25 23:04:13 +00:00
|
|
|
|
|
|
|
v, err := full.Version(context.Background())
|
|
|
|
require.NoError(t, err)
|
2021-05-18 20:32:10 +00:00
|
|
|
|
|
|
|
versions := strings.Split(v.Version, "+")
|
2021-05-25 23:04:13 +00:00
|
|
|
require.NotZero(t, len(versions), "empty version")
|
2021-05-18 20:32:10 +00:00
|
|
|
require.Equal(t, versions[0], build.BuildVersion)
|
|
|
|
}
|
|
|
|
|
2021-05-20 11:17:41 +00:00
|
|
|
func (ts *apiSuite) testID(t *testing.T) {
|
2021-05-18 20:32:10 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2021-06-18 18:45:29 +00:00
|
|
|
full, _, _ := kit.EnsembleMinimal(t, ts.opts...)
|
2021-05-25 23:04:13 +00:00
|
|
|
|
|
|
|
id, err := full.ID(ctx)
|
2021-05-18 20:32:10 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
2021-05-25 23:04:13 +00:00
|
|
|
require.Regexp(t, "^12", id.Pretty())
|
2021-05-18 20:32:10 +00:00
|
|
|
}
|
|
|
|
|
2021-05-20 11:17:41 +00:00
|
|
|
func (ts *apiSuite) testConnectTwo(t *testing.T) {
|
2021-05-18 20:32:10 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2021-06-18 18:45:29 +00:00
|
|
|
one, two, _, ens := kit.EnsembleTwoOne(t, ts.opts...)
|
2021-05-18 20:32:10 +00:00
|
|
|
|
2021-05-25 23:04:13 +00:00
|
|
|
p, err := one.NetPeers(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, p, "node one has peers")
|
2021-05-18 20:32:10 +00:00
|
|
|
|
2021-05-25 23:04:13 +00:00
|
|
|
p, err = two.NetPeers(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Empty(t, p, "node two has peers")
|
2021-05-18 20:32:10 +00:00
|
|
|
|
2021-05-25 23:04:13 +00:00
|
|
|
ens.InterconnectAll()
|
2021-05-18 20:32:10 +00:00
|
|
|
|
2021-05-25 23:04:13 +00:00
|
|
|
peers, err := one.NetPeers(ctx)
|
|
|
|
require.NoError(t, err)
|
2022-06-23 18:02:10 +00:00
|
|
|
|
|
|
|
countPeerIDs := func(peers []peer.AddrInfo) int {
|
|
|
|
peerIDs := make(map[peer.ID]struct{})
|
|
|
|
for _, p := range peers {
|
|
|
|
peerIDs[p.ID] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
return len(peerIDs)
|
|
|
|
}
|
|
|
|
|
|
|
|
require.Equal(t, countPeerIDs(peers), 2, "node one doesn't have 2 peers")
|
2021-05-18 20:32:10 +00:00
|
|
|
|
2021-05-25 23:04:13 +00:00
|
|
|
peers, err = two.NetPeers(ctx)
|
|
|
|
require.NoError(t, err)
|
2022-06-23 18:02:10 +00:00
|
|
|
require.Equal(t, countPeerIDs(peers), 2, "node one doesn't have 2 peers")
|
2021-05-18 20:32:10 +00:00
|
|
|
}
|
|
|
|
|
2021-05-25 23:04:13 +00:00
|
|
|
func (ts *apiSuite) testSearchMsg(t *testing.T) {
|
2021-05-18 20:32:10 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2021-06-18 18:45:29 +00:00
|
|
|
full, _, ens := kit.EnsembleMinimal(t, ts.opts...)
|
2021-05-25 23:04:13 +00:00
|
|
|
|
|
|
|
senderAddr, err := full.WalletDefaultAddress(ctx)
|
2021-05-18 20:32:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-05-25 23:04:13 +00:00
|
|
|
msg := &types.Message{
|
|
|
|
From: senderAddr,
|
|
|
|
To: senderAddr,
|
|
|
|
Value: big.Zero(),
|
|
|
|
}
|
|
|
|
|
|
|
|
ens.BeginMining(100 * time.Millisecond)
|
|
|
|
|
|
|
|
sm, err := full.MpoolPushMessage(ctx, msg, nil)
|
2021-05-18 20:32:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-12-10 15:08:25 +00:00
|
|
|
//stm: @CHAIN_STATE_WAIT_MSG_001
|
2021-05-25 23:04:13 +00:00
|
|
|
res, err := full.StateWaitMsg(ctx, sm.Cid(), 1, lapi.LookbackNoLimit, true)
|
2021-05-18 20:32:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-05-25 23:04:13 +00:00
|
|
|
require.Equal(t, exitcode.Ok, res.Receipt.ExitCode, "message not successful")
|
2021-05-18 20:32:10 +00:00
|
|
|
|
2021-12-10 15:08:25 +00:00
|
|
|
//stm: @CHAIN_STATE_SEARCH_MSG_001
|
2021-05-25 23:04:13 +00:00
|
|
|
searchRes, err := full.StateSearchMsg(ctx, types.EmptyTSK, sm.Cid(), lapi.LookbackNoLimit, true)
|
2021-05-18 20:32:10 +00:00
|
|
|
require.NoError(t, err)
|
2021-07-20 18:28:27 +00:00
|
|
|
require.NotNil(t, searchRes)
|
2021-05-18 20:32:10 +00:00
|
|
|
|
2021-05-25 23:04:13 +00:00
|
|
|
require.Equalf(t, res.TipSet, searchRes.TipSet, "search ts: %s, different from wait ts: %s", searchRes.TipSet, res.TipSet)
|
|
|
|
}
|
2021-05-18 20:32:10 +00:00
|
|
|
|
2022-07-18 16:36:51 +00:00
|
|
|
func (ts *apiSuite) testOutOfGasError(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
full, _, _ := kit.EnsembleMinimal(t, ts.opts...)
|
|
|
|
|
|
|
|
senderAddr, err := full.WalletDefaultAddress(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// the gas estimator API executes the message with gasLimit = BlockGasLimit
|
|
|
|
// Lowering it to 2 will cause it to run out of gas, testing the failure case we want
|
|
|
|
originalLimit := build.BlockGasLimit
|
|
|
|
build.BlockGasLimit = 2
|
|
|
|
defer func() {
|
|
|
|
build.BlockGasLimit = originalLimit
|
|
|
|
}()
|
|
|
|
|
|
|
|
msg := &types.Message{
|
|
|
|
From: senderAddr,
|
|
|
|
To: senderAddr,
|
|
|
|
Value: big.Zero(),
|
|
|
|
}
|
|
|
|
|
|
|
|
_, err = full.GasEstimateMessageGas(ctx, msg, nil, types.EmptyTSK)
|
|
|
|
require.Error(t, err, "should have failed")
|
2022-09-21 16:28:11 +00:00
|
|
|
require.True(t, xerrors.Is(err, &lapi.ErrOutOfGas{}))
|
2022-07-18 16:36:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (ts *apiSuite) testLookupNotFoundError(t *testing.T) {
|
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
full, _, _ := kit.EnsembleMinimal(t, ts.opts...)
|
|
|
|
|
|
|
|
addr, err := full.WalletNew(ctx, types.KTSecp256k1)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
_, err = full.StateLookupID(ctx, addr, types.EmptyTSK)
|
|
|
|
require.Error(t, err)
|
2022-09-21 16:28:11 +00:00
|
|
|
require.True(t, xerrors.Is(err, &lapi.ErrActorNotFound{}))
|
2022-07-18 16:36:51 +00:00
|
|
|
}
|
|
|
|
|
2021-05-25 23:04:13 +00:00
|
|
|
func (ts *apiSuite) testMining(t *testing.T) {
|
2021-05-18 20:32:10 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2021-06-18 18:45:29 +00:00
|
|
|
full, miner, _ := kit.EnsembleMinimal(t, ts.opts...)
|
2021-05-18 20:32:10 +00:00
|
|
|
|
2021-05-25 23:04:13 +00:00
|
|
|
newHeads, err := full.ChainNotify(ctx)
|
2021-05-18 20:32:10 +00:00
|
|
|
require.NoError(t, err)
|
2021-05-25 23:04:13 +00:00
|
|
|
initHead := (<-newHeads)[0]
|
|
|
|
baseHeight := initHead.Val.Height()
|
2021-05-18 20:32:10 +00:00
|
|
|
|
2021-05-25 23:04:13 +00:00
|
|
|
h1, err := full.ChainHead(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, int64(h1.Height()), int64(baseHeight))
|
2021-05-19 10:47:37 +00:00
|
|
|
|
2021-06-18 18:45:29 +00:00
|
|
|
bm := kit.NewBlockMiner(t, miner)
|
2021-05-25 23:04:13 +00:00
|
|
|
bm.MineUntilBlock(ctx, full, nil)
|
2021-05-18 20:32:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
<-newHeads
|
|
|
|
|
2021-05-25 23:04:13 +00:00
|
|
|
h2, err := full.ChainHead(ctx)
|
2021-05-18 20:32:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Greater(t, int64(h2.Height()), int64(h1.Height()))
|
|
|
|
|
2021-05-25 23:04:13 +00:00
|
|
|
bm.MineUntilBlock(ctx, full, nil)
|
2021-05-18 20:32:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
<-newHeads
|
|
|
|
|
2021-05-25 23:04:13 +00:00
|
|
|
h3, err := full.ChainHead(ctx)
|
2021-05-18 20:32:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Greater(t, int64(h3.Height()), int64(h2.Height()))
|
|
|
|
}
|
|
|
|
|
2021-05-25 23:04:13 +00:00
|
|
|
func (ts *apiSuite) testMiningReal(t *testing.T) {
|
|
|
|
build.InsecurePoStValidation = false
|
|
|
|
defer func() {
|
|
|
|
build.InsecurePoStValidation = true
|
|
|
|
}()
|
|
|
|
|
|
|
|
ts.testMining(t)
|
|
|
|
}
|
|
|
|
|
2021-08-02 21:12:00 +00:00
|
|
|
func (ts *apiSuite) testSlowNotify(t *testing.T) {
|
|
|
|
_ = logging.SetLogLevel("rpc", "ERROR")
|
|
|
|
|
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
full, miner, _ := kit.EnsembleMinimal(t, ts.opts...)
|
|
|
|
|
|
|
|
// Subscribe a bunch of times to make sure we fill up any RPC buffers.
|
|
|
|
var newHeadsChans []<-chan []*lapi.HeadChange
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
newHeads, err := full.ChainNotify(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
newHeadsChans = append(newHeadsChans, newHeads)
|
|
|
|
}
|
|
|
|
|
|
|
|
initHead := (<-newHeadsChans[0])[0]
|
|
|
|
baseHeight := initHead.Val.Height()
|
|
|
|
|
|
|
|
bm := kit.NewBlockMiner(t, miner)
|
|
|
|
bm.MineBlocks(ctx, time.Microsecond)
|
|
|
|
|
|
|
|
full.WaitTillChain(ctx, kit.HeightAtLeast(baseHeight+100))
|
|
|
|
|
2021-08-27 19:12:55 +00:00
|
|
|
// Make sure they were all closed, draining any buffered events first.
|
2021-08-02 21:12:00 +00:00
|
|
|
for _, ch := range newHeadsChans {
|
|
|
|
var ok bool
|
|
|
|
for ok {
|
|
|
|
select {
|
|
|
|
case _, ok = <-ch:
|
|
|
|
default:
|
|
|
|
t.Fatal("expected new heads channel to be closed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make sure we can resubscribe and everything still works.
|
|
|
|
newHeads, err := full.ChainNotify(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
for i := 0; i < 10; i++ {
|
|
|
|
_, ok := <-newHeads
|
|
|
|
require.True(t, ok, "notify channel closed")
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-20 11:17:41 +00:00
|
|
|
func (ts *apiSuite) testNonGenesisMiner(t *testing.T) {
|
2021-05-18 20:32:10 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2021-06-21 12:24:25 +00:00
|
|
|
full, genesisMiner, ens := kit.EnsembleMinimal(t, append(ts.opts, kit.MockProofs())...)
|
|
|
|
ens.InterconnectAll().BeginMining(4 * time.Millisecond)
|
2021-05-25 23:04:13 +00:00
|
|
|
|
2021-06-21 12:24:25 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
2021-05-18 20:32:10 +00:00
|
|
|
|
|
|
|
gaa, err := genesisMiner.ActorAddress(ctx)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-05-25 23:04:13 +00:00
|
|
|
_, err = full.StateMinerInfo(ctx, gaa, types.EmptyTSK)
|
2021-05-18 20:32:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2021-06-18 18:45:29 +00:00
|
|
|
var newMiner kit.TestMiner
|
2021-06-21 12:24:25 +00:00
|
|
|
ens.Miner(&newMiner, full,
|
|
|
|
kit.OwnerAddr(full.DefaultKey),
|
2021-08-20 14:53:24 +00:00
|
|
|
kit.SectorSize(2<<10),
|
2021-06-22 13:51:50 +00:00
|
|
|
kit.WithAllSubsystems(),
|
2021-06-21 12:24:25 +00:00
|
|
|
).Start().InterconnectAll()
|
2021-05-18 20:32:10 +00:00
|
|
|
|
2021-05-25 23:04:13 +00:00
|
|
|
ta, err := newMiner.ActorAddress(ctx)
|
2021-05-18 20:32:10 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
tid, err := address.IDFromAddress(ta)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.Equal(t, uint64(1001), tid)
|
|
|
|
}
|