Skip to content

Commit ff72bbb

Browse files
committed
refactor: Improve weight-to-gas conversion and simplify nonce validation
- Use SignedGas::from_weight_fee().to_ethereum_gas() for proper weight-to-gas conversion that accounts for both ref_time and proof_size - Simplify nonce validation in eip7702.rs using try_into() instead of separate bounds check - Compare nonces as u64 directly instead of converting back and forth with U256
1 parent 9884cc8 commit ff72bbb

File tree

2 files changed

+26
-25
lines changed

2 files changed

+26
-25
lines changed

substrate/frame/revive/src/evm/eip7702.rs

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -103,17 +103,18 @@ pub(crate) fn validate_authorization<T: Config>(
103103
return None;
104104
}
105105

106-
// Validate nonce is within bounds
107-
if auth.nonce >= U256::from(u64::MAX) {
108-
log::debug!(
109-
target: crate::LOG_TARGET,
110-
"Authorization nonce too large: {:?}",
111-
auth.nonce
112-
);
113-
return None;
114-
}
106+
let expected_nonce: u64 = match auth.nonce.try_into() {
107+
Ok(nonce) => nonce,
108+
Err(_) => {
109+
log::debug!(
110+
target: crate::LOG_TARGET,
111+
"Authorization nonce too large: {:?}",
112+
auth.nonce
113+
);
114+
return None;
115+
},
116+
};
115117

116-
// Recover authority address from signature
117118
let authority = match recover_authority(auth) {
118119
Ok(addr) => addr,
119120
Err(_) => {
@@ -122,16 +123,14 @@ pub(crate) fn validate_authorization<T: Config>(
122123
},
123124
};
124125

125-
// Verify nonce matches and check if account exists
126126
let account_id = T::AddressMapper::to_account_id(&authority);
127127
let is_new_account = !frame_system::Account::<T>::contains_key(&account_id);
128-
let current_nonce = frame_system::Pallet::<T>::account_nonce(&account_id);
129-
let expected_nonce = auth.nonce;
128+
let current_nonce: u64 = frame_system::Pallet::<T>::account_nonce(&account_id).saturated_into();
130129

131-
if U256::from(current_nonce.saturated_into::<u64>()) != expected_nonce {
130+
if current_nonce != expected_nonce {
132131
log::debug!(
133132
target: crate::LOG_TARGET,
134-
"Nonce mismatch for {authority:?}: expected {current_nonce:?}, got {expected_nonce:?}",
133+
"Nonce mismatch for {authority:?}: expected {expected_nonce:?}, got {current_nonce:?}",
135134
);
136135
return None;
137136
}

substrate/frame/revive/src/lib.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1401,11 +1401,13 @@ pub mod pallet {
14011401
&mut meter,
14021402
)?;
14031403

1404-
let consumed_weight = meter.consumed();
1405-
let gas_scale: u64 = T::GasScale::get().into();
1406-
let auth_gas = consumed_weight.ref_time().saturating_div(gas_scale);
1404+
let weight_fee = T::FeeInfo::weight_to_fee(&meter.consumed());
1405+
let auth_gas = metering::SignedGas::<T>::from_weight_fee(weight_fee)
1406+
.to_ethereum_gas()
1407+
.unwrap_or_default();
14071408

1408-
let adjusted_gas_limit = eth_gas_limit.saturating_sub(U256::from(auth_gas));
1409+
let adjusted_gas_limit =
1410+
eth_gas_limit.saturating_sub(U256::from(auth_gas.saturated_into::<u128>()));
14091411
let adjusted_weight_limit = meter.remaining();
14101412

14111413
(adjusted_gas_limit, adjusted_weight_limit)
@@ -1917,11 +1919,12 @@ impl<T: Config> Pallet<T> {
19171919
EthTransactError::Message(format!("Failed to process authorizations: {err:?}"))
19181920
})?;
19191921

1920-
let consumed_weight = meter.consumed();
1921-
let gas_scale: u64 = T::GasScale::get().into();
1922-
consumed_weight.ref_time().saturating_div(gas_scale)
1922+
let weight_fee = T::FeeInfo::weight_to_fee(&meter.consumed());
1923+
metering::SignedGas::<T>::from_weight_fee(weight_fee)
1924+
.to_ethereum_gas()
1925+
.unwrap_or_default()
19231926
} else {
1924-
0
1927+
Default::default()
19251928
};
19261929

19271930
let extract_error = |err| {
@@ -2076,8 +2079,7 @@ impl<T: Config> Pallet<T> {
20762079
if !rest.is_zero() {
20772080
eth_gas = eth_gas.saturating_add(1_u32.into());
20782081
}
2079-
// Add gas consumed by EIP-7702 authorization processing
2080-
eth_gas = eth_gas.saturating_add(U256::from(auth_gas_used));
2082+
eth_gas = eth_gas.saturating_add(U256::from(auth_gas_used.saturated_into::<u128>()));
20812083

20822084
log::debug!(target: LOG_TARGET, "\
20832085
dry_run_eth_transact finished: \

0 commit comments

Comments
 (0)