From 85abcc03be8026080349596f39f479bb0668ca4a Mon Sep 17 00:00:00 2001 From: kylezs Date: Wed, 20 Sep 2023 16:15:20 +0200 Subject: [PATCH] test: all tests passing --- .../pallets/cf-ingress-egress/src/lib.rs | 6 +- .../pallets/cf-ingress-egress/src/tests.rs | 115 +++++++++--------- 2 files changed, 63 insertions(+), 58 deletions(-) diff --git a/state-chain/pallets/cf-ingress-egress/src/lib.rs b/state-chain/pallets/cf-ingress-egress/src/lib.rs index 22940e53c1b..a21482b7b18 100644 --- a/state-chain/pallets/cf-ingress-egress/src/lib.rs +++ b/state-chain/pallets/cf-ingress-egress/src/lib.rs @@ -399,9 +399,9 @@ pub mod pallet { fn on_idle(_n: BlockNumberFor, remaining_weight: Weight) -> Weight { let current_target_chain_height = T::ChainTracking::get_block_height(); - println!("Current target chain height: {:?}", current_target_chain_height); + // println!("Current target chain height: {:?}", current_target_chain_height); - for (address, details) in DepositChannelLookup::::iter() { + for (_, details) in DepositChannelLookup::::iter() { // We add an extra lifetime of safety. // The CFEs will stop witnessing the address of this deposit channel at the // expires_at block number. However, because the CFE uses a safety margin, and here @@ -412,7 +412,7 @@ pub mod pallet { if details.expires_at + DepositChannelLifetime::::get() <= current_target_chain_height { - println!("Recyling! Adding back to pool"); + // println!("Recyling! Adding back to pool"); if let Some(state) = details.deposit_channel.state.maybe_recycle() { DepositChannelPool::::insert( details.deposit_channel.channel_id, diff --git a/state-chain/pallets/cf-ingress-egress/src/tests.rs b/state-chain/pallets/cf-ingress-egress/src/tests.rs index 635692c0b76..cd359594fcd 100644 --- a/state-chain/pallets/cf-ingress-egress/src/tests.rs +++ b/state-chain/pallets/cf-ingress-egress/src/tests.rs @@ -862,58 +862,60 @@ fn handle_pending_deployment_same_block() { }); } -// #[test] -// fn channel_reuse_with_different_assets() { -// const ASSET_1: eth::Asset = eth::Asset::Eth; -// const ASSET_2: eth::Asset = eth::Asset::Flip; -// new_test_ext() -// // First, request a deposit address and use it, then close it so it gets recycled. -// .request_address_and_deposit(&[( -// DepositRequest::Liquidity { lp_account: ALICE, asset: ASSET_1 }, -// 100_000, -// )]) -// .map_context(|mut result| result.pop().unwrap()) -// .then_execute_at_next_block(|ctx| { -// // Dispatch callbacks to finalise the ingress. -// MockEgressBroadcaster::dispatch_all_callbacks(); -// ctx -// }) -// .inspect_storage(|(request, _, address)| { -// let asset = request.source_asset(); -// assert_eq!(asset, ASSET_1); -// assert!( -// DepositChannelLookup::::get(address).unwrap().deposit_channel.asset == -// asset -// ); -// }) -// // move forward expired blocks -// .then_execute_at_next_block(|(details, channel_id, channel_address)| { -// IngressEgress::recycle_channel(channel_address); -// channel_id -// }) -// .inspect_storage(|channel_id| { -// assert!(DepositChannelLookup::::get(ALICE_ETH_ADDRESS).is_none()); -// assert!( -// DepositChannelPool::::iter_values().next().unwrap().channel_id == -// *channel_id -// ); -// }) -// // Request a new address with a different asset. -// .request_deposit_addresses(&[DepositRequest::Liquidity { -// lp_account: ALICE, -// asset: ASSET_2, -// }]) -// .map_context(|mut result| result.pop().unwrap()) -// // Ensure that the deposit channel's asset is updated. -// .inspect_storage(|(request, _, address)| { -// let asset = request.source_asset(); -// assert_eq!(asset, ASSET_2); -// assert!( -// DepositChannelLookup::::get(address).unwrap().deposit_channel.asset == -// asset -// ); -// }); -// } +#[test] +fn channel_reuse_with_different_assets() { + const ASSET_1: eth::Asset = eth::Asset::Eth; + const ASSET_2: eth::Asset = eth::Asset::Flip; + new_test_ext() + // First, request a deposit address and use it, then close it so it gets recycled. + .request_address_and_deposit(&[( + DepositRequest::Liquidity { lp_account: ALICE, asset: ASSET_1 }, + 100_000, + )]) + .map_context(|mut result| result.pop().unwrap()) + .then_execute_at_next_block(|ctx| { + // Dispatch callbacks to finalise the ingress. + MockEgressBroadcaster::dispatch_all_callbacks(); + ctx + }) + .inspect_storage(|(request, _, address)| { + let asset = request.source_asset(); + assert_eq!(asset, ASSET_1); + assert!( + DepositChannelLookup::::get(address).unwrap().deposit_channel.asset == + asset + ); + }) + // move forward expired blocks + .then_execute_at_next_block(|(_, channel_id, _)| { + let recycle_block = BlockHeightProvider::::get_block_height() + + DepositChannelLifetime::::get() * 2; + BlockHeightProvider::::set_block_height(recycle_block); + channel_id + }) + .inspect_storage(|channel_id| { + assert!(DepositChannelLookup::::get(ALICE_ETH_ADDRESS).is_none()); + assert!( + DepositChannelPool::::iter_values().next().unwrap().channel_id == + *channel_id + ); + }) + // Request a new address with a different asset. + .request_deposit_addresses(&[DepositRequest::Liquidity { + lp_account: ALICE, + asset: ASSET_2, + }]) + .map_context(|mut result| result.pop().unwrap()) + // Ensure that the deposit channel's asset is updated. + .inspect_storage(|(request, _, address)| { + let asset = request.source_asset(); + assert_eq!(asset, ASSET_2); + assert!( + DepositChannelLookup::::get(address).unwrap().deposit_channel.asset == + asset + ); + }); +} /// This is the sequence we're testing. /// 1. Request deposit address @@ -922,11 +924,14 @@ fn handle_pending_deployment_same_block() { /// 4. We need to finalise the ingress, by fetching /// 5. The fetch should succeed. #[test] -fn ingress_finalisation_succeeds_after_channel_expiry() { +fn ingress_finalisation_succeeds_after_channel_recycled() { new_test_ext().execute_with(|| { - let (_, deposit_address) = request_address_and_deposit(ALICE, eth::Asset::Eth); + request_address_and_deposit(ALICE, eth::Asset::Eth); - // On idle, to recycle the channel? + let recycle_block = BlockHeightProvider::::get_block_height() + + DepositChannelLifetime::::get() * 2; + BlockHeightProvider::::set_block_height(recycle_block); + IngressEgress::on_idle(1, Weight::MAX); IngressEgress::on_finalize(1);