Commit b3eeae90 authored by Joshua Gutow's avatar Joshua Gutow

op-e2e: Add WaitForBlockToBeFinalized & WaitForBlocktoBeSafe

These helper functions simplify TestFinalize and future tests which may
be waiting on actions rather than polling for actions.
parent 5d6a8684
......@@ -12,6 +12,7 @@ import (
"github.com/ethereum/go-ethereum/common"
"github.com/ethereum/go-ethereum/core/types"
"github.com/ethereum/go-ethereum/ethclient"
"github.com/ethereum/go-ethereum/rpc"
)
var (
......@@ -104,3 +105,34 @@ func WaitForBlock(number *big.Int, client *ethclient.Client, timeout time.Durati
}
}
}
func WaitForBlockToBeFinalized(number *big.Int, client *ethclient.Client, timeout time.Duration) (*types.Block, error) {
return WaitForBlockTag(number, client, timeout, int64(rpc.FinalizedBlockNumber))
}
func WaitForBlockToBeSafe(number *big.Int, client *ethclient.Client, timeout time.Duration) (*types.Block, error) {
return WaitForBlockTag(number, client, timeout, int64(rpc.SafeBlockNumber))
}
// WaitForBlockTag polls for a block number to reach the specified tag & then returns that block at the number.
func WaitForBlockTag(number *big.Int, client *ethclient.Client, timeout time.Duration, tag int64) (*types.Block, error) {
timeoutCh := time.After(timeout)
ctx, cancel := context.WithTimeout(context.Background(), timeout)
defer cancel()
// Wait for it to be finalized. Poll every half second.
ticker := time.NewTicker(500 * time.Millisecond)
defer ticker.Stop()
for {
select {
case <-ticker.C:
block, _ := client.BlockByNumber(ctx, big.NewInt(tag))
if block.NumberU64() >= number.Uint64() {
return client.BlockByNumber(ctx, number)
}
case <-timeoutCh:
return nil, errTimeout
}
}
}
......@@ -368,17 +368,8 @@ func TestFinalize(t *testing.T) {
l2Seq := sys.Clients["sequencer"]
// as configured in the extra geth lifecycle in testing setup
const finalizedDistance = 8
// Wait enough time for L1 to finalize and L2 to confirm its data in finalized L1 blocks
time.Sleep(time.Duration((finalizedDistance+6)*cfg.DeployConfig.L1BlockTime) * time.Second)
// fetch the finalizes head of geth
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel()
l2Finalized, err := l2Seq.BlockByNumber(ctx, big.NewInt(int64(rpc.FinalizedBlockNumber)))
require.NoError(t, err)
l2Finalized, err := geth.WaitForBlockToBeFinalized(big.NewInt(12), l2Seq, 1*time.Minute)
require.NoError(t, err, "must be able to fetch a finalized L2 block")
require.NotZerof(t, l2Finalized.NumberU64(), "must have finalized L2 block")
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment