Zero sized types (#121)

* Fix zero sized types.

* Cleanup.

* Expose genesis hash.

* Fix clippy warnings.

* Readd default type sizes for now.

* Cleanup ret type.
This commit is contained in:
David Craven
2020-06-08 13:25:51 +02:00
committed by GitHub
parent 26ada75dec
commit 91203b91d3
7 changed files with 54 additions and 75 deletions
+25 -34
View File
@@ -28,7 +28,6 @@ use std::{
HashMap,
HashSet,
},
convert::TryFrom,
marker::{
PhantomData,
Send,
@@ -87,51 +86,43 @@ pub struct EventsDecoder<T> {
marker: PhantomData<fn() -> T>,
}
impl<T: System> TryFrom<Metadata> for EventsDecoder<T> {
type Error = EventsError;
fn try_from(metadata: Metadata) -> Result<Self, Self::Error> {
impl<T: System> EventsDecoder<T> {
/// Creates a new `EventsDecoder`.
pub fn new(metadata: Metadata) -> Self {
let mut decoder = Self {
metadata,
type_sizes: HashMap::new(),
marker: PhantomData,
};
// register default event arg type sizes for dynamic decoding of events
decoder.register_type_size::<bool>("bool")?;
decoder.register_type_size::<u32>("ReferendumIndex")?;
decoder.register_type_size::<[u8; 16]>("Kind")?;
decoder.register_type_size::<[u8; 32]>("AuthorityId")?;
decoder.register_type_size::<u8>("u8")?;
decoder.register_type_size::<u32>("u32")?;
decoder.register_type_size::<u32>("AccountIndex")?;
decoder.register_type_size::<u32>("SessionIndex")?;
decoder.register_type_size::<u32>("PropIndex")?;
decoder.register_type_size::<u32>("ProposalIndex")?;
decoder.register_type_size::<u32>("AuthorityIndex")?;
decoder.register_type_size::<u64>("AuthorityWeight")?;
decoder.register_type_size::<u32>("MemberCount")?;
decoder.register_type_size::<T::AccountId>("AccountId")?;
decoder.register_type_size::<T::BlockNumber>("BlockNumber")?;
decoder.register_type_size::<T::Hash>("Hash")?;
decoder.register_type_size::<u8>("VoteThreshold")?;
Ok(decoder)
decoder.register_type_size::<bool>("bool");
decoder.register_type_size::<u32>("ReferendumIndex");
decoder.register_type_size::<[u8; 16]>("Kind");
decoder.register_type_size::<[u8; 32]>("AuthorityId");
decoder.register_type_size::<u8>("u8");
decoder.register_type_size::<u32>("u32");
decoder.register_type_size::<u32>("AccountIndex");
decoder.register_type_size::<u32>("SessionIndex");
decoder.register_type_size::<u32>("PropIndex");
decoder.register_type_size::<u32>("ProposalIndex");
decoder.register_type_size::<u32>("AuthorityIndex");
decoder.register_type_size::<u64>("AuthorityWeight");
decoder.register_type_size::<u32>("MemberCount");
decoder.register_type_size::<T::AccountId>("AccountId");
decoder.register_type_size::<T::BlockNumber>("BlockNumber");
decoder.register_type_size::<T::Hash>("Hash");
decoder.register_type_size::<u8>("VoteThreshold");
decoder
}
}
impl<T: System> EventsDecoder<T> {
/// Register a type.
pub fn register_type_size<U>(&mut self, name: &str) -> Result<usize, EventsError>
pub fn register_type_size<U>(&mut self, name: &str) -> usize
where
U: Default + Codec + Send + 'static,
{
let size = U::default().encode().len();
if size > 0 {
self.type_sizes.insert(name.to_string(), size);
Ok(size)
} else {
Err(EventsError::TypeSizeUnavailable(name.to_owned()))
}
self.type_sizes.insert(name.to_string(), size);
size
}
/// Check missing type sizes.
@@ -156,7 +147,7 @@ impl<T: System> EventsDecoder<T> {
}
}
}
if missing.len() > 0 {
if !missing.is_empty() {
log::warn!(
"The following primitive types do not have registered sizes: {:?} \
If any of these events are received, an error will occur since we cannot decode them",