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

Fix compact event field decoding #384

Merged
merged 8 commits into from
Jan 7, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
220 changes: 186 additions & 34 deletions src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -253,10 +253,10 @@ where
}
}
}
TypeDef::Compact(_compact) => {
TypeDef::Compact(compact) => {
let inner = self
.metadata
.resolve_type(type_id)
.resolve_type(compact.type_param().id())
.ok_or(MetadataError::TypeNotFound(type_id))?;
let mut decode_compact_primitive = |primitive: &TypeDefPrimitive| {
match primitive {
Expand Down Expand Up @@ -339,35 +339,187 @@ pub enum EventsDecodingError {
InvalidCompactType(String),
}

// #[cfg(test)]
// mod tests {
// use super::*;
// use std::convert::TryFrom;
//
// type DefaultConfig = crate::NodeTemplateRuntime;
//
// #[test]
// fn test_decode_option() {
// let decoder = EventsDecoder::<DefaultConfig>::new(
// Metadata::default(),
// );
//
// let value = Some(0u8);
// let input = value.encode();
// let mut output = Vec::<u8>::new();
// let mut errors = Vec::<RuntimeError>::new();
//
// decoder
// .decode_raw_bytes(
// &[EventArg::Option(Box::new(EventArg::Primitive(
// "u8".to_string(),
// )))],
// &mut &input[..],
// &mut output,
// &mut errors,
// )
// .unwrap();
//
// assert_eq!(output, vec![1, 0]);
// }
// }
#[cfg(test)]
mod tests {
use super::*;
use crate::{
Config,
DefaultConfig,
Phase,
};
use frame_metadata::{
v14::{
ExtrinsicMetadata,
PalletEventMetadata,
PalletMetadata,
RuntimeMetadataLastVersion,
},
RuntimeMetadataPrefixed,
};
use scale_info::{
meta_type,
TypeInfo,
};
use std::convert::TryFrom;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we are on edition 2021 right?

Suggested change
use std::convert::TryFrom;

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes we are, sorry I missed this comment before merging 😬


#[derive(Encode)]
pub struct EventRecord<E: Encode> {
phase: Phase,
pallet_index: u8,
event: E,
topics: Vec<<DefaultConfig as Config>::Hash>,
}

fn event_record<E: Encode>(pallet_index: u8, event: E) -> EventRecord<E> {
EventRecord {
phase: Phase::Finalization,
pallet_index,
event,
topics: vec![],
}
}

fn pallet_metadata<E: TypeInfo + 'static>(pallet_index: u8) -> PalletMetadata {
let event = PalletEventMetadata {
ty: meta_type::<E>(),
};
PalletMetadata {
name: "Test",
storage: None,
calls: None,
event: Some(event),
constants: vec![],
error: None,
index: pallet_index,
}
}

fn init_decoder(pallets: Vec<PalletMetadata>) -> EventsDecoder<DefaultConfig> {
let extrinsic = ExtrinsicMetadata {
ty: meta_type::<()>(),
version: 0,
signed_extensions: vec![],
};
let v14 = RuntimeMetadataLastVersion::new(pallets, extrinsic, meta_type::<()>());
let runtime_metadata: RuntimeMetadataPrefixed = v14.into();
let metadata = Metadata::try_from(runtime_metadata).unwrap();
EventsDecoder::<DefaultConfig>::new(metadata)
}

#[test]
fn decode_single_event() {
#[derive(Clone, Encode, TypeInfo)]
enum Event {
A(u8),
}

let pallet_index = 0;
let pallet = pallet_metadata::<Event>(pallet_index);
let decoder = init_decoder(vec![pallet]);

let event = Event::A(1);
let encoded_event = event.encode();
let event_records = vec![event_record(pallet_index, event)];

let mut input = Vec::new();
event_records.encode_to(&mut input);

let events = decoder.decode_events(&mut &input[..]).unwrap();

assert_eq!(events[0].1.variant_index, encoded_event[0]);
assert_eq!(events[0].1.data.0, encoded_event[1..]);
}

#[test]
fn decode_multiple_events() {
#[derive(Clone, Encode, TypeInfo)]
enum Event {
A(u8),
B,
C { a: u32 },
}

let pallet_index = 0;
let pallet = pallet_metadata::<Event>(pallet_index);
let decoder = init_decoder(vec![pallet]);

let event1 = Event::A(1);
let event2 = Event::B;
let event3 = Event::C { a: 3 };

let encoded_event1 = event1.encode();
let encoded_event2 = event2.encode();
let encoded_event3 = event3.encode();

let event_records = vec![
event_record(pallet_index, event1),
event_record(pallet_index, event2),
event_record(pallet_index, event3),
];

let mut input = Vec::new();
event_records.encode_to(&mut input);

let events = decoder.decode_events(&mut &input[..]).unwrap();

assert_eq!(events[0].1.variant_index, encoded_event1[0]);
assert_eq!(events[0].1.data.0, encoded_event1[1..]);

assert_eq!(events[1].1.variant_index, encoded_event2[0]);
assert_eq!(events[1].1.data.0, encoded_event2[1..]);

assert_eq!(events[2].1.variant_index, encoded_event3[0]);
assert_eq!(events[2].1.data.0, encoded_event3[1..]);
}

#[test]
fn compact_event_field() {
#[derive(Clone, Encode, TypeInfo)]
enum Event {
A(#[codec(compact)] u32),
ascjones marked this conversation as resolved.
Show resolved Hide resolved
}

let pallet_index = 0;
let pallet = pallet_metadata::<Event>(pallet_index);
let decoder = init_decoder(vec![pallet]);

let event = Event::A(u32::MAX);
let encoded_event = event.encode();
let event_records = vec![event_record(pallet_index, event)];

let mut input = Vec::new();
event_records.encode_to(&mut input);

let events = decoder.decode_events(&mut &input[..]).unwrap();

assert_eq!(events[0].1.variant_index, encoded_event[0]);
assert_eq!(events[0].1.data.0, encoded_event[1..]);
}

#[test]
fn compact_wrapper_struct_field() {
#[derive(Clone, Encode, TypeInfo)]
enum Event {
A(#[codec(compact)] CompactWrapper),
}

#[derive(Clone, codec::CompactAs, Encode, TypeInfo)]
struct CompactWrapper(u64);

let pallet_index = 0;
let pallet = pallet_metadata::<Event>(pallet_index);
let decoder = init_decoder(vec![pallet]);

let event = Event::A(CompactWrapper(0));
let encoded_event = event.encode();
let event_records = vec![event_record(pallet_index, event)];

let mut input = Vec::new();
event_records.encode_to(&mut input);

let events = decoder.decode_events(&mut &input[..]).unwrap();

assert_eq!(events[0].1.variant_index, encoded_event[0]);
assert_eq!(events[0].1.data.0, encoded_event[1..]);
}
}
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ impl codec::Encode for Encoded {
}

/// A phase of a block's execution.
#[derive(Clone, Debug, Eq, PartialEq, Decode)]
#[derive(Clone, Debug, Eq, PartialEq, Decode, Encode)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

was this required compact encoding/decoding to work or a bonus? :)

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just for the tests in order to encode some EventRecords

pub enum Phase {
/// Applying an extrinsic.
ApplyExtrinsic(u32),
Expand Down