Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Middlewares and Caching #997

Merged
merged 6 commits into from
Mar 18, 2021
Merged

Middlewares and Caching #997

merged 6 commits into from
Mar 18, 2021

Conversation

iamdefinitelyahuman
Copy link
Member

What I did

  • generalized logic for adding middlewares to the brownie web3 object
  • a basic implementation of for RPC-call caching

How I did it

Middlewares are automagically loaded from all files within brownie/network/middlewares. The only requirement is that they subclass BrownieMiddlewareABC. They are automatically added upon connection and removed at disconnect. The exact spec is fairly well documented within the natspec and comments.

The cache has two components:

  • A short-term cache that lasts as long as the current block. This is useful because it means elsewhere in the code, there's no longer an onus to think about "am I hitting this endpoint too often?" - which gets exponentially more complex when you start using threading (such as with tx replacement). Related - dropped and replaced transaction hangs brownie while repeatedly hitting infura 18,091 times with eth_getTransactionReceipt #850
  • A long-term cache that persists between sessions. Currently this is only storing eth_getCode under specific conditions (non-zero return value, no presence of the SELFDESTRUCT opcode). Looking at the Infura dashboard for my most commonly used API key, this endpoint accounts for >20% of the network traffic from Brownie, so it seems like a good first candidate. With the base logic in place for this cache it should be possible to expand it to other endpoints.

@drewwells
Copy link

How do I make use of this in a script? I'm pulling a lot of information from the network and would like to keep it locally in this cache.

@iamdefinitelyahuman
Copy link
Member Author

At present this isn't exposed in the public API, sorry. You could look at the code in network/middlewares and find a way to hack it, but it might not be pretty.

Sign up for free to 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.

2 participants