mirror of
https://git.ghostchain.io/proxmio/ghost-node.git
synced 2025-12-27 11:19:57 +00:00
extend pallet ghost-networks and create BridgedInflationCurve
Signed-off-by: Uncle Stinky <uncle.stinky@ghostchain.io>
This commit is contained in:
@@ -1,16 +1,16 @@
|
||||
use mock::{
|
||||
ExtBuilder, System, RegistererAccount, UpdaterAccount, RemoverAccount,
|
||||
RandomAccount, GhostNetworks, Test, RuntimeEvent, RuntimeOrigin,
|
||||
RandomAccount, GhostNetworks, Test, RuntimeEvent, RuntimeOrigin, RewardCurve,
|
||||
};
|
||||
use frame_support::{assert_err, assert_ok};
|
||||
use sp_runtime::DispatchError;
|
||||
use pallet_staking::EraPayout;
|
||||
use super::*;
|
||||
|
||||
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(),
|
||||
default_endpoint: "https:://some-endpoint.my-server.com/v1/my-super-secret-key".into(),
|
||||
finality_delay: Some(69),
|
||||
release_delay: Some(69),
|
||||
network_type: NetworkType::Evm,
|
||||
@@ -635,3 +635,281 @@ fn could_not_remove_non_existent_network() {
|
||||
assert_eq!(Networks::<Test>::get(chain_id), None);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn bridge_storage_is_empty_by_default() {
|
||||
ExtBuilder::build()
|
||||
.execute_with(|| {
|
||||
assert_eq!(AccumulatedCommission::<Test>::get(), 0);
|
||||
assert_eq!(BridgedImbalance::<Test>::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;
|
||||
|
||||
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));
|
||||
assert_ok!(GhostNetworks::decrease_gatekeeper_amount(&chain_id, &amount_out));
|
||||
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 commission_accumulation_is_correct() {
|
||||
ExtBuilder::build()
|
||||
.execute_with(|| {
|
||||
let commission_first: u128 = 420;
|
||||
let commission_second: u128 = 69;
|
||||
let result = commission_first + commission_second;
|
||||
|
||||
assert_eq!(AccumulatedCommission::<Test>::get(), 0);
|
||||
assert_ok!(GhostNetworks::accumulate_commission(&commission_first));
|
||||
assert_ok!(GhostNetworks::accumulate_commission(&commission_second));
|
||||
assert_eq!(AccumulatedCommission::<Test>::get(), result);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn commission_overflow_and_underflow_emits_error() {
|
||||
ExtBuilder::build()
|
||||
.execute_with(|| {
|
||||
let commission: u128 = u128::MAX - 69;
|
||||
assert_eq!(AccumulatedCommission::<Test>::get(), 0);
|
||||
assert_ok!(GhostNetworks::accumulate_commission(&commission));
|
||||
assert_err!(GhostNetworks::accumulate_commission(&commission), ());
|
||||
assert_eq!(AccumulatedCommission::<Test>::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::<Test>::get(&chain_id), 0);
|
||||
assert_ok!(GhostNetworks::increase_gatekeeper_amount(
|
||||
&chain_id,
|
||||
&commission,
|
||||
), (commission, commission));
|
||||
assert_err!(GhostNetworks::increase_gatekeeper_amount(
|
||||
&chain_id,
|
||||
&commission,
|
||||
), ());
|
||||
assert_eq!(GatekeeperAmount::<Test>::get(&chain_id), commission);
|
||||
assert_ok!(GhostNetworks::decrease_gatekeeper_amount(
|
||||
&chain_id,
|
||||
&commission,
|
||||
), (0, commission));
|
||||
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_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;
|
||||
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(
|
||||
&chain_id_second,
|
||||
&commission,
|
||||
), ());
|
||||
assert_err!(GhostNetworks::increase_gatekeeper_amount(
|
||||
&chain_id_first,
|
||||
&u128::MAX,
|
||||
), ());
|
||||
assert_err!(GhostNetworks::increase_gatekeeper_amount(
|
||||
&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,
|
||||
), ());
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn accumulated_commission_could_be_nullified() {
|
||||
ExtBuilder::build()
|
||||
.execute_with(|| {
|
||||
let commission_first: u128 = 420;
|
||||
let commission_second: u128 = 69;
|
||||
|
||||
assert_eq!(AccumulatedCommission::<Test>::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::<Test>::get(),
|
||||
commission_first + commission_second);
|
||||
GhostNetworks::nullify_commission();
|
||||
assert_eq!(AccumulatedCommission::<Test>::get(), 0);
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
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;
|
||||
let total_staked_not_ideal: u128 = 68;
|
||||
let total_issuance: u128 = 100;
|
||||
|
||||
|
||||
assert_eq!(BridgedInflationCurve::<RewardCurve, Test>::era_payout(
|
||||
total_staked_ideal * 1_000,
|
||||
total_issuance * 1_000,
|
||||
0), (0, 0));
|
||||
assert_eq!(BridgedInflationCurve::<RewardCurve, Test>::era_payout(
|
||||
total_staked_ideal * 1_000_000_000_000,
|
||||
total_issuance * 1_000_000_000_000,
|
||||
0), (0, 0));
|
||||
assert_eq!(BridgedInflationCurve::<RewardCurve, Test>::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::<RewardCurve, Test>::era_payout(
|
||||
total_staked_not_ideal * 1_000,
|
||||
total_issuance * 1_000,
|
||||
0), (0, 0));
|
||||
assert_eq!(BridgedInflationCurve::<RewardCurve, Test>::era_payout(
|
||||
total_staked_not_ideal * 1_000_000_000_000,
|
||||
total_issuance * 1_000_000_000_000,
|
||||
0), (0, 0));
|
||||
assert_eq!(BridgedInflationCurve::<RewardCurve, Test>::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::<RewardCurve, Test>::era_payout(
|
||||
1, total_issuance * 1_000, 0), (0, 0));
|
||||
assert_eq!(BridgedInflationCurve::<RewardCurve, Test>::era_payout(
|
||||
1, total_issuance * 1_000_000_000_000, 0), (0, 0));
|
||||
assert_eq!(BridgedInflationCurve::<RewardCurve, Test>::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::increase_gatekeeper_amount(&chain_id, &amount));
|
||||
|
||||
assert_eq!(BridgedInflationCurve::<RewardCurve, Test>::era_payout(
|
||||
total_staked_ideal * 1_000,
|
||||
total_issuance * 1_000 + amount,
|
||||
0), (commission, 0));
|
||||
assert_eq!(BridgedInflationCurve::<RewardCurve, Test>::era_payout(
|
||||
total_staked_ideal * 1_000_000_000_000,
|
||||
total_issuance * 1_000_000_000_000 + amount,
|
||||
0), (commission, 0));
|
||||
assert_eq!(BridgedInflationCurve::<RewardCurve, Test>::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::<RewardCurve, Test>::era_payout(
|
||||
total_staked_not_ideal * 1_000,
|
||||
total_issuance * 1_000 + amount,
|
||||
0), (13177472000, 192528000));
|
||||
assert_eq!(13177472000 + 192528000, commission);
|
||||
assert_eq!(BridgedInflationCurve::<RewardCurve, Test>::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::<RewardCurve, Test>::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::<RewardCurve, Test>::era_payout(
|
||||
1, total_issuance * 1_000 + amount, 0),
|
||||
(92386700, 13277613300));
|
||||
assert_eq!(92386700 + 13277613300, commission);
|
||||
assert_eq!(BridgedInflationCurve::<RewardCurve, Test>::era_payout(
|
||||
1, total_issuance * 1_000_000_000_000 + amount, 0),
|
||||
(92253000, 13277747000));
|
||||
assert_eq!(92253000 + 13277747000, commission);
|
||||
assert_eq!(BridgedInflationCurve::<RewardCurve, Test>::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();
|
||||
|
||||
assert_eq!(BridgedInflationCurve::<RewardCurve, Test>::era_payout(
|
||||
total_staked_ideal * 1_000,
|
||||
total_issuance * 1_000 + amount,
|
||||
0), (0, 0));
|
||||
assert_eq!(BridgedInflationCurve::<RewardCurve, Test>::era_payout(
|
||||
total_staked_ideal * 1_000_000_000_000,
|
||||
total_issuance * 1_000_000_000_000 + amount,
|
||||
0), (0, 0));
|
||||
assert_eq!(BridgedInflationCurve::<RewardCurve, Test>::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::<RewardCurve, Test>::era_payout(
|
||||
total_staked_not_ideal * 1_000,
|
||||
total_issuance * 1_000 + amount,
|
||||
0), (0, 0));
|
||||
assert_eq!(BridgedInflationCurve::<RewardCurve, Test>::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::<RewardCurve, Test>::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::<RewardCurve, Test>::era_payout(
|
||||
1, total_issuance * 1_000 + amount, 0), (0, 0));
|
||||
assert_eq!(BridgedInflationCurve::<RewardCurve, Test>::era_payout(
|
||||
1, total_issuance * 1_000_000_000_000 + amount, 0), (0, 0));
|
||||
assert_eq!(BridgedInflationCurve::<RewardCurve, Test>::era_payout(
|
||||
1, total_issuance * 1_000_000_000_000_000_000_000_000 + amount, 0), (0, 0));
|
||||
});
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user