Commit d1651bb2 authored by Sebastian Stammler's avatar Sebastian Stammler Committed by GitHub

Merge pull request #8492 from ethereum-optimism/bugfix/wait-for-balance-changes

op-e2e: Add test utility to wait for balance changes
parents b5cd4b01 ac2a7399
...@@ -4,6 +4,7 @@ import ( ...@@ -4,6 +4,7 @@ import (
"context" "context"
"errors" "errors"
"fmt" "fmt"
"math/big"
"time" "time"
"github.com/ethereum/go-ethereum" "github.com/ethereum/go-ethereum"
...@@ -13,6 +14,22 @@ import ( ...@@ -13,6 +14,22 @@ import (
"github.com/ethereum/go-ethereum/ethclient" "github.com/ethereum/go-ethereum/ethclient"
) )
func ForBalanceChange(ctx context.Context, client *ethclient.Client, address common.Address, initial *big.Int) (*big.Int, error) {
ctx, cancel := context.WithTimeout(ctx, 2*time.Minute)
defer cancel()
return AndGet[*big.Int](
ctx,
100*time.Millisecond,
func() (*big.Int, error) {
return client.BalanceAt(ctx, address, nil)
},
func(b *big.Int) bool {
return b.Cmp(initial) != 0
},
)
}
func ForReceiptOK(ctx context.Context, client *ethclient.Client, hash common.Hash) (*types.Receipt, error) { func ForReceiptOK(ctx context.Context, client *ethclient.Client, hash common.Hash) (*types.Receipt, error) {
return ForReceipt(ctx, client, hash, types.ReceiptStatusSuccessful) return ForReceipt(ctx, client, hash, types.ReceiptStatusSuccessful)
} }
......
...@@ -250,7 +250,8 @@ func runE2ESystemTest(t *testing.T, sys *System) { ...@@ -250,7 +250,8 @@ func runE2ESystemTest(t *testing.T, sys *System) {
// Confirm balance // Confirm balance
ctx, cancel = context.WithTimeout(context.Background(), 15*time.Second) ctx, cancel = context.WithTimeout(context.Background(), 15*time.Second)
defer cancel() defer cancel()
endBalance, err := l2Verif.BalanceAt(ctx, fromAddr, nil)
endBalance, err := wait.ForBalanceChange(ctx, l2Verif, fromAddr, startBalance)
require.Nil(t, err) require.Nil(t, err)
diff := new(big.Int) diff := new(big.Int)
...@@ -1047,7 +1048,7 @@ func TestWithdrawals(t *testing.T) { ...@@ -1047,7 +1048,7 @@ func TestWithdrawals(t *testing.T) {
// Start L2 balance // Start L2 balance
ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second) ctx, cancel := context.WithTimeout(context.Background(), 1*time.Second)
defer cancel() defer cancel()
startBalance, err := l2Verif.BalanceAt(ctx, fromAddr, nil) startBalanceBeforeDeposit, err := l2Verif.BalanceAt(ctx, fromAddr, nil)
require.Nil(t, err) require.Nil(t, err)
// Send deposit tx // Send deposit tx
...@@ -1060,17 +1061,17 @@ func TestWithdrawals(t *testing.T) { ...@@ -1060,17 +1061,17 @@ func TestWithdrawals(t *testing.T) {
// Confirm L2 balance // Confirm L2 balance
ctx, cancel = context.WithTimeout(context.Background(), 1*time.Second) ctx, cancel = context.WithTimeout(context.Background(), 1*time.Second)
defer cancel() defer cancel()
endBalance, err := l2Verif.BalanceAt(ctx, fromAddr, nil) endBalanceAfterDeposit, err := wait.ForBalanceChange(ctx, l2Verif, fromAddr, startBalanceBeforeDeposit)
require.Nil(t, err) require.Nil(t, err)
diff := new(big.Int) diff := new(big.Int)
diff = diff.Sub(endBalance, startBalance) diff = diff.Sub(endBalanceAfterDeposit, startBalanceBeforeDeposit)
require.Equal(t, mintAmount, diff, "Did not get expected balance change after mint") require.Equal(t, mintAmount, diff, "Did not get expected balance change after mint")
// Start L2 balance for withdrawal // Start L2 balance for withdrawal
ctx, cancel = context.WithTimeout(context.Background(), 1*time.Second) ctx, cancel = context.WithTimeout(context.Background(), 1*time.Second)
defer cancel() defer cancel()
startBalance, err = l2Seq.BalanceAt(ctx, fromAddr, nil) startBalanceBeforeWithdrawal, err := l2Seq.BalanceAt(ctx, fromAddr, nil)
require.Nil(t, err) require.Nil(t, err)
withdrawAmount := big.NewInt(500_000_000_000) withdrawAmount := big.NewInt(500_000_000_000)
...@@ -1087,11 +1088,11 @@ func TestWithdrawals(t *testing.T) { ...@@ -1087,11 +1088,11 @@ func TestWithdrawals(t *testing.T) {
ctx, cancel = context.WithTimeout(context.Background(), 1*time.Second) ctx, cancel = context.WithTimeout(context.Background(), 1*time.Second)
defer cancel() defer cancel()
endBalance, err = l2Verif.BalanceAt(ctx, fromAddr, nil) endBalanceAfterWithdrawal, err := wait.ForBalanceChange(ctx, l2Seq, fromAddr, startBalanceBeforeWithdrawal)
require.Nil(t, err) require.Nil(t, err)
// Take fee into account // Take fee into account
diff = new(big.Int).Sub(startBalance, endBalance) diff = new(big.Int).Sub(startBalanceBeforeWithdrawal, endBalanceAfterWithdrawal)
fees := calcGasFees(receipt.GasUsed, tx.GasTipCap(), tx.GasFeeCap(), header.BaseFee) fees := calcGasFees(receipt.GasUsed, tx.GasTipCap(), tx.GasFeeCap(), header.BaseFee)
fees = fees.Add(fees, receipt.L1Fee) fees = fees.Add(fees, receipt.L1Fee)
diff = diff.Sub(diff, fees) diff = diff.Sub(diff, fees)
...@@ -1100,7 +1101,7 @@ func TestWithdrawals(t *testing.T) { ...@@ -1100,7 +1101,7 @@ func TestWithdrawals(t *testing.T) {
// Take start balance on L1 // Take start balance on L1
ctx, cancel = context.WithTimeout(context.Background(), 1*time.Second) ctx, cancel = context.WithTimeout(context.Background(), 1*time.Second)
defer cancel() defer cancel()
startBalance, err = l1Client.BalanceAt(ctx, fromAddr, nil) startBalanceBeforeFinalize, err := l1Client.BalanceAt(ctx, fromAddr, nil)
require.Nil(t, err) require.Nil(t, err)
proveReceipt, finalizeReceipt := ProveAndFinalizeWithdrawal(t, cfg, l1Client, sys.EthInstances["verifier"], ethPrivKey, receipt) proveReceipt, finalizeReceipt := ProveAndFinalizeWithdrawal(t, cfg, l1Client, sys.EthInstances["verifier"], ethPrivKey, receipt)
...@@ -1108,13 +1109,13 @@ func TestWithdrawals(t *testing.T) { ...@@ -1108,13 +1109,13 @@ func TestWithdrawals(t *testing.T) {
// Verify balance after withdrawal // Verify balance after withdrawal
ctx, cancel = context.WithTimeout(context.Background(), 1*time.Second) ctx, cancel = context.WithTimeout(context.Background(), 1*time.Second)
defer cancel() defer cancel()
endBalance, err = l1Client.BalanceAt(ctx, fromAddr, nil) endBalanceAfterFinalize, err := wait.ForBalanceChange(ctx, l1Client, fromAddr, startBalanceBeforeFinalize)
require.Nil(t, err) require.Nil(t, err)
// Ensure that withdrawal - gas fees are added to the L1 balance // Ensure that withdrawal - gas fees are added to the L1 balance
// Fun fact, the fee is greater than the withdrawal amount // Fun fact, the fee is greater than the withdrawal amount
// NOTE: The gas fees include *both* the ProveWithdrawalTransaction and FinalizeWithdrawalTransaction transactions. // NOTE: The gas fees include *both* the ProveWithdrawalTransaction and FinalizeWithdrawalTransaction transactions.
diff = new(big.Int).Sub(endBalance, startBalance) diff = new(big.Int).Sub(endBalanceAfterFinalize, startBalanceBeforeFinalize)
proveFee := new(big.Int).Mul(new(big.Int).SetUint64(proveReceipt.GasUsed), proveReceipt.EffectiveGasPrice) proveFee := new(big.Int).Mul(new(big.Int).SetUint64(proveReceipt.GasUsed), proveReceipt.EffectiveGasPrice)
finalizeFee := new(big.Int).Mul(new(big.Int).SetUint64(finalizeReceipt.GasUsed), finalizeReceipt.EffectiveGasPrice) finalizeFee := new(big.Int).Mul(new(big.Int).SetUint64(finalizeReceipt.GasUsed), finalizeReceipt.EffectiveGasPrice)
fees = new(big.Int).Add(proveFee, finalizeFee) fees = new(big.Int).Add(proveFee, finalizeFee)
......
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