Skip to content

Commit

Permalink
fix: use mutate, rename
Browse files Browse the repository at this point in the history
  • Loading branch information
dandanlen committed Sep 22, 2023
1 parent 7d5ea1a commit 44ba4a0
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 44 deletions.
44 changes: 19 additions & 25 deletions state-chain/pallets/cf-ingress-egress/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ pub mod pallet {
};
use sp_std::vec::Vec;

pub(crate) type RecycleAddressAtBlock<T, I> =
pub(crate) type ChannelRecycleQueue<T, I> =
Vec<(TargetChainBlockNumber<T, I>, TargetChainAccount<T, I>)>;

pub(crate) type TargetChainAsset<T, I> = <<T as Config<I>>::TargetChain as Chain>::ChainAsset;
Expand Down Expand Up @@ -337,7 +337,7 @@ pub mod pallet {

#[pallet::storage]
pub type DepositChannelRecycleBlocks<T: Config<I>, I: 'static = ()> =
StorageValue<_, Vec<(TargetChainBlockNumber<T, I>, TargetChainAccount<T, I>)>, ValueQuery>;
StorageValue<_, ChannelRecycleQueue<T, I>, ValueQuery>;

#[pallet::event]
#[pallet::generate_deposit(pub(super) fn deposit_event)]
Expand Down Expand Up @@ -415,11 +415,13 @@ pub mod pallet {
.unwrap_or_default()
.saturated_into::<usize>();

let (can_recycle, cannot_recycle) = Self::can_and_cannot_recycle(
maximum_recycle_number,
DepositChannelRecycleBlocks::<T, I>::take(),
T::ChainTracking::get_block_height(),
);
let can_recycle = DepositChannelRecycleBlocks::<T, I>::mutate(|recycle_queue| {
Self::can_and_cannot_recycle(
recycle_queue,
maximum_recycle_number,
T::ChainTracking::get_block_height(),
)
});

for address in can_recycle.iter() {
if let Some(details) = DepositChannelLookup::<T, I>::take(address) {
Expand All @@ -432,8 +434,6 @@ pub mod pallet {
}
}

DepositChannelRecycleBlocks::<T, I>::put(cannot_recycle);

read_write_weight.saturating_mul(can_recycle.len() as u64)
}

Expand Down Expand Up @@ -587,24 +587,18 @@ pub mod pallet {

impl<T: Config<I>, I: 'static> Pallet<T, I> {
fn can_and_cannot_recycle(
channel_recycle_blocks: &mut ChannelRecycleQueue<T, I>,
maximum_recyclable_number: usize,
channel_recycle_blocks: RecycleAddressAtBlock<T, I>,
current_block_height: TargetChainBlockNumber<T, I>,
) -> (Vec<TargetChainAccount<T, I>>, RecycleAddressAtBlock<T, I>) {
let (ready_to_recycle, not_ready_to_recycle) = channel_recycle_blocks
.into_iter()
.partition::<Vec<_>, _>(|(block, _)| *block <= current_block_height);

let (can_recycle, mut cannot_recycle) =
if maximum_recyclable_number < ready_to_recycle.len() {
let (can, cannot) = ready_to_recycle.split_at(maximum_recyclable_number);
(can.to_vec(), cannot.to_vec())
} else {
(ready_to_recycle, Vec::new())
};

cannot_recycle.extend(not_ready_to_recycle.to_vec());
(can_recycle.into_iter().map(|(_, a)| a).collect(), cannot_recycle)
) -> Vec<TargetChainAccount<T, I>> {
let partition_point = sp_std::cmp::min(
channel_recycle_blocks.partition_point(|(block, _)| *block <= current_block_height),
maximum_recyclable_number,
);
channel_recycle_blocks
.drain(..partition_point)
.map(|(_, address)| address)
.collect()
}

/// Take all scheduled egress requests and send them out in an `AllBatch` call.
Expand Down
41 changes: 22 additions & 19 deletions state-chain/pallets/cf-ingress-egress/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1025,67 +1025,70 @@ fn basic_balance_tracking() {

#[test]
fn test_default_empty_amounts() {
let (can_recycle, cannot_recycle) =
IngressEgress::can_and_cannot_recycle(0, Default::default(), 0);
let mut channel_recycle_blocks = Default::default();
let can_recycle = IngressEgress::can_and_cannot_recycle(&mut channel_recycle_blocks, 0, 0);

assert_eq!(can_recycle, vec![]);
assert_eq!(cannot_recycle, vec![]);
assert_eq!(channel_recycle_blocks, vec![]);
}

#[test]
fn test_cannot_recycle_if_block_number_less_than_current_height() {
let maximum_recyclable_number = 2;
let channel_recycle_blocks =
let mut channel_recycle_blocks =
(1u64..5).map(|i| (i, H160::from([i as u8; 20]))).collect::<Vec<_>>();
let current_block_height = 3;

let (can_recycle, cannot_recycle) = IngressEgress::can_and_cannot_recycle(
let can_recycle = IngressEgress::can_and_cannot_recycle(
&mut channel_recycle_blocks,
maximum_recyclable_number,
channel_recycle_blocks,
current_block_height,
);

assert_eq!(can_recycle, vec![H160::from([1u8; 20]), H160::from([2; 20])]);
assert_eq!(cannot_recycle, vec![(3, H160::from([3u8; 20])), (4, H160::from([4u8; 20]))]);
assert_eq!(
channel_recycle_blocks,
vec![(3, H160::from([3u8; 20])), (4, H160::from([4u8; 20]))]
);
}

// Same test as above, but lower maximum recyclable number
#[test]
fn test_can_only_recycle_up_to_max_amount() {
let maximum_recyclable_number = 1;
let channel_recycle_blocks =
let mut channel_recycle_blocks =
(1u64..5).map(|i| (i, H160::from([i as u8; 20]))).collect::<Vec<_>>();
let current_block_height = 3;

let (can_recycle, cannot_recycle) = IngressEgress::can_and_cannot_recycle(
let can_recycle = IngressEgress::can_and_cannot_recycle(
&mut channel_recycle_blocks,
maximum_recyclable_number,
channel_recycle_blocks,
current_block_height,
);

assert_eq!(can_recycle, vec![H160::from([1u8; 20])]);
assert_eq!(
cannot_recycle,
channel_recycle_blocks,
vec![(2, H160::from([2; 20])), (3, H160::from([3u8; 20])), (4, H160::from([4u8; 20]))]
);
}

#[test]
fn none_can_be_recycled_due_to_low_block_number() {
let maximum_recyclable_number = 4;
let channel_recycle_blocks =
let mut channel_recycle_blocks =
(1u64..5).map(|i| (i, H160::from([i as u8; 20]))).collect::<Vec<_>>();
let current_block_height = 0;

let (can_recycle, cannot_recycle) = IngressEgress::can_and_cannot_recycle(
let can_recycle = IngressEgress::can_and_cannot_recycle(
&mut channel_recycle_blocks,
maximum_recyclable_number,
channel_recycle_blocks,
current_block_height,
);

assert!(can_recycle.is_empty());
assert_eq!(
cannot_recycle,
channel_recycle_blocks,
vec![
(1, H160::from([1u8; 20])),
(2, H160::from([2; 20])),
Expand All @@ -1098,19 +1101,19 @@ fn none_can_be_recycled_due_to_low_block_number() {
#[test]
fn all_can_be_recycled() {
let maximum_recyclable_number = 4;
let channel_recycle_blocks =
let mut channel_recycle_blocks =
(1u64..5).map(|i| (i, H160::from([i as u8; 20]))).collect::<Vec<_>>();
let current_block_height = 4;

let (can_recycle, cannot_recycle) = IngressEgress::can_and_cannot_recycle(
let can_recycle = IngressEgress::can_and_cannot_recycle(
&mut channel_recycle_blocks,
maximum_recyclable_number,
channel_recycle_blocks,
current_block_height,
);

assert_eq!(
can_recycle,
vec![H160::from([1u8; 20]), H160::from([2; 20]), H160::from([3; 20]), H160::from([4; 20])]
);
assert!(cannot_recycle.is_empty());
assert!(channel_recycle_blocks.is_empty());
}

0 comments on commit 44ba4a0

Please sign in to comment.