Skip to content

Commit

Permalink
Merge pull request #2 from PureStake/tgm/mb-session-impls
Browse files Browse the repository at this point in the history
move mb-session impl dependencies to impls module
  • Loading branch information
tgmichel authored Mar 19, 2020
2 parents da84e1b + 6394c29 commit ae173f6
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 93 deletions.
89 changes: 2 additions & 87 deletions pallets/mb-session/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
#![cfg_attr(not(feature = "std"), no_std)]

use sp_std::prelude::*;
use codec::{HasCompact, Encode, Decode};
use sp_runtime::{RuntimeDebug,Perbill};
// use sp_runtime::traits::{OpaqueKeys,Convert};
use sp_runtime::traits::{Convert,SaturatedConversion};
use sp_staking::offence::{OffenceDetails};
use sp_runtime::traits::{SaturatedConversion};
use frame_support::{decl_module, decl_storage, decl_event, decl_error, debug};
use frame_support::dispatch::{DispatchResult};
use frame_support::traits::{Currency,Get};
Expand Down Expand Up @@ -403,86 +400,4 @@ impl<T: Trait> pallet_authorship::EventHandler<T::AccountId,u32> for AuthorshipE
fn note_uncle(_author: T::AccountId, _age: u32) {

}
}

// !!!!!!
// All below are trait implemenations that we need to satisfy for the historical feature of the pallet-session
// and by offences. Find a way to remove without having to implement or move outside of the pallet.

/// The amount of exposure (to slashing) than an individual nominator has.
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Decode, RuntimeDebug)]
pub struct IndividualExposure<AccountId, Balance: HasCompact> {
/// The stash account of the nominator in question.
who: AccountId,
/// Amount of funds exposed.
#[codec(compact)]
value: Balance,
}

/// A snapshot of the stake backing a single validator in the system.
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Decode, Default, RuntimeDebug)]
pub struct Exposure<AccountId, Balance: HasCompact> {
/// The total balance backing this validator.
#[codec(compact)]
pub total: Balance,
/// The validator's own stash that is exposed.
#[codec(compact)]
pub own: Balance,
/// The portions of nominators stashes that are exposed.
pub others: Vec<IndividualExposure<AccountId, Balance>>,
}

/// A typed conversion from stash account ID to the current exposure of nominators
/// on that account.
pub struct ExposureOf<T>(sp_std::marker::PhantomData<T>);
impl<T: Trait> Convert<T::AccountId, Option<Exposure<T::AccountId, BalanceOf<T>>>>
for ExposureOf<T>
{
fn convert(_validator: T::AccountId) -> Option<Exposure<T::AccountId, BalanceOf<T>>> {
None
}
}

pub struct Offences<T>(sp_std::marker::PhantomData<T>);
impl <T: Trait> sp_staking::offence::OnOffenceHandler<T::AccountId, pallet_session::historical::IdentificationTuple<T>> for Offences<T> where
T: pallet_session::Trait<ValidatorId = <T as system::Trait>::AccountId>,
T: pallet_session::historical::Trait<
FullIdentification = Exposure<<T as system::Trait>::AccountId, BalanceOf<T>>,
FullIdentificationOf = ExposureOf<T>,
>,
T::SessionHandler: pallet_session::SessionHandler<<T as system::Trait>::AccountId>,
T::SessionManager: pallet_session::SessionManager<<T as system::Trait>::AccountId>,
T::ValidatorIdOf: Convert<<T as system::Trait>::AccountId, Option<<T as system::Trait>::AccountId>>
{
fn on_offence(
_offenders: &[OffenceDetails<T::AccountId, pallet_session::historical::IdentificationTuple<T>>],
_slash_fraction: &[Perbill],
_slash_session: u32,
) {

}
}

// struct SessionHandler<T>(T);
// impl<T: Trait> pallet_session::SessionHandler<T::AccountId> for SessionHandler<T> {

// const KEY_TYPE_IDS: &'static [KeyTypeId] = &[];

// fn on_genesis_session<Ks: OpaqueKeys>(_validators: &[(T::AccountId, Ks)]) {}

// fn on_new_session<Ks: OpaqueKeys>(
// _changed: bool,
// validators: &[(T::AccountId, Ks)],
// _queued_validators: &[(T::AccountId, Ks)],
// ) {
// SessionIndex::mutate(|x| *x + 1);
// let current_session = SessionIndex::get();
// <Module<T>>::deposit_event(
// RawEvent::NewSessionIndex(current_session)
// );
// }

// fn on_disabled(validator_index: usize) {

// }
// }
}
64 changes: 62 additions & 2 deletions runtime/src/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,16 @@
// along with Substrate. If not, see <http://www.gnu.org/licenses/>.

//! Some configurable implementations as associated type for the substrate runtime.
use sp_std::prelude::*;

use sp_runtime::traits::{Convert, Saturating};
use sp_runtime::{Fixed64, Perbill};
use sp_runtime::{Fixed64, Perbill,RuntimeDebug};
use frame_support::{traits::{OnUnbalanced, Currency, Get}, weights::Weight};
use codec::{HasCompact, Encode, Decode};
use sp_staking::offence::{OffenceDetails};

use node_primitives::Balance;
use crate::{Balances, System, Authorship, MaximumBlockWeight, NegativeImbalance};
use crate::{Balances, System, Authorship, MaximumBlockWeight, NegativeImbalance, BalanceOf};

pub struct Author;
impl OnUnbalanced<NegativeImbalance> for Author {
Expand Down Expand Up @@ -111,4 +114,61 @@ impl<T: Get<Perbill>> Convert<Fixed64, Fixed64> for TargetedFeeAdjustment<T> {
.max(Fixed64::from_rational(-1, 1))
}
}
}

// All below are trait implemenations that we need to satisfy for the historical feature of the pallet-session
// and by offences.

/// The amount of exposure (to slashing) than an individual nominator has.
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Decode, RuntimeDebug)]
pub struct IndividualExposure<AccountId, Balance: HasCompact> {
/// The stash account of the nominator in question.
who: AccountId,
/// Amount of funds exposed.
#[codec(compact)]
value: Balance,
}

/// A snapshot of the stake backing a single validator in the system.
#[derive(PartialEq, Eq, PartialOrd, Ord, Clone, Encode, Decode, Default, RuntimeDebug)]
pub struct Exposure<AccountId, Balance: HasCompact> {
/// The total balance backing this validator.
#[codec(compact)]
pub total: Balance,
/// The validator's own stash that is exposed.
#[codec(compact)]
pub own: Balance,
/// The portions of nominators stashes that are exposed.
pub others: Vec<IndividualExposure<AccountId, Balance>>,
}

/// A typed conversion from stash account ID to the current exposure of nominators
/// on that account.
pub struct ExposureOf<T>(sp_std::marker::PhantomData<T>);
impl<T: mb_session::Trait> Convert<T::AccountId, Option<Exposure<T::AccountId, BalanceOf<T>>>>
for ExposureOf<T>
{
fn convert(_validator: T::AccountId) -> Option<Exposure<T::AccountId, BalanceOf<T>>> {
None
}
}

pub struct StakingOffences<T>(sp_std::marker::PhantomData<T>);
impl <T: mb_session::Trait> sp_staking::offence::OnOffenceHandler<T::AccountId, pallet_session::historical::IdentificationTuple<T>> for StakingOffences<T> where
T: pallet_session::Trait<ValidatorId = <T as frame_system::Trait>::AccountId>,
T: pallet_session::historical::Trait<
FullIdentification = Exposure<<T as frame_system::Trait>::AccountId, BalanceOf<T>>,
FullIdentificationOf = ExposureOf<T>,
>,
T::SessionHandler: pallet_session::SessionHandler<<T as frame_system::Trait>::AccountId>,
T::SessionManager: pallet_session::SessionManager<<T as frame_system::Trait>::AccountId>,
T::ValidatorIdOf: Convert<<T as frame_system::Trait>::AccountId, Option<<T as frame_system::Trait>::AccountId>>
{
fn on_offence(
_offenders: &[OffenceDetails<T::AccountId, pallet_session::historical::IdentificationTuple<T>>],
_slash_fraction: &[Perbill],
_slash_session: u32,
) {

}
}
11 changes: 7 additions & 4 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub use mb_session;

/// Implementations of some helper traits passed into runtime modules as associated types.
pub mod impls;
use impls::{Author, LinearWeightToFee, TargetedFeeAdjustment};
use impls::{Author, LinearWeightToFee, TargetedFeeAdjustment, Exposure, ExposureOf, StakingOffences};

impl_opaque_keys! {
pub struct SessionKeys {
Expand Down Expand Up @@ -247,8 +247,8 @@ impl pallet_session::Trait for Runtime {
}

impl pallet_session::historical::Trait for Runtime {
type FullIdentification = mb_session::Exposure<AccountId, Balance>;
type FullIdentificationOf = mb_session::ExposureOf<Runtime>;
type FullIdentification = Exposure<AccountId, Balance>;
type FullIdentificationOf = ExposureOf<Runtime>;
}

parameter_types! {
Expand Down Expand Up @@ -306,7 +306,7 @@ impl pallet_im_online::Trait for Runtime {
impl pallet_offences::Trait for Runtime {
type Event = Event;
type IdentificationTuple = pallet_session::historical::IdentificationTuple<Self>;
type OnOffenceHandler = mb_session::Offences<Runtime>;
type OnOffenceHandler = StakingOffences<Runtime>;
}

impl pallet_authority_discovery::Trait for Runtime {}
Expand Down Expand Up @@ -405,6 +405,9 @@ type SubmitMBTransaction = TransactionSubmitter<
UncheckedExtrinsic
>;

pub type BalanceOf<T> =
<<T as mb_session::Trait>::Currency as Currency<<T as frame_system::Trait>::AccountId>>::Balance;

impl mb_session::Trait for Runtime {
type Currency = Balances;
type SubmitTransaction = SubmitMBTransaction;
Expand Down

0 comments on commit ae173f6

Please sign in to comment.