From 3f9b9d6f4061e77494979aad758de37f4ec276c4 Mon Sep 17 00:00:00 2001 From: ayaanoncrypto <1.06945712e+08+ayaanoncrypto@users.noreply.github.com> Date: Thu, 27 Aug 2026 02:07:39 +0000 Subject: [PATCH] Fix gas price update logic in loadtest runner --- loadtest/runner.go | 11 ------ loadtest/runner_gas_test.go | 78 +++++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 11 deletions(-) create mode 100644 loadtest/runner_gas_test.go diff --git a/loadtest/runner.go b/loadtest/runner.go index 0cd140084..0b70e660a 100644 --- a/loadtest/runner.go +++ b/loadtest/runner.go @@ -1492,10 +1492,6 @@ func (r *Runner) suggestMaxFeePerGas(ctx context.Context, blockNumber uint64, fo return nil } - if r.cachedBlockNumber != nil && blockNumber <= *r.cachedBlockNumber && r.cachedGasPrice != nil { - return r.cachedGasPrice - } - feeHistory, err := r.client.FeeHistory(ctx, 5, nil, []float64{0.5}) if err != nil { log.Error().Err(err).Msg("Unable to get fee history while checking MaxFeePerGas") @@ -1511,13 +1507,6 @@ func (r *Runner) suggestMaxFeePerGas(ctx context.Context, blockNumber uint64, fo maxFeePerGas.Mul(baseFee, big.NewInt(2)) maxFeePerGas.Add(maxFeePerGas, priorityFee) - const blocksToWait = 5 - isDecreasing := r.cachedGasPrice != nil && maxFeePerGas.Uint64() <= r.cachedGasPrice.Uint64() - canDecrease := blockNumber+blocksToWait <= header.Number.Uint64() - if isDecreasing && !canDecrease && r.cachedGasPrice != nil { - return r.cachedGasPrice - } - r.cachedGasPrice = maxFeePerGas log.Trace(). diff --git a/loadtest/runner_gas_test.go b/loadtest/runner_gas_test.go new file mode 100644 index 000000000..f9efe6f97 --- /dev/null +++ b/loadtest/runner_gas_test.go @@ -0,0 +1,78 @@ +package loadtest + +import ( + "context" + "encoding/json" + "fmt" + "math/big" + "net/http" + "net/http/httptest" + "testing" + "time" + + "github.com/0xPolygon/polygon-cli/loadtest/config" + "github.com/ethereum/go-ethereum/ethclient" +) + +type rpcReq struct { + Method string `json:"method"` + ID int `json:"id"` +} + +func TestRunnerGasPriceDecrease(t *testing.T) { + var baseFee int64 = 100 + var blockNum uint64 = 1000 + + ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + var req rpcReq + json.NewDecoder(r.Body).Decode(&req) + w.Header().Set("Content-Type", "application/json") + switch req.Method { + case "eth_blockNumber": + fmt.Fprintf(w, `{"jsonrpc":"2.0","id":%d,"result":"0x%x"}`, req.ID, blockNum) + case "eth_getBlockByNumber": + fmt.Fprintf(w, `{"jsonrpc":"2.0","id":%d,"result":{"number":"0x%x","baseFeePerGas":"0x%x","parentHash":"0x0000000000000000000000000000000000000000000000000000000000000000","difficulty":"0x0","extraData":"0x","gasLimit":"0x0","gasUsed":"0x0","miner":"0x0000000000000000000000000000000000000000","mixHash":"0x0000000000000000000000000000000000000000000000000000000000000000","nonce":"0x0000000000000000","receiptsRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","sha3Uncles":"0x0000000000000000000000000000000000000000000000000000000000000000","stateRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","timestamp":"0x0","transactionsRoot":"0x0000000000000000000000000000000000000000000000000000000000000000","logsBloom":"0x00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"}}`, req.ID, blockNum, baseFee) + case "eth_feeHistory": + // Return a fee history where the latest base fee is our current baseFee + fmt.Fprintf(w, `{"jsonrpc":"2.0","id":%d,"result":{"baseFeePerGas":["0x%x","0x%x"],"reward":[["0x%x"]]}}`, req.ID, baseFee, baseFee, 10) + case "eth_maxPriorityFeePerGas": + fmt.Fprintf(w, `{"jsonrpc":"2.0","id":%d,"result":"0xa"}`, req.ID) + case "eth_chainId": + fmt.Fprintf(w, `{"jsonrpc":"2.0","id":%d,"result":"0x89"}`, req.ID) + default: + fmt.Fprintf(w, `{"jsonrpc":"2.0","id":%d,"result":"0x0"}`, req.ID) + } + })) + defer ts.Close() + + client, _ := ethclient.Dial(ts.URL) + r := &Runner{ + cfg: &config.Config{ + ChainSupportBaseFee: true, + BigGasPriceMultiplier: big.NewFloat(1.0), + }, + client: client, + } + + ctx := context.Background() + + // 1. Initial price + gp1, _ := r.getSuggestedGasPrices(ctx) + // baseFee 100 * 2 + priority 10 = 210 + if gp1.Int64() != 210 { + t.Errorf("Expected 210, got %v", gp1) + } + + // 2. Decrease base fee and advance block + baseFee = 50 + blockNum = 1001 + // Reset the cache to force a new fetch + r.cachedLatestBlockTime = time.Time{} + + gp2, _ := r.getSuggestedGasPrices(ctx) + // new baseFee 50 * 2 + priority 10 = 110 + // Before the fix, this would have returned 210 because of the broken blocksToWait logic. + if gp2.Int64() != 110 { + t.Errorf("Expected 110, got %v", gp2) + } +}