introduce TestFullNode#WaitTillChain(predicate).

This commit is contained in:
Raúl Kripalani 2021-06-17 21:58:29 +01:00
parent 6567887e6e
commit 9436be5ff0
5 changed files with 58 additions and 42 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,
// we pass the test.
providerMined := make(chan struct{})
heads, err := client.ChainNotify(ctx)
require.NoError(t, err)
go func() {
for chg := range heads {
for _, c := range chg {
if c.Type != "apply" {
continue
}
for _, b := range c.Val.Blocks() {
if b.Miner == provider.ActorAddr {
_ = client.WaitTillChain(ctx, kit2.BlockMinedBy(provider.ActorAddr))
close(providerMined)
return
}
}
}
}
}()
// now perform the deal.

View File

@ -1,21 +0,0 @@
package kit2
import (
"context"
"testing"
"time"
"github.com/filecoin-project/go-state-types/abi"
"github.com/stretchr/testify/require"
)
func WaitTillChainHeight(ctx context.Context, t *testing.T, node *TestFullNode, blocktime time.Duration, height int) abi.ChainEpoch {
for {
h, err := node.ChainHead(ctx)
require.NoError(t, err)
if h.Height() > abi.ChainEpoch(height) {
return h.Height()
}
time.Sleep(blocktime)
}
}

View File

@ -4,8 +4,11 @@ import (
"context"
"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/v1api"
"github.com/filecoin-project/lotus/chain/types"
"github.com/filecoin-project/lotus/chain/wallet"
"github.com/multiformats/go-multiaddr"
"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)
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

@ -58,7 +58,7 @@ func TestPledgeBatching(t *testing.T) {
client, miner, ens := kit2.EnsembleMinimal(t, kit2.MockProofs(), opts)
ens.InterconnectAll().BeginMining(blockTime)
kit2.WaitTillChainHeight(ctx, t, client, blockTime, 10)
client.WaitTillChain(ctx, kit2.HeightAtLeast(10))
toCheck := miner.StartPledge(ctx, nSectors, 0, nil)
@ -115,7 +115,7 @@ func TestPledgeBeforeNv13(t *testing.T) {
client, miner, ens := kit2.EnsembleMinimal(t, kit2.MockProofs(), opts)
ens.InterconnectAll().BeginMining(blocktime)
kit2.WaitTillChainHeight(ctx, t, client, blocktime, 10)
client.WaitTillChain(ctx, kit2.HeightAtLeast(10))
toCheck := miner.StartPledge(ctx, nSectors, 0, nil)

View File

@ -57,8 +57,8 @@ func TestTerminate(t *testing.T) {
waitUntil := di.PeriodStart + di.WPoStProvingPeriod + 2
t.Logf("End for head.Height > %d", waitUntil)
height := kit2.WaitTillChainHeight(ctx, t, client, blocktime, int(waitUntil))
t.Logf("Now head.Height = %d", height)
ts := client.WaitTillChain(ctx, kit2.HeightAtLeast(waitUntil))
t.Logf("Now head.Height = %d", ts.Height())
}
nSectors++
@ -140,8 +140,8 @@ loop:
waitUntil := di.PeriodStart + di.WPoStProvingPeriod + 2
t.Logf("End for head.Height > %d", waitUntil)
height := kit2.WaitTillChainHeight(ctx, t, client, blocktime, int(waitUntil))
t.Logf("Now head.Height = %d", height)
ts := client.WaitTillChain(ctx, kit2.HeightAtLeast(waitUntil))
t.Logf("Now head.Height = %d", ts.Height())
p, err = client.StateMinerPower(ctx, maddr, types.EmptyTSK)
require.NoError(t, err)