Skip to content

Commit

Permalink
add vaultBalance diff check
Browse files Browse the repository at this point in the history
  • Loading branch information
dajuguan committed Dec 13, 2024
1 parent 8aafd47 commit 0316b17
Showing 1 changed file with 51 additions and 21 deletions.
72 changes: 51 additions & 21 deletions op-e2e/sgt/sgt_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import (
var (
seqVault = predeploys.SequencerFeeVaultAddr
baseVault = predeploys.BaseFeeVaultAddr
l1Vault = predeploys.L1FeeVault
l1Vault = predeploys.L1FeeVaultAddr
dummyAddr = common.Address{0xff, 0xff}
)

Expand All @@ -35,7 +35,7 @@ func TestSGTDepositFunctionSuccess(t *testing.T) {

sgt := NewSgtHelper(t, ctx, sys)
depositSgtValue := big.NewInt(10000)
_, _ = setUpTestAccount(t, ctx, 0, sgt, depositSgtValue, big.NewInt(0))
_, _, _ = setUpTestAccount(t, ctx, 0, sgt, depositSgtValue, big.NewInt(0))
}

// Diverse test scenarios to verify that the SoulGasToken(sgt) is utilized for gas payment firstly,
Expand Down Expand Up @@ -106,7 +106,7 @@ func TestSGTAsGasPayment(t *testing.T) {
}
}

func setUpTestAccount(t *testing.T, ctx context.Context, index int64, sgt *SgtHelper, depositSgtValue *big.Int, depositL2Value *big.Int) (*ecdsa.PrivateKey, common.Address) {
func setUpTestAccount(t *testing.T, ctx context.Context, index int64, sgt *SgtHelper, depositSgtValue *big.Int, depositL2Value *big.Int) (*ecdsa.PrivateKey, common.Address, *big.Int) {
opts := &bind.CallOpts{Context: ctx}
rng := rand.New(rand.NewSource(index))
testPrivKey := testutils.InsecureRandomKey(rng)
Expand All @@ -129,7 +129,8 @@ func setUpTestAccount(t *testing.T, ctx context.Context, index int64, sgt *SgtHe
preL2Balance, err := sgt.L2Client.BalanceAt(ctx, testAddr, nil)
require.NoError(t, err)
require.Equal(t, depositL2Value.Cmp(preL2Balance), 0)
return testPrivKey, testAddr

return testPrivKey, testAddr, calcVaultBalance(t, ctx, sgt)
}

// balance invariant check: preTotalBalance = postTotalBalance + gasCost + txValue
Expand All @@ -147,15 +148,18 @@ func nativaGasPaymentWithoutSGTSuccess(t *testing.T, ctx context.Context, index
depositSgtValue := big.NewInt(0)
depositL2Value := big.NewInt(10000000000000)
txValue := big.NewInt(0)
testAccount, testAddr := setUpTestAccount(t, ctx, index, sgt, depositSgtValue, depositL2Value)
testAccount, testAddr, vaultBalanceBefore := setUpTestAccount(t, ctx, index, sgt, depositSgtValue, depositL2Value)

// make a simple tx with the testAccount: transfer txValue from testAccount to dummyAddr
tx, err := sgt.transferNativeToken(t, ctx, testAccount, dummyAddr, txValue)
require.NoError(t, err)
receipt, err := wait.ForReceiptOK(ctx, sgt.L2Client, tx.Hash())
require.NoError(t, err)
gasCost := calcGasFee(receipt)
vaultBalanceAfter := calcVaultBalance(t, ctx, sgt)

// gasCost == vaultBalanceDiff check
require.Equal(t, vaultBalanceAfter.Sub(vaultBalanceAfter, vaultBalanceBefore).Cmp(gasCost), 0)
// post sgt balance check: it should be 0
opts := &bind.CallOpts{Context: ctx}
postSgtBalance, err := sgt.SgtContract.BalanceOf(opts, testAddr)
Expand All @@ -169,15 +173,18 @@ func fullSGTGasPaymentWithoutNativeBalanceSuccess(t *testing.T, ctx context.Cont
depositSgtValue := big.NewInt(10000000000000)
depositL2Value := big.NewInt(0)
txValue := big.NewInt(0)
testAccount, testAddr := setUpTestAccount(t, ctx, index, sgt, depositSgtValue, depositL2Value)
testAccount, testAddr, vaultBalanceBefore := setUpTestAccount(t, ctx, index, sgt, depositSgtValue, depositL2Value)

// make a simple tx with the testAccount: transfer txValue from testAccount to dummyAddr
tx, err := sgt.transferNativeToken(t, ctx, testAccount, dummyAddr, txValue)
require.NoError(t, err)
receipt, err := wait.ForReceiptOK(ctx, sgt.L2Client, tx.Hash())
require.NoError(t, err)
gasCost := calcGasFee(receipt)
vaultBalanceAfter := calcVaultBalance(t, ctx, sgt)

// gasCost == vaultBalanceDiff check
require.Equal(t, vaultBalanceAfter.Sub(vaultBalanceAfter, vaultBalanceBefore).Cmp(gasCost), 0)
// post sgt balance check: sgt should be used as gas first
opts := &bind.CallOpts{Context: ctx}
postSgtBalance, err := sgt.SgtContract.BalanceOf(opts, testAddr)
Expand All @@ -191,15 +198,18 @@ func fullSGTGasPaymentWithNativeBalanceSuccess(t *testing.T, ctx context.Context
depositSgtValue := big.NewInt(10000000000000)
depositL2Value := big.NewInt(10000000000000)
txValue := big.NewInt(0)
testAccount, testAddr := setUpTestAccount(t, ctx, index, sgt, depositSgtValue, depositL2Value)
testAccount, testAddr, vaultBalanceBefore := setUpTestAccount(t, ctx, index, sgt, depositSgtValue, depositL2Value)

// make a simple tx with the testAccount: transfer txValue from testAccount to dummyAddr
tx, err := sgt.transferNativeToken(t, ctx, testAccount, dummyAddr, txValue)
require.NoError(t, err)
receipt, err := wait.ForReceiptOK(ctx, sgt.L2Client, tx.Hash())
require.NoError(t, err)
gasCost := calcGasFee(receipt)
vaultBalanceAfter := calcVaultBalance(t, ctx, sgt)

// gasCost == vaultBalanceDiff check
require.Equal(t, vaultBalanceAfter.Sub(vaultBalanceAfter, vaultBalanceBefore).Cmp(gasCost), 0)
// post sgt balance check: sgt should be used as gas first
opts := &bind.CallOpts{Context: ctx}
postSgtBalance, err := sgt.SgtContract.BalanceOf(opts, testAddr)
Expand All @@ -213,15 +223,18 @@ func partialSGTGasPaymentSuccess(t *testing.T, ctx context.Context, index int64,
depositSgtValue := big.NewInt(1000)
depositL2Value := big.NewInt(10000000000000)
txValue := big.NewInt(0)
testAccount, testAddr := setUpTestAccount(t, ctx, index, sgt, depositSgtValue, depositL2Value)
testAccount, testAddr, vaultBalanceBefore := setUpTestAccount(t, ctx, index, sgt, depositSgtValue, depositL2Value)

// make a simple tx with the testAccount: transfer txValue from testAccount to dummyAddr
tx, err := sgt.transferNativeToken(t, ctx, testAccount, dummyAddr, txValue)
require.NoError(t, err)
receipt, err := wait.ForReceiptOK(ctx, sgt.L2Client, tx.Hash())
require.NoError(t, err)
gasCost := calcGasFee(receipt)
vaultBalanceAfter := calcVaultBalance(t, ctx, sgt)

// gasCost == vaultBalanceDiff check
require.Equal(t, vaultBalanceAfter.Sub(vaultBalanceAfter, vaultBalanceBefore).Cmp(gasCost), 0)
// post sgt balance check: sgt should be used as gas first and should be spent all
opts := &bind.CallOpts{Context: ctx}
postSgtBalance, err := sgt.SgtContract.BalanceOf(opts, testAddr)
Expand All @@ -235,15 +248,18 @@ func fullSGTGasPaymentAndNonZeroTxValueWithSufficientNativeBalanceSuccess(t *tes
depositSgtValue := big.NewInt(10000000000000)
depositL2Value := big.NewInt(10000000000000)
txValue := big.NewInt(10000)
testAccount, testAddr := setUpTestAccount(t, ctx, index, sgt, depositSgtValue, depositL2Value)
testAccount, testAddr, vaultBalanceBefore := setUpTestAccount(t, ctx, index, sgt, depositSgtValue, depositL2Value)

// make a simple tx with the testAccount: transfer txValue from testAccount to dummyAddr
tx, err := sgt.transferNativeToken(t, ctx, testAccount, dummyAddr, txValue)
require.NoError(t, err)
receipt, err := wait.ForReceiptOK(ctx, sgt.L2Client, tx.Hash())
require.NoError(t, err)
gasCost := calcGasFee(receipt)
vaultBalanceAfter := calcVaultBalance(t, ctx, sgt)

// gasCost == vaultBalanceDiff check
require.Equal(t, vaultBalanceAfter.Sub(vaultBalanceAfter, vaultBalanceBefore).Cmp(gasCost), 0)
// post sgt balance check: sgt should be used as gas first
opts := &bind.CallOpts{Context: ctx}
postSgtBalance, err := sgt.SgtContract.BalanceOf(opts, testAddr)
Expand All @@ -257,15 +273,18 @@ func partialSGTGasPaymentAndNonZeroTxValueWithSufficientNativeBalanceSuccess(t *
depositSgtValue := big.NewInt(1000)
depositL2Value := big.NewInt(10000000000000)
txValue := big.NewInt(10000)
testAccount, testAddr := setUpTestAccount(t, ctx, index, sgt, depositSgtValue, depositL2Value)
testAccount, testAddr, vaultBalanceBefore := setUpTestAccount(t, ctx, index, sgt, depositSgtValue, depositL2Value)

// make a simple tx with the testAccount: transfer txValue from testAccount to dummyAddr
tx, err := sgt.transferNativeToken(t, ctx, testAccount, dummyAddr, txValue)
require.NoError(t, err)
receipt, err := wait.ForReceiptOK(ctx, sgt.L2Client, tx.Hash())
require.NoError(t, err)
gasCost := calcGasFee(receipt)
vaultBalanceAfter := calcVaultBalance(t, ctx, sgt)

// gasCost == vaultBalanceDiff check
require.Equal(t, vaultBalanceAfter.Sub(vaultBalanceAfter, vaultBalanceBefore).Cmp(gasCost), 0)
// post sgt balance check: sgt should be used as gas first and should be spent all
opts := &bind.CallOpts{Context: ctx}
postSgtBalance, err := sgt.SgtContract.BalanceOf(opts, testAddr)
Expand All @@ -279,7 +298,7 @@ func fullSGTInsufficientGasPaymentFail(t *testing.T, ctx context.Context, index
depositSgtValue := big.NewInt(10000)
depositL2Value := big.NewInt(0)
txValue := big.NewInt(0)
testAccount, _ := setUpTestAccount(t, ctx, index, sgt, depositSgtValue, depositL2Value)
testAccount, _, _ := setUpTestAccount(t, ctx, index, sgt, depositSgtValue, depositL2Value)

// make a simple tx with the testAccount: transfer txValue from testAccount to dummyAddr
_, err := sgt.transferNativeToken(t, ctx, testAccount, dummyAddr, txValue)
Expand All @@ -290,7 +309,7 @@ func fullNativeInsufficientGasPaymentFail(t *testing.T, ctx context.Context, ind
depositSgtValue := big.NewInt(0)
depositL2Value := big.NewInt(10000)
txValue := big.NewInt(0)
testAccount, _ := setUpTestAccount(t, ctx, index, sgt, depositSgtValue, depositL2Value)
testAccount, _, _ := setUpTestAccount(t, ctx, index, sgt, depositSgtValue, depositL2Value)

// make a simple tx with the testAccount: transfer txValue from testAccount to dummyAddr
_, err := sgt.transferNativeToken(t, ctx, testAccount, dummyAddr, txValue)
Expand All @@ -301,7 +320,7 @@ func partialSGTInsufficientGasPaymentFail(t *testing.T, ctx context.Context, ind
depositSgtValue := big.NewInt(10000)
depositL2Value := big.NewInt(10000)
txValue := big.NewInt(0)
testAccount, _ := setUpTestAccount(t, ctx, index, sgt, depositSgtValue, depositL2Value)
testAccount, _, _ := setUpTestAccount(t, ctx, index, sgt, depositSgtValue, depositL2Value)

// make a simple tx with the testAccount: transfer txValue from testAccount to dummyAddr
_, err := sgt.transferNativeToken(t, ctx, testAccount, dummyAddr, txValue)
Expand All @@ -312,25 +331,21 @@ func fullSGTGasPaymentAndNonZeroTxValueWithInsufficientNativeBalanceFail(t *test
depositSgtValue := big.NewInt(10000000000000)
depositL2Value := big.NewInt(10000)
txValue := big.NewInt(10001)
testAccount, _ := setUpTestAccount(t, ctx, index, sgt, depositSgtValue, depositL2Value)
testAccount, _, _ := setUpTestAccount(t, ctx, index, sgt, depositSgtValue, depositL2Value)

// make a simple tx with the testAccount: transfer txValue from testAccount to dummyAddr
tx, err := sgt.transferNativeToken(t, ctx, testAccount, dummyAddr, txValue)
// ensure sgt balance is large enough to cover the gas cost
require.Equal(t, depositSgtValue.Cmp(tx.GasCost()), 1)
_, err := sgt.transferNativeToken(t, ctx, testAccount, dummyAddr, txValue)
require.Error(t, err)
}

func partialSGTGasPaymentAndNonZeroTxValueWithInsufficientNativeBalanceFail(t *testing.T, ctx context.Context, index int64, sgt *SgtHelper) {
depositSgtValue := big.NewInt(10000)
depositL2Value := big.NewInt(10000000000000)
txValue := big.NewInt(10000000000000 - 10000)
testAccount, _ := setUpTestAccount(t, ctx, index, sgt, depositSgtValue, depositL2Value)
testAccount, _, _ := setUpTestAccount(t, ctx, index, sgt, depositSgtValue, depositL2Value)

// make a simple tx with the testAccount: transfer txValue from testAccount to dummyAddr
tx, err := sgt.transferNativeToken(t, ctx, testAccount, dummyAddr, txValue)
// ensure native balance is large enough to cover the gas cost
require.Equal(t, depositL2Value.Cmp(tx.GasCost()), 1)
_, err := sgt.transferNativeToken(t, ctx, testAccount, dummyAddr, txValue)
require.Error(t, err)
}

Expand All @@ -340,3 +355,18 @@ func calcGasFee(receipt *types.Receipt) *big.Int {
fees = fees.Add(fees, receipt.L1Fee)
return fees
}

func calcVaultBalance(t *testing.T, ctx context.Context, sgt *SgtHelper) *big.Int {
sequencerFee, err := sgt.L2Client.BalanceAt(ctx, seqVault, nil)
require.NoError(t, err)
baseFee, err := sgt.L2Client.BalanceAt(ctx, baseVault, nil)
require.NoError(t, err)
l1Fee, err := sgt.L2Client.BalanceAt(ctx, l1Vault, nil)
require.NoError(t, err)
return sequencerFee.Add(sequencerFee, baseFee.Add(baseFee, l1Fee))
// preVaultBalance := getVaultBalance()
// err := wait.ForNextBlock(ctx, sgt.L2Client)
// require.NoError(t, err)
// postVaultBalance := getVaultBalance()
// return postVaultBalance.Sub(postVaultBalance, preVaultBalance)
}

0 comments on commit 0316b17

Please sign in to comment.