2021-05-18 20:32:10 +00:00
|
|
|
package itests
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"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-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-11 17:26:25 +00:00
|
|
|
"github.com/filecoin-project/lotus/itests/kit2"
|
2021-05-18 20:32:10 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAPI(t *testing.T) {
|
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-11 17:26:25 +00:00
|
|
|
runAPITest(t, kit2.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)
|
|
|
|
t.Run("testSearchMsg", ts.testSearchMsg)
|
|
|
|
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-11 17:26:25 +00:00
|
|
|
full, _, _ := kit2.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-11 17:26:25 +00:00
|
|
|
full, _, _ := kit2.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-11 17:26:25 +00:00
|
|
|
one, two, _, ens := kit2.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)
|
|
|
|
require.Lenf(t, peers, 1, "node one doesn't have 1 peer")
|
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)
|
|
|
|
require.Lenf(t, peers, 1, "node two doesn't have 1 peer")
|
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-11 17:26:25 +00:00
|
|
|
full, _, ens := kit2.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-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-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-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
|
|
|
|
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-11 17:26:25 +00:00
|
|
|
full, miner, _ := kit2.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-11 17:26:25 +00:00
|
|
|
bm := kit2.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-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-11 17:26:25 +00:00
|
|
|
full, genesisMiner, ens := kit2.EnsembleMinimal(t, ts.opts...)
|
2021-05-25 23:04:13 +00:00
|
|
|
|
|
|
|
ens.BeginMining(4 * time.Millisecond)
|
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-11 17:26:25 +00:00
|
|
|
var newMiner kit2.TestMiner
|
|
|
|
ens.Miner(&newMiner, full, kit2.OwnerAddr(full.DefaultKey)).Start()
|
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)
|
|
|
|
}
|