From a41b3b2441b4c31f84625c9761f9ec7f3a055daf Mon Sep 17 00:00:00 2001 From: Raid Ateir Date: Thu, 31 Aug 2023 12:42:24 +0200 Subject: [PATCH 1/4] unit tests in progress --- pallets/ddc-validator/src/lib.rs | 13 +++- pallets/ddc-validator/src/tests.rs | 97 +++++++++++++++++++++++++++++- 2 files changed, 107 insertions(+), 3 deletions(-) diff --git a/pallets/ddc-validator/src/lib.rs b/pallets/ddc-validator/src/lib.rs index 6dbf95561..e83211846 100644 --- a/pallets/ddc-validator/src/lib.rs +++ b/pallets/ddc-validator/src/lib.rs @@ -922,14 +922,23 @@ pub mod pallet { let signer = Self::get_signer().unwrap(); - let _tx_res = + let tx_res = signer.send_signed_transaction(|_acct| Call::set_validation_decision { era: current_ddc_era - 1, cdn_node: utils::string_to_account::(edge.clone()), validation_decision: final_res.clone(), }); - log::debug!("final_res: {:?}", final_res); + match &tx_res { + None | Some((_, Err(()))) => return Err("Error:"), + Some((_, Ok(()))) => {}, + } + + // match tx_res.result { + // Ok(tx_res) => println!("successfull tx: {:?}", tx_res), + // Err(error) => println!("failed tx: {:?}", error), + // } + // log::debug!("final_res: {:?}", final_res); cdn_nodes_reward_points.push(( utils::string_to_account::(final_res.edge), diff --git a/pallets/ddc-validator/src/tests.rs b/pallets/ddc-validator/src/tests.rs index b728f0fd1..f3c069d01 100644 --- a/pallets/ddc-validator/src/tests.rs +++ b/pallets/ddc-validator/src/tests.rs @@ -1,6 +1,10 @@ use super::*; -use crate::mock::{Timestamp, *}; +use crate::{ + mock::{Timestamp, *}, + Error as ValidatorError, +}; use codec::Decode; +use frame_support::{assert_noop, assert_ok}; use pallet_ddc_accounts::BucketsDetails; use pallet_ddc_staking::{DDC_ERA_DURATION_MS, DDC_ERA_START_MS}; use sp_core::offchain::{testing, OffchainDbExt, OffchainWorkerExt, TransactionPoolExt}; @@ -218,3 +222,94 @@ fn it_sets_validation_decision_with_one_validator_in_quorum() { ); }) } + +#[test] +fn send_signal_works_as_expected() { + let mut t = new_test_ext(); + + let validator_controller = AccountId::from([0xaa; 32]); + + t.execute_with(|| { + assert_eq!(DdcValidator::signal(), None); + assert_ok!(DdcValidator::send_signal(RuntimeOrigin::signed(validator_controller))); + assert_eq!(DdcValidator::signal().unwrap(), true); + }) +} + +#[test] +fn set_validation_decision_works_as_expected() { + let mut t = new_test_ext(); + + let era_to_validate: EraIndex = 3; + let cdn_node_to_validate = AccountId::from([0x1; 32]); + let not_a_cdn_node = AccountId::from([0x2; 32]); + let validator_1_stash = AccountId::from([ + 0xd2, 0xbf, 0x4b, 0x84, 0x4d, 0xfe, 0xfd, 0x67, 0x72, 0xa8, 0x84, 0x3e, 0x66, 0x9f, 0x94, + 0x34, 0x08, 0x96, 0x6a, 0x97, 0x7e, 0x3a, 0xe2, 0xaf, 0x1d, 0xd7, 0x8e, 0x0f, 0x55, 0xf4, + 0xdf, 0x67, + ]); + let validator_1_controller = AccountId::from([0xaa; 32]); + + let decision: ValidationDecision = + serde_json::from_slice(include_bytes!("./mock-data/set-1/validation-decision.json")) + .unwrap(); + + sp_std::if_std! { + println!("events"); + } + + t.execute_with(|| { + System::set_block_number(1); + + assert_ok!(DdcValidator::set_validator_key( + // register validator 1 + RuntimeOrigin::signed(validator_1_controller.clone()), + validator_1_stash.clone(), + )); + + assert_noop!( + DdcValidator::set_validation_decision( + RuntimeOrigin::signed(validator_1_controller), + era_to_validate, + cdn_node_to_validate.clone(), + decision.clone(), + ), + ValidatorError::::DDCValidatorKeyNotRegistered + ); + + assert_noop!( + DdcValidator::set_validation_decision( + RuntimeOrigin::signed(validator_1_stash.clone()), + era_to_validate, + not_a_cdn_node.clone(), + decision.clone() + ), + ValidatorError::::NodeNotActive + ); + + assert_ok!(DdcValidator::set_validation_decision( + RuntimeOrigin::signed(validator_1_stash.clone()), + era_to_validate, + cdn_node_to_validate.clone(), + decision.clone() + )); + + assert_noop!( + DdcValidator::set_validation_decision( + RuntimeOrigin::signed(validator_1_stash), + era_to_validate, + cdn_node_to_validate.clone(), + decision.clone() + ), + ValidatorError::::ValidationDecisionAlreadySet + ); + + let evt = System::events().into_iter().map(|evt| evt.event).collect::>(); + assert_eq!(evt.len(), 1); + assert_eq!( + evt[0], + crate::Event::ValidationDecision(era_to_validate, cdn_node_to_validate, decision) + .into() + ); + }) +} From c9c0bd30992b6b6ddaebe228176d3e1e4abc020b Mon Sep 17 00:00:00 2001 From: Raid Ateir Date: Fri, 1 Sep 2023 10:42:33 +0200 Subject: [PATCH 2/4] add unit tests and fix source code --- pallets/ddc-accounts/src/lib.rs | 5 +- pallets/ddc-validator/src/lib.rs | 54 +++-- pallets/ddc-validator/src/tests.rs | 333 ++++++++++++++++++++++++++--- 3 files changed, 354 insertions(+), 38 deletions(-) diff --git a/pallets/ddc-accounts/src/lib.rs b/pallets/ddc-accounts/src/lib.rs index dc2edbbf5..e988cdd0f 100644 --- a/pallets/ddc-accounts/src/lib.rs +++ b/pallets/ddc-accounts/src/lib.rs @@ -219,6 +219,8 @@ pub mod pallet { BadState, /// Current era not set during runtime DDCEraNotSet, + /// Bucket with specified id doesn't exist + BucketDoesNotExist, } #[pallet::genesis_config] @@ -532,7 +534,8 @@ pub mod pallet { let mut total_charged = BalanceOf::::zero(); for bucket_details in paying_accounts.iter() { - let bucket: Bucket = Self::buckets(bucket_details.bucket_id).unwrap(); + let bucket: Bucket = Self::buckets(bucket_details.bucket_id) + .ok_or(Error::::BucketDoesNotExist)?; let content_owner = bucket.owner_id; let amount = bucket_details.amount * pricing.saturated_into::>(); diff --git a/pallets/ddc-validator/src/lib.rs b/pallets/ddc-validator/src/lib.rs index e83211846..7eb36db1d 100644 --- a/pallets/ddc-validator/src/lib.rs +++ b/pallets/ddc-validator/src/lib.rs @@ -465,6 +465,7 @@ pub mod pallet { stakers_points: Vec<(T::AccountId, u64)>, ) -> DispatchResult { let ddc_valitor_key = ensure_signed(origin)?; + let mut rewards_counter = 0; ensure!( DDCValidatorToStashKeys::::contains_key(&ddc_valitor_key), @@ -488,18 +489,21 @@ pub mod pallet { >::mutate( &staker, |current_reward_points| { - let rewards = + let rewards: ddc_staking::EraRewardPointsPerNode = ddc_staking::EraRewardPointsPerNode { era, points }; current_reward_points.push(rewards); }, ); RewardPointsSetForNode::::insert(era, staker, true); + rewards_counter += 1; } } } }); - Self::deposit_event(Event::::EraRewardPoints(era, stakers_points)); + if rewards_counter > 0 { + Self::deposit_event(Event::::EraRewardPoints(era, stakers_points)); + } Ok(()) } @@ -911,13 +915,25 @@ pub mod pallet { return Err("signing key not set") } // ToDo: replace local call by a call from `ddc-staking` pallet - let _tx_res: Option<(frame_system::offchain::Account, Result<(), ()>)> = + let tx_res: Option<(frame_system::offchain::Account, Result<(), ()>)> = signer.send_signed_transaction(|_account| { Call::charge_payments_content_owners { paying_accounts: final_payments.clone(), } }); + for (acc, res) in &tx_res { + match res { + Ok(()) => + log::debug!("[{:?}]: submit transaction success.", acc.id), + Err(e) => log::debug!( + "[{:?}]: submit transaction failure. Reason: {:?}", + acc.id, + e + ), + } + } + let final_res = dac::get_final_decision(validations_res); let signer = Self::get_signer().unwrap(); @@ -929,17 +945,18 @@ pub mod pallet { validation_decision: final_res.clone(), }); - match &tx_res { - None | Some((_, Err(()))) => return Err("Error:"), - Some((_, Ok(()))) => {}, + for (acc, res) in &tx_res { + match res { + Ok(()) => + log::debug!("[{:?}]: submit transaction success.", acc.id), + Err(e) => log::debug!( + "[{:?}]: submit transaction failure. Reason: {:?}", + acc.id, + e + ), + } } - // match tx_res.result { - // Ok(tx_res) => println!("successfull tx: {:?}", tx_res), - // Err(error) => println!("failed tx: {:?}", error), - // } - // log::debug!("final_res: {:?}", final_res); - cdn_nodes_reward_points.push(( utils::string_to_account::(final_res.edge), final_res.totals.sent, @@ -951,11 +968,22 @@ pub mod pallet { // ToDo: replace local call by a call from `ddc-staking` pallet if cdn_nodes_reward_points.len() > 0 { - let _tx_res = + let tx_res = signer.send_signed_transaction(|_account| Call::set_era_reward_points { era: current_ddc_era - 1, stakers_points: cdn_nodes_reward_points.clone(), }); + + for (acc, res) in &tx_res { + match res { + Ok(()) => log::debug!("[{:?}]: submit transaction success.", acc.id), + Err(e) => log::debug!( + "[{:?}]: submit transaction failure. Reason: {:?}", + acc.id, + e + ), + } + } } Ok(()) diff --git a/pallets/ddc-validator/src/tests.rs b/pallets/ddc-validator/src/tests.rs index f3c069d01..a2bb9963a 100644 --- a/pallets/ddc-validator/src/tests.rs +++ b/pallets/ddc-validator/src/tests.rs @@ -1,3 +1,5 @@ +#![cfg(test)] + use super::*; use crate::{ mock::{Timestamp, *}, @@ -5,7 +7,7 @@ use crate::{ }; use codec::Decode; use frame_support::{assert_noop, assert_ok}; -use pallet_ddc_accounts::BucketsDetails; +use pallet_ddc_accounts::{BucketsDetails, Error as AccountsError}; use pallet_ddc_staking::{DDC_ERA_DURATION_MS, DDC_ERA_START_MS}; use sp_core::offchain::{testing, OffchainDbExt, OffchainWorkerExt, TransactionPoolExt}; use sp_keystore::{testing::KeyStore, KeystoreExt, SyncCryptoStore}; @@ -16,6 +18,10 @@ const OCW_PUB_KEY_STR: &str = "d2bf4b844dfefd6772a8843e669f943408966a977e3ae2af1 const OCW_SEED: &str = "news slush supreme milk chapter athlete soap sausage put clutch what kitten"; +fn last_event() -> RuntimeEvent { + System::events().pop().expect("Event expected").event.into() +} + #[test] fn it_sets_validation_decision_with_one_validator_in_quorum() { let mut t = new_test_ext(); @@ -129,24 +135,21 @@ fn it_sets_validation_decision_with_one_validator_in_quorum() { t.execute_with(|| { let era_block_number = 20 as u32 * era_to_validate; System::set_block_number(era_block_number); // required for randomness - DdcValidator::set_validator_key( + assert_ok!(DdcValidator::set_validator_key( // register validator 1 RuntimeOrigin::signed(validator_1_controller), validator_1_stash, - ) - .unwrap(); - DdcValidator::set_validator_key( + )); + assert_ok!(DdcValidator::set_validator_key( // register validator 2 RuntimeOrigin::signed(validator_2_controller), validator_2_stash, - ) - .unwrap(); - DdcValidator::set_validator_key( + )); + assert_ok!(DdcValidator::set_validator_key( // register validator 3 RuntimeOrigin::signed(validator_3_controller), validator_3_stash, - ) - .unwrap(); + )); Timestamp::set_timestamp( (DDC_ERA_START_MS + DDC_ERA_DURATION_MS * (era_to_validate as u128 - 1)) as u64, ); @@ -249,27 +252,18 @@ fn set_validation_decision_works_as_expected() { 0xdf, 0x67, ]); let validator_1_controller = AccountId::from([0xaa; 32]); + let validator_1_not_controller = AccountId::from([0xdd; 32]); let decision: ValidationDecision = serde_json::from_slice(include_bytes!("./mock-data/set-1/validation-decision.json")) .unwrap(); - sp_std::if_std! { - println!("events"); - } - t.execute_with(|| { System::set_block_number(1); - assert_ok!(DdcValidator::set_validator_key( - // register validator 1 - RuntimeOrigin::signed(validator_1_controller.clone()), - validator_1_stash.clone(), - )); - assert_noop!( DdcValidator::set_validation_decision( - RuntimeOrigin::signed(validator_1_controller), + RuntimeOrigin::signed(validator_1_controller.clone()), era_to_validate, cdn_node_to_validate.clone(), decision.clone(), @@ -277,6 +271,28 @@ fn set_validation_decision_works_as_expected() { ValidatorError::::DDCValidatorKeyNotRegistered ); + // Set a mapping from stash to not a validator + DDCValidatorToStashKeys::::insert( + validator_1_stash.clone(), + validator_1_not_controller.clone(), + ); + + assert_noop!( + DdcValidator::set_validation_decision( + RuntimeOrigin::signed(validator_1_stash.clone()), + era_to_validate, + cdn_node_to_validate.clone(), + decision.clone() + ), + ValidatorError::::NotValidatorStash + ); + + assert_ok!(DdcValidator::set_validator_key( + // register validator 1 + RuntimeOrigin::signed(validator_1_controller.clone()), + validator_1_stash.clone(), + )); + assert_noop!( DdcValidator::set_validation_decision( RuntimeOrigin::signed(validator_1_stash.clone()), @@ -304,12 +320,281 @@ fn set_validation_decision_works_as_expected() { ValidatorError::::ValidationDecisionAlreadySet ); - let evt = System::events().into_iter().map(|evt| evt.event).collect::>(); - assert_eq!(evt.len(), 1); + let evt = last_event(); + assert_eq!( - evt[0], + evt, crate::Event::ValidationDecision(era_to_validate, cdn_node_to_validate, decision) .into() ); }) } + +#[test] +fn set_era_reward_points_works_as_expected() { + let mut t = new_test_ext(); + + let era_to_validate: EraIndex = 3; + let cdn_node_to_validate = AccountId::from([0x1; 32]); + let not_a_cdn_node = AccountId::from([0x2; 32]); + let validator_1_stash = AccountId::from([ + 0xd2, 0xbf, 0x4b, 0x84, 0x4d, 0xfe, 0xfd, 0x67, 0x72, 0xa8, 0x84, 0x3e, 0x66, 0x9f, 0x94, + 0x34, 0x08, 0x96, 0x6a, 0x97, 0x7e, 0x3a, 0xe2, 0xaf, 0x1d, 0xd7, 0x8e, 0x0f, 0x55, 0xf4, + 0xdf, 0x67, + ]); + let validator_1_controller = AccountId::from([0xaa; 32]); + let validator_1_not_controller = AccountId::from([0xdd; 32]); + + let decision: ValidationDecision = + serde_json::from_slice(include_bytes!("./mock-data/set-1/validation-decision.json")) + .unwrap(); + + let stakers_points = vec![(cdn_node_to_validate.clone(), decision.totals.sent)]; + let fake_stakers_points = vec![(not_a_cdn_node.clone(), decision.totals.sent)]; + + t.execute_with(|| { + System::set_block_number(1); + + assert_noop!( + DdcValidator::set_era_reward_points( + RuntimeOrigin::signed(validator_1_controller.clone()), + era_to_validate, + stakers_points.clone(), + ), + ValidatorError::::DDCValidatorKeyNotRegistered + ); + + // Set a mapping from stash to not a validator + DDCValidatorToStashKeys::::insert( + validator_1_stash.clone(), + validator_1_not_controller.clone(), + ); + + assert_noop!( + DdcValidator::set_era_reward_points( + RuntimeOrigin::signed(validator_1_stash.clone()), + era_to_validate, + stakers_points.clone() + ), + ValidatorError::::NotValidatorStash + ); + + assert_ok!(DdcValidator::set_validator_key( + // register validator 1 + RuntimeOrigin::signed(validator_1_controller), + validator_1_stash.clone(), + )); + + // attempting to set era reward points for account, which is not an active CDN node + assert_ok!(DdcValidator::set_era_reward_points( + RuntimeOrigin::signed(validator_1_stash.clone()), + era_to_validate, + fake_stakers_points.clone() + )); + + // rewards points should not be set + assert_eq!( + DdcValidator::reward_points_set_for_node(era_to_validate, not_a_cdn_node), + false + ); + + assert_ok!(DdcValidator::set_era_reward_points( + RuntimeOrigin::signed(validator_1_stash.clone()), + era_to_validate, + stakers_points.clone() + )); + + let reward_points = ddc_staking::pallet::ErasEdgesRewardPointsPerNode::::get( + cdn_node_to_validate.clone(), + ); + + assert_eq!(reward_points.len(), 1); + + // Second run will still pass, but state will not change, as for specified node era points + // were set already + assert_ok!(DdcValidator::set_era_reward_points( + RuntimeOrigin::signed(validator_1_stash.clone()), + era_to_validate, + stakers_points.clone() + )); + + let reward_points_update_attempt = + ddc_staking::pallet::ErasEdgesRewardPointsPerNode::::get( + cdn_node_to_validate.clone(), + ); + + assert_eq!(reward_points_update_attempt.len(), 1); + + assert_eq!( + DdcValidator::reward_points_set_for_node(era_to_validate, cdn_node_to_validate), + true + ); + + assert_eq!(System::events().len(), 1); + let evt = System::events().pop().expect("Event expected").event; + + assert_eq!(evt, crate::Event::EraRewardPoints(era_to_validate, stakers_points).into()); + }) +} + +#[test] +fn charge_payments_content_owners_works_as_expected() { + let mut t = new_test_ext(); + + let era_to_validate: EraIndex = 3; + let validator_1_stash = AccountId::from([ + 0xd2, 0xbf, 0x4b, 0x84, 0x4d, 0xfe, 0xfd, 0x67, 0x72, 0xa8, 0x84, 0x3e, 0x66, 0x9f, 0x94, + 0x34, 0x08, 0x96, 0x6a, 0x97, 0x7e, 0x3a, 0xe2, 0xaf, 0x1d, 0xd7, 0x8e, 0x0f, 0x55, 0xf4, + 0xdf, 0x67, + ]); + let validator_1_controller = AccountId::from([0xaa; 32]); + let validator_1_not_controller = AccountId::from([0xdd; 32]); + + let bucket_info = BucketsDetails { bucket_id: 5, amount: 600u128 }; + + t.execute_with(|| { + System::set_block_number(1); + + assert_noop!( + DdcValidator::charge_payments_content_owners( + RuntimeOrigin::signed(validator_1_controller.clone()), + vec![bucket_info.clone()], + ), + ValidatorError::::DDCEraNotSet + ); + + let era_block_number = 20 as u32 * era_to_validate; + System::set_block_number(era_block_number); + Timestamp::set_timestamp( + (DDC_ERA_START_MS + DDC_ERA_DURATION_MS * (era_to_validate as u128 - 1)) as u64, + ); + DdcStaking::on_finalize(era_block_number - 1); + + assert_noop!( + DdcValidator::charge_payments_content_owners( + RuntimeOrigin::signed(validator_1_stash.clone()), + vec![bucket_info.clone()] + ), + ValidatorError::::DDCValidatorKeyNotRegistered + ); + + // Set a mapping from stash to not a validator + DDCValidatorToStashKeys::::insert( + validator_1_stash.clone(), + validator_1_not_controller.clone(), + ); + + assert_noop!( + DdcValidator::charge_payments_content_owners( + RuntimeOrigin::signed(validator_1_stash.clone()), + vec![bucket_info.clone()] + ), + ValidatorError::::NotValidatorStash + ); + + assert_ok!(DdcValidator::set_validator_key( + // register validator 1 + RuntimeOrigin::signed(validator_1_controller.clone()), + validator_1_stash.clone(), + )); + + assert_noop!( + DdcValidator::charge_payments_content_owners( + RuntimeOrigin::signed(validator_1_stash.clone()), + vec![bucket_info.clone()] + ), + ValidatorError::::PricingNotSet + ); + + ddc_staking::Pricing::::set(Some(1)); // set CERE token per byte / reward + + // No buckets were created at this point + assert_noop!( + DdcValidator::charge_payments_content_owners( + RuntimeOrigin::signed(validator_1_stash.clone()), + vec![bucket_info.clone()] + ), + AccountsError::::BucketDoesNotExist + ); + + // Create buckets + for range in 1..6 { + assert_ok!(ddc_accounts::Pallet::::create_bucket( + RuntimeOrigin::signed(validator_1_stash.clone()), + true, + range + )); + } + + // Account to charge payments from is not created + assert_noop!( + DdcValidator::charge_payments_content_owners( + RuntimeOrigin::signed(validator_1_stash.clone()), + vec![bucket_info.clone()] + ), + AccountsError::::NotController + ); + + // Deposit funds for account + assert_ok!(ddc_accounts::Pallet::::deposit( + RuntimeOrigin::signed(validator_1_stash.clone()), + validator_1_stash.clone(), + 1_000, + )); + + assert_ok!(DdcValidator::charge_payments_content_owners( + RuntimeOrigin::signed(validator_1_stash.clone()), + vec![bucket_info.clone()] + )); + + // Should not be able to charge account twice by the same validator during one era + assert_noop!( + DdcValidator::charge_payments_content_owners( + RuntimeOrigin::signed(validator_1_stash.clone()), + vec![bucket_info.clone()] + ), + ValidatorError::::ContentOwnersDoubleSpend + ); + + let last_evt = System::events().pop().expect("Event expected").event; + assert_eq!(last_evt, ddc_accounts::Event::Charged(bucket_info.amount).into()); + }) +} + +#[test] +fn set_validator_key_works_as_expected() { + let mut t = new_test_ext(); + + let validator_1_stash = AccountId::from([ + 0xd2, 0xbf, 0x4b, 0x84, 0x4d, 0xfe, 0xfd, 0x67, 0x72, 0xa8, 0x84, 0x3e, 0x66, 0x9f, 0x94, + 0x34, 0x08, 0x96, 0x6a, 0x97, 0x7e, 0x3a, 0xe2, 0xaf, 0x1d, 0xd7, 0x8e, 0x0f, 0x55, 0xf4, + 0xdf, 0x67, + ]); + let validator_1_controller: AccountId32 = AccountId::from([0xaa; 32]); + + t.execute_with(|| { + assert_noop!( + DdcValidator::set_validator_key( + RuntimeOrigin::signed(validator_1_stash.clone()), + validator_1_stash.clone(), + ), + ValidatorError::::NotController + ); + + assert_ok!(DdcValidator::set_validator_key( + RuntimeOrigin::signed(validator_1_controller.clone()), + validator_1_stash.clone(), + )); + + staking::Validators::::remove(validator_1_stash.clone()); + + // If stash is not a validator anymore, action will fail + assert_noop!( + DdcValidator::set_validator_key( + RuntimeOrigin::signed(validator_1_controller), + validator_1_stash, + ), + ValidatorError::::NotValidatorStash + ); + }) +} From f69abcc2c919ed0efaccfd20744938e0416f2bac Mon Sep 17 00:00:00 2001 From: Raid Ateir Date: Mon, 4 Sep 2023 10:42:27 +0200 Subject: [PATCH 3/4] resolve comments --- pallets/ddc-validator/src/lib.rs | 53 ++++++++++++++---------------- pallets/ddc-validator/src/tests.rs | 5 ++- 2 files changed, 28 insertions(+), 30 deletions(-) diff --git a/pallets/ddc-validator/src/lib.rs b/pallets/ddc-validator/src/lib.rs index 7eb36db1d..a4a5fd7e5 100644 --- a/pallets/ddc-validator/src/lib.rs +++ b/pallets/ddc-validator/src/lib.rs @@ -922,16 +922,15 @@ pub mod pallet { } }); - for (acc, res) in &tx_res { - match res { - Ok(()) => - log::debug!("[{:?}]: submit transaction success.", acc.id), - Err(e) => log::debug!( - "[{:?}]: submit transaction failure. Reason: {:?}", - acc.id, - e - ), - } + let (acc, res) = &tx_res.unwrap(); + + match res { + Ok(()) => log::debug!("[{:?}]: submit transaction success.", acc.id), + Err(e) => log::debug!( + "[{:?}]: submit transaction failure. Reason: {:?}", + acc.id, + e + ), } let final_res = dac::get_final_decision(validations_res); @@ -945,16 +944,15 @@ pub mod pallet { validation_decision: final_res.clone(), }); - for (acc, res) in &tx_res { - match res { - Ok(()) => - log::debug!("[{:?}]: submit transaction success.", acc.id), - Err(e) => log::debug!( - "[{:?}]: submit transaction failure. Reason: {:?}", - acc.id, - e - ), - } + let (acc, res) = &tx_res.unwrap(); + + match res { + Ok(()) => log::debug!("[{:?}]: submit transaction success.", acc.id), + Err(e) => log::debug!( + "[{:?}]: submit transaction failure. Reason: {:?}", + acc.id, + e + ), } cdn_nodes_reward_points.push(( @@ -974,15 +972,12 @@ pub mod pallet { stakers_points: cdn_nodes_reward_points.clone(), }); - for (acc, res) in &tx_res { - match res { - Ok(()) => log::debug!("[{:?}]: submit transaction success.", acc.id), - Err(e) => log::debug!( - "[{:?}]: submit transaction failure. Reason: {:?}", - acc.id, - e - ), - } + let (acc, res) = &tx_res.unwrap(); + + match res { + Ok(()) => log::debug!("[{:?}]: submit transaction success.", acc.id), + Err(e) => + log::debug!("[{:?}]: submit transaction failure. Reason: {:?}", acc.id, e), } } diff --git a/pallets/ddc-validator/src/tests.rs b/pallets/ddc-validator/src/tests.rs index a2bb9963a..c2c470056 100644 --- a/pallets/ddc-validator/src/tests.rs +++ b/pallets/ddc-validator/src/tests.rs @@ -235,7 +235,10 @@ fn send_signal_works_as_expected() { t.execute_with(|| { assert_eq!(DdcValidator::signal(), None); assert_ok!(DdcValidator::send_signal(RuntimeOrigin::signed(validator_controller))); - assert_eq!(DdcValidator::signal().unwrap(), true); + assert_eq!(DdcValidator::signal(), Some(true)); + DdcValidator::on_initialize(2); + System::set_block_number(2); + assert_eq!(DdcValidator::signal(), Some(false)); }) } From 39d8e69f3e19e2ed10cec86317e20cac0bbc9825 Mon Sep 17 00:00:00 2001 From: Raid Ateir Date: Mon, 4 Sep 2023 15:32:20 +0200 Subject: [PATCH 4/4] fix match for tx_res --- pallets/ddc-validator/src/lib.rs | 30 +++++++++++++++--------------- 1 file changed, 15 insertions(+), 15 deletions(-) diff --git a/pallets/ddc-validator/src/lib.rs b/pallets/ddc-validator/src/lib.rs index a4a5fd7e5..afe90a719 100644 --- a/pallets/ddc-validator/src/lib.rs +++ b/pallets/ddc-validator/src/lib.rs @@ -922,15 +922,15 @@ pub mod pallet { } }); - let (acc, res) = &tx_res.unwrap(); - - match res { - Ok(()) => log::debug!("[{:?}]: submit transaction success.", acc.id), - Err(e) => log::debug!( + match tx_res { + Some((acc, Ok(()))) => + log::debug!("[{:?}]: submit transaction success.", acc.id), + Some((acc, Err(e))) => log::debug!( "[{:?}]: submit transaction failure. Reason: {:?}", acc.id, e ), + None => log::debug!("submit transaction failure."), } let final_res = dac::get_final_decision(validations_res); @@ -944,15 +944,15 @@ pub mod pallet { validation_decision: final_res.clone(), }); - let (acc, res) = &tx_res.unwrap(); - - match res { - Ok(()) => log::debug!("[{:?}]: submit transaction success.", acc.id), - Err(e) => log::debug!( + match tx_res { + Some((acc, Ok(()))) => + log::debug!("[{:?}]: submit transaction success.", acc.id), + Some((acc, Err(e))) => log::debug!( "[{:?}]: submit transaction failure. Reason: {:?}", acc.id, e ), + None => log::debug!("submit transaction failure."), } cdn_nodes_reward_points.push(( @@ -972,12 +972,12 @@ pub mod pallet { stakers_points: cdn_nodes_reward_points.clone(), }); - let (acc, res) = &tx_res.unwrap(); - - match res { - Ok(()) => log::debug!("[{:?}]: submit transaction success.", acc.id), - Err(e) => + match tx_res { + Some((acc, Ok(()))) => + log::debug!("[{:?}]: submit transaction success.", acc.id), + Some((acc, Err(e))) => log::debug!("[{:?}]: submit transaction failure. Reason: {:?}", acc.id, e), + None => log::debug!("submit transaction failure."), } }