Skip to content

Commit

Permalink
test: eth: deflake EthBlockHashesCorrect_MultiBlockTipset (#11808)
Browse files Browse the repository at this point in the history
Increase the chances of having multiple blocks at the same height by
aligning block times across test miners. Also, make the block hash test
faster.
  • Loading branch information
Stebalien authored Apr 4, 2024
1 parent 9f9dc97 commit 1645c32
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 12 deletions.
24 changes: 15 additions & 9 deletions itests/eth_block_hash_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,36 @@ import (
func TestEthBlockHashesCorrect_MultiBlockTipset(t *testing.T) {
// miner is connected to the first node, and we want to observe the chain
// from the second node.
blocktime := 250 * time.Millisecond
blocktime := 100 * time.Millisecond
n1, m1, m2, ens := kit.EnsembleOneTwo(t,
kit.MockProofs(),
kit.ThroughRPC(),
)
ens.InterconnectAll().BeginMining(blocktime)

ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
n1.WaitTillChain(ctx, kit.HeightAtLeast(abi.ChainEpoch(5)))
defer cancel()
{
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
n1.WaitTillChain(ctx, kit.HeightAtLeast(abi.ChainEpoch(5)))
cancel()
}

var n2 kit.TestFullNode
ens.FullNode(&n2, kit.ThroughRPC()).Start().Connect(n2, n1)

// find the first tipset where all miners mined a block.
ctx, cancel = context.WithTimeout(context.Background(), 5*time.Minute)
n2.WaitTillChain(ctx, kit.BlocksMinedByAll(m1.ActorAddr, m2.ActorAddr))
defer cancel()
{
// find the first tipset where all miners mined a block.
ctx, cancel := context.WithTimeout(context.Background(), 5*time.Minute)
n2.WaitTillChain(ctx, kit.BlocksMinedByAll(m1.ActorAddr, m2.ActorAddr))
cancel()
}

head, err := n2.ChainHead(context.Background())
require.NoError(t, err)

ctx := context.Background()

// let the chain run a little bit longer to minimise the chance of reorgs
n2.WaitTillChain(ctx, kit.HeightAtLeast(head.Height()+50))
n2.WaitTillChain(ctx, kit.HeightAtLeast(head.Height()+10))

tsk := head.Key()
for i := 1; i <= int(head.Height()); i++ {
Expand Down
7 changes: 4 additions & 3 deletions itests/kit/blockminer.go
Original file line number Diff line number Diff line change
Expand Up @@ -256,8 +256,6 @@ func (bm *BlockMiner) MineBlocksMustPost(ctx context.Context, blocktime time.Dur
}

func (bm *BlockMiner) MineBlocks(ctx context.Context, blocktime time.Duration) {
time.Sleep(time.Second)

// wrap context in a cancellable context.
ctx, bm.cancel = context.WithCancel(ctx)

Expand All @@ -278,8 +276,11 @@ func (bm *BlockMiner) MineBlocks(ctx context.Context, blocktime time.Duration) {
default:
}

now := time.Duration(time.Now().UnixNano())
delay := blocktime - (now % blocktime)

select {
case <-time.After(blocktime):
case <-time.After(delay):
case <-ctx.Done():
return
}
Expand Down

0 comments on commit 1645c32

Please sign in to comment.