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

feat: query acct history #1277

Merged
merged 26 commits into from
Mar 22, 2023
Merged

Conversation

fubuloubu
Copy link
Member

@fubuloubu fubuloubu commented Feb 2, 2023

What I did

  1. Added BaseAddress.history, which fetches the relevant AccountHistory object
  2. Re-route AccountHistory.outgoing through the query manager
  3. Add a new method (raises NotImplementedError by default) to ProviderAPI to search through chain history and fetch transactions by account and range of nonces, and implement in Web3Provider (brute force method, very expensive)
  4. Add support to the default query engine to source AccountHistory queries (by nonce) through either ExplorerAPI (if available) or via new brute-force method
    NOTE: Change this functionality in ape-etherscan to QueryAPI, and remove from ExplorerAPI (non-breaking change)
  5. Add AccountHistory.query for working with account history using a dataframe approach
    6. (WIP) Add support to the cache query engine for storing and fetching AccountTransactionQuery results

fixes: #1237
fixes: APE-572

How I did it

I did this to take better advantage of the Ape query engine, and also provide more correct results in situations where an explorer wasn't available.

How to verify it

check the new feature acct.history on some local and public networks, with and without etherscan and the cache query engine

Checklist

  • All changes are completed
    • Add .query
      - [ ] Add to cache provider
  • New test cases have been added
  • Documentation has been updated

@fubuloubu fubuloubu force-pushed the feat/query-acct-history branch 3 times, most recently from b0aeca3 to 0126454 Compare February 2, 2023 13:47
@fubuloubu
Copy link
Member Author

NOTE: Can't implement cache querying until #994 is resolved

@fubuloubu fubuloubu force-pushed the feat/query-acct-history branch 5 times, most recently from e4a6f01 to a8a72a4 Compare February 16, 2023 21:38
@fubuloubu fubuloubu marked this pull request as ready for review February 16, 2023 21:38
@fubuloubu
Copy link
Member Author

fubuloubu commented Feb 16, 2023

Note for reviewers:
bee0692 is the big commit, adding a recursive algorithm for filtering blocks and finding transactions by nonce. After that, a bit of piping, and a refactor to move the ExplorerAPI method to the default query provider (take note of TODOs there). All actions within AccountHistory now source from the query provider (also some TODOs about sessional history). Lastly, added support for .query, __getitem__ (both index and slice), and finally a route from BaseAddress to AddressHistory to close the gaps for users

Some extra reasoning: this recursive algorithm is necessary in scenarios where there needs to be a backup in place for people to access these features, and push them towards using a better means to access history (explorer plugin, other query providers). From working on this PR, it became very apparently that there needs to be a refactor of both sessional history and the ExplorerAPI to take advantage of the query provider system more, but will leave those to future PRs to settle.

@fubuloubu
Copy link
Member Author

Just one of @ApeWorX/engineering needs to approve to merge

src/ape/api/providers.py Outdated Show resolved Hide resolved
src/ape/managers/chain.py Outdated Show resolved Hide resolved
Copy link
Member

@antazoey antazoey left a comment

Choose a reason for hiding this comment

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

Testing issue: I connected to mainnet using Alchemy. I loaded my account with txns. And then I did this:

next(account.history.outgoing)

I got this error:

ChainError: 'stop=4' cannot be greater than account's current nonce (3).

(other feedback is small and nitpicky - mostly focused on testing)

src/ape/managers/chain.py Outdated Show resolved Hide resolved
src/ape/managers/chain.py Outdated Show resolved Hide resolved
src/ape/api/providers.py Outdated Show resolved Hide resolved
@fubuloubu
Copy link
Member Author

Testing issue: I connected to mainnet using Alchemy. I loaded my account with txns. And then I did this:

next(account.history.outgoing)

I got this error:

ChainError: 'stop=4' cannot be greater than account's current nonce (3).

(other feedback is small and nitpicky - mostly focused on testing)

believe I solved this with 723ef84

@fubuloubu fubuloubu marked this pull request as draft February 22, 2023 05:10
@fubuloubu
Copy link
Member Author

fubuloubu commented Feb 22, 2023

For some reason, acct.history[0] doesn't work for an account that has one


EDIT: This is resolved

antazoey
antazoey previously approved these changes Feb 22, 2023
Copy link
Member

@antazoey antazoey left a comment

Choose a reason for hiding this comment

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

I ran some queries. Neat! What remains? This works a lot better than before 🙇🏻‍♀️ .

I do wonder how we can utilize a sessional cache to cache receipts from the slower HTTP-based caches (I saw your TODO). It could be opt-in.

src/ape/api/providers.py Outdated Show resolved Hide resolved
@fubuloubu
Copy link
Member Author

fubuloubu commented Feb 22, 2023

I ran some queries. Neat! What remains? This works a lot better than before 🙇🏻‍♀️ .

Just this: #1277 (comment)

I think this is just affecting the detection of nonce = 0, the rest works

I do wonder how we can utilize a sessional cache to cache receipts from the slower HTTP-based caches (I saw your TODO). It could be opt-in.

I mean we have ape-cache. For permanently existing networks, the idea is that it pushes stuff to an SQLite DB, which is good for multi-session caching. Could implement a secondary cache provider (either extending the one under ape_cache or under ape/managers/query.py alongside DefaultQueryEngine) that's just an in-memory cache (with some cache expiration to limit RAM usage), but the thing holding ape-cache back is the layout of ReceiptAPI/BlockAPI/ContractLog data structures so that it's easier to serialize them to SQL

Also the query system needs a more general refactor of how data passes through it so that it only converts if/when necessary e.g. refactor all .query(...) methods to return Iterator[dict] (instead of a pandas DataFrame) and convert from Iterator[dict] to ReceiptAPI/BlockAPI/ContractLog if that's the intended output (usually we use columns = "*" and then cast to that type), or do obj.dict() if you want Iterator[dict] output. As long as the columns field is used appropiately this should have an issue (but obiously this might have some mypy issues with the approach)

Lastly, a lot of the stuff relating to caching contracts is something I wanted to merge into the data system, as time goes on (fetching Manifests, maybe publishing them, fetch contract types via address, proxy info, etc.)

Copy link
Member

@antazoey antazoey left a comment

Choose a reason for hiding this comment

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

Ok I tested the heck out of this.
I think most of my feedback is related to the actual query system though.

Please let me know if there any other gaps you would like me to test.
Note, it is hard to ensure anything won't break in future releases without more tests.

src/ape/managers/chain.py Show resolved Hide resolved
self,
*columns: List[str],
start_nonce: int = 0,
stop_nonce: Optional[int] = None,
Copy link
Member

Choose a reason for hiding this comment

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

I wish the resulting dataframe was indexed by nonce always.
When I do:

account.history.query("transaction", start_once=3, stop_nonce=6)

it shows the result like numeric indices 0 - 3, which threw me off at first.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yeah I agree with this. Probably a breaking change though, issue to track for 0.7.0?

Copy link
Member

@antazoey antazoey Mar 20, 2023

Choose a reason for hiding this comment

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

i mean, you can't do anything with the data now.. it's just broken
Edit: nevermind, I thought this was something else.

ya this could be 0.7 or a later opportunity.

Copy link
Member Author

Choose a reason for hiding this comment

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

Actually wait, we added this method here, so I think we could actually do it for account history (but not for block history or event history)

Copy link
Member Author

Choose a reason for hiding this comment

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

nvm, it requires siginificant restructuring to how we handle the returns from the query manager in order to create an index column, so not handling this here

src/ape/api/address.py Show resolved Hide resolved
src/ape/managers/chain.py Show resolved Hide resolved
src/ape/managers/chain.py Show resolved Hide resolved
@fubuloubu fubuloubu marked this pull request as ready for review March 20, 2023 21:04
@ghost ghost self-requested a review March 20, 2023 22:10
Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

Looks good, just a few semantic comments/questions

docs/userguides/data.md Outdated Show resolved Hide resolved
docs/userguides/data.md Outdated Show resolved Hide resolved
docs/userguides/data.md Outdated Show resolved Hide resolved
src/ape/api/query.py Outdated Show resolved Hide resolved
src/ape/api/query.py Outdated Show resolved Hide resolved
src/ape/managers/chain.py Outdated Show resolved Hide resolved
docs/userguides/data.md Outdated Show resolved Hide resolved
docs/userguides/data.md Outdated Show resolved Hide resolved
@fubuloubu fubuloubu requested review from antazoey and a user March 21, 2023 01:06
Co-authored-by: helloibis <108302637+helloibis@users.noreply.github.com>
ghost
ghost previously approved these changes Mar 21, 2023
Copy link

@ghost ghost left a comment

Choose a reason for hiding this comment

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

:shipit:

@fubuloubu
Copy link
Member Author

Note: works great with latest ape-etherscan, however wildcard rules are currently fetching .call_trace, .return_value and other computed properties

@fubuloubu fubuloubu merged commit c24c9f2 into ApeWorX:main Mar 22, 2023
@fubuloubu fubuloubu deleted the feat/query-acct-history branch March 22, 2023 16:39
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.

Add .history to BaseAddress
2 participants