Skip to content

Commit

Permalink
allow to adapt an estimate_gas request (paritytech#738)
Browse files Browse the repository at this point in the history
  • Loading branch information
nanocryk committed Aug 24, 2022
1 parent b098d31 commit f636d6c
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 4 deletions.
24 changes: 23 additions & 1 deletion client/rpc/src/eth/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,25 @@ use crate::{
/// Default JSONRPC error code return by geth
pub const JSON_RPC_ERROR_DEFAULT: i32 = -32000;

impl<B, C, P, CT, BE, H: ExHashT, A: ChainApi> Eth<B, C, P, CT, BE, H, A>
/// Allow to adapt a request for `estimate_gas`.
/// Can be used to estimate gas of some contracts using a different function
/// in the case the normal gas estimation doesn't work.
///
/// Exemple: a precompile that tries to do a subcall but succeeds regardless of the
/// success of the subcall. The gas estimation will thus optimize the gas limit down
/// to the minimum, while we want to estimate a gas limit that will allow the subcall to
/// have enough gas to succeed.
pub trait EstimateGasAdapter {
fn adapt_request(request: CallRequest) -> CallRequest;
}

impl EstimateGasAdapter for () {
fn adapt_request(request: CallRequest) -> CallRequest {
request
}
}

impl<B, C, P, CT, BE, H: ExHashT, A: ChainApi, EGA> Eth<B, C, P, CT, BE, H, A, EGA>
where
B: BlockT<Hash = H256> + Send + Sync + 'static,
C: ProvideRuntimeApi<B> + StorageProvider<B, BE>,
Expand All @@ -54,6 +72,7 @@ where
BE: Backend<B> + 'static,
BE::State: StateBackend<BlakeTwo256>,
A: ChainApi<Block = B> + 'static,
EGA: EstimateGasAdapter,
{
pub fn call(&self, request: CallRequest, number: Option<BlockNumber>) -> Result<Bytes> {
let CallRequest {
Expand Down Expand Up @@ -301,6 +320,9 @@ where
// Get best hash (TODO missing support for estimating gas historically)
let best_hash = client.info().best_hash;

// Adapt request for gas estimation.
let request = EGA::adapt_request(request);

// For simple transfer to simple account, return MIN_GAS_PER_TX directly
let is_simple_transfer = match &request.data {
None => true,
Expand Down
5 changes: 3 additions & 2 deletions client/rpc/src/eth/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,11 +54,12 @@ use crate::{internal_err, overrides::OverrideHandle, public_key, signer::EthSign

pub use self::{
cache::{EthBlockDataCacheTask, EthTask},
execute::EstimateGasAdapter,
filter::EthFilter,
};

/// Eth API implementation.
pub struct Eth<B: BlockT, C, P, CT, BE, H: ExHashT, A: ChainApi> {
pub struct Eth<B: BlockT, C, P, CT, BE, H: ExHashT, A: ChainApi, EGA = ()> {
pool: Arc<P>,
graph: Arc<Pool<A>>,
client: Arc<C>,
Expand All @@ -74,7 +75,7 @@ pub struct Eth<B: BlockT, C, P, CT, BE, H: ExHashT, A: ChainApi> {
/// When using eth_call/eth_estimateGas, the maximum allowed gas limit will be
/// block.gas_limit * execute_gas_limit_multiplier
execute_gas_limit_multiplier: u64,
_marker: PhantomData<(B, BE)>,
_marker: PhantomData<(B, BE, EGA)>,
}

impl<B: BlockT, C, P, CT, BE, H: ExHashT, A: ChainApi> Eth<B, C, P, CT, BE, H, A> {
Expand Down
2 changes: 1 addition & 1 deletion client/rpc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ mod signer;
mod web3;

pub use self::{
eth::{format, Eth, EthBlockDataCacheTask, EthFilter, EthTask},
eth::{format, EstimateGasAdapter, Eth, EthBlockDataCacheTask, EthFilter, EthTask},
eth_pubsub::{EthPubSub, EthereumSubIdProvider},
net::Net,
overrides::{
Expand Down

0 comments on commit f636d6c

Please sign in to comment.