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

fix(gas): improve bundle gas estimation calculation #444

Merged
merged 3 commits into from
Oct 5, 2023

Conversation

alex-miao
Copy link
Collaborator

@alex-miao alex-miao commented Oct 4, 2023

Contributes to #349

Proposed Changes

Improve the bundle gas limit calculation. The bundle gas limit we now use is based off the following observation:

In the Entrypoint contract, the innerHandleOp call has this check that the bundle would need to pass for every single user op that it executes.

Within a bundle, let $p_i$, $v_i$, and $c_i$ be the static pre-verification gas, verification gas limit, and call gas limit of user operation $i$. Also let $m_i$ be $1$ if user op $i$ uses a paymaster, and $0$ if not. Suppose we start with initial gas $g$. When processing user op $i$, at the point of the the innerHandleOp check we have yet to execute the call for user op $i$, and the check requires that at that point the remaining gas be greater than $c_i+v_i+5000$. If user op $i$ uses a paymaster, because of the postOp calls, we would also need the remaining gas to also be greater than $c_i+2v_i$, since the up to two postOp calls can each use up $v_i$ gas.

We can express this condition as:

$$remaining\_gas_i\geq c_i+v_i+5000+m_i \max(v_i-5000,0)$$

The worst case remaining gas at this check for user operation $i$, since we have already processed user ops $1$ through $i-1$ and both the pre-verification gas and up to the verification gas limit of user op $i$, is:
$$g-\sum_{j=1}^{i-1}\big(p_j+(1+2m_j)v_j+c_j\big)-p_i-v_i$$

In order to not run out of gas and pass all the checks, we would therefore need that for all $i$:
$$g\geq \sum_{j=1}^{i-1}\big(p_j+(1+2m_j)v_j+c_j\big)+p_i+2v_i+c_i+5000+m_i \max(v_i-5000,0)$$

so the smallest initial gas limit we can assign the bundle would be the max over all $i$ of the right hand side of the inequality.

@codecov
Copy link

codecov bot commented Oct 4, 2023

Codecov Report

Merging #444 (774cfa1) into main (e220e0a) will increase coverage by 0.40%.
The diff coverage is 98.68%.

❗ Current head 774cfa1 differs from pull request most recent head 04c9c75. Consider uploading reports for the commit 04c9c75 to get more accurate results

Impacted file tree graph

Files Coverage Δ
crates/builder/src/bundle_proposer.rs 88.89% <100.00%> (-0.02%) ⬇️
crates/sim/src/precheck.rs 68.40% <100.00%> (ø)
crates/sim/src/gas/gas.rs 93.81% <98.56%> (+4.18%) ⬆️

... and 4 files with indirect coverage changes

Flag Coverage Δ
unit-tests 52.42% <98.68%> (+0.40%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Components Coverage Δ
rundler binary 0.00% <ø> (ø)
builder 45.06% <100.00%> (-0.01%) ⬇️
dev 0.00% <ø> (ø)
pool 59.65% <ø> (+0.01%) ⬆️
provider 1.33% <ø> (ø)
rpc 18.51% <ø> (ø)
sim 82.80% <98.57%> (+0.60%) ⬆️
tasks ∅ <ø> (∅)
types 93.56% <ø> (ø)
utils 7.20% <ø> (ø)

@alex-miao alex-miao force-pushed the alex/non-paymaster-bundle-gas branch from e3dcad3 to 774cfa1 Compare October 4, 2023 05:08
0xfourzerofour
0xfourzerofour previously approved these changes Oct 4, 2023
Copy link
Collaborator

@0xfourzerofour 0xfourzerofour left a comment

Choose a reason for hiding this comment

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

Good job! that math seems accurate to me

crates/sim/src/gas/gas.rs Outdated Show resolved Hide resolved
crates/sim/src/gas/gas.rs Outdated Show resolved Hide resolved
crates/sim/src/gas/gas.rs Outdated Show resolved Hide resolved
Copy link
Collaborator

@dancoombs dancoombs left a comment

Choose a reason for hiding this comment

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

This is 🔥 . Math LGTM. Want to see this in action during our load tests ASAP.

dancoombs
dancoombs previously approved these changes Oct 5, 2023
Copy link
Collaborator

@dancoombs dancoombs left a comment

Choose a reason for hiding this comment

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

🚀 🚢

Base automatically changed from alex/amortize-fixed-gas to main October 5, 2023 17:52
@alex-miao alex-miao dismissed stale reviews from dancoombs and 0xfourzerofour October 5, 2023 17:52

The base branch was changed.

@alex-miao alex-miao force-pushed the alex/non-paymaster-bundle-gas branch from 899206a to 04c9c75 Compare October 5, 2023 18:51
@alex-miao alex-miao merged commit b337dcb into main Oct 5, 2023
5 checks passed
@alex-miao alex-miao deleted the alex/non-paymaster-bundle-gas branch October 5, 2023 19:51
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.

3 participants