Skip to content

Commit

Permalink
update the comments
Browse files Browse the repository at this point in the history
  • Loading branch information
zzzckck committed Aug 26, 2024
1 parent 9ee38b9 commit 47b77bc
Showing 1 changed file with 13 additions and 11 deletions.
24 changes: 13 additions & 11 deletions cmd/faucet/faucet.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,10 +382,11 @@ func (f *faucet) apiHandler(w http.ResponseWriter, r *http.Request) {
Captcha string `json:"captcha"`
Symbol string `json:"symbol"`
}
// not sure if it is necessary or not, but it could help prevent some goroutine leakage
// not sure if it helps or not, but set a read deadline could help prevent resource leakage
// if user did not give response for too long, then the routine will be stuck.
conn.SetReadDeadline(time.Now().Add(5 * time.Minute))
if err = conn.ReadJSON(&msg); err != nil {
log.Warn("apiHandler", "ReadJSON err", err, "ip", ip)
log.Info("read json message failed", "err", err, "ip", ip)
return
}
if !*noauthFlag && !strings.HasPrefix(msg.URL, "https://twitter.com/") && !strings.HasPrefix(msg.URL, "https://www.facebook.com/") {
Expand Down Expand Up @@ -613,21 +614,21 @@ func (f *faucet) refresh(head *types.Header) error {
f.head, f.balance = head, balance
f.price, f.nonce = price, nonce
if len(f.reqs) == 0 {
log.Warn("refresh len(f.reqs) == 0", "f.nonce", f.nonce)
log.Debug("refresh len(f.reqs) == 0", "f.nonce", f.nonce)
f.lock.Unlock()
return nil
}
if f.reqs[0].Tx.Nonce() == f.nonce {
// if the next Tx failed to be included within a during(resendInterval), try to resend it
// with higher gasPrice it, as it would be discarded in the network.
// also resend extra following txs, as they could be discarded as well.
// if the next Tx failed to be included for a certain time(resendInterval), try to
// resend it with higher gasPrice, as it could be discarded in the network.
// Also resend extra following txs, as they could be discarded as well.
if time.Now().After(f.reqs[0].Time.Add(resendInterval)) {
for i, req := range f.reqs {
if i >= resendBatchSize {
break
}
prePrice := req.Tx.GasPrice()
// pump gas price 20% to replace the previous one.
// bump gas price 20% to replace the previous tx
newPrice := new(big.Int).Add(prePrice, new(big.Int).Div(prePrice, big.NewInt(5)))
if newPrice.Cmp(resendMaxGasPrice) >= 0 {
log.Info("resendMaxGasPrice reached", "newPrice", newPrice, "resendMaxGasPrice", resendMaxGasPrice, "nonce", req.Tx.Nonce())
Expand All @@ -650,13 +651,14 @@ func (f *faucet) refresh(head *types.Header) error {
}
}
}
// reset the reqs, if reqs[0] has larger nonce than next expected nonce.
// it is abnormal, could be caused by reorg?
// it is abnormal that reqs[0] has larger nonce than next expected nonce.
// could be caused by reorg? reset it
if f.reqs[0].Tx.Nonce() > f.nonce {
f.reqs = f.reqs[:0]
log.Warn("refresh, detect nonce gap", "f.nonce", f.nonce, "f.reqs[0].Tx.Nonce()", f.reqs[0].Tx.Nonce())
log.Warn("reset due to nonce gap", "f.nonce", f.nonce, "f.reqs[0].Tx.Nonce()", f.reqs[0].Tx.Nonce())
}
// remove the reqs if they have smaller nonce, which means it is no longer valid, either been accepted or replaced.
// remove the reqs if they have smaller nonce, which means it is no longer valid,
// either has been accepted or replaced.
for len(f.reqs) > 0 && f.reqs[0].Tx.Nonce() < f.nonce {
f.reqs = f.reqs[1:]
}
Expand Down

0 comments on commit 47b77bc

Please sign in to comment.