Skip to content

Commit 9036c7c

Browse files
committed
Do not resubmit TX if the original gas price was higher than allowed
1 parent 6b92fa2 commit 9036c7c

2 files changed

Lines changed: 37 additions & 0 deletions

File tree

pkg/chain/ethereum/ethutil/mine_waiter.go

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,16 @@ func (mw MiningWaiter) ForceMining(
8282
originalTransaction *types.Transaction,
8383
resubmitFn ResubmitTransactionFn,
8484
) {
85+
// if the original transaction's gas price was higher or equal the max
86+
// allowed we do nothing; we need to wait for it to be mined
87+
if originalTransaction.GasPrice().Cmp(mw.maxGasPrice) >= 0 {
88+
logger.Infof(
89+
"original transaction gas price is higher than the max allowed; " +
90+
"skipping resubmissions",
91+
)
92+
return
93+
}
94+
8595
transaction := originalTransaction
8696
for {
8797
receipt, err := mw.WaitMined(mw.checkInterval, transaction)

pkg/chain/ethereum/ethutil/mine_waiter_test.go

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,6 +168,33 @@ func TestForceMining_MaxAllowedPriceReached(t *testing.T) {
168168
}
169169
}
170170

171+
func TestForceMining_OriginalPriceHigherThanMaxAllowed(t *testing.T) {
172+
// original transaction was priced at 46 Gwei, the maximum allowed gas price
173+
// is 45 Gwei
174+
originalTransaction := createTransaction(big.NewInt(46000000000))
175+
176+
mockBackend := &mockDeployBackend{}
177+
178+
var resubmissionGasPrices []*big.Int
179+
180+
resubmitFn := func(gasPrice *big.Int) (*types.Transaction, error) {
181+
resubmissionGasPrices = append(resubmissionGasPrices, gasPrice)
182+
// not setting mockBackend.receipt, mining takes a very long time
183+
return createTransaction(gasPrice), nil
184+
}
185+
186+
waiter := NewMiningWaiter(mockBackend, checkInterval, maxGasPrice)
187+
waiter.ForceMining(
188+
originalTransaction,
189+
resubmitFn,
190+
)
191+
192+
resubmissionCount := len(resubmissionGasPrices)
193+
if resubmissionCount != 0 {
194+
t.Fatalf("expected no resubmissions; has: [%v]", resubmissionCount)
195+
}
196+
}
197+
171198
func createTransaction(gasPrice *big.Int) *types.Transaction {
172199
return types.NewTransaction(
173200
10, // nonce

0 commit comments

Comments
 (0)