Skip to content

Commit 9ac2792

Browse files
authored
Backport deterministic simulation limiter test fix to v6.7 (#4702)
`TestSimulationAPIRequestLimiter` expects a burst of concurrent simulation calls to trip the 2-slot limiter. The calls can run one after another before a second goroutine takes a slot, so nothing is rejected and the assertion fails; with `GOMAXPROCS=1` the eth_call, mixed-method and error-format subtests fail on every run. Under `-race` the same burst also reports a data race, because `MockClient.mockBlock` encodes the shared `Tx1` on every call and the tx wrapper memoizes its encoded bytes. This backports the test-only fixes already on main and release/v6.8 (#4000 and #4130). `HoldSimulationSlotsForTest` fills every limiter slot so each rejection check is deterministic, the bursts now only check that all calls are accounted for, and the mock block txs are encoded once during setup. Flaked in: https://github.com/sei-protocol/sei-chain/actions/runs/41084755515/job/124162862603
1 parent 8f17625 commit 9ac2792

3 files changed

Lines changed: 64 additions & 125 deletions

File tree

‎evmrpc/export_test.go‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package evmrpc
22

33
import (
44
"context"
5+
"errors"
56
"math/big"
67
"sync"
78

@@ -18,6 +19,21 @@ import (
1819
"github.com/sei-protocol/sei-chain/x/evm/keeper"
1920
)
2021

22+
// HoldSimulationSlotsForTest occupies simulation request slots until the returned function is called.
23+
func (s *SimulationAPI) HoldSimulationSlotsForTest(ctx context.Context, slots int64) (func(), error) {
24+
if s.requestLimiter == nil {
25+
return nil, errors.New("simulation request limiter is disabled")
26+
}
27+
if slots <= 0 {
28+
return nil, errors.New("simulation slot count must be positive")
29+
}
30+
if err := s.requestLimiter.Acquire(ctx, slots); err != nil {
31+
return nil, err
32+
}
33+
34+
return func() { s.requestLimiter.Release(slots) }, nil
35+
}
36+
2137
// RangeQueryWindowBlocksForTest exposes rangeQueryWindowBlocks so integration
2238
// tests can assert tryFilterLogsRange's window boundaries without hardcoding
2339
// the constant.

‎evmrpc/setup_test.go‎

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,6 +125,12 @@ var DebugTraceNonPanicTx sdk.Tx
125125
var DebugTraceSyntheticTx sdk.Tx
126126
var TxNonEvm sdk.Tx
127127
var TxNonEvmWithSyntheticLog sdk.Tx
128+
129+
// Tx1Bz and TxNonEvmWithSyntheticLogBz are encoded once in init: the tx
130+
// encoder memoizes into the tx wrapper, which is unsafe under the concurrent
131+
// mockBlock calls made by rate-limiter tests.
132+
var Tx1Bz []byte
133+
var TxNonEvmWithSyntheticLogBz []byte
128134
var UnconfirmedTx sdk.Tx
129135

130136
var SConfig = evmrpc.SimulateConfig{GasCap: 10000000, MaxStateOverrideAccounts: 100, MaxStateOverrideSlots: 1000}
@@ -297,16 +303,7 @@ func (c *MockClient) mockBlock(height int64) *coretypes.ResultBlock {
297303
Block: &tmtypes.Block{
298304
Header: mockBlockHeader(height),
299305
Data: tmtypes.Data{
300-
Txs: []tmtypes.Tx{
301-
func() []byte {
302-
bz, _ := Encoder(Tx1)
303-
return bz
304-
}(),
305-
func() []byte {
306-
bz, _ := Encoder(TxNonEvmWithSyntheticLog)
307-
return bz
308-
}(),
309-
},
306+
Txs: []tmtypes.Tx{Tx1Bz, TxNonEvmWithSyntheticLogBz},
310307
},
311308
LastCommit: &tmtypes.Commit{
312309
Height: MockHeight8 - 1,
@@ -918,6 +915,9 @@ func generateTxData() {
918915
TestSyntheticTxHash = syntheticEthTx.Hash().Hex()
919916
TxNonEvm = app.TestTx{}
920917
TxNonEvmWithSyntheticLog = app.TestTx{}
918+
Tx1Bz = mustEncode(Tx1)
919+
// app.TestTx is rejected by the encoder and appears in blocks as empty bytes.
920+
TxNonEvmWithSyntheticLogBz = nil
921921
bloomTx1 := ethtypes.CreateBloom(&ethtypes.Receipt{Logs: []*ethtypes.Log{{
922922
Address: common.HexToAddress("0x1111111111111111111111111111111111111111"),
923923
Topics: []common.Hash{common.HexToHash("0x1111111111111111111111111111111111111111111111111111111111111111"),
@@ -990,6 +990,14 @@ func generateTxData() {
990990
EVMKeeper.SetAddressMapping(Ctx, sdk.AccAddress(tracerTestTxFrom[:]), tracerTestTxFrom)
991991
}
992992

993+
func mustEncode(tx sdk.Tx) []byte {
994+
bz, err := Encoder(tx)
995+
if err != nil {
996+
panic(err)
997+
}
998+
return bz
999+
}
1000+
9931001
func buildTx(txData ethtypes.DynamicFeeTx) (client.TxBuilder, *ethtypes.Transaction) {
9941002
chainId := big.NewInt(config.DefaultChainID)
9951003
mnemonic := "fish mention unlock february marble dove vintage sand hub ordinary fade found inject room embark supply fabric improve spike stem give current similar glimpse"

‎evmrpc/simulate_test.go‎

Lines changed: 30 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -603,6 +603,7 @@ func TestSimulateBackendBlockResolutionCoverage(t *testing.T) {
603603
}
604604

605605
func TestSimulationAPIRequestLimiter(t *testing.T) {
606+
const maxConcurrentSimulationCalls = 2
606607

607608
type testEnv struct {
608609
simAPI *evmrpc.SimulationAPI
@@ -629,7 +630,7 @@ func TestSimulationAPIRequestLimiter(t *testing.T) {
629630
config := &evmrpc.SimulateConfig{
630631
GasCap: 1000000,
631632
EVMTimeout: 5 * time.Second,
632-
MaxConcurrentSimulationCalls: 2, // Small limit to easily trigger rate limiting
633+
MaxConcurrentSimulationCalls: maxConcurrentSimulationCalls,
633634
}
634635

635636
// Use the existing test app from the global setup
@@ -675,9 +676,21 @@ func TestSimulationAPIRequestLimiter(t *testing.T) {
675676
args: args,
676677
}
677678
}
679+
assertRateLimited := func(t *testing.T, tEnv *testEnv, expected string, invoke func() error) {
680+
t.Helper()
681+
release, err := tEnv.simAPI.HoldSimulationSlotsForTest(t.Context(), maxConcurrentSimulationCalls)
682+
require.NoError(t, err)
683+
defer release()
684+
require.EqualError(t, invoke(), expected)
685+
}
678686

679687
t.Run("TestEthCallRateLimiting", func(t *testing.T) {
680688
tEnv := newTestEnv(t)
689+
assertRateLimited(t, tEnv, "eth_call rejected due to rate limit: server busy", func() error {
690+
_, err := tEnv.simAPI.Call(t.Context(), tEnv.args, nil, nil, nil)
691+
return err
692+
})
693+
681694
// Test eth_call rate limiting with concurrent requests
682695
numRequests := 10 // Much more than the limit of 2
683696
runBurst := func() []error {
@@ -718,8 +731,9 @@ func TestSimulationAPIRequestLimiter(t *testing.T) {
718731
}
719732
}
720733

721-
// With only 2 concurrent slots and 10 requests, we should have rejections
722-
require.Greater(t, rejectedCount, 0, "Should have rejected requests due to rate limiting")
734+
// Calls can serialize before another goroutine holds a slot. The saturated
735+
// call above covers rejection deterministically; this burst covers the
736+
// concurrent response outcomes.
723737
require.Greater(t, successCount, 0, "Should have some successful requests")
724738
require.Equal(t, numRequests, successCount+rejectedCount, "All requests should be accounted for")
725739

@@ -890,122 +904,23 @@ func TestSimulationAPIRequestLimiter(t *testing.T) {
890904
})
891905

892906
t.Run("TestDifferentMethodsShareSameLimiter", func(t *testing.T) {
893-
// Test that different simulation methods share the same rate limiter.
894-
// A single burst can occasionally avoid contention on overloaded CI workers,
895-
// so retry a synchronized burst a few times.
896-
const (
897-
numCallRequests = 20
898-
numEstimateRequests = 20
899-
maxAttempts = 5
900-
)
901-
totalRequests := numCallRequests + numEstimateRequests
902-
903-
runMixedBurst := func(tEnv *testEnv) (int, int) {
904-
results := make(chan error, totalRequests)
905-
start := make(chan struct{})
906-
var wg sync.WaitGroup
907-
908-
// Start mixed requests and release them at once to maximize contention.
909-
for range numCallRequests {
910-
wg.Go(func() {
911-
<-start
912-
_, err := tEnv.simAPI.Call(t.Context(), tEnv.args, nil, nil, nil)
913-
results <- err
914-
})
915-
}
916-
for range numEstimateRequests {
917-
wg.Go(func() {
918-
<-start
919-
_, err := tEnv.simAPI.EstimateGas(t.Context(), tEnv.args, nil, nil)
920-
results <- err
921-
})
922-
}
923-
924-
close(start)
925-
wg.Wait()
926-
close(results)
927-
928-
successCount := 0
929-
rejectedCount := 0
930-
for err := range results {
931-
if err == nil {
932-
successCount++
933-
} else if strings.Contains(err.Error(), "rejected due to rate limit: server busy") {
934-
rejectedCount++
935-
}
936-
}
937-
return successCount, rejectedCount
938-
}
939-
940-
var (
941-
lastSuccess int
942-
lastRejected int
943-
attemptsUsed int
944-
observedRejection bool
945-
)
946-
for attempt := 1; attempt <= maxAttempts; attempt++ {
947-
attemptsUsed = attempt
948-
lastSuccess, lastRejected = runMixedBurst(newTestEnv(t))
949-
require.Equalf(t, totalRequests, lastSuccess+lastRejected, "All mixed method requests should be accounted for (attempt %d)", attempt)
950-
if lastRejected > 0 {
951-
observedRejection = true
952-
break
953-
}
954-
}
955-
956-
require.Truef(
957-
t,
958-
observedRejection,
959-
"Different methods should share the same rate limiter (last burst: %d successful, %d rejected)",
960-
lastSuccess,
961-
lastRejected,
962-
)
963-
t.Logf(
964-
"Mixed methods rate limiting (attempt %d/%d): %d successful, %d rejected out of %d total",
965-
attemptsUsed,
966-
maxAttempts,
967-
lastSuccess,
968-
lastRejected,
969-
totalRequests,
970-
)
907+
tEnv := newTestEnv(t)
908+
assertRateLimited(t, tEnv, "eth_call rejected due to rate limit: server busy", func() error {
909+
_, err := tEnv.simAPI.Call(t.Context(), tEnv.args, nil, nil, nil)
910+
return err
911+
})
912+
assertRateLimited(t, tEnv, "eth_estimateGas rejected due to rate limit: server busy", func() error {
913+
_, err := tEnv.simAPI.EstimateGas(t.Context(), tEnv.args, nil, nil)
914+
return err
915+
})
971916
})
972917

973918
t.Run("TestRateLimitErrorFormat", func(t *testing.T) {
974919
tEnv := newTestEnv(t)
975-
// Test the error message format by overwhelming the rate limiter
976-
const numRequests = 20
977-
results := make(chan error, numRequests)
978-
start := make(chan struct{})
979-
var wg sync.WaitGroup
980-
981-
// Release all requests at once to reliably saturate the limiter.
982-
for range numRequests {
983-
wg.Go(func() {
984-
<-start
985-
_, err := tEnv.simAPI.Call(t.Context(), tEnv.args, nil, nil, nil)
986-
results <- err
987-
})
988-
}
989-
close(start)
990-
wg.Wait()
991-
close(results)
992-
993-
var rateLimitErrors []error
994-
for err := range results {
995-
if err != nil && strings.Contains(err.Error(), "rejected due to rate limit") {
996-
rateLimitErrors = append(rateLimitErrors, err)
997-
}
998-
}
999-
1000-
require.Greater(t, len(rateLimitErrors), 0, "Should have at least one rate limit error")
1001-
1002-
// Verify error message format
1003-
for _, err := range rateLimitErrors {
1004-
require.Contains(t, err.Error(), "eth_call rejected due to rate limit: server busy")
1005-
require.Contains(t, err.Error(), "server busy")
1006-
}
1007-
1008-
t.Logf("Found %d rate limit errors with correct format", len(rateLimitErrors))
920+
assertRateLimited(t, tEnv, "eth_createAccessList rejected due to rate limit: server busy", func() error {
921+
_, err := tEnv.simAPI.CreateAccessList(t.Context(), tEnv.args, nil)
922+
return err
923+
})
1009924
})
1010925
}
1011926

0 commit comments

Comments
 (0)