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

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

Merged
merged 2 commits into from
Nov 27, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ sc-transaction-pool = { git = "https://github.com/moonbeam-foundation/polkadot-s
sc-transaction-pool-api = { git = "https://github.com/moonbeam-foundation/polkadot-sdk", branch = "moonbeam-polkadot-stable2407" }
sc-utils = { git = "https://github.com/moonbeam-foundation/polkadot-sdk", branch = "moonbeam-polkadot-stable2407" }
# Substrate Primitive
sp-trie = { git = "https://github.com/moonbeam-foundation/polkadot-sdk", branch = "moonbeam-polkadot-stable2407", default-features = false }
sp-api = { git = "https://github.com/moonbeam-foundation/polkadot-sdk", branch = "moonbeam-polkadot-stable2407", default-features = false }
sp-block-builder = { git = "https://github.com/moonbeam-foundation/polkadot-sdk", branch = "moonbeam-polkadot-stable2407", default-features = false }
sp-blockchain = { git = "https://github.com/moonbeam-foundation/polkadot-sdk", branch = "moonbeam-polkadot-stable2407" }
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"] }
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 @@ -232,7 +232,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 @@ -254,6 +254,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 @@ -365,14 +371,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 @@ -763,27 +776,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>>, old_types::OldDispatchError> 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 @@ -851,24 +893,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
57 changes: 44 additions & 13 deletions frame/evm/src/runner/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,27 +302,55 @@ where
None => 0,
};

// Measure actual proof size usage (or get computed proof size)
let estimated_proof_size = executor
.state()
.weight_info()
.unwrap_or_default()
.proof_size_usage
.unwrap_or_default();

// Obtain the actual proof size usage using the ProofSizeExt host-function or fallback
// and use the estimated proof size
let actual_proof_size = if let Some(measured_proof_size_after) = get_proof_size() {
measured_proof_size_after.saturating_sub(measured_proof_size_before)
// actual_proof_size = proof_size_base_cost + proof_size measured with ProofSizeExt
let actual_proof_size =
proof_size_base_cost.unwrap_or_default().saturating_add(
measured_proof_size_after.saturating_sub(measured_proof_size_before),
);

log::trace!(
target: "evm",
"Proof size computation: (estimated: {}, actual: {})",
estimated_proof_size,
actual_proof_size
);

// If the proof_size calculated from the host-function gives an higher cost than
// the estimated proof_size, we should use the estimated proof_size to compute
// the PoV gas.
//
// TODO: The estimated proof_size should always be an overestimate
if actual_proof_size > estimated_proof_size {
log::debug!(
target: "evm",
"Proof size underestimation detected! (estimated: {}, actual: {})",
estimated_proof_size,
actual_proof_size
);
estimated_proof_size
} else {
actual_proof_size
}
} else {
executor
.state()
.weight_info()
.unwrap_or_default()
.proof_size_usage
.unwrap_or_default()
estimated_proof_size
};

// Post execution.
let pov_gas = actual_proof_size.saturating_mul(T::GasLimitPovSizeRatio::get());
let used_gas = executor.used_gas();
let effective_gas = U256::from(core::cmp::max(
core::cmp::max(used_gas, pov_gas),
storage_gas,
));
let effective_gas = core::cmp::max(core::cmp::max(used_gas, pov_gas), storage_gas);

(reason, retv, used_gas, effective_gas)
(reason, retv, used_gas, U256::from(effective_gas))
});

let actual_fee = effective_gas.saturating_mul(total_fee_per_gas);
Expand Down Expand Up @@ -1379,6 +1407,7 @@ mod tests {
false,
None,
None,
0,
|_| {
let res = Runner::<Test>::execute(
H160::default(),
Expand All @@ -1391,6 +1420,7 @@ mod tests {
false,
None,
None,
0,
|_| (ExitReason::Succeed(ExitSucceed::Stopped), ()),
);
assert_matches!(
Expand Down Expand Up @@ -1423,6 +1453,7 @@ mod tests {
false,
None,
None,
0,
|_| (ExitReason::Succeed(ExitSucceed::Stopped), ()),
);
assert!(res.is_ok());
Expand Down
7 changes: 0 additions & 7 deletions primitives/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,9 +129,6 @@ impl WeightInfo {
(self.ref_time_usage, self.ref_time_limit)
{
let ref_time_usage = self.try_consume(cost, ref_time_limit, ref_time_usage)?;
if ref_time_usage > ref_time_limit {
return Err(ExitError::OutOfGas);
}
self.ref_time_usage = Some(ref_time_usage);
}
Ok(())
Expand All @@ -142,10 +139,6 @@ impl WeightInfo {
(self.proof_size_usage, self.proof_size_limit)
{
let proof_size_usage = self.try_consume(cost, proof_size_limit, proof_size_usage)?;
if proof_size_usage > proof_size_limit {
storage_oog::set_storage_oog();
return Err(ExitError::OutOfGas);
}
self.proof_size_usage = Some(proof_size_usage);
}
Ok(())
Expand Down