Skip to content

Conversation

@filip-parity
Copy link

@filip-parity filip-parity commented Oct 24, 2025

Limit the PVM per-call gas cap using the runtime’s Normal-class per‑extrinsic weight (BlockWeights minus base_extrinsic) and a storage deposit cap derived from the caller’s free balance minus existential deposit and value sent. The cap is encoded via the runtime’s EthGasEncoder and clamped by the transaction’s gas_limit to align execution with runtime constraints and keep gas reporting accurate.

Motivation

PVM calls were capped using unbounded values, which could exceed Substrate runtime limits and account constraints. This PR aligns the PVM gas cap with the runtime’s per‑extrinsic weight rules and storage deposit requirements to ensure realistic execution limits and consistent gas reporting.

Solution

The max gas capping flow when a contract CREATE or CALL happens in PVM mode:

Step 1: Compute system maximum weight
We read the blockchain's configuration for normal user transactions, get the maximum weight allowed per transaction, and subtract the base overhead cost, the resulting value being the max ceiling.

Step 2: User's gas request validation
We take the user's requested gas limit from their transaction, convert it into Substrate's Weight format where ref_time equals the gas amount and proof_size is set to u64::MAX, then take the minimum between the user request and system allowance.

Step 3: Compute available balance for storage
We get the caller's account balance and subtract the minimum balance that must remain (existential deposit) and the value being sent with the transaction, with the leftover value representing the maximum storage deposit they can afford.

Step 4: Execute the contract
We call the revive pallet with the computed weight_limit and storage_deposit_cap from steps 2 and 3.

Step 5: Record actual gas used
After execution, we get the consumed weight and convert it back to gas units using the same ref_time mapping, then clamp this to the transaction gas limit and record it as the gas cost for the transaction.

@filip-parity filip-parity self-assigned this Oct 24, 2025
@filip-parity filip-parity changed the title Issues/358 [issues/358] - Capped PVM max gas Oct 27, 2025
// 1) Weight cap
let block_weights = <Runtime as frame_system::Config>::BlockWeights::get();
let normal = block_weights.get(DispatchClass::Normal);
let max_weight_cap =
Copy link

Choose a reason for hiding this comment

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

seems weird to cap it to max per extrinsics. The call has a gas_limit I would expected that what we have to do is convert the gas limit to a weight and storage deposit equivalent.

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