Skip to content

Avoid initiating new attempts if the gas conditions persist unchanged#571

Merged
GabrielMartinezRodriguez merged 8 commits intomasterfrom
gabriel/avoid-create-new-attempts
Apr 13, 2025
Merged

Avoid initiating new attempts if the gas conditions persist unchanged#571
GabrielMartinezRodriguez merged 8 commits intomasterfrom
gabriel/avoid-create-new-attempts

Conversation

@GabrielMartinezRodriguez
Copy link
Contributor

@GabrielMartinezRodriguez GabrielMartinezRodriguez commented Mar 27, 2025

Description

On Friday, the randomness service crashed because the number of attempts grew exponentially. The issue that caused this behavior has already been addressed. However, we also noticed that creating a new attempt with more gas every time a transaction gets stuck in a block is unnecessary—and can even be problematic—if the gas conditions are still acceptable for the previous attempt. Instead of creating a new attempt with higher gas, we now retry the same attempt

  • Include all relevant context (but no need to repeat the issue's content).
  • Draw attention to new, noteworthy & unintuitive elements.
Toggle Checklist

Checklist

Basics

  • B1. I have applied the proper label & proper branch name (e.g. norswap/build-system-caching).
  • B2. This PR is not so big that it should be split & addresses only one concern.
  • B3. The PR targets the lowest branch it can (ideally master).

Reminder: PR review guidelines

Correctness

  • C1. Builds and passes tests.
  • C2. The code is properly parameterized & compatible with different environments (e.g. local,
    testnet, mainnet, standalone wallet, ...).
  • C3. I have manually tested my changes & connected features.

< INDICATE BROWSER, DEMO APP & OTHER ENV DETAILS USED FOR TESTING HERE >

< INDICATE TESTED SCENARIOS (USER INTERFACE INTERACTION, CODE FLOWS) HERE >

  • C4. I have performed a thorough self-review of my code after submitting the PR,
    and have updated the code & comments accordingly.

Architecture & Documentation

  • D1. I made it easy to reason locally about the code, by (1) using proper abstraction boundaries,
    (2) commenting these boundaries correctly, (3) adding inline comments for context when needed.
  • D2. All public-facing APIs & meaningful (non-local) internal APIs are properly documented in code
    comments.
  • D3. If appropriate, the general architecture of the code is documented in a code comment or
    in a Markdown document.
  • D4. An appropriate Changeset has been generated (and committed) for changes that touch npm published packages (currently packages/core and packages/react), see here for more info.

@cloudflare-workers-and-pages
Copy link

cloudflare-workers-and-pages bot commented Mar 27, 2025

Deploying happychain with  Cloudflare Pages  Cloudflare Pages

Latest commit: 7a35565
Status:⚡️  Build in progress...

View logs

This was referenced Mar 27, 2025
@GabrielMartinezRodriguez GabrielMartinezRodriguez mentioned this pull request Mar 27, 2025
11 tasks
@GabrielMartinezRodriguez GabrielMartinezRodriguez marked this pull request as ready for review March 27, 2025 14:15
Copy link

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull Request Overview

A PR to improve the transaction monitoring flow by preventing unnecessary new attempts when the gas price is still competitive. Key changes include:

  • Adding a new method in TxMonitor to determine if a new attempt should be emitted based on gas price conditions.
  • Integrating a retry logic in TransactionSubmitter with detailed error handling for transaction resubmission.
  • Exposing GasPriceOracle fee parameters as public for easier access.

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
packages/txm/lib/TxMonitor.ts Adds a new helper function to decide whether a new attempt is needed and adjusts logging accordingly.
packages/txm/lib/TransactionSubmitter.ts Introduces new error types and a retryAttempt method to resubmit transactions with improved error handling.
packages/txm/lib/GasPriceOracle.ts Changes fee parameter properties to public, enabling external access.
Comments suppressed due to low confidence (3)

packages/txm/lib/TxMonitor.ts:219

  • [nitpick] Please add an inline comment explaining the rationale behind subtracting maxPriorityFeePerGas from maxFeePerGas to aid future maintainers.
if (attempt.maxFeePerGas - attempt.maxPriorityFeePerGas < expectedNextBaseFeePerGas) {

packages/txm/lib/GasPriceOracle.ts:27

  • [nitpick] Reconsider exposing fee-related fields as public; using getters could help prevent unintended external modifications.
public expectedNextBaseFeePerGas!: bigint

packages/txm/lib/TransactionSubmitter.ts:226

  • [nitpick] Consider adding more detailed logging or context for the 'nonce too low' error to facilitate debugging in production.
if (sendRawTransactionResult.error instanceof TransactionRejectedRpcError && sendRawTransactionResult.error.message.includes("nonce too low")) {

@GabrielMartinezRodriguez GabrielMartinezRodriguez force-pushed the gabriel/dynamic-priority-fee branch from c10757e to f9acbe6 Compare March 27, 2025 14:41
@GabrielMartinezRodriguez GabrielMartinezRodriguez force-pushed the gabriel/avoid-create-new-attempts branch from 3cd874c to 87ef67e Compare March 27, 2025 14:41
@GabrielMartinezRodriguez GabrielMartinezRodriguez force-pushed the gabriel/dynamic-priority-fee branch from f9acbe6 to 22fd6dd Compare March 27, 2025 14:51
@GabrielMartinezRodriguez GabrielMartinezRodriguez force-pushed the gabriel/avoid-create-new-attempts branch from 87ef67e to 591e8b7 Compare March 27, 2025 14:51
@GabrielMartinezRodriguez GabrielMartinezRodriguez changed the title feat(txm): avoid create new attempts if not needed Avoid create new attempts if not needed Mar 27, 2025
@GabrielMartinezRodriguez GabrielMartinezRodriguez changed the title Avoid create new attempts if not needed Avoid initiating new attempts if the gas conditions persist unchanged Mar 27, 2025
@GabrielMartinezRodriguez GabrielMartinezRodriguez added the reviewing-1 Ready for, or undergoing first-line review label Mar 27, 2025
@GabrielMartinezRodriguez GabrielMartinezRodriguez force-pushed the gabriel/dynamic-priority-fee branch from 22fd6dd to 6e7f328 Compare March 27, 2025 15:39
@GabrielMartinezRodriguez GabrielMartinezRodriguez force-pushed the gabriel/avoid-create-new-attempts branch from 591e8b7 to 92fcf3f Compare March 27, 2025 15:39
return ok(undefined)
}

async retryAttempt(transaction: Transaction, attempt: Attempt): Promise<RetryAttemptResult> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why cant we re-use/call the existing attemptSubmission() like handleRetryTransaction() does?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good question. Because attemptSubmission creates a new attempt, however here we want to retry the same attempt. We do this to avoid accumulating too many attempts unnecessarily, which could cause issues with the RPC, since later during monitoring we have to request the receipt for each of these attempts

Copy link
Collaborator

@norswap norswap Apr 8, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree this is a lot of duplicate logic, is there a way to refactor this in such a way that most of the logic is shared between both functions? Maybe a single function with additional parameters / flags?

As is it's pretty hard to reason about the differences between the two paths.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I refactored the code. Now there's a private function that implements all the common logic and takes a parameter to configure whether to save the new attempt, which was the main difference

@aodhgan aodhgan added question Something has to be cleared up after review and removed reviewing-1 Ready for, or undergoing first-line review labels Apr 1, 2025
@GabrielMartinezRodriguez GabrielMartinezRodriguez force-pushed the gabriel/dynamic-priority-fee branch from bdd1d5c to d5bfe00 Compare April 10, 2025 08:51
@GabrielMartinezRodriguez GabrielMartinezRodriguez force-pushed the gabriel/avoid-create-new-attempts branch from c0e9e96 to 709b75c Compare April 10, 2025 08:51
@GabrielMartinezRodriguez GabrielMartinezRodriguez force-pushed the gabriel/dynamic-priority-fee branch from d5bfe00 to 44f4247 Compare April 10, 2025 09:12
@GabrielMartinezRodriguez GabrielMartinezRodriguez force-pushed the gabriel/avoid-create-new-attempts branch from 709b75c to 538a0a1 Compare April 10, 2025 09:12
@GabrielMartinezRodriguez GabrielMartinezRodriguez force-pushed the gabriel/dynamic-priority-fee branch from 44f4247 to 1f2858b Compare April 10, 2025 09:56
@GabrielMartinezRodriguez GabrielMartinezRodriguez force-pushed the gabriel/avoid-create-new-attempts branch from 538a0a1 to 93e241c Compare April 10, 2025 09:56
@GabrielMartinezRodriguez GabrielMartinezRodriguez added reviewing-2 Ready for, or undergoing final review and removed updating Updating after review labels Apr 10, 2025
@norswap norswap added merge-blocked Ready to merge, waiting for downstack and removed reviewing-2 Ready for, or undergoing final review labels Apr 10, 2025
@GabrielMartinezRodriguez GabrielMartinezRodriguez force-pushed the gabriel/dynamic-priority-fee branch from 1f2858b to 1356e03 Compare April 13, 2025 21:30
@GabrielMartinezRodriguez GabrielMartinezRodriguez force-pushed the gabriel/avoid-create-new-attempts branch from 42bc0db to ceb064f Compare April 13, 2025 21:30
Base automatically changed from gabriel/dynamic-priority-fee to master April 13, 2025 21:35
@GabrielMartinezRodriguez GabrielMartinezRodriguez force-pushed the gabriel/avoid-create-new-attempts branch from ceb064f to 7a35565 Compare April 13, 2025 21:36
@GabrielMartinezRodriguez GabrielMartinezRodriguez merged commit af6dfd6 into master Apr 13, 2025
2 of 4 checks passed
@GabrielMartinezRodriguez GabrielMartinezRodriguez deleted the gabriel/avoid-create-new-attempts branch April 13, 2025 21:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merge-blocked Ready to merge, waiting for downstack

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants