Skip to content
This repository has been archived by the owner on Nov 10, 2023. It is now read-only.

fix: reduce infura requests #3479

Merged
merged 6 commits into from
Feb 11, 2022
Merged

Conversation

iamacook
Copy link
Member

@iamacook iamacook commented Feb 10, 2022

What it solves

Unnecessary Infura requests

How this PR fixes it

Main investigation of our high-polled Infura requests showed that they are coming from our provider watcher (eth_chainId), Onboard (eth_getBalance) and WalletConnect (eth_blockNumber/eth_getBlockByNumber).

  • eth_blockNumber
  • eth_getBalance
  • eth_getBlockByNumber
  • eth_chainId

The base branch of this (the provider watcher cleanup) removed the 2 second interval polling for ethChainId and ethAccounts.

This removes the eth_blockNumber/eth_getBlockByNumber over-polling by reducing the blockPollingInterval of WalletConnect (via a custom WC module). This does not seem to have an adverse affect on WC, as it is apparently a remnant from legacy web3 configuration.

The overpolling of eth_getBalance stems from an issue somewhere in the depency tree of the balance stateSyncer in the Onboard's WC module. I'The modified WC module also has no balance stateSyncer. We can't retrieve the balance via Onboard, but this is never used in the Safe.

Links

  • Thread outlining the the high number of Infura requests.

How to test it

  • Connect to MM via Onboard:

    • eth_blockNumber/eth_getBlockByNumber - there should be no continuous polling per 4s for these
    • eth_chainId - there should be no continuous polling per 2s for this
  • Connect to WC via Onboard:

    • eth_getBalance - there should be no continuous polling per second for this

@iamacook iamacook self-assigned this Feb 10, 2022
@iamacook iamacook changed the base branch from dev to cleanup-provider-watcher February 10, 2022 15:21
@github-actions
Copy link

CLA Assistant Lite All Contributors have signed the CLA.

@github-actions
Copy link

github-actions bot commented Feb 10, 2022

ESLint Summary View Full Report

Annotations are provided inline on the Files Changed tab. You can also see all annotations that were generated on the annotations page.

Type Occurrences Fixable
Errors 0 0
Warnings 0 0
Ignored 0 N/A
  • Result: ✅ success
  • Annotations: 0 total

Report generated by eslint-plus-action

@github-actions
Copy link

Deployment links

🟠 Rinkeby Mainnet 🟣 Polygon 🟡 BSC Arbitrum 🟢 Gnosis Chain

@github-actions
Copy link

github-actions bot commented Feb 10, 2022

E2E Tests Failed
Check the results here: https://github.com/gnosis/safe-react-e2e-tests/actions/runs/1829317011

Failed tests:

  • ❌ Address book Address book
  • ❌ Read-only transaction creation and review Read-only transaction creation and review

@iamacook iamacook marked this pull request as ready for review February 10, 2022 16:23
@katspaugh
Copy link
Member

@iamacook could you expand a bit on the patch? Is it from a forum or you debugged it yourself?

@iamacook
Copy link
Member Author

iamacook commented Feb 10, 2022

@iamacook could you expand a bit on the patch? Is it from a forum or you debugged it yourself?

Whilst working on desktop pairing, I encountered numerous crashes trying to add a balance stateSyncer (seen here under walletInterface). As we don't use Onboard's balance state/subscription, I decided to not include one.

When investigating this, I referenced the custom pairing module that loosely follows the logic of Onboard's builtin WalletConnect one. It doesn't continuously request eth_getBalance. I used that as an example to remove the balance stateSyncer from Onboard's official WC module.

Onboard's balance state won't return a value with this patch but seeing as we don't use it, it shouldn't be an issue. I'd like to investigate this further but I think the problem lies deep in the dependancy tree of Onboard itself.

No patch is now used.

@dasanra
Copy link
Collaborator

dasanra commented Feb 11, 2022

Wouldn't make sense to upgrade bnc-onboard to latest version first? v1.37.3

https://github.com/blocknative/onboard/releases

There were quite lot of changes, even WalletConnect upgrade from v1.6.2 to 1.7.1 including several fixes
https://github.com/WalletConnect/walletconnect-monorepo/releases?page=2

@iamacook
Copy link
Member Author

Wouldn't make sense to upgrade bnc-onboard to latest version first? v1.37.3

I'll look into this.

@iamacook
Copy link
Member Author

Wouldn't make sense to upgrade bnc-onboard to latest version first? v1.37.3

They haven't updated their WalletConnect module since October. Seems like there's the same issue, even on the latest version of Onboard:

image
(these requests were in the space of 2 seconds)

I've only had success by patching out the balance subscription of the WC module, so I suspect it has nothing to do with Onboard but WC itself/it's dependencies. @katspaugh and I investigated where this could be coming from with no success.

@dasanra, what are your thoughts? If you have some spare time, I'd love to look at it together.

@iamacook
Copy link
Member Author

I've upgraded bnc-onboard and added a patch to the function responsible for the requests. Please see the description for more details. It's now a lot cleaner

-Web3ProviderEngine.prototype.sendAsync = function(payload, cb){
+Web3ProviderEngine.prototype.sendAsync = function (payload, cb) {
+ if (payload.method === 'eth_getBalance') {
+ return
Copy link
Member

Choose a reason for hiding this comment

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

Did you test it when transacting? IIRC in some methods, the web3.js calls getBalance under the hood, and it can break something

Copy link
Member Author

Choose a reason for hiding this comment

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

Testing it out now across different wallets

Copy link
Member Author

Choose a reason for hiding this comment

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

Nice find. It causes issues with the estimation. I've decided to write a modified wallet module instead for WalletConnect that has no balance subscription.

@iamacook
Copy link
Member Author

After researching this and trying different approaches, it seems to come from the balance stateSyncer in Onboard's WC module. There's a continuous loop when comparing _context.prev and _context.next.

In an effort to avoid patching code that may break, I've decided to opt for a custom module that doesn't include a balance stateSyncer. We can't get the balance via Onboard, when connected to WC, but we never use this. Retrieving the balance from web3 directly still works.

Copy link
Member

@katspaugh katspaugh left a comment

Choose a reason for hiding this comment

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

👏 👏 👏

@iamacook iamacook merged commit 400b52e into cleanup-provider-watcher Feb 11, 2022
@iamacook iamacook deleted the reduce-infura-requests branch February 11, 2022 13:35
@github-actions github-actions bot locked and limited conversation to collaborators Feb 11, 2022
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants