Skip to content

Commit

Permalink
fix block by seconds issue
Browse files Browse the repository at this point in the history
  • Loading branch information
lemunozm committed Oct 26, 2023
1 parent 26b9814 commit c2ca752
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 27 deletions.
5 changes: 3 additions & 2 deletions runtime/integration-tests/src/generic/cases/loans.rs
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,12 @@ fn update_maturity_extension<T: Runtime>() {
.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);
Expand Down
81 changes: 56 additions & 25 deletions runtime/integration-tests/src/generic/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<T: Runtime> {
pub enum Blocks<Event> {
/// 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<Event> Blocks<Event> {
fn range_for(&self, current: BlockNumber, slot_duration: Seconds) -> Range<BlockNumber> {
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
Expand All @@ -44,28 +64,15 @@ pub trait Env<T: Runtime> {
fn submit_later(&mut self, who: Keyring, call: impl Into<T::RuntimeCallExt>) -> DispatchResult;

/// Pass any number of blocks
fn pass(&mut self, blocks: Blocks<T>) {
let (next, end_block) = self.state(|| {
let current = frame_system::Pallet::<T>::block_number();

let end_block = current
+ match blocks {
Blocks::ByNumber(n) => n,
Blocks::BySeconds(secs) => {
let n = secs / pallet_aura::Pallet::<T>::slot_duration().into_seconds();
if n % pallet_aura::Pallet::<T>::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<T::RuntimeEventExt>) {
let (current, slot) = self.state(|| {
(
frame_system::Pallet::<T>::block_number(),
pallet_aura::Pallet::<T>::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() {
Expand Down Expand Up @@ -149,3 +156,27 @@ pub mod utils {
<T::Block as Block>::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<BlockNumber> {
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]);
}
}

0 comments on commit c2ca752

Please sign in to comment.