Skip to content

Commit

Permalink
Fix add size_limit to the proof_size of weight info
Browse files Browse the repository at this point in the history
  • Loading branch information
boundless-forest committed Nov 2, 2023
1 parent 56f56f9 commit 1fa7033
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 17 deletions.
4 changes: 2 additions & 2 deletions client/rpc/src/eth/execute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -867,8 +867,8 @@ where
previous_highest = highest;
}
ExitReason::Revert(_)
| ExitReason::Error(ExitError::InvalidCode(_)) |
ExitReason::Error(ExitError::OutOfGas) => {
| ExitReason::Error(ExitError::OutOfGas)
| ExitReason::Error(ExitError::InvalidCode(_)) => {
lowest = mid;
}
other => error_on_execution_failure(&other, &data)?,
Expand Down
36 changes: 21 additions & 15 deletions frame/evm/src/runner/stack.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1000,12 +1000,17 @@ where
weight_info.try_record_proof_size_or_fail(meta.size)?;
} else {
frame_support::log::debug!(target: "bear", "bear: --- record_external_operation, doesn't hit metadata");
// If it does not exist, try to record `create_contract_limit` first.
weight_info.try_record_proof_size_or_fail(size_limit)?;
let meta = Pallet::<T>::account_code_metadata(address);
let actual_size = meta.size;
// Refund if applies
weight_info.refund_proof_size(size_limit.saturating_sub(actual_size));
if let Some(remaining_proof_size) = weight_info.remaining_proof_size() {
let pre_size = remaining_proof_size.min(size_limit);
weight_info.try_record_proof_size_or_fail(pre_size)?;

let actual_size = Pallet::<T>::account_code_metadata(address).size;
if actual_size > pre_size {
return Err(ExitError::OutOfGas);
}
// Refund if applies
weight_info.refund_proof_size(pre_size.saturating_sub(actual_size));
}
}
recorded.account_codes.push(address);
}
Expand Down Expand Up @@ -1101,16 +1106,17 @@ where
weight_info.try_record_proof_size_or_fail(meta.size)?;
} else {
frame_support::log::debug!(target: "bear", "bear: --- record_external_dynamic_opcode_cost, doesn't hit metadata");
// If it does not exist, try to record `create_contract_limit` first.
if let Some(remaining_proof_size) = weight_info.remaining_proof_size() {
let pre_size = remaining_proof_size.min(size_limit);
weight_info.try_record_proof_size_or_fail(pre_size)?;

// weight_info.try_record_proof_size_or_fail(self.proof_size_left)?;
weight_info.try_record_proof_size_or_fail(size_limit)?;

let meta = Pallet::<T>::account_code_metadata(address);
let actual_size = meta.size;

// Refund if applies
weight_info.refund_proof_size(size_limit.saturating_sub(actual_size));
let actual_size = Pallet::<T>::account_code_metadata(address).size;
if actual_size > pre_size {
return Err(ExitError::OutOfGas);
}
// Refund if applies
weight_info.refund_proof_size(pre_size.saturating_sub(actual_size));
}
}
recorded.account_codes.push(address);
// Already recorded, return
Expand Down
19 changes: 19 additions & 0 deletions primitives/evm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ impl WeightInfo {
_ => return Err("must provide Some valid weight limit or None"),
})
}

fn try_consume(&self, cost: u64, limit: u64, usage: u64) -> Result<u64, ExitError> {
// frame_support::log::debug!(target: "bear", "bear: --- try_consume, original_usage: {:?}, cost: {:?}, after that: {:?}", usage, cost, usage + cost);
let usage = usage.checked_add(cost).ok_or(ExitError::OutOfGas)?;
Expand All @@ -117,6 +118,7 @@ impl WeightInfo {
}
Ok(usage)
}

pub fn try_record_ref_time_or_fail(&mut self, cost: u64) -> Result<(), ExitError> {
if let (Some(ref_time_usage), Some(ref_time_limit)) =
(self.ref_time_usage, self.ref_time_limit)
Expand All @@ -130,6 +132,7 @@ impl WeightInfo {
}
Ok(())
}

pub fn try_record_proof_size_or_fail(&mut self, cost: u64) -> Result<(), ExitError> {
if let (Some(proof_size_usage), Some(proof_size_limit)) =
(self.proof_size_usage, self.proof_size_limit)
Expand All @@ -143,19 +146,35 @@ impl WeightInfo {
}
Ok(())
}

pub fn refund_proof_size(&mut self, amount: u64) {
frame_support::log::debug!(target: "bear", "bear: --- refund_proof_size, amount: {:?}", amount);
if let Some(proof_size_usage) = self.proof_size_usage {
let proof_size_usage = proof_size_usage.saturating_sub(amount);
self.proof_size_usage = Some(proof_size_usage);
}
}

pub fn refund_ref_time(&mut self, amount: u64) {
if let Some(ref_time_usage) = self.ref_time_usage {
let ref_time_usage = ref_time_usage.saturating_sub(amount);
self.ref_time_usage = Some(ref_time_usage);
}
}
pub fn remaining_proof_size(&self) -> Option<u64> {
if let (Some(proof_size_usage), Some(proof_size_limit)) = (self.proof_size_usage, self.proof_size_limit) {
return Some(proof_size_limit.saturating_sub(proof_size_usage));
}
None
}

pub fn remaining_ref_time(&self) -> Option<u64> {
if let (Some(ref_time_usage), Some(ref_time_limit)) = (self.ref_time_usage, self.ref_time_limit) {
return Some(ref_time_limit.saturating_sub(ref_time_usage));
}
None
}

}

#[derive(Clone, Eq, PartialEq, Debug, Encode, Decode, TypeInfo)]
Expand Down

0 comments on commit 1fa7033

Please sign in to comment.