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

Roles pallet Implementation #286

Merged
merged 12 commits into from
Nov 7, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
2 changes: 1 addition & 1 deletion .github/workflows/checks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ jobs:
- name: Install latest nightly
uses: actions-rs/toolchain@v1
with:
toolchain: nightly-2022-12-26
toolchain: nightly-2023-07-16
override: true
components: rustfmt

Expand Down
24 changes: 24 additions & 0 deletions Cargo.lock

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

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ members = [
"standalone/runtime/evm_tracer",
"pallets/claims",
"pallets/transaction-pause",
"pallets/roles",
"precompiles/utils",
"precompiles/utils/macro",
"precompiles/utils/tests-external",
Expand Down Expand Up @@ -83,6 +84,7 @@ tangle-runtime = { package = "tangle-standalone-runtime", path = "standalone/run

# Tangle Dependencies
pallet-ecdsa-claims = { path = "pallets/claims", default-features = false }
pallet-roles = { path = "pallets/roles", default-features = false }

# Orml dependencies
orml-currencies = { git = "https://github.com/open-web3-stack/open-runtime-module-library.git", branch = "polkadot-v1.0.0", default-features = false }
Expand Down Expand Up @@ -121,6 +123,8 @@ sc-consensus-grandpa = { git = "https://github.com/paritytech/substrate", branch
sc-network-sync = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0" }
sp-arithmetic = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0", default-features = false }
sp-trie = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0", default-features = false }
sp-rpc = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0" }
sp-weights = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0" }

frame-benchmarking = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0", default-features = false }
frame-election-provider-support = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v1.0.0", default-features = false }
Expand Down
54 changes: 54 additions & 0 deletions pallets/roles/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
[package]
name = "pallet-roles"
version = { workspace = true }
authors = { workspace = true }
edition = { workspace = true }
license = { workspace = true }
homepage = { workspace = true }
repository = { workspace = true }

[package.metadata.docs.rs]
targets = ["x86_64-unknown-linux-gnu"]

[dependencies]
parity-scale-codec = { workspace = true, features = ["derive", "max-encoded-len"] }
scale-info = { workspace = true }
serde = { workspace = true }
frame-support = { workspace = true }
frame-system = { workspace = true }
pallet-balances = { workspace = true }
sp-io = { workspace = true }
sp-runtime = { workspace = true }
sp-std = { workspace = true }
tangle-primitives = {workspace = true, default-features = false }
frame-benchmarking = { workspace = true, optional = true }

[dev-dependencies]
hex-literal = { workspace = true }
sp-core = { workspace = true }
sp-runtime = { workspace = true }
serde_json = { workspace = true }

[features]
default = ["std"]
std = [
"serde/std",
"parity-scale-codec/std",
"scale-info/std",
"frame-support/std",
"frame-system/std",
"sp-runtime/std",
"sp-std/std",
"sp-io/std",
"pallet-balances/std",
"tangle-primitives/std"
]

try-runtime = ["frame-support/try-runtime"]
runtime-benchmarks = [
"frame-benchmarking/runtime-benchmarks",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
"sp-runtime/runtime-benchmarks",
"pallet-balances/runtime-benchmarks"
salman01zp marked this conversation as resolved.
Show resolved Hide resolved
]
81 changes: 81 additions & 0 deletions pallets/roles/src/impls.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
use super::*;
use frame_support::{pallet_prelude::DispatchResult, traits::WithdrawReasons};
use sp_runtime::Saturating;
use tangle_primitives::{roles::RoleType, traits::roles::RolesHandler};

/// Implements RolesHandler for the pallet.
impl<T: Config> RolesHandler<T::AccountId> for Pallet<T> {
fn validate_role(address: T::AccountId, role: RoleType) -> bool {
let assigned_role = AccountRolesMapping::<T>::get(address);
match assigned_role {
Some(r) =>
if r == role {
return true
},
None => return false,
}

false
}
fn slash_validator(
address: T::AccountId,
_offence: tangle_primitives::jobs::ValidatorOffence,
) -> sp_runtime::DispatchResult {
// TODO: implement calculation of slash amount.
let slash_amount = 1000u64;
Self::do_slash(address, slash_amount.into())?;
Ok(())
}
}

/// Functions for the pallet.
impl<T: Config> Pallet<T> {
/// The total balance that can be slashed from a stash account as of right now.
pub fn slashable_balance_of(stash: &T::AccountId) -> BalanceOf<T> {
// Weight note: consider making the stake accessible through stash.
Self::ledger(&stash).map(|l| l.total_locked).unwrap_or_default()
}

/// Slash staker's balance by the given amount.
pub(crate) fn do_slash(
address: T::AccountId,
slash_amount: T::CurrencyBalance,
) -> sp_runtime::DispatchResult {
let mut ledger = Self::ledger(&address).ok_or(Error::<T>::InvalidStashController)?;
let (_imbalance, _missing) = T::Currency::slash(&address, slash_amount.into());
ledger.total_locked = ledger.total_locked.saturating_sub(slash_amount.into());
Self::update_ledger(&address, &ledger);
Self::deposit_event(Event::Slashed { account: address, amount: slash_amount });
Ok(())
}

/// Update the ledger for the staker.
///
/// This will also update the stash lock.
pub(crate) fn update_ledger(staker: &T::AccountId, ledger: &RoleStakingLedger<T>) {
T::Currency::set_lock(
ROLES_STAKING_ID,
&ledger.stash,
ledger.total_locked,
WithdrawReasons::all(),
);
<Ledger<T>>::insert(staker, ledger);
}
/// Clear stash account information from pallet.
pub(crate) fn kill_stash(stash: &T::AccountId) -> DispatchResult {
<Ledger<T>>::remove(&stash);
Ok(())
}

/// Unbond the full amount of the stash.
pub(super) fn unbond(ledger: &RoleStakingLedger<T>) -> DispatchResult {
let stash = ledger.stash.clone();
if ledger.total_locked > T::Currency::minimum_balance() {
// Remove the lock.
T::Currency::remove_lock(ROLES_STAKING_ID, &stash);
// Kill the stash and related information
Self::kill_stash(&stash)?;
}
Ok(())
}
}
Loading
Loading