use network nonce

This commit is contained in:
Felipe Andrade 2023-09-06 15:12:13 -07:00
parent b5cda060b0
commit 9c56cde783

@ -33,26 +33,10 @@ func (p *Provider) RoundTrip(ctx context.Context) {
return return
} }
var nonce uint64
p.txPool.M.Lock()
if p.txPool.Nonce == uint64(0) {
nonce, err = client.PendingNonceAt(ctx, p.walletConfig.Address)
if err != nil {
log.Error("cant get nounce",
"provider", p.name,
"err", err)
p.txPool.M.Unlock()
return
}
p.txPool.Nonce = nonce
} else {
p.txPool.Nonce++
nonce = p.txPool.Nonce
}
p.txPool.M.Unlock()
txHash := common.Hash{} txHash := common.Hash{}
attempt := 0 attempt := 0
nonce := uint64(0)
// used for timeout // used for timeout
firstAttemptAt := time.Now() firstAttemptAt := time.Now()
// used for actual round trip time (disregard retry time) // used for actual round trip time (disregard retry time)
@ -68,6 +52,14 @@ func (p *Provider) RoundTrip(ctx context.Context) {
} }
} }
nonce, err = client.PendingNonceAt(ctx, p.walletConfig.Address)
if err != nil {
log.Error("cant get nounce",
"provider", p.name,
"err", err)
return
}
tx := p.createTx(nonce) tx := p.createTx(nonce)
txHash = tx.Hash() txHash = tx.Hash()
@ -79,7 +71,6 @@ func (p *Provider) RoundTrip(ctx context.Context) {
"err", err) "err", err)
return return
} }
txHash = signedTx.Hash() txHash = signedTx.Hash()
roundTripStartedAt = time.Now() roundTripStartedAt = time.Now()
@ -88,14 +79,15 @@ func (p *Provider) RoundTrip(ctx context.Context) {
if err.Error() == txpool.ErrAlreadyKnown.Error() || if err.Error() == txpool.ErrAlreadyKnown.Error() ||
err.Error() == txpool.ErrReplaceUnderpriced.Error() || err.Error() == txpool.ErrReplaceUnderpriced.Error() ||
err.Error() == core.ErrNonceTooLow.Error() { err.Error() == core.ErrNonceTooLow.Error() {
if time.Since(firstAttemptAt) >= time.Duration(p.config.SendTransactionRetryTimeout) { if time.Since(firstAttemptAt) >= time.Duration(p.config.SendTransactionRetryTimeout) {
log.Error("send transaction timed out (known already)", log.Error("send transaction timed out (known already)",
"provider", p.name, "provider", p.name,
"hash", txHash.Hex(), "hash", txHash.Hex(),
"nonce", nonce,
"elapsed", time.Since(firstAttemptAt), "elapsed", time.Since(firstAttemptAt),
"attempt", attempt, "attempt", attempt)
"nonce", nonce) metrics.RecordErrorDetails(p.name, "ethclient.SendTransaction", err)
metrics.RecordError(p.name, "ethclient.SendTransaction.nonce")
return return
} }
log.Warn("tx already known, incrementing nonce and trying again", log.Warn("tx already known, incrementing nonce and trying again",
@ -118,6 +110,7 @@ func (p *Provider) RoundTrip(ctx context.Context) {
} else { } else {
log.Error("cant send transaction", log.Error("cant send transaction",
"provider", p.name, "provider", p.name,
"nonce", nonce,
"err", err) "err", err)
metrics.RecordErrorDetails(p.name, "ethclient.SendTransaction", err) metrics.RecordErrorDetails(p.name, "ethclient.SendTransaction", err)
return return
@ -141,6 +134,7 @@ func (p *Provider) RoundTrip(ctx context.Context) {
SentAt: sentAt, SentAt: sentAt,
SeenBy: make(map[string]time.Time), SeenBy: make(map[string]time.Time),
} }
p.txPool.LastSend = sentAt
p.txPool.M.Unlock() p.txPool.M.Unlock()
var receipt *types.Receipt var receipt *types.Receipt
@ -150,6 +144,7 @@ func (p *Provider) RoundTrip(ctx context.Context) {
log.Error("receipt retrieval timed out", log.Error("receipt retrieval timed out",
"provider", p.name, "provider", p.name,
"hash", txHash, "hash", txHash,
"nonce", nonce,
"elapsed", time.Since(sentAt)) "elapsed", time.Since(sentAt))
return return
} }
@ -157,6 +152,8 @@ func (p *Provider) RoundTrip(ctx context.Context) {
if attempt%10 == 0 { if attempt%10 == 0 {
log.Debug("checking for receipt...", log.Debug("checking for receipt...",
"provider", p.name, "provider", p.name,
"hash", txHash,
"nonce", nonce,
"attempt", attempt, "attempt", attempt,
"elapsed", time.Since(sentAt)) "elapsed", time.Since(sentAt))
} }
@ -165,6 +162,7 @@ func (p *Provider) RoundTrip(ctx context.Context) {
log.Error("cant get receipt for transaction", log.Error("cant get receipt for transaction",
"provider", p.name, "provider", p.name,
"hash", txHash.Hex(), "hash", txHash.Hex(),
"nonce", nonce,
"err", err) "err", err)
return return
} }
@ -178,6 +176,7 @@ func (p *Provider) RoundTrip(ctx context.Context) {
log.Info("got transaction receipt", log.Info("got transaction receipt",
"hash", txHash.Hex(), "hash", txHash.Hex(),
"nonce", nonce,
"roundTripLatency", roundTripLatency, "roundTripLatency", roundTripLatency,
"provider", p.name, "provider", p.name,
"blockNumber", receipt.BlockNumber, "blockNumber", receipt.BlockNumber,