Light client friendly events (#2491)

* Sketch of indexed events.

* Get EventIndex by holding another variable.

* Add some docs.

* Use DoubleMap to store reverse topic index

* Implement StorageDoubleMap::append

* Use append for EventTopics.

* Refactor.

* Avoid `mutate`

* Docs.

* Add topics to EventRecord

* Update tests.

* Rebuild.

* Bump version.

* Event topics test.

* Mix in BlockNumber to distinguish updates

* Fix srml-system test.

* Post merge fixes.

* Comments/TODO.
This commit is contained in:
Sergei Pepyakin
2019-05-13 20:56:01 +02:00
committed by Gavin Wood
parent d974189e3c
commit 21773b3a07
8 changed files with 325 additions and 56 deletions
+31
View File
@@ -238,6 +238,7 @@ mod tests {
pub DataDM config(test_config) build(|_| vec![(15u32, 16u32, 42u64)]): double_map hasher(twox_64_concat) u32, blake2_256(u32) => u64;
pub GenericDataDM: double_map T::BlockNumber, twox_128(T::BlockNumber) => T::BlockNumber;
pub GenericData2DM: double_map T::BlockNumber, twox_256(T::BlockNumber) => Option<T::BlockNumber>;
pub AppendableDM: double_map u32, blake2_256(T::BlockNumber) => Vec<u32>;
}
}
@@ -367,6 +368,21 @@ mod tests {
assert_eq!(DoubleMap::get(key1, key2+1), 0u64);
assert_eq!(DoubleMap::get(key1+1, key2), 4u64);
assert_eq!(DoubleMap::get(key1+1, key2+1), 4u64);
});
}
#[test]
fn double_map_append_should_work() {
with_externalities(&mut new_test_ext(), || {
type DoubleMap = AppendableDM<Test>;
let key1 = 17u32;
let key2 = 18u32;
DoubleMap::insert(key1, key2, vec![1]);
DoubleMap::append(key1, key2, &[2, 3]);
assert_eq!(DoubleMap::get(key1, key2), vec![1, 2, 3]);
});
}
@@ -453,6 +469,21 @@ mod tests {
),
documentation: DecodeDifferent::Encode(&[]),
},
StorageFunctionMetadata {
name: DecodeDifferent::Encode("AppendableDM"),
modifier: StorageFunctionModifier::Default,
ty: StorageFunctionType::DoubleMap{
hasher: StorageHasher::Blake2_256,
key1: DecodeDifferent::Encode("u32"),
key2: DecodeDifferent::Encode("T::BlockNumber"),
value: DecodeDifferent::Encode("Vec<u32>"),
key2_hasher: DecodeDifferent::Encode("blake2_256"),
},
default: DecodeDifferent::Encode(
DefaultByteGetter(&__GetByteStructGenericData2DM(PhantomData::<Test>))
),
documentation: DecodeDifferent::Encode(&[]),
},
])
};
+28
View File
@@ -401,6 +401,20 @@ pub trait StorageDoubleMap<K1: Codec, K2: Codec, V: Codec> {
KArg1: Borrow<K1>,
KArg2: Borrow<K2>,
F: FnOnce(&mut Self::Query) -> R;
/// Append the given items to the value under the key specified.
///
/// `V` is required to implement `codec::EncodeAppend<Item=I>`.
fn append<KArg1, KArg2, I>(
k1: KArg1,
k2: KArg2,
items: &[I],
) -> Result<(), &'static str>
where
KArg1: Borrow<K1>,
KArg2: Borrow<K2>,
I: codec::Encode,
V: EncodeAppend<Item=I>;
}
impl<K1: Codec, K2: Codec, V: Codec, U> StorageDoubleMap<K1, K2, V> for U
@@ -453,6 +467,20 @@ where
{
U::mutate(k1.borrow(), k2.borrow(), f, &RuntimeStorage)
}
fn append<KArg1, KArg2, I>(
k1: KArg1,
k2: KArg2,
items: &[I],
) -> Result<(), &'static str>
where
KArg1: Borrow<K1>,
KArg2: Borrow<K2>,
I: codec::Encode,
V: EncodeAppend<Item=I>,
{
U::append(k1.borrow(), k2.borrow(), items, &RuntimeStorage)
}
}
/// child storage NOTE could replace unhashed by having only one kind of storage (root being null storage
@@ -150,4 +150,24 @@ pub trait StorageDoubleMap<K1: codec::Codec, K2: codec::Codec, V: codec::Codec>
/// Mutate the value under a key.
fn mutate<R, F: FnOnce(&mut Self::Query) -> R, S: UnhashedStorage>(k1: &K1, k2: &K2, f: F, storage: &S) -> R;
/// Append the given items to the value under the key specified.
fn append<I, S: UnhashedStorage>(
k1: &K1,
k2: &K2,
items: &[I],
storage: &S,
) -> Result<(), &'static str>
where
I: codec::Encode,
V: codec::EncodeAppend<Item=I>,
{
let key = Self::key_for(k1, k2);
let new_val = <V as codec::EncodeAppend>::append(
storage.get_raw(&key).unwrap_or_default(),
items,
).ok_or_else(|| "Could not append given item")?;
storage.put_raw(&key, &new_val);
Ok(())
}
}