Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 0 additions & 11 deletions loadtest/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -1544,10 +1544,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")
Expand All @@ -1563,13 +1559,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().
Expand Down
78 changes: 78 additions & 0 deletions loadtest/runner_gas_test.go
Original file line number Diff line number Diff line change
@@ -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)

Check failure on line 28 in loadtest/runner_gas_test.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `(*encoding/json.Decoder).Decode` is not checked (errcheck)
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)

Check failure on line 32 in loadtest/runner_gas_test.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `fmt.Fprintf` is not checked (errcheck)
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)

Check failure on line 34 in loadtest/runner_gas_test.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `fmt.Fprintf` is not checked (errcheck)
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)

Check failure on line 37 in loadtest/runner_gas_test.go

View workflow job for this annotation

GitHub Actions / Lint

Error return value of `fmt.Fprintf` is not checked (errcheck)
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)
}
}
Loading