Skip to content

Commit

Permalink
update tests, revert RUNTIME_API_BLOCK_LIMITS_COUNT
Browse files Browse the repository at this point in the history
  • Loading branch information
ukint-vs committed Sep 17, 2023
1 parent c1e1ef5 commit b7f0f3d
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 14 deletions.
2 changes: 1 addition & 1 deletion pallets/gear/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -796,7 +796,7 @@ pub mod pallet {
value,
allow_other_panics,
allow_skip_zero_replies,
Some(gas_allowance),
None,
);
GasAllowanceOf::<T>::put(gas_allowance);
if queue_processing {
Expand Down
20 changes: 16 additions & 4 deletions pallets/gear/src/runtime_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@ use core::convert::TryFrom;
use gear_core::pages::WasmPage;
use gear_wasm_instrument::syscalls::SysCallName;

// Multiplier 6 was experimentally found as median value for performance,
// security and abilities for calculations on-chain.
pub(crate) const RUNTIME_API_BLOCK_LIMITS_COUNT: u64 = 6;

pub(crate) struct CodeWithMemoryData {
pub instrumented_code: InstrumentedCode,
pub allocations: BTreeSet<WasmPage>,
Expand Down Expand Up @@ -114,7 +118,9 @@ where

let mut ext_manager = ExtManager::<T>::default();

let _ = Self::update_gas_allowance(allowance_multiplier.unwrap_or(1));
let _ = Self::update_gas_allowance(
allowance_multiplier.unwrap_or(RUNTIME_API_BLOCK_LIMITS_COUNT),
);

loop {
if QueueProcessingOf::<T>::denied() {
Expand Down Expand Up @@ -326,7 +332,9 @@ where
timestamp: <pallet_timestamp::Pallet<T>>::get().unique_saturated_into(),
};

let gas_allowance = Self::update_gas_allowance(allowance_multiplier.unwrap_or(1));
let gas_allowance = Self::update_gas_allowance(
allowance_multiplier.unwrap_or(RUNTIME_API_BLOCK_LIMITS_COUNT),
);

core_processor::informational::execute_for_reply::<ExecutionEnvironment<String>, String>(
function.into(),
Expand Down Expand Up @@ -366,7 +374,9 @@ where
timestamp: <pallet_timestamp::Pallet<T>>::get().unique_saturated_into(),
};

let gas_allowance = Self::update_gas_allowance(allowance_multiplier.unwrap_or(1));
let gas_allowance = Self::update_gas_allowance(
allowance_multiplier.unwrap_or(RUNTIME_API_BLOCK_LIMITS_COUNT),
);

core_processor::informational::execute_for_reply::<ExecutionEnvironment<String>, String>(
String::from("state"),
Expand Down Expand Up @@ -405,7 +415,9 @@ where
timestamp: <pallet_timestamp::Pallet<T>>::get().unique_saturated_into(),
};

let gas_allowance = Self::update_gas_allowance(allowance_multiplier.unwrap_or(1));
let gas_allowance = Self::update_gas_allowance(
allowance_multiplier.unwrap_or(RUNTIME_API_BLOCK_LIMITS_COUNT),
);

core_processor::informational::execute_for_reply::<ExecutionEnvironment<String>, String>(
String::from("metahash"),
Expand Down
27 changes: 18 additions & 9 deletions pallets/gear/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,7 +232,7 @@ fn read_big_state() {

assert_succeed(mid);
let state =
Gear::read_state_impl(pid, Default::default()).expect("Failed to read state");
Gear::read_state_impl(pid, Default::default(), None).expect("Failed to read state");
assert_eq!(approx_size(state.len(), i), expected_size(i));
}
});
Expand Down Expand Up @@ -2002,8 +2002,8 @@ fn read_state_works() {

let expected = Wallet::test_sequence().encode();

let res =
Gear::read_state_impl(program_id, Default::default()).expect("Failed to read state");
let res = Gear::read_state_impl(program_id, Default::default(), None)
.expect("Failed to read state");

assert_eq!(res, expected);
});
Expand Down Expand Up @@ -2044,6 +2044,7 @@ fn read_state_using_wasm_works() {
func1,
META_WASM_V1.to_vec(),
None,
None,
)
.expect("Failed to read state");

Expand All @@ -2069,6 +2070,7 @@ fn read_state_using_wasm_works() {
func2,
META_WASM_V2.to_vec(),
Some(id.encode()),
None,
)
.expect("Failed to read state");

Expand All @@ -2089,6 +2091,7 @@ fn read_state_bn_and_timestamp_works() {
"block_number",
META_WASM_V3.to_vec(),
None,
None,
)
.expect("Failed to read state");
let res = u32::decode(&mut res.as_ref()).unwrap();
Expand All @@ -2103,6 +2106,7 @@ fn read_state_bn_and_timestamp_works() {
"block_timestamp",
META_WASM_V3.to_vec(),
None,
None,
)
.expect("Failed to read state");
let res = u64::decode(&mut res.as_ref()).unwrap();
Expand Down Expand Up @@ -2164,6 +2168,7 @@ fn wasm_metadata_generation_works() {
"metadata",
META_WASM_V1.to_vec(),
None,
None,
)
.expect("Failed to read state");

Expand All @@ -2182,6 +2187,7 @@ fn wasm_metadata_generation_works() {
"metadata",
META_WASM_V2.to_vec(),
None,
None,
)
.expect("Failed to read state");

Expand Down Expand Up @@ -2234,7 +2240,8 @@ fn read_state_using_wasm_errors() {
Default::default(),
"inexistent",
meta_wasm.clone(),
None
None,
None,
)
.is_err());
// Empty function
Expand All @@ -2243,7 +2250,8 @@ fn read_state_using_wasm_errors() {
Default::default(),
"empty",
meta_wasm.clone(),
None
None,
None,
)
.is_err());
// Greed function
Expand All @@ -2252,7 +2260,8 @@ fn read_state_using_wasm_errors() {
Default::default(),
"loop",
meta_wasm,
None
None,
None,
)
.is_err());
});
Expand Down Expand Up @@ -6334,15 +6343,15 @@ fn state_request() {
run_to_next_block(None);

for (key, value) in data {
let ret =
Gear::read_state_impl(program_id, StateRequest::ForKey(key).encode()).unwrap();
let ret = Gear::read_state_impl(program_id, StateRequest::ForKey(key).encode(), None)
.unwrap();
assert_eq!(
Option::<u32>::decode(&mut ret.as_slice()).unwrap().unwrap(),
value
);
}

let ret = Gear::read_state_impl(program_id, StateRequest::Full.encode()).unwrap();
let ret = Gear::read_state_impl(program_id, StateRequest::Full.encode(), None).unwrap();
let ret = BTreeMap::<u32, u32>::decode(&mut ret.as_slice()).unwrap();
let expected: BTreeMap<u32, u32> = data.into_iter().collect();
assert_eq!(ret, expected);
Expand Down

0 comments on commit b7f0f3d

Please sign in to comment.