Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor event type decoding and declaration #221

Merged
merged 9 commits into from
Jan 20, 2021
31 changes: 16 additions & 15 deletions src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,17 +72,25 @@ impl std::fmt::Debug for RawEvent {
}
}

/// Functions that take an input stream, consume an object, and output the serialized bytes.
trait TypeSegmenterFn: Fn(&mut &[u8], &mut Vec<u8>) -> Result<(), Error> + Send {}
impl<T> TypeSegmenterFn for T where
T: Fn(&mut &[u8], &mut Vec<u8>) -> Result<(), Error> + Send
trait TypeSegmenter: Send {
/// Consumes an object from an input stream, and output the serialized bytes.
fn segment(&self, input: &mut &[u8], output: &mut Vec<u8>) -> Result<(), Error>;
}

impl<T> TypeSegmenter for PhantomData<T>
where
T: Codec + Send,
{
fn segment(&self, input: &mut &[u8], output: &mut Vec<u8>) -> Result<(), Error> {
T::decode(input).map_err(Error::from)?.encode_to(output);
Ok(())
}
}

h4x3rotab marked this conversation as resolved.
Show resolved Hide resolved
/// Events decoder.
pub struct EventsDecoder<T> {
metadata: Metadata,
type_segmenters: HashMap<String, Box<dyn TypeSegmenterFn>>,
type_segmenters: HashMap<String, Box<dyn TypeSegmenter>>,
marker: PhantomData<fn() -> T>,
}

Expand Down Expand Up @@ -141,15 +149,8 @@ impl<T: System> EventsDecoder<T> {
let size = U::default().encode().len();
// A segmenter decodes a type from an input stream (&mut &[u8]) and returns the serialized
// type to the output stream (&mut Vec<u8>).
self.type_segmenters.insert(
name.to_string(),
Box::new(
|input: &mut &[u8], output: &mut Vec<u8>| -> Result<(), Error> {
U::decode(input).map_err(Error::from)?.encode_to(output);
Ok(())
},
),
);
self.type_segmenters
.insert(name.to_string(), Box::new(PhantomData::<U>));
ascjones marked this conversation as resolved.
Show resolved Hide resolved
size
}

Expand Down Expand Up @@ -221,7 +222,7 @@ impl<T: System> EventsDecoder<T> {
_ => {
if let Some(seg) = self.type_segmenters.get(name) {
let mut buf = Vec::<u8>::new();
seg(input, &mut buf)?;
seg.segment(input, &mut buf)?;
output.write(&buf);
Ok(())
} else {
Expand Down
2 changes: 1 addition & 1 deletion src/frame/staking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ pub struct SetPayeeCall<T: Staking> {
}

/// Identity of a Grandpa authority.
pub type AuthorityId = [u8; 32];
pub type AuthorityId = crate::runtimes::app::grandpa::Public;
/// The weight of an authority.
pub type AuthorityWeight = u64;
/// A list of Grandpa authorities with associated weights.
Expand Down