separate bridged imbalance and gatekeeper amount. requested by @st1nky for ghost-slow-clap
Signed-off-by: Uncle Stretch <uncle.stretch@ghostchain.io>
This commit is contained in:
		
							parent
							
								
									1e4abbfe69
								
							
						
					
					
						commit
						b5fc86aa9e
					
				| @ -1,6 +1,6 @@ | ||||
| [package] | ||||
| name = "ghost-networks" | ||||
| version = "0.1.5" | ||||
| version = "0.1.6" | ||||
| license.workspace = true | ||||
| authors.workspace = true | ||||
| edition.workspace = true | ||||
|  | ||||
| @ -631,17 +631,7 @@ impl<T: Config> NetworkDataMutateHandler<NetworkData, BalanceOf<T>> for Pallet<T | ||||
|     fn increase_gatekeeper_amount( | ||||
|         network_id: &T::NetworkId, | ||||
|         amount: &BalanceOf<T>, | ||||
|     ) -> Result<(BalanceOf<T>, BalanceOf<T>), ()> { | ||||
|         let new_bridged_in_amount = BridgedImbalance::<T>::mutate(|bridged_imbalance| { | ||||
|             match bridged_imbalance.bridged_in.checked_add(amount) { | ||||
|                 Some(value) => { | ||||
|                     (*bridged_imbalance).bridged_in = value; | ||||
|                     Ok(value) | ||||
|                 }, | ||||
|                 None => Err(()) | ||||
|             } | ||||
|         })?; | ||||
| 
 | ||||
|     ) -> Result<BalanceOf<T>, ()> { | ||||
|         let new_gatekeeper_amount = GatekeeperAmount::<T>::mutate(network_id, |gatekeeper_amount| { | ||||
|             match gatekeeper_amount.checked_add(amount) { | ||||
|                 Some(value) => { | ||||
| @ -652,13 +642,13 @@ impl<T: Config> NetworkDataMutateHandler<NetworkData, BalanceOf<T>> for Pallet<T | ||||
|             } | ||||
|         })?; | ||||
| 
 | ||||
|         Ok((new_gatekeeper_amount, new_bridged_in_amount)) | ||||
|         Ok(new_gatekeeper_amount) | ||||
|     } | ||||
| 
 | ||||
|     fn decrease_gatekeeper_amount( | ||||
|         network_id: &T::NetworkId, | ||||
|         amount: &BalanceOf<T>, | ||||
|     ) -> Result<(BalanceOf<T>, BalanceOf<T>), ()> { | ||||
|     ) -> Result<BalanceOf<T>, ()> { | ||||
|         let new_gatekeeper_amount = GatekeeperAmount::<T>::mutate(network_id, |gatekeeper_amount| { | ||||
|             match gatekeeper_amount.checked_sub(amount) { | ||||
|                 Some(value) => { | ||||
| @ -669,6 +659,10 @@ impl<T: Config> NetworkDataMutateHandler<NetworkData, BalanceOf<T>> for Pallet<T | ||||
|             } | ||||
|         })?; | ||||
| 
 | ||||
|         Ok(new_gatekeeper_amount) | ||||
|     } | ||||
| 
 | ||||
|     fn accumulate_outgoing_imbalance(amount: &BalanceOf<T>) -> Result<BalanceOf<T>, ()> { | ||||
|         let new_bridged_out_amount = BridgedImbalance::<T>::mutate(|bridged_imbalance| { | ||||
|              match bridged_imbalance.bridged_out.checked_add(amount) { | ||||
|                  Some(value) => { | ||||
| @ -679,7 +673,21 @@ impl<T: Config> NetworkDataMutateHandler<NetworkData, BalanceOf<T>> for Pallet<T | ||||
|              } | ||||
|         })?; | ||||
| 
 | ||||
|         Ok((new_gatekeeper_amount, new_bridged_out_amount)) | ||||
|         Ok(new_bridged_out_amount) | ||||
|     } | ||||
| 
 | ||||
|     fn accumulate_incoming_imbalance(amount: &BalanceOf<T>) -> Result<BalanceOf<T>, ()> { | ||||
|         let new_bridged_in_amount = BridgedImbalance::<T>::mutate(|bridged_imbalance| { | ||||
|             match bridged_imbalance.bridged_in.checked_add(amount) { | ||||
|                 Some(value) => { | ||||
|                     (*bridged_imbalance).bridged_in = value; | ||||
|                     Ok(value) | ||||
|                 }, | ||||
|                 None => Err(()) | ||||
|             } | ||||
|         })?; | ||||
| 
 | ||||
|         Ok(new_bridged_in_amount) | ||||
|     } | ||||
| 
 | ||||
|     fn accumulate_commission(commission: &BalanceOf<T>) -> Result<BalanceOf<T>, ()> { | ||||
|  | ||||
| @ -656,8 +656,6 @@ fn gatekeeper_amount_changes_correctly() { | ||||
|             let result = amount_in - 3 * amount_out; | ||||
| 
 | ||||
|             assert_eq!(GatekeeperAmount::<Test>::get(&chain_id), 0); | ||||
|             assert_eq!(BridgedImbalance::<Test>::get(), | ||||
|                 BridgeAdjustment::default()); | ||||
| 
 | ||||
|             assert_ok!(GhostNetworks::increase_gatekeeper_amount(&chain_id, &amount_in)); | ||||
|             assert_ok!(GhostNetworks::decrease_gatekeeper_amount(&chain_id, &amount_out)); | ||||
| @ -665,9 +663,26 @@ fn gatekeeper_amount_changes_correctly() { | ||||
|             assert_ok!(GhostNetworks::decrease_gatekeeper_amount(&chain_id, &amount_out)); | ||||
| 
 | ||||
|             assert_eq!(GatekeeperAmount::<Test>::get(&chain_id), result); | ||||
|             assert_eq!(BridgedImbalance::<Test>::get(), BridgeAdjustment { | ||||
|                 bridged_out: 3 * amount_out, | ||||
|                 bridged_in: amount_in }); | ||||
|         }); | ||||
| } | ||||
| 
 | ||||
| #[test] | ||||
| fn bridged_imbalance_accumulated_correctly() { | ||||
|     ExtBuilder::build() | ||||
|         .execute_with(|| { | ||||
|             let amount_in: u128 = 420; | ||||
|             let amount_out: u128 = 69; | ||||
| 
 | ||||
|             let imbalance_before = BridgedImbalance::<Test>::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)); | ||||
| 
 | ||||
|             let imbalance_after = BridgedImbalance::<Test>::get(); | ||||
|             assert_eq!(imbalance_after.bridged_in, amount_in); | ||||
|             assert_eq!(imbalance_after.bridged_out, amount_out); | ||||
|         }); | ||||
| } | ||||
| 
 | ||||
| @ -708,7 +723,7 @@ fn gatekeeper_amount_overflow_and_underflow_emits_error() { | ||||
|             assert_ok!(GhostNetworks::increase_gatekeeper_amount( | ||||
|                     &chain_id, | ||||
|                     &commission, | ||||
|             ), (commission, commission)); | ||||
|             ), commission); | ||||
|             assert_err!(GhostNetworks::increase_gatekeeper_amount( | ||||
|                     &chain_id, | ||||
|                     &commission, | ||||
| @ -717,16 +732,33 @@ fn gatekeeper_amount_overflow_and_underflow_emits_error() { | ||||
|             assert_ok!(GhostNetworks::decrease_gatekeeper_amount( | ||||
|                     &chain_id, | ||||
|                     &commission, | ||||
|             ), (0, commission)); | ||||
|             ), 0); | ||||
|             assert_err!(GhostNetworks::decrease_gatekeeper_amount( | ||||
|                     &chain_id, | ||||
|                     &commission, | ||||
|             ), ()); | ||||
|             assert_eq!(GatekeeperAmount::<Test>::get(&chain_id), 0); | ||||
|             assert_eq!(BridgedImbalance::<Test>::get(), BridgeAdjustment { | ||||
|                 bridged_out: commission, | ||||
|                 bridged_in: commission, | ||||
|         }); | ||||
| } | ||||
| 
 | ||||
| #[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::<Test>::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), ()); | ||||
| 
 | ||||
|             let bridged_imbalance = BridgedImbalance::<Test>::get(); | ||||
|             assert_eq!(bridged_imbalance.bridged_out, amount); | ||||
|             assert_eq!(bridged_imbalance.bridged_in, amount); | ||||
|         }); | ||||
| } | ||||
| 
 | ||||
| @ -737,18 +769,16 @@ fn bridged_amount_overflow_and_underflow_emits_error() { | ||||
|             let chain_id_first: u32 = 1; | ||||
|             let chain_id_second: u32 = 2; | ||||
|             let commission: u128 = u128::MAX - 69; | ||||
|             assert_eq!(BridgedImbalance::<Test>::get(), BridgeAdjustment { | ||||
|                 bridged_out: 0, | ||||
|                 bridged_in: 0, | ||||
|             }); | ||||
| 
 | ||||
|             assert_ok!(GhostNetworks::increase_gatekeeper_amount( | ||||
|                     &chain_id_first, | ||||
|                     &commission, | ||||
|             ), (commission, commission)); | ||||
|             assert_err!(GhostNetworks::increase_gatekeeper_amount( | ||||
|             ), commission); | ||||
|             assert_ok!(GhostNetworks::increase_gatekeeper_amount( | ||||
|                     &chain_id_second, | ||||
|                     &commission, | ||||
|             ), ()); | ||||
|             ), commission); | ||||
| 
 | ||||
|             assert_err!(GhostNetworks::increase_gatekeeper_amount( | ||||
|                     &chain_id_first, | ||||
|                     &u128::MAX, | ||||
| @ -757,12 +787,18 @@ fn bridged_amount_overflow_and_underflow_emits_error() { | ||||
|                     &chain_id_first, | ||||
|                     &commission, | ||||
|             ), ()); | ||||
|             assert_eq!(GatekeeperAmount::<Test>::get(&chain_id_first), commission); | ||||
|             assert_eq!(GatekeeperAmount::<Test>::get(&chain_id_second), 0); | ||||
| 
 | ||||
|             assert_err!(GhostNetworks::decrease_gatekeeper_amount( | ||||
|                     &chain_id_second, | ||||
|                     &commission, | ||||
|                     &u128::MAX, | ||||
|             ), ()); | ||||
|             assert_ok!(GhostNetworks::decrease_gatekeeper_amount( | ||||
|                     &chain_id_second, | ||||
|                     &commission, | ||||
|             ), 0); | ||||
| 
 | ||||
|             assert_eq!(GatekeeperAmount::<Test>::get(&chain_id_first), commission); | ||||
|             assert_eq!(GatekeeperAmount::<Test>::get(&chain_id_second), 0); | ||||
|         }); | ||||
| } | ||||
| 
 | ||||
| @ -791,7 +827,6 @@ fn accumulated_commission_could_be_nullified() { | ||||
| fn bridged_inlation_reward_works() { | ||||
|     ExtBuilder::build() | ||||
|         .execute_with(|| { | ||||
|             let chain_id: u32 = 1; | ||||
|             let amount: u128 = 1337 * 1_000_000_000; | ||||
|             let commission: u128 = amount / 100; // 1% commission
 | ||||
|             let total_staked_ideal: u128 = 69; | ||||
| @ -832,7 +867,7 @@ fn bridged_inlation_reward_works() { | ||||
|                     1, total_issuance * 1_000_000_000_000_000_000_000_000, 0), (0, 0)); | ||||
| 
 | ||||
|             assert_ok!(GhostNetworks::accumulate_commission(&commission)); | ||||
|             assert_ok!(GhostNetworks::increase_gatekeeper_amount(&chain_id, &amount)); | ||||
|             assert_ok!(GhostNetworks::accumulate_incoming_imbalance(&amount)); | ||||
| 
 | ||||
|             assert_eq!(BridgedInflationCurve::<RewardCurve, Test>::era_payout( | ||||
|                     total_staked_ideal * 1_000, | ||||
| @ -917,14 +952,13 @@ fn bridged_inlation_reward_works() { | ||||
| fn bridged_inflation_era_payout_triggers_need_of_nullification() { | ||||
|     ExtBuilder::build() | ||||
|         .execute_with(|| { | ||||
|             let chain_id: u32 = 1; | ||||
|             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::increase_gatekeeper_amount(&chain_id, &amount)); | ||||
|             assert_ok!(GhostNetworks::accumulate_incoming_imbalance(&amount)); | ||||
|             assert_eq!(NullifyNeeded::<Test>::get(), false); | ||||
|             assert_eq!(BridgedInflationCurve::<RewardCurve, Test>::era_payout( | ||||
|                     total_staked_ideal * 1_000, | ||||
|  | ||||
| @ -1,6 +1,6 @@ | ||||
| [package] | ||||
| name = "ghost-traits" | ||||
| version = "0.3.21" | ||||
| version = "0.3.22" | ||||
| license.workspace = true | ||||
| authors.workspace = true | ||||
| edition.workspace = true | ||||
|  | ||||
| @ -28,14 +28,12 @@ pub trait NetworkDataMutateHandler<Network, Balance>: NetworkDataInspectHandler< | ||||
|     fn register(chain_id: Self::NetworkId, network: Network) -> DispatchResult; | ||||
|     fn remove(chain_id: Self::NetworkId) -> DispatchResult; | ||||
| 
 | ||||
|     fn increase_gatekeeper_amount( | ||||
|         chain_id: &Self::NetworkId, 
 | ||||
|         amount: &Balance, | ||||
|     ) -> Result<(Balance, Balance), ()>; | ||||
|     fn decrease_gatekeeper_amount( | ||||
|         chain_id: &Self::NetworkId, 
 | ||||
|         amount: &Balance, | ||||
|     ) -> Result<(Balance, Balance), ()>; | ||||
|     fn increase_gatekeeper_amount(chain_id: &Self::NetworkId, amount: &Balance) -> Result<Balance, ()>; | ||||
|     fn decrease_gatekeeper_amount(chain_id: &Self::NetworkId, amount: &Balance) -> Result<Balance, ()>; | ||||
| 
 | ||||
|     fn accumulate_outgoing_imbalance(amount: &Balance) -> Result<Balance, ()>; | ||||
|     fn accumulate_incoming_imbalance(amount: &Balance) -> Result<Balance, ()>; | ||||
| 
 | ||||
|     fn accumulate_commission(commission: &Balance) -> Result<Balance, ()>; | ||||
|     fn nullify_commission(); | ||||
|     fn trigger_nullification(); | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user