Skip to content

Add nonce tracker to transactions controller - #1147

Merged
adonesky1 merged 8 commits into
mainfrom
add-nonce-tracker-to-transactions-controller
Apr 11, 2023
Merged

Add nonce tracker to transactions controller#1147
adonesky1 merged 8 commits into
mainfrom
add-nonce-tracker-to-transactions-controller

Conversation

@adonesky1

@adonesky1adonesky1 commented Mar 28, 2023

Copy link
Copy Markdown
Contributor

Resolves: #1070

  • ADDED
    • Adds NonceTracker package to transaction approval flow to manage transaction nonces. This functionality was previously handled by middleware in `web3-provider-engine which is slated to be removed. #123

@socket-security

socket-securityBot commented Mar 28, 2023

Copy link
Copy Markdown

New dependency changes detected. Learn more about Socket for GitHub ↗︎


👍 No new dependency issues detected in pull request

Bot Commands

To ignore an alert, reply with a comment starting with @SocketSecurity ignore followed by a space separated list of package-name@version specifiers. e.g. @SocketSecurity ignore foo@1.0.0 bar@* or ignore all packages with @SocketSecurity ignore-all

    Pull request alert summary
    IssueStatus
    Install scripts✅ 0 issues
    Native code✅ 0 issues
    Bin script shell injection✅ 0 issues
    Unresolved require✅ 0 issues
    Invalid package.json✅ 0 issues
    HTTP dependency✅ 0 issues
    Git dependency✅ 0 issues
    Potential typo squat✅ 0 issues
    Known Malware✅ 0 issues
    Telemetry✅ 0 issues
    Protestware/Troll package✅ 0 issues

    📊 Modified Dependency Overview:

    ➕ Added PackageCapability Access+/- Transitive CountPublisher
    nonce-tracker@1.1.0None+5rekmarks

    @adonesky1
    adonesky1force-pushed the add-nonce-tracker-to-transactions-controller branch from 0e048e4 to f503244CompareMarch 30, 2023 17:49
    @adonesky1

    Copy link
    Copy Markdown
    ContributorAuthor

    @metamaskbot publish-preview

    @github-actions

    Copy link
    Copy Markdown
    Contributor

    Preview builds have been published. See these instructions for more information about preview builds.

    Expand for full list of packages and versions.
    {
    "@metamask/address-book-controller": "2.0.0-preview.a96d64c",
    "@metamask/announcement-controller": "3.0.0-preview.a96d64c",
    "@metamask/approval-controller": "2.0.0-preview.a96d64c",
    "@metamask/assets-controllers": "5.0.0-preview.a96d64c",
    "@metamask/base-controller": "2.0.0-preview.a96d64c",
    "@metamask/composable-controller": "2.0.0-preview.a96d64c",
    "@metamask/controller-utils": "3.0.0-preview.a96d64c",
    "@metamask/ens-controller": "2.0.0-preview.a96d64c",
    "@metamask/gas-fee-controller": "4.0.0-preview.a96d64c",
    "@metamask/keyring-controller": "4.0.0-preview.a96d64c",
    "@metamask/message-manager": "2.0.0-preview.a96d64c",
    "@metamask/network-controller": "5.0.0-preview.a96d64c",
    "@metamask/notification-controller": "2.0.0-preview.a96d64c",
    "@metamask/permission-controller": "3.0.0-preview.a96d64c",
    "@metamask/phishing-controller": "3.0.0-preview.a96d64c",
    "@metamask/preferences-controller": "2.1.0-preview.a96d64c",
    "@metamask/rate-limit-controller": "2.0.0-preview.a96d64c",
    "@metamask/subject-metadata-controller": "2.0.0-preview.a96d64c",
    "@metamask/transaction-controller": "4.0.0-preview.a96d64c"
    }
    

    @adonesky1
    adonesky1force-pushed the add-nonce-tracker-to-transactions-controller branch from a96d64c to 1f288cdCompareApril 3, 2023 22:25
    @adonesky1

    Copy link
    Copy Markdown
    ContributorAuthor

    @metamaskbot publish-preview

    @github-actions

    Copy link
    Copy Markdown
    Contributor

    Preview builds have been published. See these instructions for more information about preview builds.

    Expand for full list of packages and versions.
    {
    "@metamask/address-book-controller": "2.0.0-preview.1f288cd",
    "@metamask/announcement-controller": "3.0.0-preview.1f288cd",
    "@metamask/approval-controller": "2.0.0-preview.1f288cd",
    "@metamask/assets-controllers": "5.0.1-preview.1f288cd",
    "@metamask/base-controller": "2.0.0-preview.1f288cd",
    "@metamask/composable-controller": "2.0.0-preview.1f288cd",
    "@metamask/controller-utils": "3.1.0-preview.1f288cd",
    "@metamask/ens-controller": "3.0.0-preview.1f288cd",
    "@metamask/gas-fee-controller": "4.0.1-preview.1f288cd",
    "@metamask/keyring-controller": "4.0.0-preview.1f288cd",
    "@metamask/message-manager": "2.1.0-preview.1f288cd",
    "@metamask/network-controller": "6.0.0-preview.1f288cd",
    "@metamask/notification-controller": "2.0.0-preview.1f288cd",
    "@metamask/permission-controller": "3.1.0-preview.1f288cd",
    "@metamask/phishing-controller": "3.0.0-preview.1f288cd",
    "@metamask/preferences-controller": "3.0.0-preview.1f288cd",
    "@metamask/rate-limit-controller": "2.0.0-preview.1f288cd",
    "@metamask/subject-metadata-controller": "2.0.0-preview.1f288cd",
    "@metamask/transaction-controller": "4.0.1-preview.1f288cd"
    }
    

    Comment threadpackages/transaction-controller/src/TransactionController.ts Outdated
    Comment threadpackages/transaction-controller/src/TransactionController.ts Outdated
    Comment threadpackages/transaction-controller/src/TransactionController.ts Outdated
    Comment threadpackages/transaction-controller/src/TransactionController.ts Outdated

    @GudahttGudahtt left a comment

    Copy link
    Copy Markdown
    Member

    Choose a reason for hiding this comment

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

    Great work! Left some minor comments but generally this all looks correct.

    @adonesky1
    adonesky1force-pushed the add-nonce-tracker-to-transactions-controller branch from 1f288cd to 58737d6CompareApril 5, 2023 16:21
    @adonesky1
    adonesky1 marked this pull request as ready for review April 5, 2023 18:16
    @adonesky1
    adonesky1 requested a review from a team as a code ownerApril 5, 2023 18:16
    @adonesky1
    adonesky1 requested a review from GudahttApril 5, 2023 18:16
    Comment threadpackages/transaction-controller/src/TransactionController.ts Outdated
    Comment threadpackages/transaction-controller/src/TransactionController.ts Outdated
    Comment threadpackages/transaction-controller/src/TransactionController.ts Outdated
    @adonesky1
    adonesky1force-pushed the add-nonce-tracker-to-transactions-controller branch 3 times, most recently from 29f8fb4 to feda1b9CompareApril 6, 2023 17:59
    @adonesky1
    adonesky1 requested a review from mcmireApril 6, 2023 17:59
    @adonesky1
    adonesky1force-pushed the add-nonce-tracker-to-transactions-controller branch from feda1b9 to f32cf8bCompareApril 6, 2023 18:11
    Comment threadpackages/transaction-controller/src/TransactionController.ts Outdated
    @adonesky1
    adonesky1force-pushed the add-nonce-tracker-to-transactions-controller branch from f32cf8b to 28a00daCompareApril 6, 2023 18:14
    Comment threadpackages/transaction-controller/src/TransactionController.ts Outdated
    @adonesky1
    adonesky1force-pushed the add-nonce-tracker-to-transactions-controller branch from 28a00da to 4abd631CompareApril 6, 2023 18:30
    @adonesky1
    adonesky1force-pushed the add-nonce-tracker-to-transactions-controller branch from 4abd631 to f32cf8bCompareApril 6, 2023 18:53
    @adonesky1
    adonesky1force-pushed the add-nonce-tracker-to-transactions-controller branch from f32cf8b to 2138d65CompareApril 6, 2023 18:54
    * @param transactions - Array of transactionMeta objects that have been prefiltered.
    * @returns Array of transactions formatted for the nonce tracker.
    */
    export function getAndFormatTransactionsForNonceTracker(

    Copy link
    Copy Markdown
    ContributorAuthor

    Choose a reason for hiding this comment

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

    I opted to make this a utility function rather than a private method on the TransactionsController just so testing it made more sense... happy to move it to the controller if we think that makes more sense.

    Comment threadpackages/transaction-controller/src/TransactionController.ts
    @Gudahtt

    Copy link
    Copy Markdown
    Member

    Looks great! CI is failing due to a drop in test coverage though.

    @adonesky1

    Copy link
    Copy Markdown
    ContributorAuthor

    CI is failing due to a drop in test coverage though.

    Yeah been mainly focusing on validating it works in mobile and still fighting the env. Also not totally sure how best to target these lines:
    Screenshot 2023-04-06 at 5 15 22 PM

    @Gudahtt

    Copy link
    Copy Markdown
    Member

    Ah, for those lines I'd suggest we use a real nonce tracker instead of a mock one. We can mock the provider passed into the nonce tracker instead. Then those lines will be tested as part of the tests you've already written

    type BlockTracker = any;

    type BlockTrackerProxy = SwappableProxy<BlockTracker>;
    export type BlockTrackerProxy = SwappableProxy<BlockTracker>;

    Copy link
    Copy Markdown
    Contributor

    Choose a reason for hiding this comment

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

    As this is now an exported type, doesn't it warrant constraining BlockTracker to something more specific than any?

    Copy link
    Copy Markdown
    ContributorAuthor

    Choose a reason for hiding this comment

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

    Perhaps! But I don't believe that's appropriately scoped to this PR. As you can see directly above this we are doing the same with Provider:

    type Provider = any;
    export type ProviderProxy = SwappableProxy<Provider>;
    

    @legobeatlegobeatApr 10, 2023

    Copy link
    Copy Markdown
    Contributor

    Choose a reason for hiding this comment

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

    How does the type of Provider reduce the scope of this PR?

    The impacts of the type being any is different for public and private interfaces. A type which is acceptable as any internally may require constraining before being appropriate for exposure.

    Copy link
    Copy Markdown
    ContributorAuthor

    Choose a reason for hiding this comment

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

    How does the type of Provider reduce the scope of this PR?

    On that point I was just noting that we're already exporting the ProviderProxy object with a Provider type parameter that is also typed as any.

    Copy link
    Copy Markdown
    ContributorAuthor

    Choose a reason for hiding this comment

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

    Currently the BlockTracker is set here, where it is a property on the Provider. I believe there is an intention to follow back here and improve typing soon. cc @Gudahtt@mcmire

    If we think it makes sense to add the types for both Provider and BlockTracker in this PR I'm open to that.

    Copy link
    Copy Markdown
    Collaborator

    Choose a reason for hiding this comment

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

    Yes, that's right. We're using any at the moment because web3-provider-engine doesn't provide types at all, and we're about to replace it anyway (see #1116). Once the replacement is complete, then we can just use the correct types from @metamask/eth-json-rpc-provider and eth-block-tracker.

    @adonesky1

    Copy link
    Copy Markdown
    ContributorAuthor

    Ah, for those lines I'd suggest we use a real nonce tracker instead of a mock one. We can mock the provider passed into the nonce tracker instead. Then those lines will be tested as part of the tests you've already written

    done here: 9f2662e

    @Gudahtt

    Comment on lines +677 to +680
    let nonceToUse = nonce;
    // if a nonce already exists on the transactionMeta it means this is a speedup or cancel transaction
    // so we want to reuse that nonce and hope that it beats the previous attempt to chain. Otherwise use a new locked nonce
    if (!nonceToUse) {

    Copy link
    Copy Markdown
    ContributorAuthor

    Choose a reason for hiding this comment

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

    @Gudahtt I'm realizing that this isn't actually necessary in the current implementation since in this TransactionController we don't actually re-process/re-approve speedup and cancel transactions in this method as we do in the extension TransactionController. Rather we just modify the transaction and re-broadcast it directly in the speedup/cancel method, for instance here.

    That said it may make sense to leave this logic to ensure correct nonce management in future uses of this method. And as it is currently there is no issue keeping this logic since nonceToUse should always be undefined on line 680.

    Comment threadpackages/transaction-controller/src/TransactionController.ts Outdated
    Comment threadpackages/transaction-controller/src/TransactionController.test.ts Outdated
    Gudahtt
    Gudahtt previously approved these changes Apr 11, 2023

    @GudahttGudahtt left a comment

    Copy link
    Copy Markdown
    Member

    Choose a reason for hiding this comment

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

    LGTM!

    @adonesky1
    adonesky1force-pushed the add-nonce-tracker-to-transactions-controller branch from a913cf4 to c2961a4CompareApril 11, 2023 15:33
    @adonesky1
    adonesky1 merged commit e64e181 into mainApr 11, 2023
    @adonesky1
    adonesky1 deleted the add-nonce-tracker-to-transactions-controller branch April 11, 2023 15:57
    @legobeatlegobeat mentioned this pull request Apr 25, 2023
    Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

    Labels

    None yet

    Projects

    None yet

    Development

    Successfully merging this pull request may close these issues.

    Migrate nonce-tracking from web3-provider-engine to transaction controller

    4 participants

    @adonesky1@Gudahtt@mcmire@legobeat