Skip to content

Commit

Permalink
Refund pov gas based on measured proof size usage (#1613)
Browse files Browse the repository at this point in the history
* refund pov gas based on measured proof size usage (#230)

Co-authored-by: Agusrodri <[email protected]>

* fix

* fix

* Enable ProofSizeExt when estimating gas and emit ReportRefund when PoV gas is the effective gas

* fix style

* enable proof recording for template

* fix style

* fix

* add to host functions

* style

* revert meter.rs

* update cargo.lock

* use cargo.lock from master then update

---------

Co-authored-by: Éloïs <[email protected]>
Co-authored-by: Agusrodri <[email protected]>
Co-authored-by: Rodrigo Quelhas <[email protected]>
  • Loading branch information
4 people authored Mar 6, 2025
1 parent 0e7c6cd commit d5755b5
Show file tree
Hide file tree
Showing 12 changed files with 725 additions and 447 deletions.
784 changes: 456 additions & 328 deletions Cargo.lock

Large diffs are not rendered by default.

5 changes: 5 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,7 @@ sp-std = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2
sp-storage = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2412", default-features = false }
sp-timestamp = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2412", default-features = false }
sp-transaction-pool = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2412", default-features = false }
sp-trie = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2412", default-features = false }
sp-version = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2412", default-features = false }
sp-weights = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2412", default-features = false }
# Substrate FRAME
Expand All @@ -155,6 +156,10 @@ substrate-frame-rpc-system = { git = "https://github.com/paritytech/polkadot-sdk
substrate-test-runtime-client = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2412" }
substrate-wasm-builder = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2412" }

# Cumulus primitives
cumulus-primitives-proof-size-hostfunction = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2412", default-features = false }
cumulus-primitives-storage-weight-reclaim = { git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2412", default-features = false }

# XCM
xcm = { package = "staging-xcm", git = "https://github.com/paritytech/polkadot-sdk", branch = "stable2412", default-features = false }

Expand Down
1 change: 1 addition & 0 deletions client/rpc/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ sp-runtime = { workspace = true, features = ["default"] }
sp-state-machine = { workspace = true, features = ["default"] }
sp-storage = { workspace = true, features = ["default"] }
sp-timestamp = { workspace = true, features = ["default"], optional = true }
sp-trie = { workspace = true, features = ["default"] }
# Frontier
fc-api = { workspace = true }
fc-mapping-sync = { workspace = true }
Expand Down
139 changes: 105 additions & 34 deletions client/rpc/src/eth/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ where
)
};

let (substrate_hash, api) = match frontier_backend_client::native_block_id::<B, C>(
let (substrate_hash, mut api) = match frontier_backend_client::native_block_id::<B, C>(
self.client.as_ref(),
self.backend.as_ref(),
number_or_hash,
Expand All @@ -127,6 +127,12 @@ where
}
};

// Enable proof size recording
api.record_proof();
let recorder: sp_trie::recorder::Recorder<HashingFor<B>> = Default::default();
let ext = sp_trie::proof_size_extension::ProofSizeExt::new(recorder.clone());
api.register_extension(ext);

let api_version = if let Ok(Some(api_version)) =
api.api_version::<dyn EthereumRuntimeRPCApi<B>>(substrate_hash)
{
Expand Down Expand Up @@ -238,14 +244,21 @@ where
api_version,
state_overrides,
)?;

// Enable proof size recording
let recorder: sp_trie::recorder::Recorder<HashingFor<B>> = Default::default();
let ext = sp_trie::proof_size_extension::ProofSizeExt::new(recorder.clone());
let mut exts = Extensions::new();
exts.register(ext);

let params = CallApiAtParams {
at: substrate_hash,
function: "EthereumRuntimeRPCApi_call",
arguments: encoded_params,
overlayed_changes: &RefCell::new(overlayed_changes),
call_context: CallContext::Offchain,
recorder: &None,
extensions: &RefCell::new(Extensions::new()),
recorder: &Some(recorder),
extensions: &RefCell::new(exts),
};

let value = if api_version == 4 {
Expand Down Expand Up @@ -636,27 +649,56 @@ where
(info.exit_reason, info.value, info.used_gas)
} else {
// Post-london + access list support
let access_list = access_list.unwrap_or_default();
let info = api.call(
substrate_hash,
from.unwrap_or_default(),
to,
data,
value.unwrap_or_default(),
gas_limit,
max_fee_per_gas,
max_priority_fee_per_gas,
None,
estimate_mode,
Some(
let encoded_params = Encode::encode(&(
&from.unwrap_or_default(),
&to,
&data,
&value.unwrap_or_default(),
&gas_limit,
&max_fee_per_gas,
&max_priority_fee_per_gas,
&None::<Option<U256>>,
&estimate_mode,
&Some(
access_list
.unwrap_or_default()
.into_iter()
.map(|item| (item.address, item.storage_keys))
.collect(),
.collect::<Vec<(sp_core::H160, Vec<H256>)>>(),
),
)
.map_err(|err| internal_err(format!("runtime error: {err}")))?
.map_err(|err| internal_err(format!("execution fatal: {err:?}")))?;
));

// Proof size recording
let recorder: sp_trie::recorder::Recorder<HashingFor<B>> = Default::default();
let ext = sp_trie::proof_size_extension::ProofSizeExt::new(recorder.clone());
let mut exts = Extensions::new();
exts.register(ext);

let params = CallApiAtParams {
at: substrate_hash,
function: "EthereumRuntimeRPCApi_call",
arguments: encoded_params,
overlayed_changes: &RefCell::new(Default::default()),
call_context: CallContext::Offchain,
recorder: &Some(recorder),
extensions: &RefCell::new(exts),
};

let info = self
.client
.call_api_at(params)
.and_then(|r| {
Result::map_err(
<Result<ExecutionInfoV2::<Vec<u8>>, DispatchError> as Decode>::decode(&mut &r[..]),
|error| sp_api::ApiError::FailedToDecodeReturnValue {
function: "EthereumRuntimeRPCApi_call",
error,
raw: r
},
)
})
.map_err(|err| internal_err(format!("runtime error: {err}")))?
.map_err(|err| internal_err(format!("execution fatal: {err:?}")))?;

(info.exit_reason, info.value, info.used_gas.effective)
}
Expand Down Expand Up @@ -724,24 +766,53 @@ where
(info.exit_reason, Vec::new(), info.used_gas)
} else {
// Post-london + access list support
let access_list = access_list.unwrap_or_default();
let info = api.create(
substrate_hash,
from.unwrap_or_default(),
data,
value.unwrap_or_default(),
gas_limit,
max_fee_per_gas,
max_priority_fee_per_gas,
None,
estimate_mode,
Some(
let encoded_params = Encode::encode(&(
&from.unwrap_or_default(),
&data,
&value.unwrap_or_default(),
&gas_limit,
&max_fee_per_gas,
&max_priority_fee_per_gas,
&None::<Option<U256>>,
&estimate_mode,
&Some(
access_list
.unwrap_or_default()
.into_iter()
.map(|item| (item.address, item.storage_keys))
.collect(),
.collect::<Vec<(sp_core::H160, Vec<H256>)>>(),
),
)
));

// Enable proof size recording
let recorder: sp_trie::recorder::Recorder<HashingFor<B>> = Default::default();
let ext = sp_trie::proof_size_extension::ProofSizeExt::new(recorder.clone());
let mut exts = Extensions::new();
exts.register(ext);

let params = CallApiAtParams {
at: substrate_hash,
function: "EthereumRuntimeRPCApi_create",
arguments: encoded_params,
overlayed_changes: &RefCell::new(Default::default()),
call_context: CallContext::Offchain,
recorder: &Some(recorder),
extensions: &RefCell::new(exts),
};

let info = self
.client
.call_api_at(params)
.and_then(|r| {
Result::map_err(
<Result<ExecutionInfoV2::<H160>, DispatchError> as Decode>::decode(&mut &r[..]),
|error| sp_api::ApiError::FailedToDecodeReturnValue {
function: "EthereumRuntimeRPCApi_create",
error,
raw: r
},
)
})
.map_err(|err| internal_err(format!("runtime error: {err}")))?
.map_err(|err| internal_err(format!("execution fatal: {err:?}")))?;

Expand Down
6 changes: 6 additions & 0 deletions frame/evm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ impl-trait-for-tuples = "0.2.3"
log = { workspace = true }
scale-codec = { workspace = true }
scale-info = { workspace = true }

# Cumulus
cumulus-primitives-storage-weight-reclaim = { workspace = true, default-features = false }

# Substrate
frame-benchmarking = { workspace = true, optional = true }
frame-support = { workspace = true }
Expand Down Expand Up @@ -48,6 +52,8 @@ std = [
"log/std",
"scale-codec/std",
"scale-info/std",
# Cumulus
"cumulus-primitives-storage-weight-reclaim/std",
# Substrate
"frame-benchmarking?/std",
"frame-support/std",
Expand Down
Loading

0 comments on commit d5755b5

Please sign in to comment.