Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move emissions tests to its pallet #601

Open
wants to merge 7 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

251 changes: 0 additions & 251 deletions substrate/client/tests/emissions.rs

This file was deleted.

31 changes: 30 additions & 1 deletion substrate/emissions/pallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,18 @@ serai-primitives = { path = "../../primitives", default-features = false }
validator-sets-primitives = { package = "serai-validator-sets-primitives", path = "../../validator-sets/primitives", default-features = false }
emissions-primitives = { package = "serai-emissions-primitives", path = "../primitives", default-features = false }

[dev-dependencies]
pallet-babe = { git = "https://github.com/serai-dex/substrate", default-features = false }
pallet-grandpa = { git = "https://github.com/serai-dex/substrate", default-features = false }
pallet-timestamp = { git = "https://github.com/serai-dex/substrate", default-features = false }

sp-core = { git = "https://github.com/serai-dex/substrate", default-features = false }
sp-io = { git = "https://github.com/serai-dex/substrate", default-features = false }

serai-abi = { path = "../../abi", default-features = false, features = ["serde"] }

rand_core = "0.6"

[features]
std = [
"scale/std",
Expand All @@ -49,6 +61,11 @@ std = [

"sp-std/std",
"sp-runtime/std",
"sp-core/std",
"sp-io/std",

"serai-abi/std",
"serai-abi/serde",

"coins-pallet/std",
"validator-sets-pallet/std",
Expand All @@ -59,7 +76,19 @@ std = [

"serai-primitives/std",
"emissions-primitives/std",

"pallet-babe/std",
"pallet-grandpa/std",
"pallet-timestamp/std",
]

try-runtime = [
"frame-system/try-runtime",
"frame-support/try-runtime",

"sp-runtime/try-runtime",
]

fast-epoch = []
try-runtime = [] # TODO

default = ["std"]
38 changes: 21 additions & 17 deletions substrate/emissions/pallet/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
#![cfg_attr(not(feature = "std"), no_std)]

#[cfg(test)]
mod tests;

#[cfg(test)]
mod mock;

#[allow(
unreachable_patterns,
clippy::cast_possible_truncation,
Expand Down Expand Up @@ -308,15 +314,9 @@ pub mod pallet {
}

fn initial_period(n: BlockNumberFor<T>) -> bool {
#[cfg(feature = "fast-epoch")]
let initial_period_duration = FAST_EPOCH_INITIAL_PERIOD;

#[cfg(not(feature = "fast-epoch"))]
let initial_period_duration = 2 * MONTHS;

let genesis_complete_block = GenesisLiquidity::<T>::genesis_complete_block();
genesis_complete_block.is_some() &&
(n.saturated_into::<u64>() < (genesis_complete_block.unwrap() + initial_period_duration))
(n.saturated_into::<u64>() < (genesis_complete_block.unwrap() + (2 * MONTHS)))
}

/// Returns true if any of the external networks haven't reached economic security yet.
Expand Down Expand Up @@ -344,17 +344,21 @@ pub mod pallet {
}

// stake the rewards
for (p, score) in scores {
let p_reward = u64::try_from(
u128::from(reward).saturating_mul(u128::from(score)) / u128::from(total_score),
)
.unwrap();
let mut total_reward_distributed = 0u64;
for (i, (p, score)) in scores.iter().enumerate() {
let p_reward = if i == (scores.len() - 1) {
reward.saturating_sub(total_reward_distributed)
} else {
u64::try_from(
u128::from(reward).saturating_mul(u128::from(*score)) / u128::from(total_score),
)
.unwrap()
};

Coins::<T>::mint(p, Balance { coin: Coin::Serai, amount: Amount(p_reward) }).unwrap();
if ValidatorSets::<T>::distribute_block_rewards(n, p, Amount(p_reward)).is_err() {
// TODO: log the failure
continue;
}
Coins::<T>::mint(*p, Balance { coin: Coin::Serai, amount: Amount(p_reward) }).unwrap();
ValidatorSets::<T>::distribute_block_rewards(n, *p, Amount(p_reward)).unwrap();

total_reward_distributed = total_reward_distributed.saturating_add(p_reward);
}
}

Expand Down
Loading
Loading