Skip to content

Commit

Permalink
fix some pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
akildemir committed Oct 4, 2023
1 parent baaf96f commit 986495c
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 101 deletions.
2 changes: 1 addition & 1 deletion substrate/client/tests/validator_sets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ serai_test!(
.get_new_set_events(serai.get_block_by_number(0).await.unwrap().unwrap().hash())
.await
.unwrap(),
[NetworkId::Bitcoin, NetworkId::Ethereum, NetworkId::Monero, NetworkId::Serai]
[NetworkId::Serai, NetworkId::Bitcoin, NetworkId::Ethereum, NetworkId::Monero]
.iter()
.copied()
.map(|network| ValidatorSetsEvent::NewSet {
Expand Down
2 changes: 1 addition & 1 deletion substrate/node/src/chain_spec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,10 @@ fn testnet_genesis(
validator_sets: ValidatorSetsConfig {
bond: Amount(1_000_000 * 10_u64.pow(8)),
networks: vec![
(NetworkId::Serai, NETWORKS[&NetworkId::Serai].clone()),
(NetworkId::Bitcoin, NETWORKS[&NetworkId::Bitcoin].clone()),
(NetworkId::Ethereum, NETWORKS[&NetworkId::Ethereum].clone()),
(NetworkId::Monero, NETWORKS[&NetworkId::Monero].clone()),
(NetworkId::Serai, NETWORKS[&NetworkId::Serai].clone()),
],
participants: validators.iter().map(|name| account_from_name(name)).collect(),
},
Expand Down
2 changes: 1 addition & 1 deletion substrate/primitives/src/networks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,9 +124,9 @@ impl Network {
#[cfg(feature = "std")]
lazy_static::lazy_static! {
pub static ref NETWORKS: HashMap<NetworkId, Network> = HashMap::from([
(NetworkId::Serai, Network::new(vec![Coin::Serai]).unwrap()),
(NetworkId::Bitcoin, Network::new(vec![Coin::Bitcoin]).unwrap()),
(NetworkId::Ethereum, Network::new(vec![Coin::Ether, Coin::Dai]).unwrap()),
(NetworkId::Monero, Network::new(vec![Coin::Monero]).unwrap()),
(NetworkId::Serai, Network::new(vec![Coin::Serai]).unwrap()),
]);
}
2 changes: 1 addition & 1 deletion substrate/runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ impl Contains<RuntimeCall> for CallFilter {
}

if let RuntimeCall::Session(call) = call {
return matches!(call, session::Call::set_keys { .. } | session::Call::purge_keys {});
return matches!(call, session::Call::set_keys { .. });
}

false
Expand Down
2 changes: 1 addition & 1 deletion substrate/staking/pallet/LICENSE
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
AGPL-3.0-only license

Copyright (c) 2022 Luke Parker
Copyright (c) 2022-2023 Luke Parker

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License Version 3 as
Expand Down
52 changes: 13 additions & 39 deletions substrate/staking/pallet/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,14 @@ pub mod pallet {

use serai_primitives::{NetworkId, Amount, PublicKey};
use serai_validator_sets_primitives::{ValidatorSet, Session};
use staking_primitives::AllocatedStaking;

use validator_sets_pallet::{Config as VsConfig, Pallet as VsPallet};
use pallet_session::{Config as SessionConfig, SessionManager, Pallet as SessionPallet};

#[pallet::error]
pub enum Error<T> {
BondUnavailable,
InSufficientAllocation,
InsufficientAllocation,
}

// TODO: Event
Expand Down Expand Up @@ -86,7 +85,7 @@ pub mod pallet {
fn deallocate_internal(account: &T::AccountId, amount: u64) -> Result<(), Error<T>> {
Allocated::<T>::try_mutate(account, |allocated| {
if *allocated < amount {
Err(Error::<T>::InSufficientAllocation)?;
Err(Error::<T>::InsufficientAllocation)?;
}
*allocated -= amount;
Ok(())
Expand All @@ -109,8 +108,7 @@ pub mod pallet {
Ok(())
}

/// Unstake funds from this account. Only unallocated funds may be
/// unstaked.
/// Unstake funds from this account. Only unallocated funds may be unstaked.
#[pallet::call_index(1)]
#[pallet::weight((0, DispatchClass::Operational))] // TODO
pub fn unstake(origin: OriginFor<T>, #[pallet::compact] amount: u64) -> DispatchResult {
Expand All @@ -135,17 +133,11 @@ pub mod pallet {
// add to amount bonded
Self::allocate_internal(&account, amount)?;

// add to participants list for the network
let result = VsPallet::<T>::add_participant(account, Amount(amount), network);
if result.is_err() {
Self::deallocate_internal(&account, amount).unwrap();
return result;
}

Ok(())
// increase allocation for participant or add to participants list if new.
VsPallet::<T>::increase_allocation(account, Amount(amount), network)
}

/// Allocate `amount` to a given validator set.
/// Deallocate `amount` from a given validator set.
#[pallet::call_index(3)]
#[pallet::weight((0, DispatchClass::Operational))] // TODO
pub fn deallocate(
Expand All @@ -155,27 +147,20 @@ pub mod pallet {
) -> DispatchResult {
let account = ensure_signed(origin)?;

// remove the participant if necessary.
// decrease allocation and remove the participant if necessary.
// we can't directly deallocate here, since the leaving validator
// will be removed after the next session. We only deallocate then
// on `end_session` for the right index.
VsPallet::<T>::maybe_remove_participant(account, Amount(amount), network)
VsPallet::<T>::decrease_allocation(account, Amount(amount), network)
}
}

/// Call order is end_session(i - 1) -> start_session(i) -> new_session(i + 1)
/// new_session(i + 1) is called immediately after start_session(i) returns then
/// we wait until the session ends then get a call to end_session(i) and so on.
// Call order is end_session(i - 1) -> start_session(i) -> new_session(i + 1)
// new_session(i + 1) is called immediately after start_session(i) returns,
// then we wait until the session ends then get a call to end_session(i) and so on.
impl<T: Config> SessionManager<T::ValidatorId> for Pallet<T> {
fn new_session(new_index: u32) -> Option<Vec<T::ValidatorId>> {
let next_validators = VsPallet::<T>::next_validator_set(new_index, NetworkId::Serai);

// Returning None will keep the previous set going.
if next_validators.is_empty() {
return None;
}

Some(next_validators)
Some(VsPallet::<T>::next_validator_set(new_index, NetworkId::Serai))
}

fn new_session_genesis(_: u32) -> Option<Vec<T::ValidatorId>> {
Expand All @@ -192,7 +177,7 @@ pub mod pallet {
let deallocating_validators = VsPallet::<T>::deallocating_validators(key);
for (account, amount, _) in deallocating_validators {
// we can unwrap because we are not deallocating more than allocated.
<Self as AllocatedStaking<T>>::deallocate(&account, amount.0).unwrap();
Self::deallocate_internal(&account, amount.0).unwrap();
}

VsPallet::<T>::end_session(end_index, NetworkId::Serai);
Expand All @@ -203,17 +188,6 @@ pub mod pallet {
VsPallet::<T>::start_session(start_index, NetworkId::Serai, validators)
}
}

impl<T: Config> AllocatedStaking<T> for Pallet<T> {
type Error = Error<T>;

fn allocate(account: &T::AccountId, amount: u64) -> Result<(), Error<T>> {
Self::allocate_internal(account, amount)
}
fn deallocate(account: &T::AccountId, amount: u64) -> Result<(), Error<T>> {
Self::deallocate_internal(account, amount)
}
}
}

pub use pallet::*;
2 changes: 1 addition & 1 deletion substrate/validator-sets/pallet/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ runtime-benchmarks = [
"frame-system/runtime-benchmarks",
"frame-support/runtime-benchmarks",

"pallet-babe/runtime-benchmarks",
"pallet-babe/runtime-benchmarks",
]

default = ["std"]
Loading

0 comments on commit 986495c

Please sign in to comment.