From f7ba592d50a47ba32a2240a2df925d5e7ac925fa Mon Sep 17 00:00:00 2001 From: Uncle Stinky Date: Wed, 25 Jun 2025 18:17:55 +0300 Subject: [PATCH] rustfmt the ghost-networks pallet Signed-off-by: Uncle Stinky --- pallets/networks/Cargo.toml | 2 +- pallets/networks/src/benchmarking.rs | 19 +- pallets/networks/src/lib.rs | 249 ++-- pallets/networks/src/mock.rs | 13 +- pallets/networks/src/tests.rs | 1958 +++++++++++++++----------- 5 files changed, 1282 insertions(+), 959 deletions(-) diff --git a/pallets/networks/Cargo.toml b/pallets/networks/Cargo.toml index 7bfa6d0..857a05e 100644 --- a/pallets/networks/Cargo.toml +++ b/pallets/networks/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ghost-networks" -version = "0.1.10" +version = "0.1.11" license.workspace = true authors.workspace = true edition.workspace = true diff --git a/pallets/networks/src/benchmarking.rs b/pallets/networks/src/benchmarking.rs index f726a3e..d586bb7 100644 --- a/pallets/networks/src/benchmarking.rs +++ b/pallets/networks/src/benchmarking.rs @@ -13,17 +13,19 @@ fn assert_last_event(generic_event: ::RuntimeEvent) { frame_system::Pallet::::assert_last_event(generic_event.into()); } -fn prepare_network( - n: u32, m: u32, -) -> (::NetworkId, NetworkData) { +fn prepare_network(n: u32, m: u32) -> (::NetworkId, NetworkData) { let chain_id: ::NetworkId = Default::default(); let chain_id = chain_id.saturating_add((n + m).into()); let mut gatekeeper = b"0x".to_vec(); - for i in 0..40 { gatekeeper.push(i); } + for i in 0..40 { + gatekeeper.push(i); + } let mut topic_name = b"0x".to_vec(); - for i in 0..64 { topic_name.push(i); } + for i in 0..64 { + topic_name.push(i); + } let network = NetworkData { chain_name: sp_std::vec![0x69; n as usize], @@ -51,8 +53,11 @@ fn create_network( let authority = T::RegisterOrigin::try_successful_origin() .map_err(|_| BenchmarkError::Weightless)?; GhostNetworks::::register_network( - authority.clone(), chain_id.clone(), network.clone() - ).map_err(|_| BenchmarkError::Weightless)?; + authority.clone(), + chain_id.clone(), + network.clone(), + ) + .map_err(|_| BenchmarkError::Weightless)?; network } }; diff --git a/pallets/networks/src/lib.rs b/pallets/networks/src/lib.rs index 3c61883..109edf9 100644 --- a/pallets/networks/src/lib.rs +++ b/pallets/networks/src/lib.rs @@ -4,27 +4,27 @@ use frame_support::{ pallet_prelude::*, - storage::PrefixIterator, traits::{tokens::fungible::Inspect, EnsureOrigin}, + storage::PrefixIterator, + traits::{tokens::fungible::Inspect, EnsureOrigin}, }; use frame_system::pallet_prelude::*; use scale_info::TypeInfo; use sp_runtime::{ - traits::{CheckedSub, CheckedAdd, AtLeast32BitUnsigned, Member}, curve::PiecewiseLinear, + traits::{AtLeast32BitUnsigned, CheckedAdd, CheckedSub, Member}, DispatchResult, }; -use sp_std::{prelude::*, convert::TryInto}; +use sp_std::{convert::TryInto, prelude::*}; pub use ghost_traits::networks::{ - NetworkDataBasicHandler, NetworkDataInspectHandler, - NetworkDataMutateHandler, + NetworkDataBasicHandler, NetworkDataInspectHandler, NetworkDataMutateHandler, }; mod weights; -pub use module::*; pub use crate::weights::WeightInfo; +pub use module::*; #[cfg(any(feature = "runtime-benchmarks", test))] mod benchmarking; @@ -33,9 +33,8 @@ mod mock; #[cfg(all(feature = "std", test))] mod tests; -pub type BalanceOf = <::Currency as Inspect< - ::AccountId, ->>::Balance; +pub type BalanceOf = + <::Currency as Inspect<::AccountId>>::Balance; #[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo)] pub enum NetworkType { @@ -45,7 +44,9 @@ pub enum NetworkType { } impl Default for NetworkType { - fn default() -> Self { NetworkType::Evm } + fn default() -> Self { + NetworkType::Evm + } } #[derive(Encode, Decode, Clone, PartialEq, Eq, RuntimeDebug, TypeInfo)] @@ -69,7 +70,8 @@ pub struct BridgeAdjustment { } pub struct BridgedInflationCurve(core::marker::PhantomData<(RewardCurve, T)>); -impl pallet_staking::EraPayout for BridgedInflationCurve +impl pallet_staking::EraPayout + for BridgedInflationCurve where Balance: Default + AtLeast32BitUnsigned + Clone + Copy + From, RewardCurve: Get<&'static PiecewiseLinear<'static>>, @@ -99,10 +101,11 @@ where match piecewise_linear .calculate_for_fraction_times_denominator(total_staked, adjusted_issuance) .checked_mul(&accumulated_balance) - .and_then(|product| product.checked_div(&adjusted_issuance)) { - Some(payout) => (payout, accumulated_balance.saturating_sub(payout)), - None => (Balance::default(), Balance::default()), - } + .and_then(|product| product.checked_div(&adjusted_issuance)) + { + Some(payout) => (payout, accumulated_balance.saturating_sub(payout)), + None => (Balance::default(), Balance::default()), + } } } @@ -128,7 +131,7 @@ pub mod module { + TypeInfo + MaybeSerializeDeserialize + MaxEncodedLen; - + /// The origin required to register new network. type RegisterOrigin: EnsureOrigin; /// The origin required to update network information. @@ -155,18 +158,53 @@ pub mod module { #[pallet::event] #[pallet::generate_deposit(pub(crate) fn deposit_event)] pub enum Event { - NetworkRegistered { chain_id: T::NetworkId, network: NetworkData }, - NetworkNameUpdated { chain_id: T::NetworkId, chain_name: Vec }, - NetworkEndpointUpdated { chain_id: T::NetworkId, default_endpoint: Vec }, - NetworkFinalityDelayUpdated { chain_id: T::NetworkId, finality_delay: u64 }, - NetworkRateLimitDelayUpdated { chain_id: T::NetworkId, rate_limit_delay: u64 }, - NetworkBlockDistanceUpdated { chain_id: T::NetworkId, block_distance: u64 }, - NetworkTypeUpdated { chain_id: T::NetworkId, network_type: NetworkType }, - NetworkGatekeeperUpdated { chain_id: T::NetworkId, gatekeeper: Vec }, - NetworkTopicNameUpdated { chain_id: T::NetworkId, topic_name: Vec }, - NetworkIncomingFeeUpdated { chain_id: T::NetworkId, incoming_fee: u32 }, - NetworkOutgoingFeeUpdated { chain_id: T::NetworkId, outgoing_fee: u32 }, - NetworkRemoved { chain_id: T::NetworkId }, + NetworkRegistered { + chain_id: T::NetworkId, + network: NetworkData, + }, + NetworkNameUpdated { + chain_id: T::NetworkId, + chain_name: Vec, + }, + NetworkEndpointUpdated { + chain_id: T::NetworkId, + default_endpoint: Vec, + }, + NetworkFinalityDelayUpdated { + chain_id: T::NetworkId, + finality_delay: u64, + }, + NetworkRateLimitDelayUpdated { + chain_id: T::NetworkId, + rate_limit_delay: u64, + }, + NetworkBlockDistanceUpdated { + chain_id: T::NetworkId, + block_distance: u64, + }, + NetworkTypeUpdated { + chain_id: T::NetworkId, + network_type: NetworkType, + }, + NetworkGatekeeperUpdated { + chain_id: T::NetworkId, + gatekeeper: Vec, + }, + NetworkTopicNameUpdated { + chain_id: T::NetworkId, + topic_name: Vec, + }, + NetworkIncomingFeeUpdated { + chain_id: T::NetworkId, + incoming_fee: u32, + }, + NetworkOutgoingFeeUpdated { + chain_id: T::NetworkId, + outgoing_fee: u32, + }, + NetworkRemoved { + chain_id: T::NetworkId, + }, } #[pallet::storage] @@ -180,8 +218,7 @@ pub mod module { #[pallet::storage] #[pallet::getter(fn accumulated_commission)] - pub type AccumulatedCommission = - StorageValue<_, BalanceOf, ValueQuery>; + pub type AccumulatedCommission = StorageValue<_, BalanceOf, ValueQuery>; #[pallet::storage] #[pallet::getter(fn networks)] @@ -190,13 +227,8 @@ pub mod module { #[pallet::storage] #[pallet::getter(fn gatekeeper_amount)] - pub type GatekeeperAmount = StorageMap< - _, - Twox64Concat, - T::NetworkId, - BalanceOf, - ValueQuery, - >; + pub type GatekeeperAmount = + StorageMap<_, Twox64Concat, T::NetworkId, BalanceOf, ValueQuery>; #[pallet::genesis_config] pub struct GenesisConfig { @@ -205,7 +237,7 @@ pub mod module { impl Default for GenesisConfig { fn default() -> Self { - Self { networks: vec![] } + Self { networks: vec![] } } } @@ -213,12 +245,13 @@ pub mod module { impl BuildGenesisConfig for GenesisConfig { fn build(&self) { if !self.networks.is_empty() { - self.networks.iter().for_each(|(chain_id, network_metadata)| { - let network = - NetworkData::decode(&mut &network_metadata[..]) - .expect("Error decoding NetworkData"); - Pallet::::do_register_network(chain_id.clone(), network) - .expect("Error registering network"); + self.networks + .iter() + .for_each(|(chain_id, network_metadata)| { + let network = NetworkData::decode(&mut &network_metadata[..]) + .expect("Error decoding NetworkData"); + Pallet::::do_register_network(chain_id.clone(), network) + .expect("Error registering network"); }); } } @@ -268,10 +301,7 @@ pub mod module { chain_name: Vec, ) -> DispatchResult { T::UpdateOrigin::ensure_origin_or_root(origin)?; - Self::do_update_network_name( - chain_id, - chain_name, - ) + Self::do_update_network_name(chain_id, chain_name) } #[pallet::call_index(2)] @@ -284,10 +314,7 @@ pub mod module { default_endpoint: Vec, ) -> DispatchResult { T::UpdateOrigin::ensure_origin_or_root(origin)?; - Self::do_update_network_endpoint( - chain_id, - default_endpoint, - ) + Self::do_update_network_endpoint(chain_id, default_endpoint) } #[pallet::call_index(3)] @@ -298,10 +325,7 @@ pub mod module { finality_delay: u64, ) -> DispatchResult { T::UpdateOrigin::ensure_origin_or_root(origin)?; - Self::do_update_network_finality_delay( - chain_id, - finality_delay, - ) + Self::do_update_network_finality_delay(chain_id, finality_delay) } #[pallet::call_index(4)] @@ -312,10 +336,7 @@ pub mod module { rate_limit_delay: u64, ) -> DispatchResult { T::UpdateOrigin::ensure_origin_or_root(origin)?; - Self::do_update_network_rate_limit_delay( - chain_id, - rate_limit_delay, - ) + Self::do_update_network_rate_limit_delay(chain_id, rate_limit_delay) } #[pallet::call_index(5)] @@ -326,10 +347,7 @@ pub mod module { block_distance: u64, ) -> DispatchResult { T::UpdateOrigin::ensure_origin_or_root(origin)?; - Self::do_update_network_block_distance( - chain_id, - block_distance, - ) + Self::do_update_network_block_distance(chain_id, block_distance) } #[pallet::call_index(6)] @@ -340,10 +358,7 @@ pub mod module { network_type: NetworkType, ) -> DispatchResult { T::UpdateOrigin::ensure_origin_or_root(origin)?; - Self::do_update_network_type( - chain_id, - network_type, - ) + Self::do_update_network_type(chain_id, network_type) } #[pallet::call_index(7)] @@ -354,10 +369,7 @@ pub mod module { gatekeeper: Vec, ) -> DispatchResult { T::UpdateOrigin::ensure_origin_or_root(origin)?; - Self::do_update_network_gatekeeper( - chain_id, - gatekeeper, - ) + Self::do_update_network_gatekeeper(chain_id, gatekeeper) } #[pallet::call_index(8)] @@ -368,10 +380,7 @@ pub mod module { topic_name: Vec, ) -> DispatchResult { T::UpdateOrigin::ensure_origin_or_root(origin)?; - Self::do_update_network_topic_name( - chain_id, - topic_name, - ) + Self::do_update_network_topic_name(chain_id, topic_name) } #[pallet::call_index(9)] @@ -382,10 +391,7 @@ pub mod module { incoming_fee: u32, ) -> DispatchResult { T::UpdateOrigin::ensure_origin_or_root(origin)?; - Self::do_update_incoming_network_fee( - chain_id, - incoming_fee, - ) + Self::do_update_incoming_network_fee(chain_id, incoming_fee) } #[pallet::call_index(10)] @@ -396,18 +402,12 @@ pub mod module { outgoing_fee: u32, ) -> DispatchResult { T::UpdateOrigin::ensure_origin_or_root(origin)?; - Self::do_update_outgoing_network_fee( - chain_id, - outgoing_fee, - ) + Self::do_update_outgoing_network_fee(chain_id, outgoing_fee) } #[pallet::call_index(11)] #[pallet::weight(T::WeightInfo::remove_network())] - pub fn remove_network( - origin: OriginFor, - chain_id: T::NetworkId, - ) -> DispatchResult { + pub fn remove_network(origin: OriginFor, chain_id: T::NetworkId) -> DispatchResult { T::RemoveOrigin::ensure_origin_or_root(origin)?; Self::do_remove_network(chain_id) } @@ -416,12 +416,12 @@ pub mod module { impl Pallet { /// Register a new network. - pub fn do_register_network( - chain_id: T::NetworkId, - network: NetworkData, - ) -> DispatchResult { + pub fn do_register_network(chain_id: T::NetworkId, network: NetworkData) -> DispatchResult { Networks::::try_mutate(&chain_id, |maybe_network| -> DispatchResult { - ensure!(maybe_network.is_none(), Error::::NetworkAlreadyRegistered); + ensure!( + maybe_network.is_none(), + Error::::NetworkAlreadyRegistered + ); *maybe_network = Some(network.clone()); Ok(()) })?; @@ -443,10 +443,7 @@ impl Pallet { } /// Update existent network name. - pub fn do_update_network_name( - chain_id: T::NetworkId, - chain_name: Vec, - ) -> DispatchResult { + pub fn do_update_network_name(chain_id: T::NetworkId, chain_name: Vec) -> DispatchResult { Networks::::try_mutate(&chain_id, |maybe_network| -> DispatchResult { ensure!(maybe_network.is_some(), Error::::NetworkDoesNotExist); let net = maybe_network.as_mut().unwrap(); @@ -562,8 +559,10 @@ impl Pallet { chain_id: T::NetworkId, gatekeeper: Vec, ) -> DispatchResult { - ensure!(gatekeeper.len() == 42 && gatekeeper[0] == 48 && gatekeeper[1] == 120, - Error::::WrongGatekeeperAddress); + ensure!( + gatekeeper.len() == 42 && gatekeeper[0] == 48 && gatekeeper[1] == 120, + Error::::WrongGatekeeperAddress + ); Networks::::try_mutate(&chain_id, |maybe_network| -> DispatchResult { ensure!(maybe_network.is_some(), Error::::NetworkDoesNotExist); let net = maybe_network.as_mut().unwrap(); @@ -583,8 +582,10 @@ impl Pallet { chain_id: T::NetworkId, topic_name: Vec, ) -> DispatchResult { - ensure!(topic_name.len() == 66 && topic_name[0] == 48 && topic_name[1] == 120, - Error::::WrongTopicName); + ensure!( + topic_name.len() == 66 && topic_name[0] == 48 && topic_name[1] == 120, + Error::::WrongTopicName + ); Networks::::try_mutate(&chain_id, |maybe_network| -> DispatchResult { ensure!(maybe_network.is_some(), Error::::NetworkDoesNotExist); let net = maybe_network.as_mut().unwrap(); @@ -667,15 +668,16 @@ impl NetworkDataMutateHandler> for Pallet, ) -> Result, ()> { - let new_gatekeeper_amount = GatekeeperAmount::::mutate(network_id, |gatekeeper_amount| { - match gatekeeper_amount.checked_add(amount) { + let new_gatekeeper_amount = + GatekeeperAmount::::mutate(network_id, |gatekeeper_amount| match gatekeeper_amount + .checked_add(amount) + { Some(value) => { *gatekeeper_amount = value; Ok(value) - }, - None => Err(()) - } - })?; + } + None => Err(()), + })?; Ok(new_gatekeeper_amount) } @@ -684,28 +686,29 @@ impl NetworkDataMutateHandler> for Pallet, ) -> Result, ()> { - let new_gatekeeper_amount = GatekeeperAmount::::mutate(network_id, |gatekeeper_amount| { - match gatekeeper_amount.checked_sub(amount) { + let new_gatekeeper_amount = + GatekeeperAmount::::mutate(network_id, |gatekeeper_amount| match gatekeeper_amount + .checked_sub(amount) + { Some(value) => { *gatekeeper_amount = value; Ok(value) - }, - None => Err(()) - } - })?; + } + None => Err(()), + })?; Ok(new_gatekeeper_amount) } fn accumulate_outgoing_imbalance(amount: &BalanceOf) -> Result, ()> { let new_bridged_out_amount = BridgedImbalance::::mutate(|bridged_imbalance| { - match bridged_imbalance.bridged_out.checked_add(amount) { - Some(value) => { - (*bridged_imbalance).bridged_out = value; - Ok(value) - }, - None => Err(()) - } + match bridged_imbalance.bridged_out.checked_add(amount) { + Some(value) => { + (*bridged_imbalance).bridged_out = value; + Ok(value) + } + None => Err(()), + } })?; Ok(new_bridged_out_amount) @@ -717,8 +720,8 @@ impl NetworkDataMutateHandler> for Pallet { (*bridged_imbalance).bridged_in = value; Ok(value) - }, - None => Err(()) + } + None => Err(()), } })?; @@ -731,7 +734,7 @@ impl NetworkDataMutateHandler> for Pallet { *accumulated = value; Ok(value) - }, + } None => Err(()), } }) diff --git a/pallets/networks/src/mock.rs b/pallets/networks/src/mock.rs index 09eca1a..3b18062 100644 --- a/pallets/networks/src/mock.rs +++ b/pallets/networks/src/mock.rs @@ -1,17 +1,16 @@ use crate::{self as ghost_networks}; -use frame_system::EnsureSignedBy; use frame_support::{ construct_runtime, ord_parameter_types, parameter_types, traits::{ConstU128, ConstU32, Everything}, }; +use frame_system::EnsureSignedBy; pub use primitives::{ - AccountId, Balance, Nonce, BlockNumber, Hash, - ReserveIdentifier, FreezeIdentifier, + AccountId, Balance, BlockNumber, FreezeIdentifier, Hash, Nonce, ReserveIdentifier, }; use sp_runtime::{ curve::PiecewiseLinear, traits::{AccountIdLookup, BlakeTwo256}, - BuildStorage + BuildStorage, }; parameter_types! { @@ -96,9 +95,9 @@ impl ghost_networks::Config for Test { type RuntimeEvent = RuntimeEvent; type Currency = Balances; type NetworkId = u32; - type RegisterOrigin = EnsureSignedBy::; - type UpdateOrigin = EnsureSignedBy::; - type RemoveOrigin = EnsureSignedBy::; + type RegisterOrigin = EnsureSignedBy; + type UpdateOrigin = EnsureSignedBy; + type RemoveOrigin = EnsureSignedBy; type WeightInfo = crate::weights::SubstrateWeight; } diff --git a/pallets/networks/src/tests.rs b/pallets/networks/src/tests.rs index c33e69c..2cfd92d 100644 --- a/pallets/networks/src/tests.rs +++ b/pallets/networks/src/tests.rs @@ -1,1044 +1,1360 @@ -use mock::{ - ExtBuilder, System, RegistererAccount, UpdaterAccount, RemoverAccount, - RandomAccount, GhostNetworks, Test, RuntimeEvent, RuntimeOrigin, RewardCurve, -}; -use frame_support::{assert_err, assert_ok}; -use sp_runtime::DispatchError; -use pallet_staking::EraPayout; use super::*; +use frame_support::{assert_err, assert_ok}; +use mock::{ + ExtBuilder, GhostNetworks, RandomAccount, RegistererAccount, RemoverAccount, RewardCurve, + RuntimeEvent, RuntimeOrigin, System, Test, UpdaterAccount, +}; +use pallet_staking::EraPayout; +use sp_runtime::DispatchError; fn prepare_network_data() -> (u32, NetworkData) { - (1u32, NetworkData { - chain_name: "Ethereum".into(), - default_endpoint: "https:://some-endpoint.my-server.com/v1/my-super-secret-key".into(), - finality_delay: 69, - rate_limit_delay: 69, - block_distance: 69, - network_type: NetworkType::Evm, - gatekeeper: b"0x1234567891234567891234567891234567891234".to_vec(), - topic_name: b"0x12345678912345678912345678912345678912345678912345678912345678".to_vec(), - incoming_fee: 0, - outgoing_fee: 0, - }) + ( + 1u32, + NetworkData { + chain_name: "Ethereum".into(), + default_endpoint: "https:://some-endpoint.my-server.com/v1/my-super-secret-key".into(), + finality_delay: 69, + rate_limit_delay: 69, + block_distance: 69, + network_type: NetworkType::Evm, + gatekeeper: b"0x1234567891234567891234567891234567891234".to_vec(), + topic_name: b"0x12345678912345678912345678912345678912345678912345678912345678" + .to_vec(), + incoming_fee: 0, + outgoing_fee: 0, + }, + ) } fn register_and_check_network(chain_id: u32, network: NetworkData) { assert_ok!(GhostNetworks::register_network( - RuntimeOrigin::signed(RegistererAccount::get()), - chain_id, - network.clone())); + RuntimeOrigin::signed(RegistererAccount::get()), + chain_id, + network.clone() + )); assert_eq!(Networks::::get(chain_id), Some(network.clone())); } #[test] fn could_add_network_from_authority() { - ExtBuilder::build() - .execute_with(|| { - let (chain_id, network) = prepare_network_data(); - assert_eq!(Networks::::get(chain_id), None); - assert_ok!(GhostNetworks::register_network( - RuntimeOrigin::signed(RegistererAccount::get()), - chain_id, - network.clone(), - )); - System::assert_last_event(RuntimeEvent::GhostNetworks( - crate::Event::NetworkRegistered { - chain_id, network: network.clone() - })); - assert_eq!(Networks::::get(chain_id), Some(network)); - }); + ExtBuilder::build().execute_with(|| { + let (chain_id, network) = prepare_network_data(); + assert_eq!(Networks::::get(chain_id), None); + assert_ok!(GhostNetworks::register_network( + RuntimeOrigin::signed(RegistererAccount::get()), + chain_id, + network.clone(), + )); + System::assert_last_event(RuntimeEvent::GhostNetworks( + crate::Event::NetworkRegistered { + chain_id, + network: network.clone(), + }, + )); + assert_eq!(Networks::::get(chain_id), Some(network)); + }); } #[test] fn could_not_add_network_from_random_account() { - ExtBuilder::build() - .execute_with(|| { - let (chain_id, network) = prepare_network_data(); - assert_eq!(Networks::::get(chain_id), None); - assert_err!(GhostNetworks::register_network( - RuntimeOrigin::signed(RandomAccount::get()), - chain_id, - network.clone(), - ), DispatchError::BadOrigin); - assert_err!(GhostNetworks::register_network( - RuntimeOrigin::signed(UpdaterAccount::get()), - chain_id, - network.clone(), - ), DispatchError::BadOrigin); - assert_err!(GhostNetworks::register_network( - RuntimeOrigin::signed(RemoverAccount::get()), - chain_id, - network, - ), DispatchError::BadOrigin); - assert_eq!(Networks::::get(chain_id), None); - }); + ExtBuilder::build().execute_with(|| { + let (chain_id, network) = prepare_network_data(); + assert_eq!(Networks::::get(chain_id), None); + assert_err!( + GhostNetworks::register_network( + RuntimeOrigin::signed(RandomAccount::get()), + chain_id, + network.clone(), + ), + DispatchError::BadOrigin + ); + assert_err!( + GhostNetworks::register_network( + RuntimeOrigin::signed(UpdaterAccount::get()), + chain_id, + network.clone(), + ), + DispatchError::BadOrigin + ); + assert_err!( + GhostNetworks::register_network( + RuntimeOrigin::signed(RemoverAccount::get()), + chain_id, + network, + ), + DispatchError::BadOrigin + ); + assert_eq!(Networks::::get(chain_id), None); + }); } #[test] fn could_update_network_name_from_authority_account() { - ExtBuilder::build() - .execute_with(|| { - let new_name = b"Polygon".to_vec(); - let (chain_id, network) = prepare_network_data(); - register_and_check_network(chain_id, network.clone()); - assert_ok!(GhostNetworks::update_network_name( - RuntimeOrigin::signed(UpdaterAccount::get()), - chain_id, new_name.clone())); - System::assert_last_event(RuntimeEvent::GhostNetworks( - crate::Event::NetworkNameUpdated { - chain_id, - chain_name: new_name.clone() })); - let mut final_network = network.clone(); - final_network.chain_name = new_name; - assert_eq!(Networks::::get(chain_id), Some(final_network.clone())); - assert_ne!(network, final_network); - }); + ExtBuilder::build().execute_with(|| { + let new_name = b"Polygon".to_vec(); + let (chain_id, network) = prepare_network_data(); + register_and_check_network(chain_id, network.clone()); + assert_ok!(GhostNetworks::update_network_name( + RuntimeOrigin::signed(UpdaterAccount::get()), + chain_id, + new_name.clone() + )); + System::assert_last_event(RuntimeEvent::GhostNetworks( + crate::Event::NetworkNameUpdated { + chain_id, + chain_name: new_name.clone(), + }, + )); + let mut final_network = network.clone(); + final_network.chain_name = new_name; + assert_eq!(Networks::::get(chain_id), Some(final_network.clone())); + assert_ne!(network, final_network); + }); } #[test] fn could_update_network_endpoint_from_authority_account() { - ExtBuilder::build() - .execute_with(|| { - let new_endpoint = b"https:://google.com".to_vec(); - let (chain_id, network) = prepare_network_data(); - register_and_check_network(chain_id, network.clone()); - assert_ok!(GhostNetworks::update_network_endpoint( - RuntimeOrigin::signed(UpdaterAccount::get()), - chain_id, new_endpoint.clone())); - System::assert_last_event(RuntimeEvent::GhostNetworks( - crate::Event::NetworkEndpointUpdated { - chain_id, - default_endpoint: new_endpoint.clone() })); - let mut final_network = network.clone(); - final_network.default_endpoint = new_endpoint; - assert_eq!(Networks::::get(chain_id), Some(final_network.clone())); - assert_ne!(network, final_network); - }); + ExtBuilder::build().execute_with(|| { + let new_endpoint = b"https:://google.com".to_vec(); + let (chain_id, network) = prepare_network_data(); + register_and_check_network(chain_id, network.clone()); + assert_ok!(GhostNetworks::update_network_endpoint( + RuntimeOrigin::signed(UpdaterAccount::get()), + chain_id, + new_endpoint.clone() + )); + System::assert_last_event(RuntimeEvent::GhostNetworks( + crate::Event::NetworkEndpointUpdated { + chain_id, + default_endpoint: new_endpoint.clone(), + }, + )); + let mut final_network = network.clone(); + final_network.default_endpoint = new_endpoint; + assert_eq!(Networks::::get(chain_id), Some(final_network.clone())); + assert_ne!(network, final_network); + }); } #[test] fn could_update_network_finality_delay_from_authority_account() { - ExtBuilder::build() - .execute_with(|| { - let new_finality_delay = 1337; - let (chain_id, network) = prepare_network_data(); - register_and_check_network(chain_id, network.clone()); - assert_ok!(GhostNetworks::update_network_finality_delay( - RuntimeOrigin::signed(UpdaterAccount::get()), - chain_id, new_finality_delay)); - System::assert_last_event(RuntimeEvent::GhostNetworks( - crate::Event::NetworkFinalityDelayUpdated { - chain_id, - finality_delay: new_finality_delay })); - let mut final_network = network.clone(); - final_network.finality_delay = new_finality_delay; - assert_eq!(Networks::::get(chain_id), Some(final_network.clone())); - assert_ne!(network, final_network); - }); + ExtBuilder::build().execute_with(|| { + let new_finality_delay = 1337; + let (chain_id, network) = prepare_network_data(); + register_and_check_network(chain_id, network.clone()); + assert_ok!(GhostNetworks::update_network_finality_delay( + RuntimeOrigin::signed(UpdaterAccount::get()), + chain_id, + new_finality_delay + )); + System::assert_last_event(RuntimeEvent::GhostNetworks( + crate::Event::NetworkFinalityDelayUpdated { + chain_id, + finality_delay: new_finality_delay, + }, + )); + let mut final_network = network.clone(); + final_network.finality_delay = new_finality_delay; + assert_eq!(Networks::::get(chain_id), Some(final_network.clone())); + assert_ne!(network, final_network); + }); } #[test] fn could_update_network_rate_limit_delay_from_authority_account() { - ExtBuilder::build() - .execute_with(|| { - let new_rate_limit_delay = 1337; - let (chain_id, network) = prepare_network_data(); - register_and_check_network(chain_id, network.clone()); - assert_ok!(GhostNetworks::update_network_rate_limit_delay( - RuntimeOrigin::signed(UpdaterAccount::get()), - chain_id, new_rate_limit_delay)); - System::assert_last_event(RuntimeEvent::GhostNetworks( - crate::Event::NetworkRateLimitDelayUpdated { - chain_id, - rate_limit_delay: new_rate_limit_delay })); - let mut final_network = network.clone(); - final_network.rate_limit_delay = new_rate_limit_delay; - assert_eq!(Networks::::get(chain_id), Some(final_network.clone())); - assert_ne!(network, final_network); - }); + ExtBuilder::build().execute_with(|| { + let new_rate_limit_delay = 1337; + let (chain_id, network) = prepare_network_data(); + register_and_check_network(chain_id, network.clone()); + assert_ok!(GhostNetworks::update_network_rate_limit_delay( + RuntimeOrigin::signed(UpdaterAccount::get()), + chain_id, + new_rate_limit_delay + )); + System::assert_last_event(RuntimeEvent::GhostNetworks( + crate::Event::NetworkRateLimitDelayUpdated { + chain_id, + rate_limit_delay: new_rate_limit_delay, + }, + )); + let mut final_network = network.clone(); + final_network.rate_limit_delay = new_rate_limit_delay; + assert_eq!(Networks::::get(chain_id), Some(final_network.clone())); + assert_ne!(network, final_network); + }); } #[test] fn could_update_network_block_distance_from_authority_account() { - ExtBuilder::build() - .execute_with(|| { - let new_block_distance = 1337; - let (chain_id, network) = prepare_network_data(); - register_and_check_network(chain_id, network.clone()); - assert_ok!(GhostNetworks::update_network_block_distance( - RuntimeOrigin::signed(UpdaterAccount::get()), - chain_id, new_block_distance)); - System::assert_last_event(RuntimeEvent::GhostNetworks( - crate::Event::NetworkBlockDistanceUpdated { - chain_id, - block_distance: new_block_distance })); - let mut final_network = network.clone(); - final_network.block_distance = new_block_distance; - assert_eq!(Networks::::get(chain_id), Some(final_network.clone())); - assert_ne!(network, final_network); - }); + ExtBuilder::build().execute_with(|| { + let new_block_distance = 1337; + let (chain_id, network) = prepare_network_data(); + register_and_check_network(chain_id, network.clone()); + assert_ok!(GhostNetworks::update_network_block_distance( + RuntimeOrigin::signed(UpdaterAccount::get()), + chain_id, + new_block_distance + )); + System::assert_last_event(RuntimeEvent::GhostNetworks( + crate::Event::NetworkBlockDistanceUpdated { + chain_id, + block_distance: new_block_distance, + }, + )); + let mut final_network = network.clone(); + final_network.block_distance = new_block_distance; + assert_eq!(Networks::::get(chain_id), Some(final_network.clone())); + assert_ne!(network, final_network); + }); } #[test] fn could_update_network_type_from_authority_account() { - ExtBuilder::build() - .execute_with(|| { - let new_type = NetworkType::Utxo; - let (chain_id, network) = prepare_network_data(); - register_and_check_network(chain_id, network.clone()); - assert_ok!(GhostNetworks::update_network_type( - RuntimeOrigin::signed(UpdaterAccount::get()), - chain_id, new_type.clone())); - System::assert_last_event(RuntimeEvent::GhostNetworks( - crate::Event::NetworkTypeUpdated { - chain_id, - network_type: new_type.clone() })); - let mut final_network = network.clone(); - final_network.network_type = new_type; - assert_eq!(Networks::::get(chain_id), Some(final_network.clone())); - assert_ne!(network, final_network); - }); + ExtBuilder::build().execute_with(|| { + let new_type = NetworkType::Utxo; + let (chain_id, network) = prepare_network_data(); + register_and_check_network(chain_id, network.clone()); + assert_ok!(GhostNetworks::update_network_type( + RuntimeOrigin::signed(UpdaterAccount::get()), + chain_id, + new_type.clone() + )); + System::assert_last_event(RuntimeEvent::GhostNetworks( + crate::Event::NetworkTypeUpdated { + chain_id, + network_type: new_type.clone(), + }, + )); + let mut final_network = network.clone(); + final_network.network_type = new_type; + assert_eq!(Networks::::get(chain_id), Some(final_network.clone())); + assert_ne!(network, final_network); + }); } #[test] fn could_update_network_gatekeeper_from_authority_account() { - ExtBuilder::build() - .execute_with(|| { - let new_gatekeeper = b"0x9876543219876543219876543219876543219876".to_vec(); - let (chain_id, network) = prepare_network_data(); - register_and_check_network(chain_id, network.clone()); - assert_ok!(GhostNetworks::update_network_gatekeeper( - RuntimeOrigin::signed(UpdaterAccount::get()), - chain_id, new_gatekeeper.clone())); - System::assert_last_event(RuntimeEvent::GhostNetworks( - crate::Event::NetworkGatekeeperUpdated { - chain_id, - gatekeeper: new_gatekeeper.clone() })); - let mut final_network = network.clone(); - final_network.gatekeeper = new_gatekeeper; - assert_eq!(Networks::::get(chain_id), Some(final_network.clone())); - assert_ne!(network, final_network); - }); + ExtBuilder::build().execute_with(|| { + let new_gatekeeper = b"0x9876543219876543219876543219876543219876".to_vec(); + let (chain_id, network) = prepare_network_data(); + register_and_check_network(chain_id, network.clone()); + assert_ok!(GhostNetworks::update_network_gatekeeper( + RuntimeOrigin::signed(UpdaterAccount::get()), + chain_id, + new_gatekeeper.clone() + )); + System::assert_last_event(RuntimeEvent::GhostNetworks( + crate::Event::NetworkGatekeeperUpdated { + chain_id, + gatekeeper: new_gatekeeper.clone(), + }, + )); + let mut final_network = network.clone(); + final_network.gatekeeper = new_gatekeeper; + assert_eq!(Networks::::get(chain_id), Some(final_network.clone())); + assert_ne!(network, final_network); + }); } #[test] fn could_update_network_topic_name_from_authority_account() { - ExtBuilder::build() - .execute_with(|| { - let new_topic_name = b"0x9876543219876543219876543219876543219876543219876543219876543219".to_vec(); - let (chain_id, network) = prepare_network_data(); - register_and_check_network(chain_id, network.clone()); - assert_ok!(GhostNetworks::update_network_topic_name( - RuntimeOrigin::signed(UpdaterAccount::get()), - chain_id, new_topic_name.clone())); - System::assert_last_event(RuntimeEvent::GhostNetworks( - crate::Event::NetworkTopicNameUpdated { - chain_id, - topic_name: new_topic_name.clone() })); - let mut final_network = network.clone(); - final_network.topic_name = new_topic_name; - assert_eq!(Networks::::get(chain_id), Some(final_network.clone())); - assert_ne!(network, final_network); - }); + ExtBuilder::build().execute_with(|| { + let new_topic_name = + b"0x9876543219876543219876543219876543219876543219876543219876543219".to_vec(); + let (chain_id, network) = prepare_network_data(); + register_and_check_network(chain_id, network.clone()); + assert_ok!(GhostNetworks::update_network_topic_name( + RuntimeOrigin::signed(UpdaterAccount::get()), + chain_id, + new_topic_name.clone() + )); + System::assert_last_event(RuntimeEvent::GhostNetworks( + crate::Event::NetworkTopicNameUpdated { + chain_id, + topic_name: new_topic_name.clone(), + }, + )); + let mut final_network = network.clone(); + final_network.topic_name = new_topic_name; + assert_eq!(Networks::::get(chain_id), Some(final_network.clone())); + assert_ne!(network, final_network); + }); } #[test] fn could_update_incoming_network_fee_from_authority_account() { - ExtBuilder::build() - .execute_with(|| { - let new_incoming_fee = 69; - let (chain_id, network) = prepare_network_data(); - register_and_check_network(chain_id, network.clone()); - assert_ok!(GhostNetworks::update_incoming_network_fee( - RuntimeOrigin::signed(UpdaterAccount::get()), - chain_id, new_incoming_fee)); - System::assert_last_event(RuntimeEvent::GhostNetworks( - crate::Event::NetworkIncomingFeeUpdated { - chain_id, - incoming_fee: new_incoming_fee })); - let mut final_network = network.clone(); - final_network.incoming_fee = new_incoming_fee; - assert_eq!(Networks::::get(chain_id), Some(final_network.clone())); - assert_ne!(network, final_network); - }); + ExtBuilder::build().execute_with(|| { + let new_incoming_fee = 69; + let (chain_id, network) = prepare_network_data(); + register_and_check_network(chain_id, network.clone()); + assert_ok!(GhostNetworks::update_incoming_network_fee( + RuntimeOrigin::signed(UpdaterAccount::get()), + chain_id, + new_incoming_fee + )); + System::assert_last_event(RuntimeEvent::GhostNetworks( + crate::Event::NetworkIncomingFeeUpdated { + chain_id, + incoming_fee: new_incoming_fee, + }, + )); + let mut final_network = network.clone(); + final_network.incoming_fee = new_incoming_fee; + assert_eq!(Networks::::get(chain_id), Some(final_network.clone())); + assert_ne!(network, final_network); + }); } #[test] fn could_update_outgoing_network_fee_from_authority_account() { - ExtBuilder::build() - .execute_with(|| { - let new_outgoing_fee = 69; - let (chain_id, network) = prepare_network_data(); - register_and_check_network(chain_id, network.clone()); - assert_ok!(GhostNetworks::update_outgoing_network_fee( - RuntimeOrigin::signed(UpdaterAccount::get()), - chain_id, new_outgoing_fee)); - System::assert_last_event(RuntimeEvent::GhostNetworks( - crate::Event::NetworkOutgoingFeeUpdated { - chain_id, - outgoing_fee: new_outgoing_fee })); - let mut final_network = network.clone(); - final_network.outgoing_fee = new_outgoing_fee; - assert_eq!(Networks::::get(chain_id), Some(final_network.clone())); - assert_ne!(network, final_network); - }); + ExtBuilder::build().execute_with(|| { + let new_outgoing_fee = 69; + let (chain_id, network) = prepare_network_data(); + register_and_check_network(chain_id, network.clone()); + assert_ok!(GhostNetworks::update_outgoing_network_fee( + RuntimeOrigin::signed(UpdaterAccount::get()), + chain_id, + new_outgoing_fee + )); + System::assert_last_event(RuntimeEvent::GhostNetworks( + crate::Event::NetworkOutgoingFeeUpdated { + chain_id, + outgoing_fee: new_outgoing_fee, + }, + )); + let mut final_network = network.clone(); + final_network.outgoing_fee = new_outgoing_fee; + assert_eq!(Networks::::get(chain_id), Some(final_network.clone())); + assert_ne!(network, final_network); + }); } #[test] fn could_not_update_network_name_from_random_account() { - ExtBuilder::build() - .execute_with(|| { - let (chain_id, network) = prepare_network_data(); - register_and_check_network(chain_id, network.clone()); - assert_err!(GhostNetworks::update_network_name( - RuntimeOrigin::signed(RegistererAccount::get()), - chain_id, "Binance".into()), - DispatchError::BadOrigin); - assert_err!(GhostNetworks::update_network_name( - RuntimeOrigin::signed(RemoverAccount::get()), - chain_id, "Binance".into()), - DispatchError::BadOrigin); - assert_err!(GhostNetworks::update_network_name( - RuntimeOrigin::signed(RandomAccount::get()), - chain_id, "Binance".into()), - DispatchError::BadOrigin); - assert_eq!(Networks::::get(chain_id), Some(network)); - }); + ExtBuilder::build().execute_with(|| { + let (chain_id, network) = prepare_network_data(); + register_and_check_network(chain_id, network.clone()); + assert_err!( + GhostNetworks::update_network_name( + RuntimeOrigin::signed(RegistererAccount::get()), + chain_id, + "Binance".into() + ), + DispatchError::BadOrigin + ); + assert_err!( + GhostNetworks::update_network_name( + RuntimeOrigin::signed(RemoverAccount::get()), + chain_id, + "Binance".into() + ), + DispatchError::BadOrigin + ); + assert_err!( + GhostNetworks::update_network_name( + RuntimeOrigin::signed(RandomAccount::get()), + chain_id, + "Binance".into() + ), + DispatchError::BadOrigin + ); + assert_eq!(Networks::::get(chain_id), Some(network)); + }); } #[test] fn could_not_update_network_endpoint_from_random_account() { - ExtBuilder::build() - .execute_with(|| { - let (chain_id, network) = prepare_network_data(); - register_and_check_network(chain_id, network.clone()); - assert_err!(GhostNetworks::update_network_endpoint( - RuntimeOrigin::signed(RegistererAccount::get()), - chain_id, "https:://google.com".into()), - DispatchError::BadOrigin); - assert_err!(GhostNetworks::update_network_endpoint( - RuntimeOrigin::signed(RemoverAccount::get()), - chain_id, "https:://google.com".into()), - DispatchError::BadOrigin); - assert_err!(GhostNetworks::update_network_endpoint( - RuntimeOrigin::signed(RandomAccount::get()), - chain_id, "https:://google.com".into()), - DispatchError::BadOrigin); - assert_eq!(Networks::::get(chain_id), Some(network)); - }); + ExtBuilder::build().execute_with(|| { + let (chain_id, network) = prepare_network_data(); + register_and_check_network(chain_id, network.clone()); + assert_err!( + GhostNetworks::update_network_endpoint( + RuntimeOrigin::signed(RegistererAccount::get()), + chain_id, + "https:://google.com".into() + ), + DispatchError::BadOrigin + ); + assert_err!( + GhostNetworks::update_network_endpoint( + RuntimeOrigin::signed(RemoverAccount::get()), + chain_id, + "https:://google.com".into() + ), + DispatchError::BadOrigin + ); + assert_err!( + GhostNetworks::update_network_endpoint( + RuntimeOrigin::signed(RandomAccount::get()), + chain_id, + "https:://google.com".into() + ), + DispatchError::BadOrigin + ); + assert_eq!(Networks::::get(chain_id), Some(network)); + }); } #[test] fn could_not_update_network_finality_delay_from_random_account() { - ExtBuilder::build() - .execute_with(|| { - let (chain_id, network) = prepare_network_data(); - let finality_delay = 1337; - register_and_check_network(chain_id, network.clone()); - assert_err!(GhostNetworks::update_network_finality_delay( - RuntimeOrigin::signed(RegistererAccount::get()), - chain_id, finality_delay), - DispatchError::BadOrigin); - assert_err!(GhostNetworks::update_network_finality_delay( - RuntimeOrigin::signed(RemoverAccount::get()), - chain_id, finality_delay), - DispatchError::BadOrigin); - assert_err!(GhostNetworks::update_network_finality_delay( - RuntimeOrigin::signed(RandomAccount::get()), - chain_id, finality_delay), - DispatchError::BadOrigin); - assert_eq!(Networks::::get(chain_id), Some(network)); - }); + ExtBuilder::build().execute_with(|| { + let (chain_id, network) = prepare_network_data(); + let finality_delay = 1337; + register_and_check_network(chain_id, network.clone()); + assert_err!( + GhostNetworks::update_network_finality_delay( + RuntimeOrigin::signed(RegistererAccount::get()), + chain_id, + finality_delay + ), + DispatchError::BadOrigin + ); + assert_err!( + GhostNetworks::update_network_finality_delay( + RuntimeOrigin::signed(RemoverAccount::get()), + chain_id, + finality_delay + ), + DispatchError::BadOrigin + ); + assert_err!( + GhostNetworks::update_network_finality_delay( + RuntimeOrigin::signed(RandomAccount::get()), + chain_id, + finality_delay + ), + DispatchError::BadOrigin + ); + assert_eq!(Networks::::get(chain_id), Some(network)); + }); } #[test] fn could_not_update_network_rate_limit_delay_from_random_account() { - ExtBuilder::build() - .execute_with(|| { - let (chain_id, network) = prepare_network_data(); - let rate_limit_delay = 1337; - register_and_check_network(chain_id, network.clone()); - assert_err!(GhostNetworks::update_network_rate_limit_delay( - RuntimeOrigin::signed(RegistererAccount::get()), - chain_id, rate_limit_delay), - DispatchError::BadOrigin); - assert_err!(GhostNetworks::update_network_rate_limit_delay( - RuntimeOrigin::signed(RemoverAccount::get()), - chain_id, rate_limit_delay), - DispatchError::BadOrigin); - assert_err!(GhostNetworks::update_network_rate_limit_delay( - RuntimeOrigin::signed(RandomAccount::get()), - chain_id, rate_limit_delay), - DispatchError::BadOrigin); - assert_eq!(Networks::::get(chain_id), Some(network)); - }); + ExtBuilder::build().execute_with(|| { + let (chain_id, network) = prepare_network_data(); + let rate_limit_delay = 1337; + register_and_check_network(chain_id, network.clone()); + assert_err!( + GhostNetworks::update_network_rate_limit_delay( + RuntimeOrigin::signed(RegistererAccount::get()), + chain_id, + rate_limit_delay + ), + DispatchError::BadOrigin + ); + assert_err!( + GhostNetworks::update_network_rate_limit_delay( + RuntimeOrigin::signed(RemoverAccount::get()), + chain_id, + rate_limit_delay + ), + DispatchError::BadOrigin + ); + assert_err!( + GhostNetworks::update_network_rate_limit_delay( + RuntimeOrigin::signed(RandomAccount::get()), + chain_id, + rate_limit_delay + ), + DispatchError::BadOrigin + ); + assert_eq!(Networks::::get(chain_id), Some(network)); + }); } #[test] fn could_not_update_network_release_delay_from_random_account() { - ExtBuilder::build() - .execute_with(|| { - let (chain_id, network) = prepare_network_data(); - let block_distance = 1337; - register_and_check_network(chain_id, network.clone()); - assert_err!(GhostNetworks::update_network_block_distance( - RuntimeOrigin::signed(RegistererAccount::get()), - chain_id, block_distance), - DispatchError::BadOrigin); - assert_err!(GhostNetworks::update_network_block_distance( - RuntimeOrigin::signed(RemoverAccount::get()), - chain_id, block_distance), - DispatchError::BadOrigin); - assert_err!(GhostNetworks::update_network_block_distance( - RuntimeOrigin::signed(RandomAccount::get()), - chain_id, block_distance), - DispatchError::BadOrigin); - assert_eq!(Networks::::get(chain_id), Some(network)); - }); + ExtBuilder::build().execute_with(|| { + let (chain_id, network) = prepare_network_data(); + let block_distance = 1337; + register_and_check_network(chain_id, network.clone()); + assert_err!( + GhostNetworks::update_network_block_distance( + RuntimeOrigin::signed(RegistererAccount::get()), + chain_id, + block_distance + ), + DispatchError::BadOrigin + ); + assert_err!( + GhostNetworks::update_network_block_distance( + RuntimeOrigin::signed(RemoverAccount::get()), + chain_id, + block_distance + ), + DispatchError::BadOrigin + ); + assert_err!( + GhostNetworks::update_network_block_distance( + RuntimeOrigin::signed(RandomAccount::get()), + chain_id, + block_distance + ), + DispatchError::BadOrigin + ); + assert_eq!(Networks::::get(chain_id), Some(network)); + }); } #[test] fn could_not_update_network_type_from_random_account() { - ExtBuilder::build() - .execute_with(|| { - let (chain_id, network) = prepare_network_data(); - register_and_check_network(chain_id, network.clone()); - assert_err!(GhostNetworks::update_network_type( - RuntimeOrigin::signed(RegistererAccount::get()), - chain_id, NetworkType::Utxo), - DispatchError::BadOrigin); - assert_err!(GhostNetworks::update_network_type( - RuntimeOrigin::signed(RemoverAccount::get()), - chain_id, NetworkType::Utxo), - DispatchError::BadOrigin); - assert_err!(GhostNetworks::update_network_type( - RuntimeOrigin::signed(RandomAccount::get()), - chain_id, NetworkType::Utxo), - DispatchError::BadOrigin); - assert_eq!(Networks::::get(chain_id), Some(network)); - }); + ExtBuilder::build().execute_with(|| { + let (chain_id, network) = prepare_network_data(); + register_and_check_network(chain_id, network.clone()); + assert_err!( + GhostNetworks::update_network_type( + RuntimeOrigin::signed(RegistererAccount::get()), + chain_id, + NetworkType::Utxo + ), + DispatchError::BadOrigin + ); + assert_err!( + GhostNetworks::update_network_type( + RuntimeOrigin::signed(RemoverAccount::get()), + chain_id, + NetworkType::Utxo + ), + DispatchError::BadOrigin + ); + assert_err!( + GhostNetworks::update_network_type( + RuntimeOrigin::signed(RandomAccount::get()), + chain_id, + NetworkType::Utxo + ), + DispatchError::BadOrigin + ); + assert_eq!(Networks::::get(chain_id), Some(network)); + }); } #[test] fn could_not_update_network_gatekeeper_from_random_account() { - ExtBuilder::build() - .execute_with(|| { - let (chain_id, network) = prepare_network_data(); - register_and_check_network(chain_id, network.clone()); - assert_err!(GhostNetworks::update_network_gatekeeper( - RuntimeOrigin::signed(RegistererAccount::get()), - chain_id, b"0x9876543219876543219876543219876543219876".to_vec()), - DispatchError::BadOrigin); - assert_err!(GhostNetworks::update_network_gatekeeper( - RuntimeOrigin::signed(RemoverAccount::get()), - chain_id, b"0x9876543219876543219876543219876543219876".to_vec()), - DispatchError::BadOrigin); - assert_err!(GhostNetworks::update_network_gatekeeper( - RuntimeOrigin::signed(RandomAccount::get()), - chain_id, b"0x9876543219876543219876543219876543219876".to_vec()), - DispatchError::BadOrigin); - assert_eq!(Networks::::get(chain_id), Some(network)); - }); + ExtBuilder::build().execute_with(|| { + let (chain_id, network) = prepare_network_data(); + register_and_check_network(chain_id, network.clone()); + assert_err!( + GhostNetworks::update_network_gatekeeper( + RuntimeOrigin::signed(RegistererAccount::get()), + chain_id, + b"0x9876543219876543219876543219876543219876".to_vec() + ), + DispatchError::BadOrigin + ); + assert_err!( + GhostNetworks::update_network_gatekeeper( + RuntimeOrigin::signed(RemoverAccount::get()), + chain_id, + b"0x9876543219876543219876543219876543219876".to_vec() + ), + DispatchError::BadOrigin + ); + assert_err!( + GhostNetworks::update_network_gatekeeper( + RuntimeOrigin::signed(RandomAccount::get()), + chain_id, + b"0x9876543219876543219876543219876543219876".to_vec() + ), + DispatchError::BadOrigin + ); + assert_eq!(Networks::::get(chain_id), Some(network)); + }); } #[test] fn could_not_update_network_topic_name_from_random_account() { - ExtBuilder::build() - .execute_with(|| { - let (chain_id, network) = prepare_network_data(); - register_and_check_network(chain_id, network.clone()); - assert_err!(GhostNetworks::update_network_topic_name( - RuntimeOrigin::signed(RegistererAccount::get()), - chain_id, b"0x9876543219876543219876543219876543219876543219876543219876543219".to_vec()), - DispatchError::BadOrigin); - assert_err!(GhostNetworks::update_network_topic_name( - RuntimeOrigin::signed(RemoverAccount::get()), - chain_id, b"0x9876543219876543219876543219876543219876543219876543219876543219".to_vec()), - DispatchError::BadOrigin); - assert_err!(GhostNetworks::update_network_topic_name( - RuntimeOrigin::signed(RandomAccount::get()), - chain_id, b"0x9876543219876543219876543219876543219876543219876543219876543219".to_vec()), - DispatchError::BadOrigin); - assert_eq!(Networks::::get(chain_id), Some(network)); - }); + ExtBuilder::build().execute_with(|| { + let (chain_id, network) = prepare_network_data(); + register_and_check_network(chain_id, network.clone()); + assert_err!( + GhostNetworks::update_network_topic_name( + RuntimeOrigin::signed(RegistererAccount::get()), + chain_id, + b"0x9876543219876543219876543219876543219876543219876543219876543219".to_vec() + ), + DispatchError::BadOrigin + ); + assert_err!( + GhostNetworks::update_network_topic_name( + RuntimeOrigin::signed(RemoverAccount::get()), + chain_id, + b"0x9876543219876543219876543219876543219876543219876543219876543219".to_vec() + ), + DispatchError::BadOrigin + ); + assert_err!( + GhostNetworks::update_network_topic_name( + RuntimeOrigin::signed(RandomAccount::get()), + chain_id, + b"0x9876543219876543219876543219876543219876543219876543219876543219".to_vec() + ), + DispatchError::BadOrigin + ); + assert_eq!(Networks::::get(chain_id), Some(network)); + }); } #[test] fn could_not_update_network_incoming_fee_from_random_account() { - ExtBuilder::build() - .execute_with(|| { - let (chain_id, network) = prepare_network_data(); - register_and_check_network(chain_id, network.clone()); - assert_err!(GhostNetworks::update_incoming_network_fee( - RuntimeOrigin::signed(RegistererAccount::get()), - chain_id, 69), - DispatchError::BadOrigin); - assert_err!(GhostNetworks::update_incoming_network_fee( - RuntimeOrigin::signed(RemoverAccount::get()), - chain_id, 69), - DispatchError::BadOrigin); - assert_err!(GhostNetworks::update_incoming_network_fee( - RuntimeOrigin::signed(RandomAccount::get()), - chain_id, 69), - DispatchError::BadOrigin); - assert_eq!(Networks::::get(chain_id), Some(network)); - }); + ExtBuilder::build().execute_with(|| { + let (chain_id, network) = prepare_network_data(); + register_and_check_network(chain_id, network.clone()); + assert_err!( + GhostNetworks::update_incoming_network_fee( + RuntimeOrigin::signed(RegistererAccount::get()), + chain_id, + 69 + ), + DispatchError::BadOrigin + ); + assert_err!( + GhostNetworks::update_incoming_network_fee( + RuntimeOrigin::signed(RemoverAccount::get()), + chain_id, + 69 + ), + DispatchError::BadOrigin + ); + assert_err!( + GhostNetworks::update_incoming_network_fee( + RuntimeOrigin::signed(RandomAccount::get()), + chain_id, + 69 + ), + DispatchError::BadOrigin + ); + assert_eq!(Networks::::get(chain_id), Some(network)); + }); } #[test] fn could_not_update_network_outgoing_fee_from_random_account() { - ExtBuilder::build() - .execute_with(|| { - let (chain_id, network) = prepare_network_data(); - register_and_check_network(chain_id, network.clone()); - assert_err!(GhostNetworks::update_outgoing_network_fee( - RuntimeOrigin::signed(RegistererAccount::get()), - chain_id, 69), - DispatchError::BadOrigin); - assert_err!(GhostNetworks::update_outgoing_network_fee( - RuntimeOrigin::signed(RemoverAccount::get()), - chain_id, 69), - DispatchError::BadOrigin); - assert_err!(GhostNetworks::update_outgoing_network_fee( - RuntimeOrigin::signed(RandomAccount::get()), - chain_id, 69), - DispatchError::BadOrigin); - assert_eq!(Networks::::get(chain_id), Some(network)); - }); + ExtBuilder::build().execute_with(|| { + let (chain_id, network) = prepare_network_data(); + register_and_check_network(chain_id, network.clone()); + assert_err!( + GhostNetworks::update_outgoing_network_fee( + RuntimeOrigin::signed(RegistererAccount::get()), + chain_id, + 69 + ), + DispatchError::BadOrigin + ); + assert_err!( + GhostNetworks::update_outgoing_network_fee( + RuntimeOrigin::signed(RemoverAccount::get()), + chain_id, + 69 + ), + DispatchError::BadOrigin + ); + assert_err!( + GhostNetworks::update_outgoing_network_fee( + RuntimeOrigin::signed(RandomAccount::get()), + chain_id, + 69 + ), + DispatchError::BadOrigin + ); + assert_eq!(Networks::::get(chain_id), Some(network)); + }); } #[test] fn could_not_update_name_for_non_existent_network() { - ExtBuilder::build() - .execute_with(|| { - let chain_id: u32 = 1; - assert_eq!(Networks::::get(chain_id), None); - assert_err!(GhostNetworks::update_network_name( - RuntimeOrigin::signed(UpdaterAccount::get()), - chain_id, "Binance".into()), - crate::Error::::NetworkDoesNotExist); - assert_eq!(Networks::::get(chain_id), None); - }); + ExtBuilder::build().execute_with(|| { + let chain_id: u32 = 1; + assert_eq!(Networks::::get(chain_id), None); + assert_err!( + GhostNetworks::update_network_name( + RuntimeOrigin::signed(UpdaterAccount::get()), + chain_id, + "Binance".into() + ), + crate::Error::::NetworkDoesNotExist + ); + assert_eq!(Networks::::get(chain_id), None); + }); } #[test] fn could_not_update_endpoint_for_non_existent_network() { - ExtBuilder::build() - .execute_with(|| { - let chain_id: u32 = 1; - assert_eq!(Networks::::get(chain_id), None); - assert_err!(GhostNetworks::update_network_endpoint( - RuntimeOrigin::signed(UpdaterAccount::get()), - chain_id, "https:://google.com".into()), - crate::Error::::NetworkDoesNotExist); - assert_eq!(Networks::::get(chain_id), None); - }); + ExtBuilder::build().execute_with(|| { + let chain_id: u32 = 1; + assert_eq!(Networks::::get(chain_id), None); + assert_err!( + GhostNetworks::update_network_endpoint( + RuntimeOrigin::signed(UpdaterAccount::get()), + chain_id, + "https:://google.com".into() + ), + crate::Error::::NetworkDoesNotExist + ); + assert_eq!(Networks::::get(chain_id), None); + }); } #[test] fn could_not_update_finality_delay_for_non_existent_network() { - ExtBuilder::build() - .execute_with(|| { - let chain_id: u32 = 1; - assert_eq!(Networks::::get(chain_id), None); - assert_err!(GhostNetworks::update_network_finality_delay( - RuntimeOrigin::signed(UpdaterAccount::get()), - chain_id, 1337), - crate::Error::::NetworkDoesNotExist); - assert_eq!(Networks::::get(chain_id), None); - }); + ExtBuilder::build().execute_with(|| { + let chain_id: u32 = 1; + assert_eq!(Networks::::get(chain_id), None); + assert_err!( + GhostNetworks::update_network_finality_delay( + RuntimeOrigin::signed(UpdaterAccount::get()), + chain_id, + 1337 + ), + crate::Error::::NetworkDoesNotExist + ); + assert_eq!(Networks::::get(chain_id), None); + }); } #[test] fn could_not_update_rate_limit_delay_for_non_existent_network() { - ExtBuilder::build() - .execute_with(|| { - let chain_id: u32 = 1; - assert_eq!(Networks::::get(chain_id), None); - assert_err!(GhostNetworks::update_network_rate_limit_delay( - RuntimeOrigin::signed(UpdaterAccount::get()), - chain_id, 1337), - crate::Error::::NetworkDoesNotExist); - assert_eq!(Networks::::get(chain_id), None); - }); + ExtBuilder::build().execute_with(|| { + let chain_id: u32 = 1; + assert_eq!(Networks::::get(chain_id), None); + assert_err!( + GhostNetworks::update_network_rate_limit_delay( + RuntimeOrigin::signed(UpdaterAccount::get()), + chain_id, + 1337 + ), + crate::Error::::NetworkDoesNotExist + ); + assert_eq!(Networks::::get(chain_id), None); + }); } #[test] fn could_not_update_release_delay_for_non_existent_network() { - ExtBuilder::build() - .execute_with(|| { - let chain_id: u32 = 1; - assert_eq!(Networks::::get(chain_id), None); - assert_err!(GhostNetworks::update_network_block_distance( - RuntimeOrigin::signed(UpdaterAccount::get()), - chain_id, 1337), - crate::Error::::NetworkDoesNotExist); - assert_eq!(Networks::::get(chain_id), None); - }); + ExtBuilder::build().execute_with(|| { + let chain_id: u32 = 1; + assert_eq!(Networks::::get(chain_id), None); + assert_err!( + GhostNetworks::update_network_block_distance( + RuntimeOrigin::signed(UpdaterAccount::get()), + chain_id, + 1337 + ), + crate::Error::::NetworkDoesNotExist + ); + assert_eq!(Networks::::get(chain_id), None); + }); } #[test] fn could_not_update_type_for_non_existent_network() { - ExtBuilder::build() - .execute_with(|| { - let chain_id: u32 = 1; - assert_eq!(Networks::::get(chain_id), None); - assert_err!(GhostNetworks::update_network_type( - RuntimeOrigin::signed(UpdaterAccount::get()), - chain_id, NetworkType::Utxo), - crate::Error::::NetworkDoesNotExist); - assert_eq!(Networks::::get(chain_id), None); - }); + ExtBuilder::build().execute_with(|| { + let chain_id: u32 = 1; + assert_eq!(Networks::::get(chain_id), None); + assert_err!( + GhostNetworks::update_network_type( + RuntimeOrigin::signed(UpdaterAccount::get()), + chain_id, + NetworkType::Utxo + ), + crate::Error::::NetworkDoesNotExist + ); + assert_eq!(Networks::::get(chain_id), None); + }); } #[test] fn could_not_update_gatekeeper_for_non_existent_network() { - ExtBuilder::build() - .execute_with(|| { - let chain_id: u32 = 1; - assert_eq!(Networks::::get(chain_id), None); - assert_err!(GhostNetworks::update_network_gatekeeper( - RuntimeOrigin::signed(UpdaterAccount::get()), - chain_id, b"0x9876543219876543219876543219876543219876".to_vec()), - crate::Error::::NetworkDoesNotExist); - assert_eq!(Networks::::get(chain_id), None); - }); + ExtBuilder::build().execute_with(|| { + let chain_id: u32 = 1; + assert_eq!(Networks::::get(chain_id), None); + assert_err!( + GhostNetworks::update_network_gatekeeper( + RuntimeOrigin::signed(UpdaterAccount::get()), + chain_id, + b"0x9876543219876543219876543219876543219876".to_vec() + ), + crate::Error::::NetworkDoesNotExist + ); + assert_eq!(Networks::::get(chain_id), None); + }); } #[test] fn could_not_update_topic_name_for_non_existent_network() { - ExtBuilder::build() - .execute_with(|| { - let chain_id: u32 = 1; - assert_eq!(Networks::::get(chain_id), None); - assert_err!(GhostNetworks::update_network_topic_name( - RuntimeOrigin::signed(UpdaterAccount::get()), - chain_id, b"0x9876543219876543219876543219876543219876543219876543219876543219".to_vec()), - crate::Error::::NetworkDoesNotExist); - assert_eq!(Networks::::get(chain_id), None); - }); + ExtBuilder::build().execute_with(|| { + let chain_id: u32 = 1; + assert_eq!(Networks::::get(chain_id), None); + assert_err!( + GhostNetworks::update_network_topic_name( + RuntimeOrigin::signed(UpdaterAccount::get()), + chain_id, + b"0x9876543219876543219876543219876543219876543219876543219876543219".to_vec() + ), + crate::Error::::NetworkDoesNotExist + ); + assert_eq!(Networks::::get(chain_id), None); + }); } #[test] fn could_not_update_incoming_fee_for_non_existent_network() { - ExtBuilder::build() - .execute_with(|| { - let chain_id: u32 = 1; - assert_eq!(Networks::::get(chain_id), None); - assert_err!(GhostNetworks::update_incoming_network_fee( - RuntimeOrigin::signed(UpdaterAccount::get()), - chain_id, 1337), - crate::Error::::NetworkDoesNotExist); - assert_eq!(Networks::::get(chain_id), None); - }); + ExtBuilder::build().execute_with(|| { + let chain_id: u32 = 1; + assert_eq!(Networks::::get(chain_id), None); + assert_err!( + GhostNetworks::update_incoming_network_fee( + RuntimeOrigin::signed(UpdaterAccount::get()), + chain_id, + 1337 + ), + crate::Error::::NetworkDoesNotExist + ); + assert_eq!(Networks::::get(chain_id), None); + }); } #[test] fn could_not_update_outgoing_fee_for_non_existent_network() { - ExtBuilder::build() - .execute_with(|| { - let chain_id: u32 = 1; - assert_eq!(Networks::::get(chain_id), None); - assert_err!(GhostNetworks::update_outgoing_network_fee( - RuntimeOrigin::signed(UpdaterAccount::get()), - chain_id, 1337), - crate::Error::::NetworkDoesNotExist); - assert_eq!(Networks::::get(chain_id), None); - }); + ExtBuilder::build().execute_with(|| { + let chain_id: u32 = 1; + assert_eq!(Networks::::get(chain_id), None); + assert_err!( + GhostNetworks::update_outgoing_network_fee( + RuntimeOrigin::signed(UpdaterAccount::get()), + chain_id, + 1337 + ), + crate::Error::::NetworkDoesNotExist + ); + assert_eq!(Networks::::get(chain_id), None); + }); } #[test] fn could_remove_network_from_authority_account() { - ExtBuilder::build() - .execute_with(|| { - let (chain_id, network) = prepare_network_data(); - register_and_check_network(chain_id, network.clone()); - assert_ok!(GhostNetworks::remove_network( - RuntimeOrigin::signed(RemoverAccount::get()), - chain_id, - )); - assert_eq!(Networks::::get(chain_id), None); - }); + ExtBuilder::build().execute_with(|| { + let (chain_id, network) = prepare_network_data(); + register_and_check_network(chain_id, network.clone()); + assert_ok!(GhostNetworks::remove_network( + RuntimeOrigin::signed(RemoverAccount::get()), + chain_id, + )); + assert_eq!(Networks::::get(chain_id), None); + }); } #[test] fn could_not_remove_network_from_random_account() { - ExtBuilder::build() - .execute_with(|| { - let (chain_id, network) = prepare_network_data(); - register_and_check_network(chain_id, network.clone()); - assert_err!(GhostNetworks::remove_network( - RuntimeOrigin::signed(RegistererAccount::get()), - chain_id), - DispatchError::BadOrigin); - assert_err!(GhostNetworks::remove_network( - RuntimeOrigin::signed(UpdaterAccount::get()), - chain_id), - DispatchError::BadOrigin); - assert_err!(GhostNetworks::remove_network( - RuntimeOrigin::signed(RandomAccount::get()), - chain_id), - DispatchError::BadOrigin); - assert_eq!(Networks::::get(chain_id), Some(network)); - }); + ExtBuilder::build().execute_with(|| { + let (chain_id, network) = prepare_network_data(); + register_and_check_network(chain_id, network.clone()); + assert_err!( + GhostNetworks::remove_network( + RuntimeOrigin::signed(RegistererAccount::get()), + chain_id + ), + DispatchError::BadOrigin + ); + assert_err!( + GhostNetworks::remove_network(RuntimeOrigin::signed(UpdaterAccount::get()), chain_id), + DispatchError::BadOrigin + ); + assert_err!( + GhostNetworks::remove_network(RuntimeOrigin::signed(RandomAccount::get()), chain_id), + DispatchError::BadOrigin + ); + assert_eq!(Networks::::get(chain_id), Some(network)); + }); } #[test] fn could_not_remove_non_existent_network() { - ExtBuilder::build() - .execute_with(|| { - let chain_id: u32 = 1; - assert_eq!(Networks::::get(chain_id), None); - assert_err!(GhostNetworks::remove_network( - RuntimeOrigin::signed(RemoverAccount::get()), - chain_id), - crate::Error::::NetworkDoesNotExist); - assert_eq!(Networks::::get(chain_id), None); - }); + ExtBuilder::build().execute_with(|| { + let chain_id: u32 = 1; + assert_eq!(Networks::::get(chain_id), None); + assert_err!( + GhostNetworks::remove_network(RuntimeOrigin::signed(RemoverAccount::get()), chain_id), + crate::Error::::NetworkDoesNotExist + ); + assert_eq!(Networks::::get(chain_id), None); + }); } #[test] fn bridge_storage_is_empty_by_default() { - ExtBuilder::build() - .execute_with(|| { - assert_eq!(AccumulatedCommission::::get(), 0); - assert_eq!(BridgedImbalance::::get(), - BridgeAdjustment::default()); - }); + ExtBuilder::build().execute_with(|| { + assert_eq!(AccumulatedCommission::::get(), 0); + assert_eq!(BridgedImbalance::::get(), BridgeAdjustment::default()); + }); } #[test] fn gatekeeper_amount_changes_correctly() { - ExtBuilder::build() - .execute_with(|| { - let chain_id: u32 = 1; - let amount_in: u128 = 420; - let amount_out: u128 = 69; - let result = amount_in - 3 * amount_out; + ExtBuilder::build().execute_with(|| { + let chain_id: u32 = 1; + let amount_in: u128 = 420; + let amount_out: u128 = 69; + let result = amount_in - 3 * amount_out; - assert_eq!(GatekeeperAmount::::get(&chain_id), 0); + assert_eq!(GatekeeperAmount::::get(&chain_id), 0); - assert_ok!(GhostNetworks::increase_gatekeeper_amount(&chain_id, &amount_in)); - assert_ok!(GhostNetworks::decrease_gatekeeper_amount(&chain_id, &amount_out)); - assert_ok!(GhostNetworks::decrease_gatekeeper_amount(&chain_id, &amount_out)); - assert_ok!(GhostNetworks::decrease_gatekeeper_amount(&chain_id, &amount_out)); + assert_ok!(GhostNetworks::increase_gatekeeper_amount( + &chain_id, &amount_in + )); + assert_ok!(GhostNetworks::decrease_gatekeeper_amount( + &chain_id, + &amount_out + )); + assert_ok!(GhostNetworks::decrease_gatekeeper_amount( + &chain_id, + &amount_out + )); + assert_ok!(GhostNetworks::decrease_gatekeeper_amount( + &chain_id, + &amount_out + )); - assert_eq!(GatekeeperAmount::::get(&chain_id), result); - }); + assert_eq!(GatekeeperAmount::::get(&chain_id), result); + }); } #[test] fn bridged_imbalance_accumulated_correctly() { - ExtBuilder::build() - .execute_with(|| { - let amount_in: u128 = 420; - let amount_out: u128 = 69; + ExtBuilder::build().execute_with(|| { + let amount_in: u128 = 420; + let amount_out: u128 = 69; - let imbalance_before = BridgedImbalance::::get(); - assert_eq!(imbalance_before.bridged_in, 0); - assert_eq!(imbalance_before.bridged_out, 0); + let imbalance_before = BridgedImbalance::::get(); + assert_eq!(imbalance_before.bridged_in, 0); + assert_eq!(imbalance_before.bridged_out, 0); - assert_ok!(GhostNetworks::accumulate_incoming_imbalance(&amount_in)); - assert_ok!(GhostNetworks::accumulate_outgoing_imbalance(&amount_out)); + assert_ok!(GhostNetworks::accumulate_incoming_imbalance(&amount_in)); + assert_ok!(GhostNetworks::accumulate_outgoing_imbalance(&amount_out)); - let imbalance_after = BridgedImbalance::::get(); - assert_eq!(imbalance_after.bridged_in, amount_in); - assert_eq!(imbalance_after.bridged_out, amount_out); - }); + let imbalance_after = BridgedImbalance::::get(); + assert_eq!(imbalance_after.bridged_in, amount_in); + assert_eq!(imbalance_after.bridged_out, amount_out); + }); } #[test] fn commission_accumulation_is_correct() { - ExtBuilder::build() - .execute_with(|| { - let commission_first: u128 = 420; - let commission_second: u128 = 69; - let result = commission_first + commission_second; + ExtBuilder::build().execute_with(|| { + let commission_first: u128 = 420; + let commission_second: u128 = 69; + let result = commission_first + commission_second; - assert_eq!(AccumulatedCommission::::get(), 0); - assert_ok!(GhostNetworks::accumulate_commission(&commission_first)); - assert_ok!(GhostNetworks::accumulate_commission(&commission_second)); - assert_eq!(AccumulatedCommission::::get(), result); - }); + assert_eq!(AccumulatedCommission::::get(), 0); + assert_ok!(GhostNetworks::accumulate_commission(&commission_first)); + assert_ok!(GhostNetworks::accumulate_commission(&commission_second)); + assert_eq!(AccumulatedCommission::::get(), result); + }); } #[test] fn commission_overflow_and_underflow_emits_error() { - ExtBuilder::build() - .execute_with(|| { - let commission: u128 = u128::MAX - 69; - assert_eq!(AccumulatedCommission::::get(), 0); - assert_ok!(GhostNetworks::accumulate_commission(&commission)); - assert_err!(GhostNetworks::accumulate_commission(&commission), ()); - assert_eq!(AccumulatedCommission::::get(), commission); - }); + ExtBuilder::build().execute_with(|| { + let commission: u128 = u128::MAX - 69; + assert_eq!(AccumulatedCommission::::get(), 0); + assert_ok!(GhostNetworks::accumulate_commission(&commission)); + assert_err!(GhostNetworks::accumulate_commission(&commission), ()); + assert_eq!(AccumulatedCommission::::get(), commission); + }); } #[test] fn gatekeeper_amount_overflow_and_underflow_emits_error() { - ExtBuilder::build() - .execute_with(|| { - let chain_id: u32 = 1; - let commission: u128 = u128::MAX - 69; - assert_eq!(GatekeeperAmount::::get(&chain_id), 0); - assert_ok!(GhostNetworks::increase_gatekeeper_amount( - &chain_id, - &commission, - ), commission); - assert_err!(GhostNetworks::increase_gatekeeper_amount( - &chain_id, - &commission, - ), ()); - assert_eq!(GatekeeperAmount::::get(&chain_id), commission); - assert_ok!(GhostNetworks::decrease_gatekeeper_amount( - &chain_id, - &commission, - ), 0); - assert_err!(GhostNetworks::decrease_gatekeeper_amount( - &chain_id, - &commission, - ), ()); - assert_eq!(GatekeeperAmount::::get(&chain_id), 0); - }); + ExtBuilder::build().execute_with(|| { + let chain_id: u32 = 1; + let commission: u128 = u128::MAX - 69; + assert_eq!(GatekeeperAmount::::get(&chain_id), 0); + assert_ok!( + GhostNetworks::increase_gatekeeper_amount(&chain_id, &commission,), + commission + ); + assert_err!( + GhostNetworks::increase_gatekeeper_amount(&chain_id, &commission,), + () + ); + assert_eq!(GatekeeperAmount::::get(&chain_id), commission); + assert_ok!( + GhostNetworks::decrease_gatekeeper_amount(&chain_id, &commission,), + 0 + ); + assert_err!( + GhostNetworks::decrease_gatekeeper_amount(&chain_id, &commission,), + () + ); + assert_eq!(GatekeeperAmount::::get(&chain_id), 0); + }); } #[test] fn bridged_imbalance_overflow_emits_error() { - ExtBuilder::build() - .execute_with(|| { - let chain_id: u32 = 1; - let amount: u128 = u128::MAX - 69; - assert_eq!(GatekeeperAmount::::get(&chain_id), 0); - assert_ok!(GhostNetworks::accumulate_outgoing_imbalance(&amount), amount); - assert_ok!(GhostNetworks::accumulate_incoming_imbalance(&amount), amount); + ExtBuilder::build().execute_with(|| { + let chain_id: u32 = 1; + let amount: u128 = u128::MAX - 69; + assert_eq!(GatekeeperAmount::::get(&chain_id), 0); + assert_ok!( + GhostNetworks::accumulate_outgoing_imbalance(&amount), + amount + ); + assert_ok!( + GhostNetworks::accumulate_incoming_imbalance(&amount), + amount + ); - assert_err!(GhostNetworks::accumulate_outgoing_imbalance(&amount), ()); - assert_err!(GhostNetworks::accumulate_incoming_imbalance(&amount), ()); - assert_err!(GhostNetworks::accumulate_outgoing_imbalance(&u128::MAX), ()); - assert_err!(GhostNetworks::accumulate_incoming_imbalance(&u128::MAX), ()); + assert_err!(GhostNetworks::accumulate_outgoing_imbalance(&amount), ()); + assert_err!(GhostNetworks::accumulate_incoming_imbalance(&amount), ()); + assert_err!(GhostNetworks::accumulate_outgoing_imbalance(&u128::MAX), ()); + assert_err!(GhostNetworks::accumulate_incoming_imbalance(&u128::MAX), ()); - let bridged_imbalance = BridgedImbalance::::get(); - assert_eq!(bridged_imbalance.bridged_out, amount); - assert_eq!(bridged_imbalance.bridged_in, amount); - }); + let bridged_imbalance = BridgedImbalance::::get(); + assert_eq!(bridged_imbalance.bridged_out, amount); + assert_eq!(bridged_imbalance.bridged_in, amount); + }); } #[test] fn bridged_amount_overflow_and_underflow_emits_error() { - ExtBuilder::build() - .execute_with(|| { - let chain_id_first: u32 = 1; - let chain_id_second: u32 = 2; - let commission: u128 = u128::MAX - 69; + ExtBuilder::build().execute_with(|| { + let chain_id_first: u32 = 1; + let chain_id_second: u32 = 2; + let commission: u128 = u128::MAX - 69; - assert_ok!(GhostNetworks::increase_gatekeeper_amount( - &chain_id_first, - &commission, - ), commission); - assert_ok!(GhostNetworks::increase_gatekeeper_amount( - &chain_id_second, - &commission, - ), commission); + assert_ok!( + GhostNetworks::increase_gatekeeper_amount(&chain_id_first, &commission,), + commission + ); + assert_ok!( + GhostNetworks::increase_gatekeeper_amount(&chain_id_second, &commission,), + commission + ); - assert_err!(GhostNetworks::increase_gatekeeper_amount( - &chain_id_first, - &u128::MAX, - ), ()); - assert_err!(GhostNetworks::increase_gatekeeper_amount( - &chain_id_first, - &commission, - ), ()); + assert_err!( + GhostNetworks::increase_gatekeeper_amount(&chain_id_first, &u128::MAX,), + () + ); + assert_err!( + GhostNetworks::increase_gatekeeper_amount(&chain_id_first, &commission,), + () + ); - assert_err!(GhostNetworks::decrease_gatekeeper_amount( - &chain_id_second, - &u128::MAX, - ), ()); - assert_ok!(GhostNetworks::decrease_gatekeeper_amount( - &chain_id_second, - &commission, - ), 0); + assert_err!( + GhostNetworks::decrease_gatekeeper_amount(&chain_id_second, &u128::MAX,), + () + ); + assert_ok!( + GhostNetworks::decrease_gatekeeper_amount(&chain_id_second, &commission,), + 0 + ); - assert_eq!(GatekeeperAmount::::get(&chain_id_first), commission); - assert_eq!(GatekeeperAmount::::get(&chain_id_second), 0); - }); + assert_eq!(GatekeeperAmount::::get(&chain_id_first), commission); + assert_eq!(GatekeeperAmount::::get(&chain_id_second), 0); + }); } #[test] fn accumulated_commission_could_be_nullified() { - ExtBuilder::build() - .execute_with(|| { - let commission_first: u128 = 420; - let commission_second: u128 = 69; + ExtBuilder::build().execute_with(|| { + let commission_first: u128 = 420; + let commission_second: u128 = 69; - assert_eq!(AccumulatedCommission::::get(), 0); - assert_ok!(GhostNetworks::accumulate_commission( - &commission_first - ), commission_first); - assert_ok!(GhostNetworks::accumulate_commission( - &commission_second, - ), commission_first + commission_second); - assert_eq!(AccumulatedCommission::::get(), - commission_first + commission_second); - GhostNetworks::nullify_commission(); - assert_eq!(AccumulatedCommission::::get(), 0); - }); + assert_eq!(AccumulatedCommission::::get(), 0); + assert_ok!( + GhostNetworks::accumulate_commission(&commission_first), + commission_first + ); + assert_ok!( + GhostNetworks::accumulate_commission(&commission_second,), + commission_first + commission_second + ); + assert_eq!( + AccumulatedCommission::::get(), + commission_first + commission_second + ); + GhostNetworks::nullify_commission(); + assert_eq!(AccumulatedCommission::::get(), 0); + }); } #[test] fn bridged_inlation_reward_works() { - ExtBuilder::build() - .execute_with(|| { - let amount: u128 = 1337 * 1_000_000_000; - let commission: u128 = amount / 100; // 1% commission - let total_staked_ideal: u128 = 69; - let total_staked_not_ideal: u128 = 68; - let total_issuance: u128 = 100; + ExtBuilder::build().execute_with(|| { + let amount: u128 = 1337 * 1_000_000_000; + let commission: u128 = amount / 100; // 1% commission + let total_staked_ideal: u128 = 69; + let total_staked_not_ideal: u128 = 68; + let total_issuance: u128 = 100; - assert_eq!(BridgedInflationCurve::::era_payout( - total_staked_ideal * 1_000, - total_issuance * 1_000, - 0), (0, 0)); - assert_eq!(BridgedInflationCurve::::era_payout( - total_staked_ideal * 1_000_000_000_000, - total_issuance * 1_000_000_000_000, - 0), (0, 0)); - assert_eq!(BridgedInflationCurve::::era_payout( - total_staked_ideal * 1_000_000_000_000_000_000_000_000, - total_issuance * 1_000_000_000_000_000_000_000_000, - 0), (0, 0)); + assert_eq!( + BridgedInflationCurve::::era_payout( + total_staked_ideal * 1_000, + total_issuance * 1_000, + 0 + ), + (0, 0) + ); + assert_eq!( + BridgedInflationCurve::::era_payout( + total_staked_ideal * 1_000_000_000_000, + total_issuance * 1_000_000_000_000, + 0 + ), + (0, 0) + ); + assert_eq!( + BridgedInflationCurve::::era_payout( + total_staked_ideal * 1_000_000_000_000_000_000_000_000, + total_issuance * 1_000_000_000_000_000_000_000_000, + 0 + ), + (0, 0) + ); - assert_eq!(BridgedInflationCurve::::era_payout( - total_staked_not_ideal * 1_000, - total_issuance * 1_000, - 0), (0, 0)); - assert_eq!(BridgedInflationCurve::::era_payout( - total_staked_not_ideal * 1_000_000_000_000, - total_issuance * 1_000_000_000_000, - 0), (0, 0)); - assert_eq!(BridgedInflationCurve::::era_payout( - total_staked_not_ideal * 1_000_000_000_000_000_000_000_000, - total_issuance * 1_000_000_000_000_000_000_000_000, - 0), (0, 0)); + assert_eq!( + BridgedInflationCurve::::era_payout( + total_staked_not_ideal * 1_000, + total_issuance * 1_000, + 0 + ), + (0, 0) + ); + assert_eq!( + BridgedInflationCurve::::era_payout( + total_staked_not_ideal * 1_000_000_000_000, + total_issuance * 1_000_000_000_000, + 0 + ), + (0, 0) + ); + assert_eq!( + BridgedInflationCurve::::era_payout( + total_staked_not_ideal * 1_000_000_000_000_000_000_000_000, + total_issuance * 1_000_000_000_000_000_000_000_000, + 0 + ), + (0, 0) + ); - assert_eq!(BridgedInflationCurve::::era_payout( - 1, total_issuance * 1_000, 0), (0, 0)); - assert_eq!(BridgedInflationCurve::::era_payout( - 1, total_issuance * 1_000_000_000_000, 0), (0, 0)); - assert_eq!(BridgedInflationCurve::::era_payout( - 1, total_issuance * 1_000_000_000_000_000_000_000_000, 0), (0, 0)); + assert_eq!( + BridgedInflationCurve::::era_payout(1, total_issuance * 1_000, 0), + (0, 0) + ); + assert_eq!( + BridgedInflationCurve::::era_payout( + 1, + total_issuance * 1_000_000_000_000, + 0 + ), + (0, 0) + ); + assert_eq!( + BridgedInflationCurve::::era_payout( + 1, + total_issuance * 1_000_000_000_000_000_000_000_000, + 0 + ), + (0, 0) + ); - assert_ok!(GhostNetworks::accumulate_commission(&commission)); - assert_ok!(GhostNetworks::accumulate_incoming_imbalance(&amount)); + assert_ok!(GhostNetworks::accumulate_commission(&commission)); + assert_ok!(GhostNetworks::accumulate_incoming_imbalance(&amount)); - assert_eq!(BridgedInflationCurve::::era_payout( - total_staked_ideal * 1_000, - total_issuance * 1_000 + amount, - 0), (commission, 0)); - assert_eq!(BridgedInflationCurve::::era_payout( - total_staked_ideal * 1_000_000_000_000, - total_issuance * 1_000_000_000_000 + amount, - 0), (commission, 0)); - assert_eq!(BridgedInflationCurve::::era_payout( - total_staked_ideal * 1_000_000_000_000_000_000_000_000, - total_issuance * 1_000_000_000_000_000_000_000_000 + amount, - 0), (commission, 0)); + assert_eq!( + BridgedInflationCurve::::era_payout( + total_staked_ideal * 1_000, + total_issuance * 1_000 + amount, + 0 + ), + (commission, 0) + ); + assert_eq!( + BridgedInflationCurve::::era_payout( + total_staked_ideal * 1_000_000_000_000, + total_issuance * 1_000_000_000_000 + amount, + 0 + ), + (commission, 0) + ); + assert_eq!( + BridgedInflationCurve::::era_payout( + total_staked_ideal * 1_000_000_000_000_000_000_000_000, + total_issuance * 1_000_000_000_000_000_000_000_000 + amount, + 0 + ), + (commission, 0) + ); - assert_eq!(BridgedInflationCurve::::era_payout( - total_staked_not_ideal * 1_000, - total_issuance * 1_000 + amount, - 0), (13177472000, 192528000)); - assert_eq!(13177472000 + 192528000, commission); - assert_eq!(BridgedInflationCurve::::era_payout( - total_staked_not_ideal * 1_000_000_000_000, - total_issuance * 1_000_000_000_000 + amount, - 0), (13177568884, 192431116)); - assert_eq!(13177568884 + 192431116, commission); - assert_eq!(BridgedInflationCurve::::era_payout( - total_staked_not_ideal * 1_000_000_000_000_000_000_000_000, - total_issuance * 1_000_000_000_000_000_000_000_000 + amount, - 0), (13177568884, 192431116)); - assert_eq!(13177568884 + 192431116, commission); + assert_eq!( + BridgedInflationCurve::::era_payout( + total_staked_not_ideal * 1_000, + total_issuance * 1_000 + amount, + 0 + ), + (13177472000, 192528000) + ); + assert_eq!(13177472000 + 192528000, commission); + assert_eq!( + BridgedInflationCurve::::era_payout( + total_staked_not_ideal * 1_000_000_000_000, + total_issuance * 1_000_000_000_000 + amount, + 0 + ), + (13177568884, 192431116) + ); + assert_eq!(13177568884 + 192431116, commission); + assert_eq!( + BridgedInflationCurve::::era_payout( + total_staked_not_ideal * 1_000_000_000_000_000_000_000_000, + total_issuance * 1_000_000_000_000_000_000_000_000 + amount, + 0 + ), + (13177568884, 192431116) + ); + assert_eq!(13177568884 + 192431116, commission); - assert_eq!(BridgedInflationCurve::::era_payout( - 1, total_issuance * 1_000 + amount, 0), - (92386700, 13277613300)); - assert_eq!(92386700 + 13277613300, commission); - assert_eq!(BridgedInflationCurve::::era_payout( - 1, total_issuance * 1_000_000_000_000 + amount, 0), - (92253000, 13277747000)); - assert_eq!(92253000 + 13277747000, commission); - assert_eq!(BridgedInflationCurve::::era_payout( - 1, total_issuance * 1_000_000_000_000_000_000_000_000 + amount, 0), - (92253000, 13277747000)); - assert_eq!(92253000 + 13277747000, commission); + assert_eq!( + BridgedInflationCurve::::era_payout( + 1, + total_issuance * 1_000 + amount, + 0 + ), + (92386700, 13277613300) + ); + assert_eq!(92386700 + 13277613300, commission); + assert_eq!( + BridgedInflationCurve::::era_payout( + 1, + total_issuance * 1_000_000_000_000 + amount, + 0 + ), + (92253000, 13277747000) + ); + assert_eq!(92253000 + 13277747000, commission); + assert_eq!( + BridgedInflationCurve::::era_payout( + 1, + total_issuance * 1_000_000_000_000_000_000_000_000 + amount, + 0 + ), + (92253000, 13277747000) + ); + assert_eq!(92253000 + 13277747000, commission); - GhostNetworks::nullify_commission(); + GhostNetworks::nullify_commission(); - assert_eq!(BridgedInflationCurve::::era_payout( - total_staked_ideal * 1_000, - total_issuance * 1_000 + amount, - 0), (0, 0)); - assert_eq!(BridgedInflationCurve::::era_payout( - total_staked_ideal * 1_000_000_000_000, - total_issuance * 1_000_000_000_000 + amount, - 0), (0, 0)); - assert_eq!(BridgedInflationCurve::::era_payout( - total_staked_ideal * 1_000_000_000_000_000_000_000_000, - total_issuance * 1_000_000_000_000_000_000_000_000 + amount, - 0), (0, 0)); + assert_eq!( + BridgedInflationCurve::::era_payout( + total_staked_ideal * 1_000, + total_issuance * 1_000 + amount, + 0 + ), + (0, 0) + ); + assert_eq!( + BridgedInflationCurve::::era_payout( + total_staked_ideal * 1_000_000_000_000, + total_issuance * 1_000_000_000_000 + amount, + 0 + ), + (0, 0) + ); + assert_eq!( + BridgedInflationCurve::::era_payout( + total_staked_ideal * 1_000_000_000_000_000_000_000_000, + total_issuance * 1_000_000_000_000_000_000_000_000 + amount, + 0 + ), + (0, 0) + ); - assert_eq!(BridgedInflationCurve::::era_payout( - total_staked_not_ideal * 1_000, - total_issuance * 1_000 + amount, - 0), (0, 0)); - assert_eq!(BridgedInflationCurve::::era_payout( - total_staked_not_ideal * 1_000_000_000_000, - total_issuance * 1_000_000_000_000 + amount, - 0), (0, 0)); - assert_eq!(BridgedInflationCurve::::era_payout( - total_staked_not_ideal * 1_000_000_000_000_000_000_000_000, - total_issuance * 1_000_000_000_000_000_000_000_000 + amount, - 0), (0, 0)); + assert_eq!( + BridgedInflationCurve::::era_payout( + total_staked_not_ideal * 1_000, + total_issuance * 1_000 + amount, + 0 + ), + (0, 0) + ); + assert_eq!( + BridgedInflationCurve::::era_payout( + total_staked_not_ideal * 1_000_000_000_000, + total_issuance * 1_000_000_000_000 + amount, + 0 + ), + (0, 0) + ); + assert_eq!( + BridgedInflationCurve::::era_payout( + total_staked_not_ideal * 1_000_000_000_000_000_000_000_000, + total_issuance * 1_000_000_000_000_000_000_000_000 + amount, + 0 + ), + (0, 0) + ); - assert_eq!(BridgedInflationCurve::::era_payout( - 1, total_issuance * 1_000 + amount, 0), (0, 0)); - assert_eq!(BridgedInflationCurve::::era_payout( - 1, total_issuance * 1_000_000_000_000 + amount, 0), (0, 0)); - assert_eq!(BridgedInflationCurve::::era_payout( - 1, total_issuance * 1_000_000_000_000_000_000_000_000 + amount, 0), (0, 0)); - }); + assert_eq!( + BridgedInflationCurve::::era_payout( + 1, + total_issuance * 1_000 + amount, + 0 + ), + (0, 0) + ); + assert_eq!( + BridgedInflationCurve::::era_payout( + 1, + total_issuance * 1_000_000_000_000 + amount, + 0 + ), + (0, 0) + ); + assert_eq!( + BridgedInflationCurve::::era_payout( + 1, + total_issuance * 1_000_000_000_000_000_000_000_000 + amount, + 0 + ), + (0, 0) + ); + }); } #[test] fn bridged_inflation_era_payout_triggers_need_of_nullification() { - ExtBuilder::build() - .execute_with(|| { - let amount: u128 = 1337 * 1_000_000_000; - let commission: u128 = amount / 100; // 1% commission - let total_staked_ideal: u128 = 69; - let total_issuance: u128 = 100; + ExtBuilder::build().execute_with(|| { + let amount: u128 = 1337 * 1_000_000_000; + let commission: u128 = amount / 100; // 1% commission + let total_staked_ideal: u128 = 69; + let total_issuance: u128 = 100; - assert_ok!(GhostNetworks::accumulate_commission(&commission)); - assert_ok!(GhostNetworks::accumulate_incoming_imbalance(&amount)); - assert_eq!(NullifyNeeded::::get(), false); - assert_eq!(BridgedInflationCurve::::era_payout( - total_staked_ideal * 1_000, - total_issuance * 1_000 + amount, - 0), (commission, 0)); - assert_eq!(NullifyNeeded::::get(), true); - GhostNetworks::on_finalize(69); - assert_eq!(NullifyNeeded::::get(), false); - }); + assert_ok!(GhostNetworks::accumulate_commission(&commission)); + assert_ok!(GhostNetworks::accumulate_incoming_imbalance(&amount)); + assert_eq!(NullifyNeeded::::get(), false); + assert_eq!( + BridgedInflationCurve::::era_payout( + total_staked_ideal * 1_000, + total_issuance * 1_000 + amount, + 0 + ), + (commission, 0) + ); + assert_eq!(NullifyNeeded::::get(), true); + GhostNetworks::on_finalize(69); + assert_eq!(NullifyNeeded::::get(), false); + }); } #[test] fn trigger_nullification_works_as_expected() { - ExtBuilder::build() - .execute_with(|| { - assert_eq!(NullifyNeeded::::get(), false); - GhostNetworks::trigger_nullification(); - assert_eq!(NullifyNeeded::::get(), true); - GhostNetworks::trigger_nullification(); - assert_eq!(NullifyNeeded::::get(), false); - }); + ExtBuilder::build().execute_with(|| { + assert_eq!(NullifyNeeded::::get(), false); + GhostNetworks::trigger_nullification(); + assert_eq!(NullifyNeeded::::get(), true); + GhostNetworks::trigger_nullification(); + assert_eq!(NullifyNeeded::::get(), false); + }); }