Skip to content

Commit

Permalink
fix(rpc): add a buffer to call gas limit estimation (#628)
Browse files Browse the repository at this point in the history
  • Loading branch information
dancoombs authored Mar 4, 2024
1 parent 8702543 commit 555d230
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions crates/sim/src/estimation/estimation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,11 @@
// You should have received a copy of the GNU General Public License along with Rundler.
// If not, see https://www.gnu.org/licenses/.

use std::{cmp, ops::Deref, sync::Arc};
use std::{
cmp,
ops::{Add, Deref},
sync::Arc,
};

use anyhow::{anyhow, Context};
use ethers::{
Expand Down Expand Up @@ -52,7 +56,11 @@ const GAS_ROUNDING: u64 = 4096;
/// `GAS_ESTIMATION_ERROR_MARGIN` of each other.
const GAS_ESTIMATION_ERROR_MARGIN: f64 = 0.1;

/// Percentage by which to increase the verification gas limit after binary search
const VERIFICATION_GAS_BUFFER_PERCENT: u64 = 10;
/// Absolute value by which to increase the call gas limit after binary search
/// TODO(danc): remove this in 0.7 entry point else users will get overcharged
const CALL_GAS_BUFFER_VALUE: U256 = U256([3000, 0, 0, 0]);

/// Offset at which the proxy target address appears in the proxy bytecode. Must
/// be updated whenever `CallGasEstimationProxy.sol` changes.
Expand Down Expand Up @@ -159,10 +167,15 @@ impl<P: Provider, E: EntryPoint> GasEstimator for GasEstimatorImpl<P, E> {
)
.min(settings.max_verification_gas.into());

// Add a buffer to the call gas limit and clamp
let call_gas_limit = call_gas_limit
.add(CALL_GAS_BUFFER_VALUE)
.clamp(MIN_CALL_GAS_LIMIT, settings.max_call_gas.into());

Ok(GasEstimate {
pre_verification_gas,
verification_gas_limit,
call_gas_limit: call_gas_limit.clamp(MIN_CALL_GAS_LIMIT, settings.max_call_gas.into()),
call_gas_limit,
})
}
}
Expand Down Expand Up @@ -1202,7 +1215,10 @@ mod tests {
);

// input gas limit clamped with the set limit in settings and constant MIN
assert_eq!(estimation.call_gas_limit, U256::from(10000));
assert_eq!(
estimation.call_gas_limit,
U256::from(10000) + CALL_GAS_BUFFER_VALUE
);
}

#[tokio::test]
Expand Down

0 comments on commit 555d230

Please sign in to comment.