Skip to content
This repository has been archived by the owner on Jul 7, 2021. It is now read-only.

pallet-simple-staking #28

Merged
merged 60 commits into from
Apr 19, 2021
Merged
Show file tree
Hide file tree
Changes from 5 commits
Commits
Show all changes
60 commits
Select commit Hold shift + click to select a range
0701fb0
outline
JesseAbram Apr 6, 2021
7332288
added try mutate
JesseAbram Apr 6, 2021
fbfa722
add reserve and unreserve
JesseAbram Apr 6, 2021
6b7706b
tests
JesseAbram Apr 7, 2021
3fe9d0c
fix no op issue
JesseAbram Apr 7, 2021
c4bd203
Merge branch 'master' of github.com:paritytech/statemint into jesse-s…
JesseAbram Apr 7, 2021
f98bebc
fix build
JesseAbram Apr 7, 2021
c2274cf
Update pallets/simple-staking/src/benchmarking.rs
JesseAbram Apr 8, 2021
c9cbe28
Update pallets/simple-staking/src/benchmarking.rs
JesseAbram Apr 8, 2021
c2b0414
Update pallets/simple-staking/src/lib.rs
JesseAbram Apr 8, 2021
274330c
Update pallets/simple-staking/src/lib.rs
JesseAbram Apr 8, 2021
3d0bfc8
Update pallets/simple-staking/src/lib.rs
JesseAbram Apr 8, 2021
fa6e23e
Update pallets/simple-staking/src/lib.rs
JesseAbram Apr 8, 2021
7cb60f8
Update pallets/simple-staking/src/lib.rs
JesseAbram Apr 8, 2021
9d8e37c
Update pallets/simple-staking/src/lib.rs
JesseAbram Apr 8, 2021
e474f07
Update pallets/simple-staking/src/lib.rs
JesseAbram Apr 8, 2021
00258ad
Update pallets/simple-staking/src/mock.rs
JesseAbram Apr 8, 2021
c6d3af6
build fix
JesseAbram Apr 8, 2021
49d16b3
fix tests
JesseAbram Apr 8, 2021
c2dcfc8
tweaks
JesseAbram Apr 8, 2021
961335e
Fix test build
kianenigma Apr 9, 2021
029e3ae
added updater origin
JesseAbram Apr 12, 2021
dd30264
Merge branch 'master' of github.com:paritytech/statemint into jesse-s…
JesseAbram Apr 13, 2021
acf9416
on initalize for fee split
JesseAbram Apr 13, 2021
f92d157
mock
JesseAbram Apr 13, 2021
4fd533c
on init test
JesseAbram Apr 13, 2021
c48f90e
added genesis config for invulnerables
JesseAbram Apr 14, 2021
a6e3bc8
benchmarks
JesseAbram Apr 14, 2021
30255bf
kian and jesse
JesseAbram Apr 14, 2021
a43b75b
Make everything build
kianenigma Apr 14, 2021
fbaa653
Merge branch 'master' of github.com:paritytech/statemint into jesse-s…
JesseAbram Apr 15, 2021
fae7a57
add invulnerables to chain spec
JesseAbram Apr 15, 2021
3b2a6e3
Update pallets/simple-staking/src/lib.rs
apopiak Apr 15, 2021
a523737
configs and runtime, transaction payment
JesseAbram Apr 15, 2021
bbaf6f6
changes on joe's recommendation
JesseAbram Apr 15, 2021
13717ab
Update pallets/simple-staking/src/benchmarking.rs
JesseAbram Apr 16, 2021
de8e540
Merge branch 'master' of github.com:paritytech/statemint into jesse-s…
JesseAbram Apr 16, 2021
e9ffb98
weights (not done)
JesseAbram Apr 16, 2021
4e15709
added wight attributes
JesseAbram Apr 16, 2021
4f0fff0
test for impl fees
JesseAbram Apr 19, 2021
425393e
Update node/Cargo.toml
JesseAbram Apr 19, 2021
3e5bce1
Update pallets/simple-staking/src/benchmarking.rs
JesseAbram Apr 19, 2021
942caf5
Update pallets/simple-staking/src/benchmarking.rs
JesseAbram Apr 19, 2021
b390033
Update pallets/simple-staking/src/lib.rs
JesseAbram Apr 19, 2021
d9cd21e
Update pallets/simple-staking/src/benchmarking.rs
JesseAbram Apr 19, 2021
8be8865
Update pallets/simple-staking/src/benchmarking.rs
JesseAbram Apr 19, 2021
3d0a7b2
Update pallets/simple-staking/src/benchmarking.rs
JesseAbram Apr 19, 2021
f8a9669
Update pallets/simple-staking/src/lib.rs
JesseAbram Apr 19, 2021
07c8383
Update pallets/simple-staking/src/lib.rs
JesseAbram Apr 19, 2021
0ec019b
Update pallets/simple-staking/src/lib.rs
JesseAbram Apr 19, 2021
dff932f
Update pallets/simple-staking/src/mock.rs
JesseAbram Apr 19, 2021
8da9ff6
Update pallets/simple-staking/src/tests.rs
JesseAbram Apr 19, 2021
bcde167
Update pallets/simple-staking/src/tests.rs
JesseAbram Apr 19, 2021
0f5cda5
Update pallets/simple-staking/src/tests.rs
JesseAbram Apr 19, 2021
f391e88
Update pallets/simple-staking/src/tests.rs
JesseAbram Apr 19, 2021
96b0606
Update pallets/simple-staking/src/tests.rs
JesseAbram Apr 19, 2021
8b13bb3
format and potId
JesseAbram Apr 19, 2021
37fc3a9
license
JesseAbram Apr 19, 2021
b57f2ac
rename
JesseAbram Apr 19, 2021
739520e
move weight impl to own file
JesseAbram Apr 19, 2021
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
33 changes: 17 additions & 16 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ panic = 'unwind'
[workspace]
members = [
'node',
'pallets/template',
'pallets/simple-staking',
'runtime/statemint',
'runtime/statemine',
]
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ description = 'FRAME pallet template for defining custom runtime logic.'
edition = '2018'
homepage = 'https://substrate.dev'
license = 'Unlicense'
name = 'pallet-template'
name = 'pallet-simple-staking'
readme = 'README.md'
repository = 'https://github.com/paritytech/substrate/'
version = '3.0.0'
Expand All @@ -20,6 +20,7 @@ std = [
'frame-support/std',
'frame-system/std',
'frame-benchmarking/std',
'sp-runtime/std',
]

[dependencies.codec]
Expand All @@ -28,6 +29,12 @@ features = ['derive']
package = 'parity-scale-codec'
version = '2.0.0'

[dev-dependencies.pallet-balances]
default-features = false
git = 'https://github.com/paritytech/substrate.git'
branch = 'rococo-v1'
version = '3.0.0'

[dependencies.frame-benchmarking]
default-features = false
optional = true
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
use super::*;

#[allow(unused)]
use crate::Module as Template;
use crate::Module as simple_staking;
JesseAbram marked this conversation as resolved.
Show resolved Hide resolved
use frame_benchmarking::{benchmarks, impl_benchmark_test_suite, whitelisted_caller};
use frame_system::RawOrigin;

Expand All @@ -17,4 +17,4 @@ benchmarks! {
}
}

impl_benchmark_test_suite!(Template, crate::mock::new_test_ext(), crate::mock::Test,);
impl_benchmark_test_suite!(simple_staking, crate::mock::new_test_ext(), crate::mock::Test,);
JesseAbram marked this conversation as resolved.
Show resolved Hide resolved
161 changes: 161 additions & 0 deletions pallets/simple-staking/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
#![cfg_attr(not(feature = "std"), no_std)]

/// Edit this file to define custom logic or remove it if it is not needed.
/// Learn more about FRAME and the core library of Substrate FRAME pallets:
/// <https://substrate.dev/docs/en/knowledgebase/runtime/frame>
JesseAbram marked this conversation as resolved.
Show resolved Hide resolved
pub use pallet::*;

#[cfg(test)]
mod mock;

#[cfg(test)]
mod tests;

#[cfg(feature = "runtime-benchmarks")]
JesseAbram marked this conversation as resolved.
Show resolved Hide resolved
mod benchmarking;

#[frame_support::pallet]
pub mod pallet {
use frame_support::{dispatch::DispatchResultWithPostInfo, pallet_prelude::*, traits::{Currency, ReservableCurrency, Vec}};
use frame_system::{pallet_prelude::*, ensure_root};
use frame_system::Config as SystemConfig;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is not substrate-idomatic IMO, but not a big deal.

use frame_support::sp_runtime::{RuntimeDebug};
/// Configure the pallet by specifying the parameters and types on which it depends.
#[pallet::config]
pub trait Config: frame_system::Config {
/// Because this pallet emits events, it depends on the runtime's definition of an event.
type Event: From<Event<Self>> + IsType<<Self as frame_system::Config>::Event>;
/// The currency mechanism.
type Currency: ReservableCurrency<Self::AccountId>;
}
type BalanceOf<T> = <<T as Config>::Currency as Currency<<T as SystemConfig>::AccountId>>::Balance;


#[derive(PartialEq, Eq, Clone, Encode, Decode, RuntimeDebug)]
pub struct AuthorInfo<AccountId, Balance, BlockNumber> {
pub who: AccountId,
pub deposit: Balance,
pub last_block: BlockNumber
}
JesseAbram marked this conversation as resolved.
Show resolved Hide resolved

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
pub struct Pallet<T>(_);

// The pallet's runtime storage items.
// https://substrate.dev/docs/en/knowledgebase/runtime/storage

#[pallet::storage]
#[pallet::getter(fn invulnerables)]
pub type Invulnerables<T: Config>= StorageValue<_, Vec<T::AccountId>>;

#[pallet::storage]
#[pallet::getter(fn authors)]
pub type Authors<T: Config> = StorageValue<_, Vec<AuthorInfo<T::AccountId, BalanceOf<T>, T::BlockNumber>>, ValueQuery>;

#[pallet::storage]
#[pallet::getter(fn max_authors)]
pub type MaxAuthors<T> = StorageValue<_, u32>;

#[pallet::storage]
#[pallet::getter(fn authority_bond)]
pub type AuthorityBond<T: Config>= StorageValue<_, BalanceOf<T>>;


#[pallet::event]
#[pallet::metadata(T::AccountId = "AccountId")]
JesseAbram marked this conversation as resolved.
Show resolved Hide resolved
#[pallet::generate_deposit(pub(super) fn deposit_event)]
pub enum Event<T: Config> {
/// Event documentation should end with an array that provides descriptive names for event
/// parameters. [something, who]
NewInvulnerables(Vec<T::AccountId>),
NewMaxAuthorCount(u32),
AuthorAdded(T::AccountId, BalanceOf<T>),
AuthorRemoved(T::AccountId)
}

// Errors inform users that something went wrong.
#[pallet::error]
pub enum Error<T> {
MaxAuthors,
Unknown,
Permission,
AlreadyAuthor,
NotAuthor
}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
//TODO on init (or finalize) add to aura set at next era
//TODO split half the pot to the author per block
}

// Dispatchable functions allows users to interact with the pallet and invoke state changes.
// These functions materialize as "extrinsics", which are often compared to transactions.
// Dispatchable functions must be annotated with a weight and must return a DispatchResult.
#[pallet::call]
impl<T: Config> Pallet<T> {

JesseAbram marked this conversation as resolved.
Show resolved Hide resolved

#[pallet::weight(10_000)]
pub fn set_invulnerables(origin: OriginFor<T>, new: Vec<T::AccountId>) -> DispatchResultWithPostInfo {
//TODO chose protective scheme requires message from relay chain
ensure_root(origin)?;
<Invulnerables<T>>::put(&new);
Self::deposit_event(Event::NewInvulnerables(new));
Ok(().into())
}

#[pallet::weight(10_000)]
pub fn set_max_author_count(origin: OriginFor<T>, max_authors: u32) -> DispatchResultWithPostInfo {
//TODO chose protective scheme requires message from relay chain
ensure_root(origin)?;
<MaxAuthors<T>>::put(&max_authors);
Self::deposit_event(Event::NewMaxAuthorCount(max_authors));
Ok(().into())

}

#[pallet::weight(10_000)]
pub fn author_intent(origin: OriginFor<T>, deposit: BalanceOf<T>) -> DispatchResultWithPostInfo {
// lock deposit to start or require min?
kianenigma marked this conversation as resolved.
Show resolved Hide resolved
let who = ensure_signed(origin)?;
let length = match <Authors<T>>::decode_len() {
Some(len) => len,
None => 0
};
JesseAbram marked this conversation as resolved.
Show resolved Hide resolved
ensure!(length + 1 <= MaxAuthors::<T>::get().ok_or(Error::<T>::MaxAuthors)? as usize, Error::<T>::MaxAuthors);
JesseAbram marked this conversation as resolved.
Show resolved Hide resolved
let new_author = AuthorInfo {
who: who.clone(),
deposit,
last_block: frame_system::Pallet::<T>::block_number()
};
<Authors<T>>::try_mutate(|authors| -> DispatchResult {
let exists = authors.into_iter().any(|author| author.who == who);
match exists {
false => {
T::Currency::reserve(&who, deposit)?;
Ok(authors.push(new_author))
},
true => Err(Error::<T>::AlreadyAuthor)?,
}
})?;
Self::deposit_event(Event::AuthorAdded(who, deposit));
Ok(().into())
}

#[pallet::weight(10_000)]
pub fn leave_intent(origin: OriginFor<T>) -> DispatchResultWithPostInfo {
let who = ensure_signed(origin)?;
<Authors<T>>::try_mutate(|authors| -> DispatchResult {
let index = authors.iter().position(|author| author.who == who).ok_or(Error::<T>::NotAuthor)?;
T::Currency::unreserve(&who, authors[index].deposit);
authors.remove(index);
Ok(())
})?;
Self::deposit_event(Event::AuthorRemoved(who));
Ok(().into())

}
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate as pallet_template;
use super::*;
JesseAbram marked this conversation as resolved.
Show resolved Hide resolved
use crate as simple_staking;
use sp_core::H256;
use frame_support::parameter_types;
use sp_runtime::{
Expand All @@ -17,7 +18,8 @@ frame_support::construct_runtime!(
UncheckedExtrinsic = UncheckedExtrinsic,
{
System: frame_system::{Pallet, Call, Config, Storage, Event<T>},
TemplateModule: pallet_template::{Pallet, Call, Storage, Event<T>},
Balances: pallet_balances::{Pallet, Call, Storage, Config<T>, Event<T>},
SimpleStaking: simple_staking::{Pallet, Call, Storage, Event<T>},
}
);

Expand All @@ -44,18 +46,43 @@ impl system::Config for Test {
type BlockHashCount = BlockHashCount;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = ();
type AccountData = pallet_balances::AccountData<u64>;
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
type SS58Prefix = SS58Prefix;
}

impl pallet_template::Config for Test {
parameter_types! {
pub const ExistentialDeposit: u64 = 1;
}

impl pallet_balances::Config for Test {
type Balance = u64;
type Event = Event;
type DustRemoval = ();
type ExistentialDeposit = ExistentialDeposit;
type AccountStore = System;
type WeightInfo = ();
type MaxLocks = ();
}

// Build genesis storage according to the mock runtime.
pub fn new_test_ext() -> sp_io::TestExternalities {
system::GenesisConfig::default().build_storage::<Test>().unwrap().into()
impl Config for Test {
type Event = Event;
type Currency = Balances;
}

const A: u64 = 1;
const B: u64 = 2;

pub fn new_test_ext() -> sp_io::TestExternalities {
let mut t = frame_system::GenesisConfig::default().build_storage::<Test>().unwrap();
let genesis = pallet_balances::GenesisConfig::<Test> {
balances: vec![
(A, 100),
(B, 200),
],
};
genesis.assimilate_storage(&mut t).unwrap();
t.into()
}
JesseAbram marked this conversation as resolved.
Show resolved Hide resolved
Loading