Skip to content

Commit 281d293

Browse files
committed
refactoring
1 parent a6306b5 commit 281d293

File tree

3 files changed

+20
-8
lines changed

3 files changed

+20
-8
lines changed

cmd/loadtest/gasmanager/gas_provider.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ func (o *GasProviderBase) watchNewHeaders(ctx context.Context) {
4242
if o.onNewHeader == nil {
4343
return
4444
}
45+
const intervalToCheckNewHeader = 1 * time.Second
4546
log.Trace().Msg("starting to watch for new block headers")
4647
var lastHeader *types.Header
4748
for {
@@ -51,12 +52,13 @@ func (o *GasProviderBase) watchNewHeaders(ctx context.Context) {
5152
return
5253
default:
5354
log.Trace().Msg("fetching latest block header")
54-
time.Sleep(1 * time.Second)
55+
time.Sleep(intervalToCheckNewHeader)
5556
}
5657

5758
header, err := o.client.HeaderByNumber(ctx, nil)
5859
if err != nil {
5960
log.Warn().Err(err).Msg("failed to fetch latest block header, retrying...")
61+
continue
6062
}
6163

6264
// Only trigger when there is a new header

cmd/loadtest/gasmanager/gas_vault.go

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,27 @@ func (o *GasVault) AddGas(gas uint64) {
3737

3838
// SpendOrWaitAvailableBudget attempts to spend the specified amount of gas from the vault's available budget.
3939
func (o *GasVault) SpendOrWaitAvailableBudget(gas uint64) {
40+
const intervalToCheckBudgetAvailability = 100 * time.Millisecond
4041
for {
41-
o.mu.Lock()
42-
if gas <= o.gasBudgetAvailable {
43-
o.gasBudgetAvailable -= gas
44-
o.mu.Unlock()
42+
if spent := o.trySpendBudget(gas); spent {
4543
break
4644
}
47-
o.mu.Unlock()
48-
time.Sleep(100 * time.Millisecond)
45+
time.Sleep(intervalToCheckBudgetAvailability)
4946
}
5047
}
5148

49+
// trySpendBudget tries to spend the specified amount of gas from the vault's available budget.
50+
// It returns true if the gas was successfully spent, or false if there was insufficient budget.
51+
func (o *GasVault) trySpendBudget(gas uint64) bool {
52+
o.mu.Lock()
53+
defer o.mu.Unlock()
54+
if gas <= o.gasBudgetAvailable {
55+
o.gasBudgetAvailable -= gas
56+
return true
57+
}
58+
return false
59+
}
60+
5261
// GetAvailableBudget returns the current available gas budget in the vault.
5362
func (o *GasVault) GetAvailableBudget() uint64 {
5463
o.mu.Lock()

cmd/loadtest/loadtest.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1346,8 +1346,9 @@ func loadTestTransaction(ctx context.Context, c *ethclient.Client, tops *bind.Tr
13461346
to = getRandomAddress()
13471347
}
13481348

1349+
const eoaTransferGasLimit = 21000
13491350
if tops.GasLimit == 0 {
1350-
tops.GasLimit = uint64(21000)
1351+
tops.GasLimit = uint64(eoaTransferGasLimit)
13511352
}
13521353

13531354
amount := ltp.SendAmount

0 commit comments

Comments
 (0)