From 37169d4ce3880f8ba4253827047cb74794dea8d8 Mon Sep 17 00:00:00 2001 From: Peter White Date: Sat, 2 Mar 2024 14:46:20 -0700 Subject: [PATCH] feat: add secure weight charging for pop-api extension --- runtime/src/extensions/pop_api_extension.rs | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/runtime/src/extensions/pop_api_extension.rs b/runtime/src/extensions/pop_api_extension.rs index 7e3b77c5..c3f1bf21 100644 --- a/runtime/src/extensions/pop_api_extension.rs +++ b/runtime/src/extensions/pop_api_extension.rs @@ -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: ::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) => {