Skip to content

Commit

Permalink
feat: add secure weight charging for pop-api extension
Browse files Browse the repository at this point in the history
  • Loading branch information
peterwht committed Mar 2, 2024
1 parent 6c2b419 commit 37169d4
Showing 1 changed file with 9 additions and 11 deletions.
20 changes: 9 additions & 11 deletions runtime/src/extensions/pop_api_extension.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,26 +60,24 @@ where
{
let mut env = env.buf_in_buf_out();

// charge max weight before reading contract memory
// TODO: causing "1010: block limits exhausted" error
// let weight_limit = env.ext().gas_meter().gas_left();
// let charged_weight = env.charge_weight(weight_limit)?;

// TODO: debug_message weight is a good approximation of the additional overhead of going
// from contract layer to substrate layer.

// input length
let len = env.in_len();
let call: <T as SysConfig>::RuntimeCall = env.read_as_unbounded(len)?;

// conservative weight estimate for deserializing the input. The actual weight is less and should utilize a custom benchmark
let base_weight: Weight = T::DbWeight::get().reads(len.into());

// weight for dispatching the call
let dispatch_weight = call.get_dispatch_info().weight;

// charge weight for the cost of the deserialization and the dispatch
let _ = env.charge_weight(base_weight.saturating_add(dispatch_weight))?;

log::debug!(target:LOG_TARGET, " dispatch inputted RuntimeCall: {:?}", call);

let sender = env.ext().caller();
let origin: T::RuntimeOrigin = RawOrigin::Signed(sender.account_id()?.clone()).into();

// TODO: uncomment once charged_weight is fixed
// let actual_weight = call.get_dispatch_info().weight;
// env.adjust_weight(charged_weight, actual_weight);
let result = call.dispatch(origin);
match result {
Ok(info) => {
Expand Down

0 comments on commit 37169d4

Please sign in to comment.