Add hooks to register event types for decoding (#227)

* Global registration of type segmenters for event decoding

* Perform type sizes check when building client

* Introduce EventTypeRegistry for global runtime type sizes

* Fmt

* Register runtime type sizes on creation of EventTypeRegistry

* Register more default dispatch types

* Add missing type sizes

* fmt

* Fix up register_type_size builder method

* Update doc comments

* Make register_default_type_sizes public

* Don't allow duplicate registered types

* Remove call to supertraits type registration, done manually in Runtime

* Fix tests and warnings

* Fix duplicate type registration

* Fmt

* review: use is_empty()

Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>

* Add panic docs

Co-authored-by: Niklas Adolfsson <niklasadolfsson1@gmail.com>
This commit is contained in:
Andrew Jones
2021-02-18 10:28:40 +00:00
committed by GitHub
parent 2c8e5211aa
commit de859e7396
19 changed files with 436 additions and 280 deletions
-14
View File
@@ -32,10 +32,6 @@ pub fn call(s: Structure) -> TokenStream {
let generics = &s.ast().generics;
let params = utils::type_params(generics);
let module = utils::module_name(generics);
let with_module = format_ident!(
"with_{}",
utils::path_to_ident(module).to_string().to_snake_case()
);
let call_name = utils::ident_to_name(ident, "Call").to_snake_case();
let bindings = utils::bindings(&s);
let fields = utils::fields(&bindings);
@@ -51,11 +47,6 @@ pub fn call(s: Structure) -> TokenStream {
impl#generics #subxt::Call<T> for #ident<#(#params),*> {
const MODULE: &'static str = MODULE;
const FUNCTION: &'static str = #call_name;
fn events_decoder(
decoder: &mut #subxt::EventsDecoder<T>,
) {
decoder.#with_module();
}
}
/// Call extension trait.
@@ -118,11 +109,6 @@ mod tests {
impl<'a, T: Balances> substrate_subxt::Call<T> for TransferCall<'a, T> {
const MODULE: &'static str = MODULE;
const FUNCTION: &'static str = "transfer";
fn events_decoder(
decoder: &mut substrate_subxt::EventsDecoder<T>,
) {
decoder.with_balances();
}
}
/// Call extension trait.
+4 -5
View File
@@ -62,17 +62,16 @@ use synstructure::{
///
/// const MODULE: &str = "Herd";
///
/// // `EventsDecoder` extension trait.
/// pub trait HerdEventsDecoder {
/// // `EventTypeRegistry` extension trait.
/// pub trait HerdEventTypeRegistry {
/// // Registers this modules types.
/// fn with_herd(&mut self);
/// }
///
/// impl<T: Herd> HerdEventsDecoder for
/// substrate_subxt::EventsDecoder<T>
/// impl<T: Herd + Runtime> EventTypeRegistry for
/// substrate_subxt::EventTypeRegistry<T>
/// {
/// fn with_herd(&mut self) {
/// self.with_husbandry();
/// self.register_type_size::<T::Hooves>("Hooves");
/// self.register_type_size::<T::Wool>("Wool");
/// }
+15 -29
View File
@@ -62,8 +62,8 @@ fn ignore(attrs: &[syn::Attribute]) -> bool {
false
}
fn events_decoder_trait_name(module: &syn::Ident) -> syn::Ident {
format_ident!("{}EventsDecoder", module.to_string())
fn event_type_registry_trait_name(module: &syn::Ident) -> syn::Ident {
format_ident!("{}EventTypeRegistry", module.to_string())
}
fn with_module_ident(module: &syn::Ident) -> syn::Ident {
@@ -128,20 +128,9 @@ pub fn module(_args: TokenStream, tokens: TokenStream) -> TokenStream {
let subxt = utils::use_crate("substrate-subxt");
let module = &input.ident;
let module_name = module.to_string();
let module_events_decoder = events_decoder_trait_name(module);
let module_events_type_registry = event_type_registry_trait_name(module);
let with_module = with_module_ident(module);
let bounds = input.supertraits.iter().filter_map(|bound| {
if let syn::TypeParamBound::Trait(syn::TraitBound { path, .. }) = bound {
let module = utils::path_to_ident(path);
let with_module = with_module_ident(module);
Some(quote! {
self.#with_module();
})
} else {
None
}
});
let associated_types = input.items.iter().filter_map(|item| {
if let syn::TraitItem::Type(ty) = item {
if ignore(&ty.attrs) {
@@ -168,17 +157,16 @@ pub fn module(_args: TokenStream, tokens: TokenStream) -> TokenStream {
const MODULE: &str = #module_name;
/// `EventsDecoder` extension trait.
pub trait #module_events_decoder {
/// `EventTypeRegistry` extension trait.
pub trait #module_events_type_registry {
/// Registers this modules types.
fn #with_module(&mut self);
}
impl<T: #module> #module_events_decoder for
#subxt::EventsDecoder<T>
impl<T: #module + #subxt::Runtime> #module_events_type_registry for
#subxt::EventTypeRegistry<T>
{
fn #with_module(&mut self) {
#(#bounds)*
#(#associated_types)*
#(#types)*
}
@@ -221,17 +209,16 @@ mod tests {
const MODULE: &str = "Balances";
/// `EventsDecoder` extension trait.
pub trait BalancesEventsDecoder {
/// `EventTypeRegistry` extension trait.
pub trait BalancesEventTypeRegistry {
/// Registers this modules types.
fn with_balances(&mut self);
}
impl<T: Balances> BalancesEventsDecoder for
substrate_subxt::EventsDecoder<T>
impl<T: Balances + substrate_subxt::Runtime> BalancesEventTypeRegistry for
substrate_subxt::EventTypeRegistry<T>
{
fn with_balances(&mut self) {
self.with_system();
self.register_type_size::<T::Balance>("Balance");
}
}
@@ -262,17 +249,16 @@ mod tests {
const MODULE: &str = "Herd";
/// `EventsDecoder` extension trait.
pub trait HerdEventsDecoder {
/// `EventTypeRegistry` extension trait.
pub trait HerdEventTypeRegistry {
/// Registers this modules types.
fn with_herd(&mut self);
}
impl<T: Herd> HerdEventsDecoder for
substrate_subxt::EventsDecoder<T>
impl<T: Herd + substrate_subxt::Runtime> HerdEventTypeRegistry for
substrate_subxt::EventTypeRegistry<T>
{
fn with_herd(&mut self) {
self.with_husbandry();
self.register_type_size::<T::Hoves>("Hoves");
self.register_type_size::<T::Wool>("Wool");
}
+1 -4
View File
@@ -30,10 +30,7 @@ use substrate_subxt::{
MaybeSerialize,
Member,
},
system::{
System,
SystemEventsDecoder,
},
system::System,
ClientBuilder,
KusamaRuntime,
PairSigner,