Skip to content

Commit db51d7a

Browse files
committed
Use a separate context for TransactionReceipt call
Using the same context leads to connection reset on context timeout and the same connection is used by subscriptions.
1 parent 4b0032f commit db51d7a

1 file changed

Lines changed: 26 additions & 11 deletions

File tree

pkg/chain/ethereum/ethutil/mine_waiter.go

Lines changed: 26 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ package ethutil
22

33
import (
44
"context"
5-
"time"
65
"math/big"
6+
"time"
77

88
"github.com/ethereum/go-ethereum/accounts/abi/bind"
99
"github.com/ethereum/go-ethereum/core/types"
@@ -12,9 +12,9 @@ import (
1212
// MiningWaiter allows to block the execution until the given transaction is
1313
// mined.
1414
type MiningWaiter struct {
15-
backend bind.DeployBackend
15+
backend bind.DeployBackend
1616
checkInterval time.Duration
17-
maxGasPrice *big.Int
17+
maxGasPrice *big.Int
1818
}
1919

2020
// NewMiningWaiter creates a new MiningWaiter instance for the provided
@@ -35,12 +35,27 @@ func NewMiningWaiter(
3535
// hash is mined. Execution is blocked until the transaction is mined or until
3636
// the given timeout passes.
3737
func (mw *MiningWaiter) WaitMined(
38-
timeout time.Duration,
38+
timeout time.Duration,
3939
tx *types.Transaction,
4040
) (*types.Receipt, error) {
4141
ctx, cancel := context.WithTimeout(context.Background(), timeout)
4242
defer cancel()
43-
return bind.WaitMined(ctx, mw.backend, tx)
43+
44+
queryTicker := time.NewTicker(time.Second)
45+
defer queryTicker.Stop()
46+
47+
for {
48+
receipt, _ := mw.backend.TransactionReceipt(context.TODO(), tx.Hash())
49+
if receipt != nil {
50+
return receipt, nil
51+
}
52+
53+
select {
54+
case <-ctx.Done():
55+
return nil, ctx.Err()
56+
case <-queryTicker.C:
57+
}
58+
}
4459
}
4560

4661
type ResubmitTransactionFn func(gasPrice *big.Int) (*types.Transaction, error)
@@ -59,7 +74,7 @@ func (mw MiningWaiter) ForceMining(
5974
err,
6075
)
6176
}
62-
77+
6378
// transaction mined, we are good
6479
if receipt != nil {
6580
logger.Infof(
@@ -75,8 +90,8 @@ func (mw MiningWaiter) ForceMining(
7590
gasPrice := transaction.GasPrice()
7691
twentyPercent := new(big.Int).Div(gasPrice, big.NewInt(5))
7792
gasPrice = new(big.Int).Add(gasPrice, twentyPercent)
78-
79-
// transaction not yet mined but we reached the maximum allowed gas
93+
94+
// transaction not yet mined but we reached the maximum allowed gas
8095
// price; giving up, we need to wait for the last submitted TX to be
8196
// mined
8297
if gasPrice.Cmp(mw.maxGasPrice) > 0 {
@@ -87,15 +102,15 @@ func (mw MiningWaiter) ForceMining(
87102
// transaction not yet mined and we can still increase gas price
88103
// resubmitting transaction with 20% higher gas price
89104
logger.Infof(
90-
"resubmitting previous transaction [%v] with higher gas price [%v]",
105+
"resubmitting previous transaction [%v] with a higher gas price [%v]",
91106
transaction.Hash().TerminalString(),
92107
gasPrice,
93108
)
94-
109+
95110
transaction, err = resubmitFn(gasPrice)
96111
if err != nil {
97112
logger.Errorf("failed resubmitting TX with higher gas price: [%v]", err)
98113
return
99114
}
100115
}
101-
}
116+
}

0 commit comments

Comments
 (0)