|
| 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