Merge pull request #6507 from filecoin-project/refactor/itest-sector-terminate

refactor sector terminate test to use kit2
This commit is contained in:
raulk 2021-06-18 10:23:06 +01:00 committed by GitHub
commit c4e0ac21a0
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 73 additions and 98 deletions

View File

@ -239,23 +239,10 @@ func TestFirstDealEnablesMining(t *testing.T) {
// once the provider has mined a block, thanks to the power acquired from the deal, // once the provider has mined a block, thanks to the power acquired from the deal,
// we pass the test. // we pass the test.
providerMined := make(chan struct{}) providerMined := make(chan struct{})
heads, err := client.ChainNotify(ctx)
require.NoError(t, err)
go func() { go func() {
for chg := range heads { _ = client.WaitTillChain(ctx, kit2.BlockMinedBy(provider.ActorAddr))
for _, c := range chg { close(providerMined)
if c.Type != "apply" {
continue
}
for _, b := range c.Val.Blocks() {
if b.Miner == provider.ActorAddr {
close(providerMined)
return
}
}
}
}
}() }()
// now perform the deal. // now perform the deal.

View File

@ -23,7 +23,7 @@ type ensembleOpts struct {
} }
var DefaultEnsembleOpts = ensembleOpts{ var DefaultEnsembleOpts = ensembleOpts{
pastOffset: 100000 * time.Second, // time sufficiently in the past to trigger catch-up mining. pastOffset: 10000000 * time.Second, // time sufficiently in the past to trigger catch-up mining.
proofType: abi.RegisteredSealProof_StackedDrg2KiBV1, proofType: abi.RegisteredSealProof_StackedDrg2KiBV1,
} }

View File

@ -4,8 +4,11 @@ import (
"context" "context"
"testing" "testing"
"github.com/filecoin-project/go-address"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/lotus/api" "github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/api/v1api" "github.com/filecoin-project/lotus/api/v1api"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/wallet" "github.com/filecoin-project/lotus/chain/wallet"
"github.com/multiformats/go-multiaddr" "github.com/multiformats/go-multiaddr"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
@ -33,3 +36,50 @@ func (f *TestFullNode) CreateImportFile(ctx context.Context, rseed int, size int
require.NoError(f.t, err) require.NoError(f.t, err)
return res, path return res, path
} }
// WaitTillChain waits until a specified chain condition is met. It returns
// the first tipset where the condition is met.
func (f *TestFullNode) WaitTillChain(ctx context.Context, pred ChainPredicate) *types.TipSet {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
heads, err := f.ChainNotify(ctx)
require.NoError(f.t, err)
for chg := range heads {
for _, c := range chg {
if c.Type != "apply" {
continue
}
if ts := c.Val; pred(ts) {
return ts
}
}
}
require.Fail(f.t, "chain condition not met")
return nil
}
// ChainPredicate encapsulates a chain condition.
type ChainPredicate func(set *types.TipSet) bool
// HeightAtLeast returns a ChainPredicate that is satisfied when the chain
// height is equal or higher to the target.
func HeightAtLeast(target abi.ChainEpoch) ChainPredicate {
return func(ts *types.TipSet) bool {
return ts.Height() >= target
}
}
// BlockMinedBy returns a ChainPredicate that is satisfied when we observe the
// first block mined by the specified miner.
func BlockMinedBy(miner address.Address) ChainPredicate {
return func(ts *types.TipSet) bool {
for _, b := range ts.Blocks() {
if b.Miner == miner {
return true
}
}
return false
}
}

View File

@ -9,8 +9,6 @@ import (
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
"github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/lotus/api" "github.com/filecoin-project/lotus/api"
"github.com/filecoin-project/lotus/build" "github.com/filecoin-project/lotus/build"
sealing "github.com/filecoin-project/lotus/extern/storage-sealing" sealing "github.com/filecoin-project/lotus/extern/storage-sealing"
@ -60,7 +58,7 @@ func TestPledgeBatching(t *testing.T) {
client, miner, ens := kit2.EnsembleMinimal(t, kit2.MockProofs(), opts) client, miner, ens := kit2.EnsembleMinimal(t, kit2.MockProofs(), opts)
ens.InterconnectAll().BeginMining(blockTime) ens.InterconnectAll().BeginMining(blockTime)
waitTillChainHeight(ctx, t, client, 10) client.WaitTillChain(ctx, kit2.HeightAtLeast(10))
toCheck := miner.StartPledge(ctx, nSectors, 0, nil) toCheck := miner.StartPledge(ctx, nSectors, 0, nil)
@ -117,7 +115,7 @@ func TestPledgeBeforeNv13(t *testing.T) {
client, miner, ens := kit2.EnsembleMinimal(t, kit2.MockProofs(), opts) client, miner, ens := kit2.EnsembleMinimal(t, kit2.MockProofs(), opts)
ens.InterconnectAll().BeginMining(blocktime) ens.InterconnectAll().BeginMining(blocktime)
waitTillChainHeight(ctx, t, client, 10) client.WaitTillChain(ctx, kit2.HeightAtLeast(10))
toCheck := miner.StartPledge(ctx, nSectors, 0, nil) toCheck := miner.StartPledge(ctx, nSectors, 0, nil)
@ -145,13 +143,3 @@ func TestPledgeBeforeNv13(t *testing.T) {
runTest(t, 100) runTest(t, 100)
}) })
} }
func waitTillChainHeight(ctx context.Context, t *testing.T, node *kit2.TestFullNode, height int) {
for {
h, err := node.ChainHead(ctx)
require.NoError(t, err)
if h.Height() > abi.ChainEpoch(height) {
return
}
}
}

View File

@ -2,18 +2,15 @@ package itests
import ( import (
"context" "context"
"fmt"
"os" "os"
"testing" "testing"
"time" "time"
"github.com/filecoin-project/go-bitfield" "github.com/filecoin-project/go-bitfield"
"github.com/filecoin-project/go-state-types/abi" "github.com/filecoin-project/go-state-types/abi"
"github.com/filecoin-project/lotus/build"
"github.com/filecoin-project/lotus/chain/types" "github.com/filecoin-project/lotus/chain/types"
sealing "github.com/filecoin-project/lotus/extern/storage-sealing" sealing "github.com/filecoin-project/lotus/extern/storage-sealing"
"github.com/filecoin-project/lotus/itests/kit" "github.com/filecoin-project/lotus/itests/kit2"
"github.com/filecoin-project/lotus/node/impl"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -22,7 +19,7 @@ func TestTerminate(t *testing.T) {
t.Skip("this takes a few minutes, set LOTUS_TEST_WINDOW_POST=1 to run") t.Skip("this takes a few minutes, set LOTUS_TEST_WINDOW_POST=1 to run")
} }
kit.QuietMiningLogs() kit2.QuietMiningLogs()
const blocktime = 2 * time.Millisecond const blocktime = 2 * time.Millisecond
@ -31,42 +28,9 @@ func TestTerminate(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background()) ctx, cancel := context.WithCancel(context.Background())
defer cancel() defer cancel()
n, sn := kit.MockMinerBuilder(t, opts := kit2.ConstructorOpts(kit2.LatestActorsAt(-1))
[]kit.FullNodeOpts{kit.FullNodeWithLatestActorsAt(-1)}, client, miner, ens := kit2.EnsembleMinimal(t, kit2.MockProofs(), opts)
[]kit.StorageMiner{{Full: 0, Preseal: int(nSectors)}}, ens.InterconnectAll().BeginMining(blocktime)
)
client := n[0].FullNode.(*impl.FullNodeAPI)
miner := sn[0]
addrinfo, err := client.NetAddrsListen(ctx)
if err != nil {
t.Fatal(err)
}
if err := miner.NetConnect(ctx, addrinfo); err != nil {
t.Fatal(err)
}
build.Clock.Sleep(time.Second)
done := make(chan struct{})
go func() {
defer close(done)
for ctx.Err() == nil {
build.Clock.Sleep(blocktime)
if err := sn[0].MineOne(ctx, kit.MineNext); err != nil {
if ctx.Err() != nil {
// context was canceled, ignore the error.
return
}
t.Error(err)
}
}
}()
defer func() {
cancel()
<-done
}()
maddr, err := miner.ActorAddress(ctx) maddr, err := miner.ActorAddress(ctx)
require.NoError(t, err) require.NoError(t, err)
@ -79,11 +43,11 @@ func TestTerminate(t *testing.T) {
require.Equal(t, p.MinerPower, p.TotalPower) require.Equal(t, p.MinerPower, p.TotalPower)
require.Equal(t, p.MinerPower.RawBytePower, types.NewInt(uint64(ssz)*nSectors)) require.Equal(t, p.MinerPower.RawBytePower, types.NewInt(uint64(ssz)*nSectors))
fmt.Printf("Seal a sector\n") t.Log("Seal a sector")
kit.PledgeSectors(t, ctx, miner, 1, 0, nil) miner.PledgeSectors(ctx, 1, 0, nil)
fmt.Printf("wait for power\n") t.Log("wait for power")
{ {
// Wait until proven. // Wait until proven.
@ -91,17 +55,10 @@ func TestTerminate(t *testing.T) {
require.NoError(t, err) require.NoError(t, err)
waitUntil := di.PeriodStart + di.WPoStProvingPeriod + 2 waitUntil := di.PeriodStart + di.WPoStProvingPeriod + 2
fmt.Printf("End for head.Height > %d\n", waitUntil) t.Logf("End for head.Height > %d", waitUntil)
for { ts := client.WaitTillChain(ctx, kit2.HeightAtLeast(waitUntil))
head, err := client.ChainHead(ctx) t.Logf("Now head.Height = %d", ts.Height())
require.NoError(t, err)
if head.Height() > waitUntil {
fmt.Printf("Now head.Height = %d\n", head.Height())
break
}
}
} }
nSectors++ nSectors++
@ -111,7 +68,7 @@ func TestTerminate(t *testing.T) {
require.Equal(t, p.MinerPower, p.TotalPower) require.Equal(t, p.MinerPower, p.TotalPower)
require.Equal(t, p.MinerPower.RawBytePower, types.NewInt(uint64(ssz)*nSectors)) require.Equal(t, p.MinerPower.RawBytePower, types.NewInt(uint64(ssz)*nSectors))
fmt.Println("Terminate a sector") t.Log("Terminate a sector")
toTerminate := abi.SectorNumber(3) toTerminate := abi.SectorNumber(3)
@ -124,7 +81,7 @@ loop:
si, err := miner.SectorsStatus(ctx, toTerminate, false) si, err := miner.SectorsStatus(ctx, toTerminate, false)
require.NoError(t, err) require.NoError(t, err)
fmt.Println("state: ", si.State, msgTriggerred) t.Log("state: ", si.State, msgTriggerred)
switch sealing.SectorState(si.State) { switch sealing.SectorState(si.State) {
case sealing.Terminating: case sealing.Terminating:
@ -140,7 +97,7 @@ loop:
require.NoError(t, err) require.NoError(t, err)
if c != nil { if c != nil {
msgTriggerred = true msgTriggerred = true
fmt.Println("terminate message:", c) t.Log("terminate message:", c)
{ {
p, err := miner.SectorTerminatePending(ctx) p, err := miner.SectorTerminatePending(ctx)
@ -180,18 +137,11 @@ loop:
di, err := client.StateMinerProvingDeadline(ctx, maddr, types.EmptyTSK) di, err := client.StateMinerProvingDeadline(ctx, maddr, types.EmptyTSK)
require.NoError(t, err) require.NoError(t, err)
for {
head, err := client.ChainHead(ctx)
require.NoError(t, err)
if head.Height() > di.PeriodStart+di.WPoStProvingPeriod+2 { waitUntil := di.PeriodStart + di.WPoStProvingPeriod + 2
fmt.Printf("Now head.Height = %d\n", head.Height()) t.Logf("End for head.Height > %d", waitUntil)
break ts := client.WaitTillChain(ctx, kit2.HeightAtLeast(waitUntil))
} t.Logf("Now head.Height = %d", ts.Height())
build.Clock.Sleep(blocktime)
}
require.NoError(t, err)
fmt.Printf("End for head.Height > %d\n", di.PeriodStart+di.WPoStProvingPeriod+2)
p, err = client.StateMinerPower(ctx, maddr, types.EmptyTSK) p, err = client.StateMinerPower(ctx, maddr, types.EmptyTSK)
require.NoError(t, err) require.NoError(t, err)