// This file is part of Bizinikiwi. // Copyright (C) Parity Technologies (UK) Ltd. and Dijital Kurdistan Tech Institute // SPDX-License-Identifier: Apache-2.0 // Licensed under the Apache License, Version 2.0 (the "License"); // you may not use this file except in compliance with the License. // You may obtain a copy of the License at // // http://www.apache.org/licenses/LICENSE-2.0 // // Unless required by applicable law or agreed to in writing, software // distributed under the License is distributed on an "AS IS" BASIS, // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. // See the License for the specific language governing permissions and // limitations under the License. //! Tests for try-state checks. use super::*; use pezframe_support::assert_ok; #[test] fn try_state_works_with_uninitialized_pallet() { pezsp_io::TestExternalities::default().execute_with(|| { // Verify the pezpallet is uninitialized assert!(ActiveEra::::get().is_none()); assert!(CurrentEra::::get().is_none()); assert_eq!(Bonded::::iter().count(), 0); assert_eq!(Ledger::::iter().count(), 0); assert_eq!(Validators::::iter().count(), 0); assert_eq!(Nominators::::iter().count(), 0); // Try-state should pass with uninitialized state assert_ok!(Staking::do_try_state(System::block_number())); }); } #[test] fn try_state_detects_inconsistent_active_current_era() { ExtBuilder::default().has_stakers(false).build_and_execute(|| { // Set only ActiveEra (CurrentEra remains None) - this violates the invariant ActiveEra::::put(ActiveEraInfo { index: 1, start: None }); CurrentEra::::kill(); // Try-state should fail due to inconsistent state assert!(Staking::do_try_state(System::block_number()).is_err()); // Now set only CurrentEra (ActiveEra None) - this also violates the invariant ActiveEra::::kill(); CurrentEra::::put(1); // Try-state should fail due to inconsistent state assert!(Staking::do_try_state(System::block_number()).is_err()); // Both None should pass ActiveEra::::kill(); CurrentEra::::kill(); assert_ok!(Staking::do_try_state(System::block_number())); // Both Some should pass (assuming other invariants are met) ActiveEra::::put(ActiveEraInfo { index: 1, start: None }); CurrentEra::::put(1); // Need to set up bonded eras for this to pass use pezframe_support::BoundedVec; let bonded_eras: BoundedVec<(u32, u32), _> = BoundedVec::try_from(vec![(0, 0), (1, 0)]).unwrap(); BondedEras::::put(bonded_eras); assert_ok!(Staking::do_try_state(System::block_number())); }); } #[test] fn try_state_bad_exposure() { ExtBuilder::default().try_state(false).build_and_execute(|| { Session::roll_until_active_era(2); assert!(Staking::do_try_state(System::block_number()).is_ok()); let (validator, mut metadata) = ErasStakersOverview::::iter() .take(1) .map(|(_era, validator, metadata)| (validator, metadata)) .collect::>() .pop() .unwrap(); metadata.total += 1; ErasStakersOverview::::insert(2, validator, metadata); assert!(Staking::do_try_state(System::block_number()).is_err()); }); } #[test] fn try_state_bad_eras_total_stake() { ExtBuilder::default().try_state(false).build_and_execute(|| { Session::roll_until_active_era(2); assert!(Staking::do_try_state(System::block_number()).is_ok()); ErasTotalStake::::mutate(2, |s| *s -= 1); assert!(Staking::do_try_state(System::block_number()).is_err()); }); }