Skip to content

Commit 3258209

Browse files
committed
refactor: defined constants and better comments
1 parent 2b25d45 commit 3258209

File tree

3 files changed

+21
-19
lines changed

3 files changed

+21
-19
lines changed

core/chainio/avs_writer.go

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,11 @@ import (
2121
)
2222

2323
const (
24-
GasBumpPercentage int = 20
25-
// wait as much as 3 blocks time for the receipt
24+
// How much to bump every retry (constant)
25+
GasBaseBumpPercentage int = 20
26+
// An extra percentage to bump every retry i*5 (linear)
27+
GasBumpIncrementalBumpPercentage int = 5
28+
// Wait as much as 3 blocks time for the receipt
2629
SendAggregateResponseReceiptTimeout time.Duration = time.Second * 36
2730
)
2831

@@ -82,9 +85,9 @@ func NewAvsWriterFromConfig(baseConfig *config.BaseConfig, ecdsaConfig *config.E
8285
// it will try again bumping the last tx gas price based on `CalculateGasPriceBump`
8386
// This process happens indefinitely until the transaction is included.
8487
//
85-
// Note: If the rpc endpoints fail, the retry will stop it returning a permanent error.
86-
// This is because the retries are infinite and we want to prevent increasing the time between them too much as it is exponential.
87-
// And we might also if the rpc is down for a good period of time, we might fall into an infinite waiting.
88+
// Note: If the rpc endpoints fail, the retry mechanism stops, returning a permanent error.
89+
// This is because retries are infinite and we want to prevent increasing the time between them too much as it is exponential.
90+
// And, if the rpc is down for a good period of time, we might fall into an infinite waiting.
8891
func (w *AvsWriter) SendAggregatedResponse(batchIdentifierHash [32]byte, batchMerkleRoot [32]byte, senderAddress [20]byte, nonSignerStakesAndSignature servicemanager.IBLSSignatureCheckerNonSignerStakesAndSignature, onRetry func()) (*types.Receipt, error) {
8992
txOpts := *w.Signer.GetTxOpts()
9093
txOpts.NoSend = true // simulate the transaction
@@ -124,7 +127,7 @@ func (w *AvsWriter) SendAggregatedResponse(batchIdentifierHash [32]byte, batchMe
124127
return nil, connection.PermanentError{Inner: err}
125128
}
126129
}
127-
txOpts.GasPrice = utils.CalculateGasPriceBumpBasedOnRetry(gasPrice, GasBumpPercentage, i)
130+
txOpts.GasPrice = utils.CalculateGasPriceBumpBasedOnRetry(gasPrice, GasBaseBumpPercentage, GasBumpIncrementalBumpPercentage, i)
128131

129132
w.logger.Infof("Sending ResponseToTask transaction with a gas price of %v", txOpts.GasPrice)
130133
err = w.checkRespondToTaskFeeLimit(tx, txOpts, batchIdentifierHash, senderAddress)

core/connection.go

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,11 +19,13 @@ func (e PermanentError) Is(err error) bool {
1919
return ok
2020
}
2121

22-
// Same as Retry only that the functionToRetry can return a value upon correct execution
22+
// Retries a given function in an exponential backoff manner and returns a value upon correct execution.
23+
// It will retry calling the function while it returns a non permanent error, until the max retries.
24+
// If maxTries == 0 then the retry function will run indefinitely until success or until a `PermanentError` is returned.
2325
func RetryWithData[T any](functionToRetry func() (*T, error), minDelay uint64, factor float64, maxTries uint64) (*T, error) {
2426
f := func() (*T, error) {
2527
val, err := functionToRetry()
26-
if perm, ok := err.(PermanentError); err != nil && ok {
28+
if perm, ok := err.(PermanentError); ok && err != nil {
2729
return nil, backoff.Permanent(perm.Inner)
2830
}
2931
return val, err
@@ -46,15 +48,12 @@ func RetryWithData[T any](functionToRetry func() (*T, error), minDelay uint64, f
4648
}
4749

4850
// Retries a given function in an exponential backoff manner.
49-
// It will retry calling the function while it returns an error, until the max retries.
50-
// If maxTries == 0 then the retry function will run indefinitely until success
51-
// from the configuration are reached, or until a `PermanentError` is returned.
52-
// The function to be retried should return `PermanentError` when the condition for stop retrying
53-
// is met.
51+
// It will retry calling the function while it returns a non permanent error, until the max retries.
52+
// If maxTries == 0 then the retry function will run indefinitely.
5453
func Retry(functionToRetry func() error, minDelay uint64, factor float64, maxTries uint64) error {
5554
f := func() error {
5655
err := functionToRetry()
57-
if perm, ok := err.(PermanentError); err != nil && ok {
56+
if perm, ok := err.(PermanentError); ok && err != nil {
5857
return backoff.Permanent(perm.Inner)
5958
}
6059
return err

core/utils/eth_client_utils.go

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,13 +48,13 @@ func BytesToQuorumThresholdPercentages(quorumThresholdPercentagesBytes []byte) e
4848
}
4949

5050
// Simple algorithm to calculate the gasPrice bump based on:
51-
// the currentGasPrice, a base bump percentage, and the retry count.
51+
// the currentGasPrice, a base bump percentage, a retry percentage, and the retry count.
5252
// Formula: currentGasPrice + (currentGasPrice * (baseBumpPercentage + retryCount * incrementalRetryPercentage) / 100)
53-
func CalculateGasPriceBumpBasedOnRetry(currentGasPrice *big.Int, baseBumpPercentage int, retryCount int) *big.Int {
54-
// Incremental percentage increase for each retry attempt (5%)
55-
incrementalRetryPercentage := new(big.Int).Mul(big.NewInt(5), big.NewInt(int64(retryCount)))
53+
func CalculateGasPriceBumpBasedOnRetry(currentGasPrice *big.Int, baseBumpPercentage int, retryAttemptPercentage int, retryCount int) *big.Int {
54+
// Incremental percentage increase for each retry attempt (i*5%)
55+
incrementalRetryPercentage := new(big.Int).Mul(big.NewInt(int64(retryAttemptPercentage)), big.NewInt(int64(retryCount)))
5656

57-
// Total bump percentage: base bump + incremental percentage based on retry count
57+
// Total bump percentage: base bump + incremental retry percentage
5858
totalBumpPercentage := new(big.Int).Add(big.NewInt(int64(baseBumpPercentage)), incrementalRetryPercentage)
5959

6060
// Calculate the bump amount: currentGasPrice * totalBumpPercentage / 100

0 commit comments

Comments
 (0)