-
Notifications
You must be signed in to change notification settings - Fork 887
Use DBImpl for giga mock balance validation #3750
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
d11034c
4d657fe
9301b67
05cd4da
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2060,13 +2060,17 @@ func (app *App) executeEVMTxWithGigaExecutor(ctx sdk.Context, msg *evmtypes.MsgE | |
| }, nil | ||
| } | ||
|
|
||
| // Prepare context for EVM transaction (set infinite gas meter like original flow) | ||
| ctx = ctx.WithGasMeter(sdk.NewInfiniteGasMeterWithMultiplier(ctx)) | ||
|
|
||
| _, isAssociated := app.GigaEvmKeeper.GetEVMAddress(ctx, seiAddr) | ||
|
|
||
| // Run validation checks (fee/nonce/balance - stateless checks done earlier) | ||
| validation := app.validateGigaEVMTx(ctx, ethTx, sender, seiAddr, isAssociated) | ||
| // Create state DB before validation so balance checks use DBImpl hooks. | ||
| stateDB := gigaevmstate.NewDBImpl(ctx, &app.GigaEvmKeeper, false) | ||
| defer stateDB.Cleanup() | ||
|
|
||
| // Prepare context for EVM transaction (set infinite gas meter like original flow) | ||
| ctx = ctx.WithGasMeter(sdk.NewInfiniteGasMeterWithMultiplier(ctx)) | ||
| // Run validation checks (fee/nonce/balance - stateless checks done earlier) | ||
| validation := app.validateGigaEVMTx(ctx, stateDB, ethTx, sender, seiAddr, isAssociated) | ||
|
|
||
| if validation.err != nil { | ||
| // v2 rejects fee/nonce/balance failures in its ante chain, whose | ||
|
|
@@ -2082,10 +2086,6 @@ func (app *App) executeEVMTxWithGigaExecutor(ctx sdk.Context, msg *evmtypes.MsgE | |
| return nil, gigaprecompiles.ErrBalanceMigrationRequired | ||
| } | ||
|
|
||
| // Create state DB for this transaction (only for valid transactions) | ||
| stateDB := gigaevmstate.NewDBImpl(ctx, &app.GigaEvmKeeper, false) | ||
| defer stateDB.Cleanup() | ||
|
|
||
| // Pre-charge gas fee (like V2's ante handler), then execute with feeAlreadyCharged=true. | ||
| // V2 charges fees in the ante handler, then runs the EVM with feeAlreadyCharged=true | ||
| // which skips buyGas/refundGas/coinbase. Without this, GasUsed differs between Giga | ||
|
|
@@ -3059,6 +3059,7 @@ type gigaValidationResult struct { | |
| // 7. Balance check | ||
| func (app *App) validateGigaEVMTx( | ||
| ctx sdk.Context, | ||
| stateDB *gigaevmstate.DBImpl, | ||
| ethTx *ethtypes.Transaction, | ||
| sender common.Address, | ||
| seiAddr sdk.AccAddress, | ||
|
|
@@ -3209,13 +3210,17 @@ func (app *App) validateGigaEVMTx( | |
| balanceCheck := new(big.Int).Mul(new(big.Int).SetUint64(ethTx.Gas()), ethTx.GasFeeCap()) | ||
| balanceCheck.Add(balanceCheck, ethTx.Value()) | ||
|
|
||
| senderBalance := app.GigaEvmKeeper.GetBalance(ctx, seiAddr) | ||
| // Route validation balance reads through DBImpl so mock_balances follows | ||
| // the same ensureMinimumBalance behavior as V2's StateTransition.BuyGas. | ||
| senderBalance := stateDB.GetBalance(sender).ToBig() | ||
|
|
||
| // Include cast address balance for unassociated addresses (matches V2 PreprocessDecorator) | ||
| // Include the derived Sei address balance for unassociated addresses (matches V2 PreprocessDecorator). | ||
| if !isAssociated { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [suggestion] Confirmed the Codex P2 concern, scoped to |
||
| castAddr := sdk.AccAddress(sender[:]) | ||
| castBalance := app.GigaEvmKeeper.GetBalance(ctx, castAddr) | ||
| senderBalance = new(big.Int).Add(senderBalance, castBalance) | ||
| seiEVMAddr := common.BytesToAddress(seiAddr) | ||
| if seiEVMAddr != sender { | ||
| seiBalance := stateDB.GetBalance(seiEVMAddr).ToBig() | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [suggestion] mock_balances only: for an unassociated sender both |
||
| senderBalance = new(big.Int).Add(senderBalance, seiBalance) | ||
| } | ||
| } | ||
|
|
||
| if senderBalance.Cmp(balanceCheck) < 0 { | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,32 @@ | ||
| //go:build mock_balances | ||
|
|
||
| package giga_test | ||
|
|
||
| import ( | ||
| "math/big" | ||
| "testing" | ||
| "time" | ||
|
|
||
| "github.com/sei-protocol/sei-chain/occ_tests/utils" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func TestGigaMockBalancesValidationUsesDBImpl(t *testing.T) { | ||
| blockTime := time.Now() | ||
| accts := utils.NewTestAccounts(3) | ||
| signer := utils.NewSigner() | ||
| recipient := utils.NewSigner() | ||
|
|
||
| gigaCtx := NewGigaTestContext(t, accts, blockTime, 1, ModeGigaSequential) | ||
| gigaCtx.TestApp.GigaEvmKeeper.SetAddressMapping(gigaCtx.Ctx, signer.AccountAddress, signer.EvmAddress) | ||
|
|
||
| to := recipient.EvmAddress | ||
| value := big.NewInt(1_000_000_000_000_000_000) | ||
| fee := big.NewInt(100000000000) | ||
| tx := createCustomEVMTx(t, gigaCtx, signer, &to, value, 21000, fee, fee, 0) | ||
|
|
||
| _, results, err := RunBlock(t, gigaCtx, [][]byte{tx}) | ||
| require.NoError(t, err) | ||
| require.Len(t, results, 1) | ||
| require.Equal(t, uint32(0), results[0].Code, results[0].Log) | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[suggestion] mock_balances only:
stateDB.GetBalance(sender)invokesensureMinimumBalance, which mints a 100-SEI top-off directly via the bank keeper onctx. That mint is not journaled in the DBImpl, sodefer stateDB.Cleanup()does not revert it. If validation then fails (e.g. cost exceeds the top-off),executeEVMTxWithGigaExecutorreturns(validation.err, nil)and the synchronous caller still runsctx.GigaMultiStore().WriteGiga()(app.go:1636), committing the top-off. A rejected tx therefore increases the sender's balance — unlike V2, whose ante-handler top-off runs on a cache context discarded on failure. Test/benchmark-build only (panics on pacific-1), so no consensus impact, but it diverges from the V2 parity this PR is trying to achieve.