Skip to content

Commit dced959

Browse files
authored
fix(giga): fall back to legacy SSTORE gas when the param reads as 0 (#4450)
## Summary Fixes [CON-510](https://linear.app/seilabs/issue/CON-510). Giga's `newGigaBlockCache` (`app/app.go`) read `SeiSstoreSetGasEip2200` straight off the stored param, with no fallback for a stored/absent 0 — real for blocks created before the param existed, or any state that materializes an explicit 0. V2 always goes through `GetSstoreSetGasEIP2200`, which maps 0 → the legacy hardcoded value (20000). Giga's keeper had no equivalent getter. With a 0 param, Giga: - Undercharges a clean `0 -> non-zero` SSTORE (cold-access cost only, no set cost) — ~10x-35x cheaper storage writes than V2. - Computes `AddRefund(param - WarmStorageReadCost)` as a uint64 underflow for a `0 -> A -> 0` SSTORE in one tx. Neither Sei StateDB's `AddRefund` guards against overflow, so this corrupts the refund counter instead of erroring, and a further storage-slot recreate can then underflow `SubRefund` and panic — causing a tx that succeeds on V2 to fail on Giga. ## Changes - `giga/deps/xevm/keeper/params.go`: add `GetSstoreSetGasEIP2200` (+ `LegacySstoreSetGasEIP2200` const), mirroring V2's getter. - `app/app.go`: `newGigaBlockCache` now calls the new getter instead of reading the raw param. - `giga/deps/xevm/keeper/params_test.go`: unit test for the new getter's 0 → legacy fallback. - `giga/tests/sstore_gas_zero_parity_test.go`: V2-vs-GigaSequential-vs-GigaOCC parity tests with the param forced to a raw 0 (bypassing `SetParams` validation, which rejects 0) — clean SSTORE gas, set/reset gas, and the refund-underflow panic case. All three fail against the pre-fix code and pass after the fix. Audited the rest of `newGigaBlockCache` and the Giga keeper for other raw param reads feeding gas/chain config; this was the only one missing a fallback. ## Test plan - [x] `go test ./giga/... ./x/evm/... ./app/... -count=1` passes - [x] New parity tests confirmed failing against the unfixed code, passing after the fix - [x] `gofmt`/`goimports`/`go vet` clean on touched packages
1 parent 94967eb commit dced959

4 files changed

Lines changed: 208 additions & 1 deletion

File tree

‎app/app.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -339,7 +339,7 @@ func newGigaBlockCache(ctx sdk.Context, keeper *gigaevmkeeper.Keeper) (*gigaBloc
339339
if err != nil {
340340
return nil, err
341341
}
342-
sstore := keeper.GetParams(ctx).SeiSstoreSetGasEip2200
342+
sstore := keeper.GetSstoreSetGasEIP2200(ctx)
343343
chainConfig := evmtypes.DefaultChainConfig().EthereumConfigWithSstore(chainID, &sstore)
344344
baseFee := keeper.GetBaseFee(ctx)
345345
return &gigaBlockCache{

‎giga/deps/xevm/keeper/params.go‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,19 @@ func (k *Keeper) ChainID(ctx sdk.Context) *big.Int {
231231

232232
}
233233

234+
// LegacySstoreSetGasEIP2200 is the SSTORE gas cost used before the param existed.
235+
const LegacySstoreSetGasEIP2200 = uint64(20000)
236+
237+
// GetSstoreSetGasEIP2200 returns the SSTORE gas cost, falling back to the
238+
// legacy value when the stored param is 0.
239+
func (k *Keeper) GetSstoreSetGasEIP2200(ctx sdk.Context) uint64 {
240+
sstore := k.GetParams(ctx).SeiSstoreSetGasEip2200
241+
if sstore == 0 {
242+
return LegacySstoreSetGasEIP2200
243+
}
244+
return sstore
245+
}
246+
234247
/*
235248
*
236249
sei gas = evm gas * multiplier

‎giga/deps/xevm/keeper/params_test.go‎

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"time"
66

77
testkeeper "github.com/sei-protocol/sei-chain/giga/deps/testutil/keeper"
8+
"github.com/sei-protocol/sei-chain/giga/deps/xevm/keeper"
89
"github.com/sei-protocol/sei-chain/giga/deps/xevm/types"
910
sdk "github.com/sei-protocol/sei-chain/sei-cosmos/types"
1011
"github.com/stretchr/testify/require"
@@ -51,6 +52,27 @@ func TestGetParamsIfExists(t *testing.T) {
5152
require.Equal(t, types.DefaultParams().DeliverTxHookWasmGasLimit, params.DeliverTxHookWasmGasLimit)
5253
}
5354

55+
func TestGetSstoreSetGasEIP2200(t *testing.T) {
56+
k, ctx := testkeeper.MockEVMKeeper(t)
57+
ctx = ctx.WithBlockTime(time.Now())
58+
59+
require.Equal(t, types.DefaultSeiSstoreSetGasEIP2200, k.GetSstoreSetGasEIP2200(ctx))
60+
61+
// SetParams rejects 0 ...
62+
p := k.GetParams(ctx)
63+
p.SeiSstoreSetGasEip2200 = 0
64+
require.Error(t, p.Validate())
65+
66+
// ... but a store with the key absent/zero (pre-param heights, or a migration
67+
// that materializes an explicit 0) falls back to the legacy value.
68+
k.Paramstore.Set(ctx, types.KeySeiSstoreSetGasEIP2200, uint64(0))
69+
require.Equal(t, keeper.LegacySstoreSetGasEIP2200, k.GetSstoreSetGasEIP2200(ctx))
70+
require.Equal(t, uint64(20000), keeper.LegacySstoreSetGasEIP2200)
71+
72+
k.Paramstore.Set(ctx, types.KeySeiSstoreSetGasEIP2200, uint64(72000))
73+
require.Equal(t, uint64(72000), k.GetSstoreSetGasEIP2200(ctx))
74+
}
75+
5476
func TestParamGettersTracingVersions(t *testing.T) {
5577
k, baseCtx := testkeeper.MockEVMKeeper(t)
5678

Lines changed: 172 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,172 @@
1+
package giga_test
2+
3+
import (
4+
"math/big"
5+
"testing"
6+
"time"
7+
8+
"github.com/ethereum/go-ethereum/common"
9+
"github.com/ethereum/go-ethereum/core/vm"
10+
"github.com/ethereum/go-ethereum/crypto"
11+
gigatypes "github.com/sei-protocol/sei-chain/giga/deps/xevm/types"
12+
"github.com/sei-protocol/sei-chain/occ_tests/utils"
13+
abci "github.com/sei-protocol/sei-chain/sei-tendermint/abci/types"
14+
evmtypes "github.com/sei-protocol/sei-chain/x/evm/types"
15+
"github.com/stretchr/testify/require"
16+
)
17+
18+
// CON-510: forces the SSTORE gas param to a raw 0 (bypassing SetParams
19+
// validation) and checks Giga agrees with V2 on gas, receipts, and LastResultsHash.
20+
21+
// ---- minimal bytecode assembler for SSTORE test contracts ----
22+
23+
type sstoreBC struct{ code []byte }
24+
25+
func (b *sstoreBC) op(ops ...vm.OpCode) *sstoreBC {
26+
for _, o := range ops {
27+
b.code = append(b.code, byte(o))
28+
}
29+
return b
30+
}
31+
32+
func (b *sstoreBC) push1(v byte) *sstoreBC { b.code = append(b.code, byte(vm.PUSH1), v); return b }
33+
func (b *sstoreBC) bytes() []byte { return b.code }
34+
func (b *sstoreBC) pc() int { return len(b.code) }
35+
36+
// sstoreInitcode wraps runtime in a trivial constructor that just returns it.
37+
func sstoreInitcode(runtime []byte) []byte {
38+
const off = 11
39+
if len(runtime) > 255 {
40+
panic("sstoreInitcode: runtime too large for PUSH1 encoding")
41+
}
42+
pre := (&sstoreBC{}).push1(byte(len(runtime))).op(vm.DUP1).push1(off).push1(0).op(vm.CODECOPY).push1(0).op(vm.RETURN)
43+
return append(pre.bytes(), runtime...)
44+
}
45+
46+
// setSstoreParamRaw writes the SSTORE gas param directly, bypassing
47+
// SetParams validation (which rejects 0).
48+
func setSstoreParamRaw(tCtx *GigaTestContext, v uint64) {
49+
if tCtx.isGigaMode() {
50+
tCtx.TestApp.GigaEvmKeeper.Paramstore.Set(tCtx.Ctx, gigatypes.KeySeiSstoreSetGasEIP2200, v)
51+
} else {
52+
tCtx.TestApp.EvmKeeper.Paramstore.Set(tCtx.Ctx, evmtypes.KeySeiSstoreSetGasEIP2200, v)
53+
}
54+
}
55+
56+
// setStateRaw writes a contract storage slot directly via the keeper, with
57+
// no EVM execution and no gas cost.
58+
func setStateRaw(tCtx *GigaTestContext, addr common.Address, slot, val common.Hash) {
59+
if tCtx.isGigaMode() {
60+
tCtx.TestApp.GigaEvmKeeper.SetState(tCtx.Ctx, addr, slot, val)
61+
} else {
62+
tCtx.TestApp.EvmKeeper.SetState(tCtx.Ctx, addr, slot, val)
63+
}
64+
}
65+
66+
// deployAndCall deploys initcode and calls it once with no calldata, both in
67+
// one block. deployer must be shared across modes so receipts stay comparable.
68+
func deployAndCall(t testing.TB, tCtx *GigaTestContext, deployer utils.TestAcct, initcode []byte) []*abci.ExecTxResult {
69+
isGiga := tCtx.isGigaMode()
70+
contractAddr := crypto.CreateAddress(deployer.EvmAddress, 0)
71+
72+
deployTxs := CreateContractDeployTxs(t, tCtx, []EVMContractDeploy{
73+
{Signer: deployer, Bytecode: initcode, Nonce: 0},
74+
}, isGiga)
75+
callTxs := CreateContractCallTxs(t, tCtx, []EVMContractCall{
76+
{Signer: deployer, Contract: contractAddr, Nonce: 1},
77+
}, isGiga)
78+
79+
_, results, err := RunBlock(t, tCtx, append(deployTxs, callTxs...))
80+
require.NoError(t, err)
81+
require.Len(t, results, 2)
82+
return results
83+
}
84+
85+
// runSstoreZeroScenario builds a fresh context for mode with the SSTORE param
86+
// forced to 0, then deploys and calls initcode as deployer.
87+
func runSstoreZeroScenario(t *testing.T, mode ExecutorMode, workers int, deployer utils.TestAcct, initcode []byte) (*GigaTestContext, []*abci.ExecTxResult) {
88+
accts := utils.NewTestAccounts(3)
89+
tCtx := NewGigaTestContext(t, accts, time.Now(), workers, mode)
90+
setSstoreParamRaw(tCtx, 0)
91+
return tCtx, deployAndCall(t, tCtx, deployer, initcode)
92+
}
93+
94+
// assertZeroParamParity checks V2, GigaSequential, and GigaOCC agree (code,
95+
// gas, receipts, LastResultsHash) with the SSTORE param forced to 0.
96+
func assertZeroParamParity(t *testing.T, testName string, initcode []byte) (v2Results, seqResults, occResults []*abci.ExecTxResult) {
97+
deployer := utils.NewSigner()
98+
v2Ctx, v2Results := runSstoreZeroScenario(t, ModeV2withOCC, 1, deployer, initcode)
99+
seqCtx, seqResults := runSstoreZeroScenario(t, ModeGigaSequential, 1, deployer, initcode)
100+
occCtx, occResults := runSstoreZeroScenario(t, ModeGigaOCC, 4, deployer, initcode)
101+
102+
CompareResults(t, testName+"_V2VsGigaSeq", v2Results, seqResults)
103+
CompareReceipts(t, testName+"_V2VsGigaSeq", v2Ctx, v2Results, seqCtx, seqResults)
104+
CompareLastResultsHash(t, testName+"_V2VsGigaSeq", v2Results, seqResults)
105+
106+
CompareResults(t, testName+"_GigaSeqVsGigaOCC", seqResults, occResults)
107+
CompareReceipts(t, testName+"_GigaSeqVsGigaOCC", seqCtx, seqResults, occCtx, occResults)
108+
CompareLastResultsHash(t, testName+"_GigaSeqVsGigaOCC", seqResults, occResults)
109+
110+
return v2Results, seqResults, occResults
111+
}
112+
113+
// A clean 0 -> non-zero SSTORE: Giga undercharges when the param reads as 0.
114+
func TestSstoreGasZeroParity_CleanSet(t *testing.T) {
115+
runtime := (&sstoreBC{}).push1(0x2a).push1(0).op(vm.SSTORE, vm.STOP).bytes()
116+
assertZeroParamParity(t, "SstoreGasZeroParity_CleanSet", sstoreInitcode(runtime))
117+
}
118+
119+
// 0 -> A -> 0 in one tx: the refund math (AddRefund(param - 100)) underflows
120+
// when the param reads as 0, corrupting the refund counter instead of erroring.
121+
func TestSstoreGasZeroParity_SetThenReset(t *testing.T) {
122+
runtime := (&sstoreBC{}).
123+
push1(0x2a).push1(0).op(vm.SSTORE). // SSTORE(slot0, 0x2a)
124+
push1(0).push1(0).op(vm.SSTORE). // SSTORE(slot0, 0) - back to original
125+
op(vm.STOP).
126+
bytes()
127+
assertZeroParamParity(t, "SstoreGasZeroParity_SetThenReset", sstoreInitcode(runtime))
128+
}
129+
130+
// Drives the corrupted refund counter negative via clear/reset/recreate.
131+
// Pre-fix this panics on Giga (recovered as a failed tx) while V2 succeeds.
132+
func TestSstoreGasZeroParity_RefundUnderflowPanic(t *testing.T) {
133+
runtime := (&sstoreBC{}).
134+
push1(0).push1(0).op(vm.SSTORE). // SSTORE(slot0, 0): clear
135+
push1(0x2a).push1(1).op(vm.SSTORE). // SSTORE(slot1, 0x2a): fresh set
136+
push1(0).push1(1).op(vm.SSTORE). // SSTORE(slot1, 0): back to original
137+
push1(0x2a).push1(0).op(vm.SSTORE). // SSTORE(slot0, 0x2a): recreate cleared slot
138+
op(vm.STOP).
139+
bytes()
140+
initcode := sstoreInitcode(runtime)
141+
142+
deployer := utils.NewSigner()
143+
contractAddr := crypto.CreateAddress(deployer.EvmAddress, 0)
144+
slot0 := common.BigToHash(big.NewInt(0))
145+
nonzero := common.BigToHash(big.NewInt(1))
146+
147+
run := func(mode ExecutorMode, workers int) (*GigaTestContext, []*abci.ExecTxResult) {
148+
accts := utils.NewTestAccounts(3)
149+
tCtx := NewGigaTestContext(t, accts, time.Now(), workers, mode)
150+
setSstoreParamRaw(tCtx, 0)
151+
setStateRaw(tCtx, contractAddr, slot0, nonzero) // slot0's "original" for the call tx below
152+
return tCtx, deployAndCall(t, tCtx, deployer, initcode)
153+
}
154+
155+
v2Ctx, v2Results := run(ModeV2withOCC, 1)
156+
seqCtx, seqResults := run(ModeGigaSequential, 1)
157+
occCtx, occResults := run(ModeGigaOCC, 4)
158+
159+
const testName = "SstoreGasZeroParity_RefundUnderflowPanic"
160+
CompareResults(t, testName+"_V2VsGigaSeq", v2Results, seqResults)
161+
CompareReceipts(t, testName+"_V2VsGigaSeq", v2Ctx, v2Results, seqCtx, seqResults)
162+
CompareLastResultsHash(t, testName+"_V2VsGigaSeq", v2Results, seqResults)
163+
164+
CompareResults(t, testName+"_GigaSeqVsGigaOCC", seqResults, occResults)
165+
CompareReceipts(t, testName+"_GigaSeqVsGigaOCC", seqCtx, seqResults, occCtx, occResults)
166+
CompareLastResultsHash(t, testName+"_GigaSeqVsGigaOCC", seqResults, occResults)
167+
168+
// All three must agree the call tx SUCCEEDS, not just agree with each other.
169+
require.Equal(t, uint32(0), v2Results[1].Code, "V2 call should succeed: %s", v2Results[1].Log)
170+
require.Equal(t, uint32(0), seqResults[1].Code, "GigaSequential call should succeed: %s", seqResults[1].Log)
171+
require.Equal(t, uint32(0), occResults[1].Code, "GigaOCC call should succeed: %s", occResults[1].Log)
172+
}

0 commit comments

Comments
 (0)