-
Notifications
You must be signed in to change notification settings - Fork 83
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
Introduce Component for Balance Overrides #3125
Introduce Component for Balance Overrides #3125
Conversation
This pull request has been marked as stale because it has been inactive a while. Please update this pull request or it will be automatically closed. |
@MartinquaXD is on vacation and will be back mid next week to take a look |
This PR is a follow up to cowprotocol#3125 and uses the component introduced in the aforementioned PR for setting up a simulated token balance using state overrides in order for quote verification to work even when the trader does not have sufficient balance. The way it works is by configuring known mapping slots for the `mapping(address => uint256) balances` in ERC20 token contract implementations and using this to compute the slot for overriding the **solver's** token balance for the simulation. The solver can then use this balance to top up the trader's account if needed (in the case were we allow mocking preconditions in the simulation - currently this is determined by whether or not the quote has hooks). We intentionally do not override the trader's balance in order to not interfere with hook execution in verified quotes. Note that the type of the state override changed slightly. This is because it was wrong to begin with. Node implementations I tested with (Geth and Anvil) expect both the slot and the value for state overrides to be exactly 32-bytes long (so `H256`). I guess this feature of the state override in the `ethrpc` crate was not used in the past and therefore no one noticed 🤷. ### Test Plan Added an E2E test that uses the new token balance override feature in order to produce a verified quote for a trader with no balances. Note that commenting out the API arguments causes the test to fail as expected.
Note that I finished the follow up PR with a working E2E test 🎉. I created the PR to my fork (since I don't know how to create PR stacks when forking a repo). |
This pull request has been marked as stale because it has been inactive a while. Please update this pull request or it will be automatically closed. |
Please don't close! I humbly request @MartinquaXD to save my PR from the Mark and Sweep Garbage Collector. |
I think the idea is pretty cool and can be very powerful but I'm a bit worried about the overhead of maintaining the override list. Do you have an idea how we could automatically detect which storage slot needs to be overwritten?
That way we can make use of that feature with minimal intervention and maintenance overhead. Ideally we'd only use the hardcoded list for relevant tokens with deterministic target slots which we can't automatically detect for whatever reason. WDYT? Edit: Maybe we can use |
Agree that this is kind of annoying.
This is possible, and I have a decent method for doing it and have played around with some ideas. I will implement it on top of my other PR in nlordell#1. Just wanted to get a rough sense of "is this something that can eventually be merged into the codebase" before going too deep into the rabbit hole :P |
We would definitely be happy about more verifiable quotes. We just need to find a good balance between manual overhead, correctness and complexity. I think we are on a good path. 👍 |
My idea was even sillier, but slightly more straight forward and only relies on storage overrides, which you need for simulation anyway (while
|
@MartinquaXD - regarding auto-detection I implemented something here: nlordell#3 I will make the final PR change the |
This PR is a follow up to cowprotocol#3125 and uses the component introduced in the aforementioned PR for setting up a simulated token balance using state overrides in order for quote verification to work even when the trader does not have sufficient balance. The way it works is by configuring known mapping slots for the `mapping(address => uint256) balances` in ERC20 token contract implementations and using this to compute the slot for overriding the **solver's** token balance for the simulation. The solver can then use this balance to top up the trader's account if needed (in the case were we allow mocking preconditions in the simulation - currently this is determined by whether or not the quote has hooks). We intentionally do not override the trader's balance in order to not interfere with hook execution in verified quotes. Note that the type of the state override changed slightly. This is because it was wrong to begin with. Node implementations I tested with (Geth and Anvil) expect both the slot and the value for state overrides to be exactly 32-bytes long (so `H256`). I guess this feature of the state override in the `ethrpc` crate was not used in the past and therefore no one noticed 🤷. ### Test Plan Added an E2E test that uses the new token balance override feature in order to produce a verified quote for a trader with no balances. Note that commenting out the API arguments causes the test to fail as expected.
This PR is the first in a stack to add a system for overriding balances so that more quotes can be verified. One issue with the current system, is that it requires that the account creating the quote has the balance available to the account (or available to the account after a pre-hook is executed) in order for the quote to be properly verified. This isn't always possible for all flows (and notably flows at Safe, where transactions to prepare the balance happens at the same time as the `setPreSignature` transaction executes and after the quote). The overall solution I would propose (hopefully a pragmatic one that isn't TOO hacky) would be enable special handling for the most commonly traded tokens, by configuring for each token how the storage slot is computed for the token balance. This way, you could maintain a file that contains a `token => computation_strategy` map for the most popular tokens allowing trades to be verified even for quotes from users without the balance available. If this strategy is accepted, in a follow up PRs I would: 1. Add the component to the trade verifier and use it to fund the trader when simulating quotes 2. Pipe configuration to the trade verifier and balance overrides component
e2ac55c
to
f09b0e8
Compare
This PR is a follow up to cowprotocol#3125 and uses the component introduced in the aforementioned PR for setting up a simulated token balance using state overrides in order for quote verification to work even when the trader does not have sufficient balance. The way it works is by configuring known mapping slots for the `mapping(address => uint256) balances` in ERC20 token contract implementations and using this to compute the slot for overriding the **solver's** token balance for the simulation. The solver can then use this balance to top up the trader's account if needed (in the case were we allow mocking preconditions in the simulation - currently this is determined by whether or not the quote has hooks). We intentionally do not override the trader's balance in order to not interfere with hook execution in verified quotes. Note that the type of the state override changed slightly. This is because it was wrong to begin with. Node implementations I tested with (Geth and Anvil) expect both the slot and the value for state overrides to be exactly 32-bytes long (so `H256`). I guess this feature of the state override in the `ethrpc` crate was not used in the past and therefore no one noticed 🤷. ### Test Plan Added an E2E test that uses the new token balance override feature in order to produce a verified quote for a trader with no balances. Note that commenting out the API arguments causes the test to fail as expected.
Alright @MartinquaXD, I think the PR stack is ready and freshly rebased to latest
I'm not sure what the best way to review such a stack would be. I made sure to keep my commits nice and tidy (1 commit per PR; and each commit is self-contained and can be merged into
Let me know what you prefer and makes your lives easier :) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think it's okay to merge the PRs individually since I already have a good understanding of the whole stack.
Hmm, looks like forked node connection issues:
Not sure how to debug this TBH. |
This is a known issue. Github doesn't pass the node url secret to PRs coming from forked repos. So all is good with this PR. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Since oncall and tight with time for reviewing, I just read the description and reviewed this PR, so far LGTM.
Description
This PR is the first in a stack to add a system for overriding balances so that more quotes can be verified.
One limitation with the current quote verification system, is that it requires that the
from
account in the quote has the sell token balance available (or available after a pre-hook is executed) in order for the quote to be properly verified. This isn't always possible for all flows (and notably flows at Safe, where transactions to prepare the balance happens at the same time as thesetPreSignature
transaction executes, so after the quote).The overall solution I would propose (hopefully a pragmatic one that isn't considered too hacky) would be enable special handling for the most commonly traded tokens, by configuring for each token how the storage slot is computed for the token balance. This way, you could maintain a file that contains a
token => computation_strategy
map for the most popular tokens allowing trades to be verified even for quotes from users without the balance available.This PR is the first piece for this overall solution, which introduces a component for computing storage slots needed for overriding balances for
eth_call
simulations. If this strategy is accepted, in a follow up PRs I would:Solver
simulation entrypoint, which would top up theTrader
balance if needed; this way the missing balance can be reported as part of the simulation and logged).Changes
BalanceOverriding
componentHow to test
Added unit tests verifying logic.