Skip to content
Snippets Groups Projects
Commit a3e2a710 authored by thiolliere's avatar thiolliere Committed by Gavin Wood
Browse files

Fix linkemap swap (#3468)

* fix linkedmap swap

* version bump
parent 725aa0ae
No related merge requests found
......@@ -81,7 +81,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion {
// implementation changes and behavior does not, then leave spec_version as
// is and increment impl_version.
spec_version: 149,
impl_version: 149,
impl_version: 150,
apis: RUNTIME_API_VERSIONS,
};
......
......@@ -666,6 +666,42 @@ impl<'a, I: Iterator<Item=syn::Meta>> Impls<'a, I> {
#mutate_impl;
ret
}
// Swap must be overriden not to break links.
fn swap<S: #scrate::HashedStorage<Self::Hasher>>(
key1: &#kty,
key2: &#kty,
storage: &mut S,
) {
use self::#inner_module::Utils;
let final_key1 = &*#as_map::key_for(key1);
let final_key2 = &*#as_map::key_for(key2);
let full_value_1 = Self::read_with_linkage(storage, final_key1);
let full_value_2 = Self::read_with_linkage(storage, final_key2);
match (full_value_1, full_value_2) {
// Just keep linkage in order and only swap values.
(Some((value1, linkage1)), Some((value2, linkage2))) => {
storage.put(final_key1, &(value2, linkage1));
storage.put(final_key2, &(value1, linkage2));
}
// Remove key and insert the new one.
(Some((value, linkage)), None) => {
#as_map::remove(key1, storage);
let linkage = Self::new_head_linkage(storage, key2);
storage.put(final_key2, &(value, linkage));
}
// Remove key and insert the new one.
(None, Some((value, linkage))) => {
#as_map::remove(key2, storage);
let linkage = Self::new_head_linkage(storage, key1);
storage.put(final_key1, &(value, linkage));
}
// No-op.
(None, None) => (),
}
}
}
impl<#impl_trait> #scrate::storage::hashed::generator::EnumerableStorageMap<#kty, #typ>
......
......@@ -342,6 +342,35 @@ mod tests {
});
}
#[test]
fn linked_map_swap_works() {
with_externalities(&mut new_test_ext(), || {
OptionLinkedMap::insert(0, 0);
OptionLinkedMap::insert(1, 1);
OptionLinkedMap::insert(2, 2);
OptionLinkedMap::insert(3, 3);
let collect = || OptionLinkedMap::enumerate().collect::<Vec<_>>();
assert_eq!(collect(), vec![(3, 3), (2, 2), (1, 1), (0, 0)]);
// Two existing
OptionLinkedMap::swap(1, 2);
assert_eq!(collect(), vec![(3, 3), (2, 1), (1, 2), (0, 0)]);
// Back to normal
OptionLinkedMap::swap(2, 1);
assert_eq!(collect(), vec![(3, 3), (2, 2), (1, 1), (0, 0)]);
// Left existing
OptionLinkedMap::swap(2, 5);
assert_eq!(collect(), vec![(5, 2), (3, 3), (1, 1), (0, 0)]);
// Right existing
OptionLinkedMap::swap(5, 2);
assert_eq!(collect(), vec![(2, 2), (3, 3), (1, 1), (0, 0)]);
});
}
#[test]
fn linked_map_basic_insert_remove_should_work() {
with_externalities(&mut new_test_ext(), || {
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment