Skip to content

Commit cca3191

Browse files
committed
max-base-fee-gwei is now max-base-fee-wei
1 parent bbe9802 commit cca3191

File tree

2 files changed

+77
-44
lines changed

2 files changed

+77
-44
lines changed

cmd/loadtest/app.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ type (
8888
WaitForReceipt *bool
8989
ReceiptRetryMax *uint
9090
ReceiptRetryInitialDelayMs *uint
91-
MaxBaseFeeGwei *uint64
91+
MaxBaseFeeWei *uint64
9292

9393
// Computed
9494
CurrentGasPrice *big.Int
@@ -252,7 +252,7 @@ func initFlags() {
252252
ltp.PreFundSendingAccounts = LoadtestCmd.Flags().Bool("pre-fund-sending-accounts", false, "If set to true, the sending accounts will be funded at the start of the execution, otherwise all accounts will be funded when used for the first time.")
253253
ltp.RefundRemainingFunds = LoadtestCmd.Flags().Bool("refund-remaining-funds", false, "If set to true, the funded amount will be refunded to the funding account. Otherwise, the funded amount will remain in the sending accounts.")
254254
ltp.SendingAccountsFile = LoadtestCmd.Flags().String("sending-accounts-file", "", "The file containing the sending accounts private keys, one per line. This is useful for avoiding pool account queue but also to keep the same sending accounts for different execution cycles.")
255-
ltp.MaxBaseFeeGwei = LoadtestCmd.Flags().Uint64("max-base-fee-gwei", 0, "The maximum base fee in gwei. If the base fee exceeds this value, sending tx will be paused and while paused, existing in-flight transactions continue to confirmation, but no additional SendTransaction calls occur. This is useful to avoid sending transactions when the network is congested.")
255+
ltp.MaxBaseFeeWei = LoadtestCmd.Flags().Uint64("max-base-fee-wei", 0, "The maximum base fee in wei. If the base fee exceeds this value, sending tx will be paused and while paused, existing in-flight transactions continue to confirmation, but no additional SendTransaction calls occur. This is useful to avoid sending transactions when the network is congested.")
256256

257257
// Local flags.
258258
ltp.Modes = LoadtestCmd.Flags().StringSliceP("mode", "m", []string{"t"}, `The testing mode to use. It can be multiple like: "d,t"

cmd/loadtest/loadtest.go

Lines changed: 75 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -786,48 +786,7 @@ func mainLoop(ctx context.Context, c *ethclient.Client, rpc *ethrpc.Client) erro
786786
return err
787787
}
788788

789-
// monitor max base fee if configured
790-
maxBaseFeeCtx, maxBaseFeeCtxCancel := context.WithCancel(ctx)
791-
defer maxBaseFeeCtxCancel()
792-
mustCheckMaxBaseFee := *ltp.MaxBaseFeeGwei > 0
793-
var waitBaseFeeToDrop atomic.Bool
794-
waitBaseFeeToDrop.Store(false)
795-
if mustCheckMaxBaseFee {
796-
log.Info().
797-
Msg("max base fee monitoring enabled")
798-
go func(ctx context.Context, c *ethclient.Client) {
799-
for {
800-
header, err := c.HeaderByNumber(ctx, nil)
801-
if err != nil {
802-
log.Error().Err(err).Msg("Unable to get latest block header for base fee check")
803-
continue
804-
}
805-
806-
if header.BaseFee != nil {
807-
baseFeeGwei := new(big.Int).Div(header.BaseFee, big.NewInt(1e9)) // Convert wei to gwei
808-
if baseFeeGwei.Uint64() > *ltp.MaxBaseFeeGwei {
809-
waitBaseFeeToDrop.Store(true)
810-
log.Warn().
811-
Msgf("PAUSE: base fee %d Gwei > limit %d Gwei", baseFeeGwei.Uint64(), *ltp.MaxBaseFeeGwei)
812-
} else {
813-
if waitBaseFeeToDrop.Load() {
814-
waitBaseFeeToDrop.Store(false)
815-
log.Info().
816-
Msgf("RESUME: base fee %d Gwei ≤ limit %d Gwei", baseFeeGwei.Uint64(), *ltp.MaxBaseFeeGwei)
817-
}
818-
}
819-
} else {
820-
waitBaseFeeToDrop.Store(false)
821-
}
822-
select {
823-
case <-ctx.Done():
824-
return
825-
default:
826-
time.Sleep(time.Second)
827-
}
828-
}
829-
}(maxBaseFeeCtx, c)
830-
}
789+
mustCheckMaxBaseFee, maxBaseFeeCtxCancel, waitBaseFeeToDrop := setupBaseFeeMonitoring(ctx, c, ltp)
831790

832791
log.Debug().Msg("Starting main load test loop")
833792
var wg sync.WaitGroup
@@ -1005,6 +964,80 @@ func mainLoop(ctx context.Context, c *ethclient.Client, rpc *ethrpc.Client) erro
1005964
return nil
1006965
}
1007966

967+
func setupBaseFeeMonitoring(ctx context.Context, c *ethclient.Client, ltp loadTestParams) (bool, context.CancelFunc, *atomic.Bool) {
968+
// monitor max base fee if configured
969+
maxBaseFeeCtx, maxBaseFeeCtxCancel := context.WithCancel(ctx)
970+
mustCheckMaxBaseFee := *ltp.MaxBaseFeeWei > 0
971+
var waitBaseFeeToDrop atomic.Bool
972+
waitBaseFeeToDrop.Store(false)
973+
if mustCheckMaxBaseFee {
974+
log.Info().
975+
Msg("max base fee monitoring enabled")
976+
977+
wg := sync.WaitGroup{}
978+
wg.Add(1)
979+
// start a goroutine to monitor the base fee while load test is running
980+
go func(ctx context.Context, c *ethclient.Client, waitToDrop *atomic.Bool, maxBaseFeeWei uint64) {
981+
firstRun := true
982+
for {
983+
select {
984+
case <-ctx.Done():
985+
return
986+
default:
987+
currentBaseFeeIsGreater, currentBaseFeeWei, err := isCurrentBaseFeeGreaterThanMaxBaseFee(ctx, c, maxBaseFeeWei)
988+
if err != nil {
989+
log.Error().
990+
Err(err).
991+
Msg("Error checking base fee during load test")
992+
} else {
993+
if currentBaseFeeIsGreater {
994+
log.Warn().
995+
Msgf("PAUSE: base fee %d Wei > limit %d Wei", currentBaseFeeWei.Uint64(), maxBaseFeeWei)
996+
waitToDrop.Store(true)
997+
} else {
998+
log.Info().
999+
Msgf("RESUME: base fee %d Wei ≤ limit %d Wei", currentBaseFeeWei.Uint64(), maxBaseFeeWei)
1000+
waitToDrop.Store(false)
1001+
}
1002+
1003+
if firstRun {
1004+
firstRun = false
1005+
wg.Done()
1006+
}
1007+
}
1008+
time.Sleep(time.Second)
1009+
}
1010+
}
1011+
}(maxBaseFeeCtx, c, &waitBaseFeeToDrop, *ltp.MaxBaseFeeWei)
1012+
1013+
// wait for first run to complete so we know if we need to wait or not for base fee to drop
1014+
wg.Wait()
1015+
}
1016+
return mustCheckMaxBaseFee, maxBaseFeeCtxCancel, &waitBaseFeeToDrop
1017+
}
1018+
1019+
func isCurrentBaseFeeGreaterThanMaxBaseFee(ctx context.Context, c *ethclient.Client, maxBaseFee uint64) (bool, *big.Int, error) {
1020+
header, err := c.HeaderByNumber(ctx, nil)
1021+
if errors.Is(err, context.Canceled) {
1022+
log.Debug().Msg("max base fee monitoring context canceled")
1023+
return false, nil, nil
1024+
} else if err != nil {
1025+
log.Error().Err(err).Msg("Unable to get latest block header to check base fee")
1026+
return false, nil, err
1027+
}
1028+
1029+
if header.BaseFee != nil {
1030+
currentBaseFee := header.BaseFee
1031+
if currentBaseFee.Cmp(new(big.Int).SetUint64(maxBaseFee)) > 0 {
1032+
return true, currentBaseFee, nil
1033+
} else {
1034+
return false, currentBaseFee, nil
1035+
}
1036+
}
1037+
1038+
return false, nil, nil
1039+
}
1040+
10081041
func getLoadTestContract(ctx context.Context, c *ethclient.Client, tops *bind.TransactOpts, cops *bind.CallOpts) (ltAddr ethcommon.Address, ltContract *tester.LoadTester, err error) {
10091042
ltAddr = ethcommon.HexToAddress(*inputLoadTestParams.LoadtestContractAddress)
10101043

0 commit comments

Comments
 (0)