From 8eceeafdf6b6036540547095a5c62b56edb7a3e5 Mon Sep 17 00:00:00 2001 From: Michael Brozhko Date: Fri, 5 Apr 2024 11:45:50 +0100 Subject: [PATCH] update tests and allow lower_id with zero value --- pallets/token-manager/src/lib.rs | 14 ++- pallets/token-manager/src/mock.rs | 8 ++ pallets/token-manager/src/test_avt_tokens.rs | 99 ++++++++++++++++++++ primitives/avn-common/src/event_types.rs | 5 - 4 files changed, 116 insertions(+), 10 deletions(-) diff --git a/pallets/token-manager/src/lib.rs b/pallets/token-manager/src/lib.rs index 4229353e8..ac124f411 100644 --- a/pallets/token-manager/src/lib.rs +++ b/pallets/token-manager/src/lib.rs @@ -88,7 +88,6 @@ mod test_deferred_lower; mod test_growth; #[cfg(test)] mod test_lower_proof_generation; - #[cfg(test)] mod test_non_avt_tokens; #[cfg(test)] @@ -918,10 +917,15 @@ impl Pallet { let event_validity = T::ProcessedEventsChecker::check_event(event_id); ensure!(event_validity, Error::::NoTier1EventForLogLowerClaimed); - if !data.is_valid() { - Err(Error::::InvalidLowerId)? - } + ensure!( + LowersReadyToClaim::::contains_key(data.lower_id) == true, + Error::::InvalidLowerId + ); LowersReadyToClaim::::remove(data.lower_id); + ensure!( + LowersReadyToClaim::::contains_key(data.lower_id) == false, + Error::::InvalidLowerId + ); Self::deposit_event(Event::::AvtLowerClaimed { lower_id: data.lower_id }); @@ -971,7 +975,7 @@ impl Pallet { .map_err(|_| Error::::InvalidLowerCall)?, )?; - >::mutate(|mut nonce| *nonce += 1); + >::mutate(|nonce| *nonce += 1); Self::deposit_event(Event::::LowerRequested { token_id, diff --git a/pallets/token-manager/src/mock.rs b/pallets/token-manager/src/mock.rs index 7193e2ca5..2b03150cd 100644 --- a/pallets/token-manager/src/mock.rs +++ b/pallets/token-manager/src/mock.rs @@ -529,6 +529,7 @@ pub struct MockData { pub avt_token_lift_event: EthEvent, pub non_avt_token_lift_event: EthEvent, pub empty_data_lift_event: EthEvent, + pub lower_claimed_event: EthEvent, pub receiver_account_id: ::AccountId, pub token_balance_123_tokens: ::TokenBalance, } @@ -571,6 +572,13 @@ impl MockData { }, event_data: EventData::EmptyEvent, }, + lower_claimed_event: EthEvent { + event_id: EthEventId { + signature: ValidEvents::AvtLowerClaimed.signature(), + transaction_hash: H256::random(), + }, + event_data: EventData::LogLowerClaimed(AvtLowerClaimedData { lower_id: 0 }), + }, receiver_account_id, token_balance_123_tokens: Self::get_token_balance(AMOUNT_123_TOKEN), } diff --git a/pallets/token-manager/src/test_avt_tokens.rs b/pallets/token-manager/src/test_avt_tokens.rs index e55eeb14d..6d3153a0b 100644 --- a/pallets/token-manager/src/test_avt_tokens.rs +++ b/pallets/token-manager/src/test_avt_tokens.rs @@ -26,6 +26,61 @@ use pallet_balances::Error as BalancesError; const USE_RECEIVER_WITH_EXISTING_AMOUNT: bool = true; const USE_RECEIVER_WITH_0_AMOUNT: bool = false; +fn schedule_lower( + from: AccountId, + amount: u128, + t1_recipient: H160, + expected_lower_id: u32, + burn_acc: AccountId, +) { + assert_ok!(TokenManager::schedule_direct_lower( + RuntimeOrigin::signed(from), + from, + AVT_TOKEN_CONTRACT, + amount, + t1_recipient + )); + + // execute lower + fast_forward_to_block(get_expected_execution_block()); + + // Event emitted + assert!(System::events().iter().any(|a| a.event == + RuntimeEvent::TokenManager(crate::Event::::AvtLowered { + sender: from, + recipient: burn_acc, + amount, + t1_recipient, + lower_id: expected_lower_id + }))); +} + +fn perform_lower_setup(lower_id: u32) { + let (_, from, burn_acc, t1_recipient) = MockData::setup_lower_request_data(); + let pre_lower_balance = ::Balances::get((NON_AVT_TOKEN_ID, from)); + let amount = pre_lower_balance; + + let expected_lower_id = lower_id; + schedule_lower(from, amount, t1_recipient, expected_lower_id, burn_acc); + assert!(>::get(expected_lower_id).is_some()); + + let test_proof_data: Vec = "lowerProofReady".to_string().into(); + // Simulate the response from eth-bridge + assert_ok!(TokenManager::process_lower_proof_result( + expected_lower_id, + PALLET_ID.to_vec(), + Ok(test_proof_data.clone()) + )); + + // Generated proof should be stored in LowerReadyToClaim + assert_eq!( + >::get(expected_lower_id) + .unwrap() + .encoded_lower_data, + test_proof_data + ); +} + #[test] fn avn_test_lift_to_zero_balance_account_should_succeed() { let mut ext = ExtBuilder::build_default() @@ -341,3 +396,47 @@ fn avn_test_avt_token_total_lowered_amount_greater_than_balance_max_value_ok() { }))); }); } + +#[test] +fn avt_lower_claimed_succesfully() { + let mut ext = ExtBuilder::build_default() + .with_genesis_config() + .with_balances() + .as_externality(); + + ext.execute_with(|| { + let mock_data = MockData::setup(AMOUNT_123_TOKEN, USE_RECEIVER_WITH_0_AMOUNT); + let mock_event = &mock_data.lower_claimed_event; + insert_to_mock_processed_events(&mock_event.event_id); + + let lower_id = 0; + + perform_lower_setup(lower_id); + + assert_ok!(TokenManager::on_event_processed(&mock_event)); + + assert!(System::events().iter().any(|a| a.event == + RuntimeEvent::TokenManager(crate::Event::::AvtLowerClaimed { + lower_id + }))); + }); +} + +#[test] +fn avt_lower_claimed_fails_due_with_invalid_lower_id() { + let mut ext = ExtBuilder::build_default() + .with_genesis_config() + .with_balances() + .as_externality(); + + ext.execute_with(|| { + let mock_data = MockData::setup(AMOUNT_123_TOKEN, USE_RECEIVER_WITH_0_AMOUNT); + let mock_event = &mock_data.lower_claimed_event; + insert_to_mock_processed_events(&mock_event.event_id); + + assert_noop!( + TokenManager::on_event_processed(&mock_event), + Error::::InvalidLowerId + ); + }); +} diff --git a/primitives/avn-common/src/event_types.rs b/primitives/avn-common/src/event_types.rs index 871b94d3b..182006504 100644 --- a/primitives/avn-common/src/event_types.rs +++ b/primitives/avn-common/src/event_types.rs @@ -581,10 +581,6 @@ pub struct AvtLowerClaimedData { impl AvtLowerClaimedData { const TOPIC_LOWER_ID: usize = 1; - pub fn is_valid(&self) -> bool { - return !self.lower_id.is_zero() - } - pub fn parse_bytes(data: Option>, topics: Vec>) -> Result { if data.is_some() { return Err(Error::AvtLowerClaimedEventMissingData) @@ -632,7 +628,6 @@ impl EventData { EventData::LogNftCancelListing(d) => d.is_valid(), EventData::LogNftEndBatchListing(d) => d.is_valid(), EventData::LogAvtGrowthLifted(d) => d.is_valid(), - EventData::LogLowerClaimed(d) => d.is_valid(), EventData::EmptyEvent => true, _ => false, }