Skip to content

Commit

Permalink
Fix nil pointer exception when increasing gas price (0xPolygonHermez#…
Browse files Browse the repository at this point in the history
  • Loading branch information
arnaubennassar authored Oct 10, 2022
1 parent bec6a92 commit abf4e2e
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 6 deletions.
19 changes: 13 additions & 6 deletions ethtxmanager/ethtxmanager.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,12 +57,11 @@ func (c *Client) SequenceBatches(sequences []ethmanTypes.Sequence) {
if err != nil {
attempts++
if strings.Contains(err.Error(), "out of gas") {
gas = (tx.Gas() * (oneHundred + c.cfg.PercentageToIncreaseGasLimit)) / oneHundred
gas = increaseGasLimit(tx.Gas(), c.cfg.PercentageToIncreaseGasLimit)
log.Infof("out of gas with %d, retrying with %d", tx.Gas(), gas)
continue
} else if strings.Contains(err.Error(), "timeout has been reached") {
gasPrice.Mul(tx.GasPrice(), new(big.Int).SetUint64(uint64(oneHundred)+c.cfg.PercentageToIncreaseGasPrice))
gasPrice.Div(gasPrice, big.NewInt(oneHundred))
gasPrice = increaseGasPrice(tx.GasPrice(), c.cfg.PercentageToIncreaseGasPrice)
log.Infof("tx %s reached timeout, retrying with gas price = %d", tx.Hash(), gasPrice)
continue
}
Expand Down Expand Up @@ -101,12 +100,11 @@ func (c *Client) VerifyBatch(batchNum uint64, resGetProof *pb.GetProofResponse)
if err != nil {
attempts++
if strings.Contains(err.Error(), "out of gas") {
gas = (tx.Gas() * (oneHundred + c.cfg.PercentageToIncreaseGasLimit)) / oneHundred
gas = increaseGasLimit(tx.Gas(), c.cfg.PercentageToIncreaseGasLimit)
log.Infof("out of gas with %d, retrying with %d", tx.Gas(), gas)
continue
} else if strings.Contains(err.Error(), "timeout has been reached") {
gasPrice.Mul(tx.GasPrice(), new(big.Int).SetUint64(uint64(oneHundred)+c.cfg.PercentageToIncreaseGasPrice))
gasPrice.Div(gasPrice, big.NewInt(oneHundred))
gasPrice = increaseGasPrice(tx.GasPrice(), c.cfg.PercentageToIncreaseGasPrice)
log.Infof("tx %s reached timeout, retrying with gas price = %d", tx.Hash(), gasPrice)
continue
}
Expand All @@ -117,3 +115,12 @@ func (c *Client) VerifyBatch(batchNum uint64, resGetProof *pb.GetProofResponse)
}
}
}

func increaseGasPrice(currentGasPrice *big.Int, percentageIncrease uint64) *big.Int {
gasPrice := big.NewInt(0).Mul(currentGasPrice, new(big.Int).SetUint64(uint64(oneHundred)+percentageIncrease))
return gasPrice.Div(gasPrice, big.NewInt(oneHundred))
}

func increaseGasLimit(currentGasLimit uint64, percentageIncrease uint64) uint64 {
return currentGasLimit * (oneHundred + percentageIncrease) / oneHundred
}
18 changes: 18 additions & 0 deletions ethtxmanager/ethtxmanager_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
package ethtxmanager

import (
"math/big"
"testing"

"github.com/stretchr/testify/assert"
)

func TestIncreaseGasPrice(t *testing.T) {
actual := increaseGasPrice(big.NewInt(100), 1)
assert.Equal(t, big.NewInt(101), actual)
}

func TestIncreaseGasLimit(t *testing.T) {
actual := increaseGasLimit(100, 1)
assert.Equal(t, uint64(101), actual)
}

0 comments on commit abf4e2e

Please sign in to comment.