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

Refactor balance override detection #3239

Merged
merged 2 commits into from
Jan 17, 2025

Conversation

MartinquaXD
Copy link
Contributor

Description

With #3238 merged it now becomes apparent why we have such a poor quote verification ratio on arbitrum. All bridged tokens store their balances mapping at storage slot 51.

Changes

To avoid having to merge another PR every time we find an important token that requires a deeper probing depth I made the probing depth controllable via a CLI parameter.
While I was at it I did the same for the token cache size (should have been like that from the start given that the change was trivial with the given architecture).

Making the probing depth runtime dependent made using static variables a lot more annoying. Instead I created an Inner struct which contains all the static data so now it can be initialized once at runtime without more exotic types like LazyLock or OnceLock. As long as we ensure that only 1 instance of Detector gets created this would be equivalent in terms of memory usage and performance.

I also refactored the logic that constructs the strategy list. It should now be dead simple to see what's going on and how to extend it. First we hardcode a list of known non-variable storage slots and afterwards we probe the first N slots starting from known entry points. This is currently just the 0th slot but could theoretically be extended with other relevant slots. Even if it doesn't get extended I think the refactor still makes the code easier.

How to test

e2e tests and units tests still work
also added another unit test for a bridged token on arbitrum

@MartinquaXD MartinquaXD requested a review from a team as a code owner January 17, 2025 09:58
Comment on lines +66 to +77
// For each entry point probe the first n following slots.
let entry_points = [
// solc lays out memory linearly starting at 0 by default
"0000000000000000000000000000000000000000000000000000000000000000",
];
for start_slot in entry_points {
let mut slot = U256::from(start_slot);
for _ in 0..probing_depth {
strategies.push(Strategy::SolidityMapping { slot });
slot += U256::one();
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Why do we need an array for entry_points here?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It's not needed at the moment but I wanted to make this code easily extensible if we find cases where user defined data sits at a library defined entry point.
Given that this code requires knowledge about a bunch of low level EVM details I wanted to make this as "cookie cutter" as possible.

Copy link
Contributor

@squadgazzz squadgazzz left a comment

Choose a reason for hiding this comment

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

LGTM

@MartinquaXD MartinquaXD merged commit df6306c into main Jan 17, 2025
11 checks passed
@MartinquaXD MartinquaXD deleted the refactor-balance-override-detection branch January 17, 2025 13:56
@github-actions github-actions bot locked and limited conversation to collaborators Jan 17, 2025
writeln!(
f,
"quote_autodetect_token_balance_overrides_probing_depth: {:?}",
quote_autodetect_token_balance_overrides_probing_depth
Copy link
Contributor

Choose a reason for hiding this comment

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

I'm wondering if we need to use debug formatting for ordinary number here?

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