Skip to content

Commit 30a053d

Browse files
committed
feat: new gas price algorithm for respondtotask
1 parent 7d8660b commit 30a053d

File tree

2 files changed

+17
-9
lines changed

2 files changed

+17
-9
lines changed

core/chainio/avs_writer.go

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import (
2121
)
2222

2323
const (
24-
gasBumpPercentage int = 10
24+
gasBumpPercentage int = 20
2525
)
2626

2727
type AvsWriter struct {
@@ -79,6 +79,7 @@ func NewAvsWriterFromConfig(baseConfig *config.BaseConfig, ecdsaConfig *config.E
7979
// Sends AggregatedResponse and waits for the receipt for three blocks, if not received
8080
// it will try again bumping the last tx gas price based on `CalculateGasPriceBump`
8181
// This process happens indefinitely until the transaction is included.
82+
// Note: If the rpc endpoints fail, the retry will stop, as it will infinitely try
8283
func (w *AvsWriter) SendAggregatedResponse(batchIdentifierHash [32]byte, batchMerkleRoot [32]byte, senderAddress [20]byte, nonSignerStakesAndSignature servicemanager.IBLSSignatureCheckerNonSignerStakesAndSignature, onRetry func()) (*types.Receipt, error) {
8384
txOpts := *w.Signer.GetTxOpts()
8485
txOpts.NoSend = true // simulate the transaction
@@ -102,17 +103,23 @@ func (w *AvsWriter) SendAggregatedResponse(batchIdentifierHash [32]byte, batchMe
102103
txOpts.NoSend = false
103104
txOpts.Nonce = txNonce
104105

105-
lastTxGasPrice := tx.GasPrice()
106106
i := 0
107107
sendTransaction := func() (*types.Receipt, error) {
108108
if i > 0 {
109109
onRetry()
110110
}
111111
i++
112112

113-
bumpedGasPrice := utils.CalculateGasPriceBump(lastTxGasPrice, gasBumpPercentage)
114-
lastTxGasPrice = bumpedGasPrice
115-
txOpts.GasPrice = bumpedGasPrice
113+
// get gas price
114+
gasPrice, err := w.Client.SuggestGasPrice(context.Background())
115+
if err != nil {
116+
// Retry with fallback
117+
gasPrice, err = w.ClientFallback.SuggestGasPrice(context.Background())
118+
if err != nil {
119+
return nil, fmt.Errorf("transaction simulation failed: %v", err)
120+
}
121+
}
122+
txOpts.GasPrice = utils.CalculateGasPriceBumpBasedOnRetry(gasPrice, gasBumpPercentage, i)
116123

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

core/utils/eth_client_utils.go

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

51-
// Very basic algorithm to calculate the gasPrice bump based on the currentGasPrice and percentage.
52-
// It adds a the percentage to the current gas price.
53-
func CalculateGasPriceBump(currentGasPrice *big.Int, percentage int) *big.Int {
54-
percentageBump := big.NewInt(int64(percentage))
51+
// Very basic algorithm to calculate the gasPrice bump based on the currentGasPrice a constant percentage and the retry number.
52+
// It adds a the percentage to the current gas price and a 5% * i, where i is the iteration number. That is:
53+
func CalculateGasPriceBumpBasedOnRetry(currentGasPrice *big.Int, percentage int, i int) *big.Int {
54+
retryPercentage := new(big.Int).Mul(big.NewInt(5), big.NewInt(int64(i)))
55+
percentageBump := new(big.Int).Add(big.NewInt(int64(percentage)), retryPercentage)
5556
bumpAmount := new(big.Int).Mul(currentGasPrice, percentageBump)
5657
bumpAmount = new(big.Int).Div(bumpAmount, big.NewInt(100))
5758
bumpedGasPrice := new(big.Int).Add(currentGasPrice, bumpAmount)

0 commit comments

Comments
 (0)