From 9436be5ff03744609d0a28c5fdcecb497ba33ecc Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ra=C3=BAl=20Kripalani?= Date: Thu, 17 Jun 2021 21:58:29 +0100 Subject: [PATCH] introduce TestFullNode#WaitTillChain(predicate). --- itests/deals_test.go | 17 ++--------- itests/kit2/chain.go | 21 -------------- itests/kit2/node_full.go | 50 +++++++++++++++++++++++++++++++++ itests/sector_pledge_test.go | 4 +-- itests/sector_terminate_test.go | 8 +++--- 5 files changed, 58 insertions(+), 42 deletions(-) delete mode 100644 itests/kit2/chain.go diff --git a/itests/deals_test.go b/itests/deals_test.go index 3a6d9c868..af0ef68c4 100644 --- a/itests/deals_test.go +++ b/itests/deals_test.go @@ -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 { - close(providerMined) - return - } - } - } - } + _ = client.WaitTillChain(ctx, kit2.BlockMinedBy(provider.ActorAddr)) + close(providerMined) }() // now perform the deal. diff --git a/itests/kit2/chain.go b/itests/kit2/chain.go deleted file mode 100644 index 9563bae9f..000000000 --- a/itests/kit2/chain.go +++ /dev/null @@ -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) - } -} diff --git a/itests/kit2/node_full.go b/itests/kit2/node_full.go index b0b39b471..3dadb4d8d 100644 --- a/itests/kit2/node_full.go +++ b/itests/kit2/node_full.go @@ -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 + } +} diff --git a/itests/sector_pledge_test.go b/itests/sector_pledge_test.go index 73fd2204e..8e87f2658 100644 --- a/itests/sector_pledge_test.go +++ b/itests/sector_pledge_test.go @@ -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) diff --git a/itests/sector_terminate_test.go b/itests/sector_terminate_test.go index d1852ec39..faf12228c 100644 --- a/itests/sector_terminate_test.go +++ b/itests/sector_terminate_test.go @@ -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)