Skip to content

Commit 207339f

Browse files
committed
Improved mine waiter documentation
1 parent fd45595 commit 207339f

1 file changed

Lines changed: 25 additions & 7 deletions

File tree

pkg/chain/ethereum/ethutil/mine_waiter.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -10,15 +10,26 @@ import (
1010
)
1111

1212
// MiningWaiter allows to block the execution until the given transaction is
13-
// mined.
13+
// mined as well as monitor the transaction and bump up the gas price in case
14+
// it is not mined in the given timeout.
1415
type MiningWaiter struct {
1516
backend bind.DeployBackend
1617
checkInterval time.Duration
1718
maxGasPrice *big.Int
1819
}
1920

2021
// NewMiningWaiter creates a new MiningWaiter instance for the provided
21-
// client backend.
22+
// client backend. It accepts two parameters setting up monitoring rules of the
23+
// transaction mining status.
24+
//
25+
// Check interval is the time given for the transaction to be mined. If the
26+
// transaction is not mined within that time, the gas price is increased by
27+
// 20% and transaction is replaced with the one with a higher gas price.
28+
//
29+
// Max gas price specifies the maximum gas price the client is willing to pay
30+
// for the transaction to be mined. The offered transaction gas price can not
31+
// be higher than this value. If the maximum allowed gas price is reached, no
32+
// further resubmission attempts are performed.
2233
func NewMiningWaiter(
2334
backend bind.DeployBackend,
2435
checkInterval time.Duration,
@@ -58,8 +69,15 @@ func (mw *MiningWaiter) WaitMined(
5869
}
5970
}
6071

72+
// ResubmitTransactionFn implements the code for resubmitting the transaction
73+
// with the higher gas price. It should guarantee the same nonce is used for
74+
// transaction resubmission.
6175
type ResubmitTransactionFn func(gasPrice *big.Int) (*types.Transaction, error)
6276

77+
// ForceMining blocks until the transaction is mined and bumps up the gas price
78+
// by 20% in the intervals defined by MiningWaiter in case the transaction has
79+
// not been mined yet. It accepts the original transaction reference and the
80+
// function responsible for executing transaction resubmission.
6381
func (mw MiningWaiter) ForceMining(
6482
originalTransaction *types.Transaction,
6583
resubmitFn ResubmitTransactionFn,
@@ -86,7 +104,7 @@ func (mw MiningWaiter) ForceMining(
86104
return
87105
}
88106

89-
// add 20% to the previous gas price
107+
// transaction not yet mined, add 20% to the previous gas price
90108
gasPrice := transaction.GasPrice()
91109
twentyPercent := new(big.Int).Div(gasPrice, big.NewInt(5))
92110
gasPrice = new(big.Int).Add(gasPrice, twentyPercent)
@@ -99,17 +117,17 @@ func (mw MiningWaiter) ForceMining(
99117
return
100118
}
101119

102-
// transaction not yet mined and we can still increase gas price
103-
// resubmitting transaction with 20% higher gas price
120+
// transaction not yet mined and we are still under the maximum allowed
121+
// gas price; resubmitting transaction with 20% higher gas price
122+
// evaluated earlier
104123
logger.Infof(
105124
"resubmitting previous transaction [%v] with a higher gas price [%v]",
106125
transaction.Hash().TerminalString(),
107126
gasPrice,
108127
)
109-
110128
transaction, err = resubmitFn(gasPrice)
111129
if err != nil {
112-
logger.Errorf("failed resubmitting TX with higher gas price: [%v]", err)
130+
logger.Errorf("failed resubmitting TX with a higher gas price: [%v]", err)
113131
return
114132
}
115133
}

0 commit comments

Comments
 (0)