Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 6 commits
Commits
Show all changes
85 commits
Select commit Hold shift + click to select a range
e89dff9
First steps to stash/controller separation
gavofyork Feb 7, 2019
8e9c2d5
Merge remote-tracking branch 'origin/master' into gav-new-staking
gavofyork Feb 13, 2019
29d1b20
More drafting
gavofyork Feb 14, 2019
ac5d897
More drafting
gavofyork Feb 14, 2019
bfc089f
Finish draft.
gavofyork Feb 14, 2019
fe19f73
Optimisation
gavofyork Feb 14, 2019
24cedfb
Remove accidental commit
gavofyork Feb 14, 2019
0876c86
Make it build.
gavofyork Feb 15, 2019
c9f643f
Fix linked map for traits.
tomusdrw Feb 15, 2019
9190f7d
Fix Option<_> variant.
tomusdrw Feb 15, 2019
4b5274e
Merge branch 'master' into td-fixlinked
tomusdrw Feb 15, 2019
5354a2e
Improve naming a tad
tomusdrw Feb 15, 2019
00ba07e
Rebuild runtime
tomusdrw Feb 15, 2019
78bcf1a
Merge remote-tracking branch 'origin/master' into gav-new-staking2
gavofyork Feb 15, 2019
5ec3741
Merge remote-tracking branch 'origin/td-fixlinked' into gav-new-staking2
gavofyork Feb 15, 2019
ca18c79
Builds!
gavofyork Feb 15, 2019
a24dd8a
First test.
gavofyork Feb 15, 2019
15a3100
Bump RT version
gavofyork Feb 15, 2019
bd0a3de
Minor fix
gavofyork Feb 15, 2019
68f5405
Update Mock
shawntabrizi Feb 19, 2019
001a665
adds the correct reward testcase (+staking eras which was already ok)
kianenigma Feb 19, 2019
7042842
fixes the basic staking testcase to work properly (along with a small…
kianenigma Feb 20, 2019
75bf9d7
New logic to avoid controller transferring stash.
gavofyork Feb 20, 2019
f4ba1d6
Fix some build issues.
gavofyork Feb 20, 2019
a008ee1
Merge branch 'gav-new-staking' of github.com:paritytech/substrate int…
gavofyork Feb 20, 2019
27ad50e
adding some comments to tests
shawntabrizi Feb 20, 2019
93d0eb0
Merge branch 'gav-new-staking' of https://github.com/paritytech/subst…
shawntabrizi Feb 20, 2019
9f7ba25
Merge remote-tracking branch 'origin/master' into gav-new-staking
gavofyork Feb 20, 2019
b755466
Merge branch 'gav-new-staking' of github.com:paritytech/substrate int…
gavofyork Feb 20, 2019
50a212c
Fix impls.
gavofyork Feb 20, 2019
1eb43f7
adds a few more lines to explain the test case
kianenigma Feb 20, 2019
6eaddd1
Merge branch 'gav-new-staking' of github.com:paritytech/substrate int…
kianenigma Feb 20, 2019
e8661b2
More fixes.
gavofyork Feb 20, 2019
7284530
Merge branch 'gav-new-staking' of github.com:paritytech/substrate int…
kianenigma Feb 20, 2019
7e9d469
gets the basic test up and running again
kianenigma Feb 20, 2019
31bed92
Fix rest of build
gavofyork Feb 20, 2019
cadce5e
Merge branch 'gav-new-staking' of github.com:paritytech/substrate int…
gavofyork Feb 20, 2019
37adc08
Rebuild wasm
gavofyork Feb 20, 2019
039f4ea
Fix docs.
gavofyork Feb 20, 2019
4e67748
fix staking test with new chnages
kianenigma Feb 21, 2019
7fe7e01
updating some tests, pending questions
shawntabrizi Feb 21, 2019
a911ee9
Merge branch 'gav-new-staking' of https://github.com/paritytech/subst…
shawntabrizi Feb 21, 2019
514d5a6
More working tests
shawntabrizi Feb 21, 2019
1808869
adds double staking test
kianenigma Feb 21, 2019
2c4f738
Docs
gavofyork Feb 21, 2019
a163f87
Merge branch 'gav-new-staking' of github.com:paritytech/substrate int…
gavofyork Feb 21, 2019
1296562
Merge remote-tracking branch 'origin/master' into gav-new-staking
gavofyork Feb 22, 2019
200eea1
remove invalid slashing test
kianenigma Feb 23, 2019
15b9da0
Payee stuff.
gavofyork Feb 25, 2019
634ab7b
Merge branch 'gav-new-staking' of github.com:paritytech/substrate int…
gavofyork Feb 25, 2019
94a46d4
Fix build
gavofyork Feb 25, 2019
1a2ec9e
Docs
gavofyork Feb 25, 2019
dd7fba6
Fix test
gavofyork Feb 25, 2019
4b0e1e6
Fix a couple of tests
gavofyork Feb 25, 2019
61224f6
Layout plan for finishing tests before Pragmen
shawntabrizi Feb 25, 2019
e4ea05f
Merge branch 'gav-new-staking' of https://github.com/paritytech/subst…
shawntabrizi Feb 25, 2019
3b9917a
Add some working tests
shawntabrizi Feb 25, 2019
6f8f93d
re-build staking and reward tests
kianenigma Feb 25, 2019
4029139
Merge branch 'gav-new-staking' of github.com:paritytech/substrate int…
kianenigma Feb 25, 2019
cc8f195
Add more tests
shawntabrizi Feb 25, 2019
b0129a0
Merge branch 'gav-new-staking' of https://github.com/paritytech/subst…
shawntabrizi Feb 25, 2019
92c0621
fix offline grace test
shawntabrizi Feb 26, 2019
16a9239
Nominator should have payee checked for cleanup
shawntabrizi Feb 26, 2019
1064cfc
adds more nomination tets
kianenigma Feb 26, 2019
6865917
merged
kianenigma Feb 26, 2019
937c89c
adds validator prefs tests
kianenigma Feb 26, 2019
f606db2
Fix and clean up some TODOs
shawntabrizi Feb 27, 2019
6bf11f9
Fix a couple of issues
gavofyork Feb 27, 2019
9a02201
Fix tests
shawntabrizi Feb 27, 2019
be9f4f6
Merge branch 'gav-new-staking' of github.com:paritytech/substrate int…
kianenigma Feb 27, 2019
4ddaa42
noting warnings from tests
shawntabrizi Feb 28, 2019
54ee55b
final fix of local tests
kianenigma Feb 28, 2019
a53c3fd
Merge branch 'gav-new-staking' of https://github.com/paritytech/subst…
shawntabrizi Feb 28, 2019
93da8d8
Fix slot_stake bug
shawntabrizi Feb 28, 2019
4603cfe
Half baked test
shawntabrizi Feb 28, 2019
8bb2e43
fix a few gobal tests
kianenigma Feb 28, 2019
2f43287
Merge branch 'gav-new-staking' of github.com:paritytech/substrate int…
kianenigma Feb 28, 2019
a4ca045
Add logic to limit `unstake_threshold` set in storage
shawntabrizi Feb 28, 2019
ee54da0
Make sure to check before writing!
shawntabrizi Feb 28, 2019
51d25ed
Move a couple of comments
gavofyork Mar 1, 2019
231a02d
devops-parity updated wasm runtime blobs b58d9517 and merged in maste…
devops-parity Mar 1, 2019
68b5938
fix last broken slot_stake test
kianenigma Mar 1, 2019
3a74b73
Merge branch 'gav-new-staking' of github.com:paritytech/substrate int…
kianenigma Mar 1, 2019
ae1e1e5
Merge remote-tracking branch 'origin/master' into gav-new-staking
gavofyork Mar 1, 2019
da9cb52
Ignore broken test
gavofyork Mar 2, 2019
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
Binary file not shown.
Binary file not shown.
266 changes: 138 additions & 128 deletions srml/support/procedural/src/storage/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,11 +193,10 @@ impl<'a> Impls<'a> {
let head_key = format!("head of {}", prefix);
let prefix = format!("{}", prefix);
let name_lowercase = name.to_string().to_lowercase();
let key_for = syn::Ident::new(&format!("key_for_{}", name_lowercase), name.span());
let internal_module = syn::Ident::new(&format!("__internal_do_not_use_{}", name_lowercase), name.span());
let linkage = syn::Ident::new(&format!("Linkage{}", name), name.span());
let borrowing_linkage = syn::Ident::new(&format!("Borrowing{}", linkage), name.span());
let enumerator = syn::Ident::new(&format!("Enumerator{}", name), name.span());
let key_for = syn::Ident::new(&format!("__decl_storage_key_for_{}", name_lowercase), name.span());
let linkage = syn::Ident::new(&format!("__DeclStorageLinkage{}", name), name.span());
let enumerator = syn::Ident::new(&format!("__DeclStorageEnumerator{}", name), name.span());
let phantom_data = quote! { #scrate::storage::generator::PhantomData };
let put_or_insert = quote! {
match linkage {
Some(linkage) => storage.put(key_for, &(val, linkage)),
Expand All @@ -216,140 +215,147 @@ impl<'a> Impls<'a> {
};

// generator for linked map
quote! {

mod #internal_module {
use super::*;
let helpers = quote! {
#[derive(parity_codec_derive::Encode, parity_codec_derive::Decode)]
struct #linkage<Key> {
/// Previous element key in storage (None for the first element)
previous: Option<Key>,
/// Next element key in storage (None for the last element)
next: Option<Key>,
}

#[derive(Default, parity_codec_derive::Encode, parity_codec_derive::Decode)]
pub struct #linkage {
/// Previous element key in storage (None for the first element)
previous: Option<#kty>,
/// Next element key in storage (None for the last element)
next: Option<#kty>,
impl<Key> Default for #linkage<Key> {
fn default() -> Self {
Self {
previous: None,
next: None,
}
}
}

/// A helper struct to avoid unnecessary key cloning.
///
/// NOTE It has to have exact same parity-codec encoding as #linkage!
#[derive(parity_codec_derive::Encode)]
struct #borrowing_linkage<'a> {
previous: Option<&'a #kty>,
next: Option<&'a #kty>,
struct #enumerator<'a, S, K, V> {
storage: &'a S,
next: Option<K>,
_data: #phantom_data<V>,
}

impl<'a, S: #scrate::GenericStorage, K, V> Iterator for #enumerator<'a, S, K, V> where
K: 'a + #scrate::codec::Codec,
V: 'a + #scrate::codec::Decode,
{
type Item = (K, V);

fn next(&mut self) -> Option<Self::Item> {
let next = self.next.take()?;
let key_for = #key_for(&next);
let (val, linkage): (V, #linkage<K>) = self.storage.get(&*key_for)
.expect("previous/next only contain existing entires; we enumerate using next; entry exists; qed");
self.next = linkage.next;
Some((next, val))
}
}

impl #linkage {
/// Update linkage when this element is removed.
///
/// Takes care of updating previous and next elements points
/// as well as updates head if the element is first or last.
pub fn remove<S: #scrate::GenericStorage>(
self,
storage: &S,
) {
let next_key = self.next.as_ref().map(|x| #key_for(x));
let prev_key = self.previous.as_ref().map(|x| #key_for(x));

if let Some(prev_key) = prev_key {
// Retrieve previous element and update `next`
let mut res = Self::read(storage, &*prev_key)
.expect("Linkage is updated in case entry is removed; it always points to existing keys; qed");
res.1.next = self.next;
storage.put(&*prev_key, &res);
} else {
// we were first so let's update the head
Self::write_head(storage, self.next.as_ref());
}
fn #key_for<Key: #scrate::codec::Encode>(key: &Key) -> #scrate::rstd::vec::Vec<u8> {
let mut key_for = #prefix.as_bytes().to_vec();
#scrate::codec::Encode::encode_to(&key, &mut key_for);
key_for
}
};

if let Some(next_key) = next_key {
// Update previous of next element
let mut res = Self::read(storage, &*next_key)
.expect("Linkage is updated in case entry is removed; it always points to existing keys; qed");
res.1.previous = self.previous;
storage.put(&*next_key, &res);
}
}
let structure = quote! {
#visibility struct #name<#traitinstance: #traittype>(#phantom_data<#traitinstance>);

/// Read the contained data and it's linkage.
pub fn read<S: #scrate::GenericStorage>(storage: &S, key: &[u8]) -> Option<(#value_type, #linkage)> {
storage.get(key)
impl<#traitinstance: #traittype> #name<#traitinstance> {
/// Update linkage when this element is removed.
///
/// Takes care of updating previous and next elements points
/// as well as updates head if the element is first or last.
fn remove_linkage<S: #scrate::GenericStorage>(
linkage: #linkage<#kty>,
storage: &S,
) {
let next_key = linkage.next.as_ref().map(|x| #key_for(x));
let prev_key = linkage.previous.as_ref().map(|x| #key_for(x));

if let Some(prev_key) = prev_key {
// Retrieve previous element and update `next`
let mut res = Self::read_with_linkage(storage, &*prev_key)
.expect("Linkage is updated in case entry is removed; it always points to existing keys; qed");
res.1.next = linkage.next;
storage.put(&*prev_key, &res);
} else {
// we were first so let's update the head
Self::write_head(storage, linkage.next.as_ref());
}

/// Generate linkage for newly inserted element.
///
/// Takes care of updating head and previous head's pointer.
pub fn insert_new_head<S: #scrate::GenericStorage>(
storage: &S,
key: &#kty,
) -> Self {
if let Some(head) = Self::read_head(storage) {
// update previous head predecessor
{
let head_key = #key_for(&head);
let (data, linkage) = Self::read(storage, &*head_key).expect(r#"
head is set when first element is inserted and unset when last element is removed;
if head is Some then it points to existing key; qed
"#);
storage.put(&*head_key, &(data, #borrowing_linkage {
next: linkage.next.as_ref(),
previous: Some(key),
}));
}
// update to current head
Self::write_head(storage, Some(key));
// return linkage with pointer to previous head
let mut linkage = Self::default();
linkage.next = Some(head);
linkage
} else {
// we are first - update the head and produce empty linkage
Self::write_head(storage, Some(key));
Self::default()
}
if let Some(next_key) = next_key {
// Update previous of next element
let mut res = Self::read_with_linkage(storage, &*next_key)
.expect("Linkage is updated in case entry is removed; it always points to existing keys; qed");
res.1.previous = linkage.previous;
storage.put(&*next_key, &res);
}
}

/// Read current head pointer.
pub fn read_head<S: #scrate::GenericStorage>(storage: &S) -> Option<#kty> {
storage.get(#head_key.as_bytes())
}
/// Read the contained data and it's linkage.
fn read_with_linkage<S: #scrate::GenericStorage>(storage: &S, key: &[u8]) -> Option<(#value_type, #linkage<#kty>)> {
storage.get(key)
}

/// Overwrite current head pointer.
///
/// If `None` is given head is removed from storage.
fn write_head<S: #scrate::GenericStorage>(storage: &S, head: Option<&#kty>) {
match head {
Some(head) => storage.put(#head_key.as_bytes(), head),
None => storage.kill(#head_key.as_bytes()),
/// Generate linkage for newly inserted element.
///
/// Takes care of updating head and previous head's pointer.
fn new_head_linkage<S: #scrate::GenericStorage>(
storage: &S,
key: &#kty,
) -> #linkage<#kty> {
if let Some(head) = Self::read_head(storage) {
// update previous head predecessor
{
let head_key = #key_for(&head);
let (data, linkage) = Self::read_with_linkage(storage, &*head_key).expect(r#"
head is set when first element is inserted and unset when last element is removed;
if head is Some then it points to existing key; qed
"#);
storage.put(&*head_key, &(data, #linkage {
next: linkage.next.as_ref(),
previous: Some(key),
}));
}
// update to current head
Self::write_head(storage, Some(key));
// return linkage with pointer to previous head
let mut linkage = #linkage::default();
linkage.next = Some(head);
linkage
} else {
// we are first - update the head and produce empty linkage
Self::write_head(storage, Some(key));
#linkage::default()
}
}

pub struct #enumerator<'a, S> {
pub storage: &'a S,
pub next: Option<#kty>,
/// Read current head pointer.
fn read_head<S: #scrate::GenericStorage>(storage: &S) -> Option<#kty> {
storage.get(#head_key.as_bytes())
}

impl<'a, S: #scrate::GenericStorage> Iterator for #enumerator<'a, S> {
type Item = (#kty, #typ);

fn next(&mut self) -> Option<Self::Item> {
let next = self.next.take()?;
let key_for = #key_for(&next);
let (val, linkage) = #linkage::read(self.storage, &*key_for)
.expect("previous/next only contain existing entires; we enumerate using next; entry exists; qed");
self.next = linkage.next;
Some((next, val))
/// Overwrite current head pointer.
///
/// If `None` is given head is removed from storage.
fn write_head<S: #scrate::GenericStorage>(storage: &S, head: Option<&#kty>) {
match head {
Some(head) => storage.put(#head_key.as_bytes(), head),
None => storage.kill(#head_key.as_bytes()),
}
}
}
};

fn #key_for(key: &#kty) -> #scrate::rstd::vec::Vec<u8> {
let mut key_for = #prefix.as_bytes().to_vec();
#scrate::codec::Encode::encode_to(&key, &mut key_for);
key_for
}
quote! {
#helpers

#visibility struct #name<#traitinstance: #traittype>(#scrate::storage::generator::PhantomData<#traitinstance>);
#structure

impl<#traitinstance: #traittype> #scrate::storage::generator::StorageMap<#kty, #typ> for #name<#traitinstance> {
type Query = #value_type;
Expand All @@ -371,13 +377,13 @@ impl<'a> Impls<'a> {

/// Take the value, reading and removing it.
fn take<S: #scrate::GenericStorage>(key: &#kty, storage: &S) -> Self::Query {
let res: Option<(#value_type, self::#internal_module::#linkage)> = storage.take(&*#key_for(key));
let res: Option<(#value_type, #linkage<#kty>)> = storage.take(&*#key_for(key));
match res {
Some((data, linkage)) => {
linkage.remove(storage);
Self::remove_linkage(linkage, storage);
data
},
None => #fielddefault
None => #fielddefault,
}
}

Expand All @@ -389,21 +395,21 @@ impl<'a> Impls<'a> {
/// Store a value to be associated with the given key from the map.
fn insert<S: #scrate::GenericStorage>(key: &#kty, val: &#typ, storage: &S) {
let key_for = &*#key_for(key);
let linkage = match self::#internal_module::#linkage::read(storage, key_for) {
let linkage = match Self::read_with_linkage(storage, key_for) {
// overwrite but reuse existing linkage
Some((_data, linkage)) => linkage,
// create new linkage
None => self::#internal_module::#linkage::insert_new_head(storage, key),
None => Self::new_head_linkage(storage, key),
};
storage.put(key_for, &(*val, linkage))
storage.put(key_for, &(val, linkage))
}

/// Mutate the value under a key
fn mutate<R, F: FnOnce(&mut Self::Query) -> R, S: #scrate::GenericStorage>(key: &#kty, f: F, storage: &S) -> R {
let key_for = &*#key_for(key);
let (mut val, linkage) = self::#internal_module::#linkage::read(storage, key_for)
let (mut val, linkage) = Self::read_with_linkage(storage, key_for)
.map(|(data, linkage)| (data, Some(linkage)))
.#option_simple_1(|| (#fielddefault, None));
.unwrap_or_else(|| (#fielddefault, None));

let ret = f(&mut val);
#mutate_impl ;
Expand All @@ -413,13 +419,17 @@ impl<'a> Impls<'a> {

impl<#traitinstance: #traittype> #scrate::storage::generator::EnumerableStorageMap<#kty, #typ> for #name<#traitinstance> {
fn head<S: #scrate::GenericStorage>(storage: &S) -> Option<#kty> {
self::#internal_module::#linkage::read_head(storage)
Self::read_head(storage)
}

fn enumerate<'a, S: #scrate::GenericStorage>(storage: &'a S) -> #scrate::storage::generator::Box<dyn Iterator<Item = (#kty, #typ)> + 'a> {
#scrate::storage::generator::Box::new(self::#internal_module::#enumerator {
next: self::#internal_module::#linkage::read_head(storage),
fn enumerate<'a, S: #scrate::GenericStorage>(storage: &'a S) -> #scrate::storage::generator::Box<dyn Iterator<Item = (#kty, #typ)> + 'a> where
#kty: 'a,
#typ: 'a,
{
#scrate::storage::generator::Box::new(#enumerator {
next: Self::read_head(storage),
storage,
_data: #phantom_data::<#typ>::default(),
})
}
}
Expand Down
5 changes: 4 additions & 1 deletion srml/support/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,11 +136,12 @@ macro_rules! for_each_tuple {
#[cfg(test)]
mod tests {
use super::*;
use parity_codec::Codec;
use runtime_io::{with_externalities, Blake2Hasher};
use runtime_primitives::BuildStorage;

pub trait Trait {
type BlockNumber;
type BlockNumber: Codec + Default;
type Origin;
}

Expand All @@ -160,6 +161,8 @@ mod tests {
decl_storage! {
trait Store for Module<T: Trait> as Example {
pub Data get(data) build(|_| vec![(15u32, 42u64)]): linked_map u32 => u64;
pub GenericData get(generic_data): linked_map T::BlockNumber => T::BlockNumber;
pub GenericData2 get(generic_data2): linked_map T::BlockNumber => Option<T::BlockNumber>;
}
}

Expand Down
2 changes: 1 addition & 1 deletion srml/support/src/storage/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -223,7 +223,7 @@ pub trait EnumerableStorageMap<K: codec::Codec, V: codec::Codec>: StorageMap<K,
fn head<S: Storage>(storage: &S) -> Option<K>;

/// Enumerate all elements in the map.
fn enumerate<'a, S: Storage>(storage: &'a S) -> Box<dyn Iterator<Item = (K, V)> + 'a>;
fn enumerate<'a, S: Storage>(storage: &'a S) -> Box<dyn Iterator<Item = (K, V)> + 'a> where K: 'a, V: 'a;
}

// FIXME #1466 Remove this in favour of `decl_storage` macro.
Expand Down
4 changes: 2 additions & 2 deletions srml/support/src/storage/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,15 +345,15 @@ pub trait EnumerableStorageMap<K: Codec, V: Codec>: StorageMap<K, V> {
fn head() -> Option<K>;

/// Enumerate all elements in the map.
fn enumerate() -> Box<dyn Iterator<Item = (K, V)>>;
fn enumerate() -> Box<dyn Iterator<Item = (K, V)>> where K: 'static, V: 'static;
}

impl<K: Codec, V: Codec, U> EnumerableStorageMap<K, V> for U where U: generator::EnumerableStorageMap<K, V> {
fn head() -> Option<K> {
<U as generator::EnumerableStorageMap<K, V>>::head(&RuntimeStorage)
}

fn enumerate() -> Box<dyn Iterator<Item = (K, V)>> {
fn enumerate() -> Box<dyn Iterator<Item = (K, V)>> where K: 'static, V: 'static {
<U as generator::EnumerableStorageMap<K, V>>::enumerate(&RuntimeStorage)
}
}
Expand Down