diff --git a/runtime/integration-tests/src/generic/cases/loans.rs b/runtime/integration-tests/src/generic/cases/loans.rs index 40ccbe6dfc..cab0d66112 100644 --- a/runtime/integration-tests/src/generic/cases/loans.rs +++ b/runtime/integration-tests/src/generic/cases/loans.rs @@ -356,11 +356,12 @@ fn update_maturity_extension() { .unwrap(); let change_id = common::last_change_id(&env); - env.submit_now(LOAN_ADMIN, call::apply_loan_mutation(change_id)) + env.submit_now(ANY, call::apply_loan_mutation(change_id)) .unwrap(); // Now the loan is no longer overdue and can be borrowed again - env.submit_now(ANY, call::borrow_internal(loan_id)).unwrap(); + env.submit_now(BORROWER, call::borrow_internal(loan_id)) + .unwrap(); } crate::test_for_runtimes!(all, internal_priced); diff --git a/runtime/integration-tests/src/generic/env.rs b/runtime/integration-tests/src/generic/env.rs index 8dee7b01ec..d8dd5a48e4 100644 --- a/runtime/integration-tests/src/generic/env.rs +++ b/runtime/integration-tests/src/generic/env.rs @@ -6,25 +6,45 @@ use sp_runtime::{ traits::{Block, Extrinsic}, DispatchError, DispatchResult, MultiSignature, Storage, }; +use sp_std::ops::Range; use crate::{generic::config::Runtime, utils::accounts::Keyring}; /// Used by Env::pass() to determine how many blocks should be passed #[derive(Clone)] -pub enum Blocks { +pub enum Blocks { /// Pass X blocks ByNumber(BlockNumber), /// Pass a number of blocks enough to emulate the given passage of time. /// i.e. choosing 1 sec would pass 1 block to emulate such change in the /// time. + /// See the test below for an example BySeconds(Seconds), /// Pass a number of block until find an event or reach the limit - UntilEvent { - event: T::RuntimeEventExt, - limit: BlockNumber, - }, + UntilEvent { event: Event, limit: BlockNumber }, +} + +impl Blocks { + fn range_for(&self, current: BlockNumber, slot_duration: Seconds) -> Range { + let blocks = match self { + Blocks::ByNumber(n) => *n, + Blocks::BySeconds(secs) => { + let n = secs / slot_duration; + if secs % slot_duration != 0 { + n as BlockNumber + 1 + } else { + n as BlockNumber + } + } + Blocks::UntilEvent { limit, .. } => *limit, + }; + + dbg!(blocks); + + (current + 1)..(current + 1 + blocks) + } } /// Define an environment behavior @@ -44,28 +64,15 @@ pub trait Env { fn submit_later(&mut self, who: Keyring, call: impl Into) -> DispatchResult; /// Pass any number of blocks - fn pass(&mut self, blocks: Blocks) { - let (next, end_block) = self.state(|| { - let current = frame_system::Pallet::::block_number(); - - let end_block = current - + match blocks { - Blocks::ByNumber(n) => n, - Blocks::BySeconds(secs) => { - let n = secs / pallet_aura::Pallet::::slot_duration().into_seconds(); - if n % pallet_aura::Pallet::::slot_duration() != 0 { - n as BlockNumber + 1 - } else { - n as BlockNumber - } - } - Blocks::UntilEvent { limit, .. } => limit, - }; - - (current + 1, end_block) + fn pass(&mut self, blocks: Blocks) { + let (current, slot) = self.state(|| { + ( + frame_system::Pallet::::block_number(), + pallet_aura::Pallet::::slot_duration().into_seconds(), + ) }); - for i in next..end_block { + for i in blocks.range_for(current, slot) { self.__priv_build_block(i); if let Blocks::UntilEvent { event, .. } = blocks.clone() { @@ -149,3 +156,27 @@ pub mod utils { ::Extrinsic::new(runtime_call, Some(multi_address)).unwrap() } } + +mod tests { + use super::*; + struct MockEnv; + + const SLOT_DURATION: Seconds = 12; + + fn blocks_from(current: BlockNumber, blocks: Blocks<()>) -> Vec { + blocks + .range_for(current, SLOT_DURATION) + .into_iter() + .collect() + } + + #[test] + fn by_seconds() { + assert_eq!(blocks_from(0, Blocks::BySeconds(0)), [] as [BlockNumber; 0]); + assert_eq!(blocks_from(0, Blocks::BySeconds(1)), [1]); + assert_eq!(blocks_from(0, Blocks::BySeconds(12)), [1]); + assert_eq!(blocks_from(5, Blocks::BySeconds(0)), [] as [BlockNumber; 0]); + assert_eq!(blocks_from(5, Blocks::BySeconds(12)), [6]); + assert_eq!(blocks_from(5, Blocks::BySeconds(60)), [6, 7, 8, 9, 10]); + } +}