Improve error handling in proc-macros, handle DispatchError etc. (#123)

* Improve error handling.

* Fix build.

* Handle runtime errors.

* Add runtime trait for better type inference.

* Use runtime trait part 1.

* wip

* Add support for sudo.

* Finish error handling.

* Fix tests.

* Fix clippy warnings.
This commit is contained in:
David Craven
2020-06-22 08:39:40 +02:00
committed by GitHub
parent 21d07c6c24
commit 3080ec91a6
23 changed files with 557 additions and 373 deletions
+14 -21
View File
@@ -28,7 +28,6 @@ use synstructure::Structure;
pub fn call(s: Structure) -> TokenStream {
let subxt = utils::use_crate("substrate-subxt");
let codec = utils::use_crate("parity-scale-codec");
let ident = &s.ast().ident;
let generics = &s.ast().generics;
let params = utils::type_params(generics);
@@ -60,32 +59,29 @@ pub fn call(s: Structure) -> TokenStream {
}
/// Call extension trait.
pub trait #call_trait<T: #module, S: #codec::Encode, E: #subxt::SignedExtra<T>> {
pub trait #call_trait<T: #subxt::Runtime + #module> {
/// Create and submit an extrinsic.
fn #call<'a>(
&'a self,
signer: &'a (dyn #subxt::Signer<T, S, E> + Send + Sync),
signer: &'a (dyn #subxt::Signer<T> + Send + Sync),
#args
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<T::Hash, #subxt::Error>> + Send + 'a>>;
/// Create, submit and watch an extrinsic.
fn #call_and_watch<'a>(
&'a self,
signer: &'a (dyn #subxt::Signer<T, S, E> + Send + Sync),
signer: &'a (dyn #subxt::Signer<T> + Send + Sync),
#args
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<#subxt::ExtrinsicSuccess<T>, #subxt::Error>> + Send + 'a>>;
}
impl<T, S, E> #call_trait<T, S, E> for #subxt::Client<T, S, E>
impl<T: #subxt::Runtime + #module> #call_trait<T> for #subxt::Client<T>
where
T: #module + #subxt::system::System + Send + Sync + 'static,
S: #codec::Encode + Send + Sync + 'static,
E: #subxt::SignedExtra<T> + #subxt::sp_runtime::traits::SignedExtension + Send + Sync + 'static,
<<E as #subxt::SignedExtra<T>>::Extra as #subxt::sp_runtime::traits::SignedExtension>::AdditionalSigned: Send + Sync,
<<T::Extra as #subxt::SignedExtra<T>>::Extra as #subxt::SignedExtension>::AdditionalSigned: Send + Sync,
{
fn #call<'a>(
&'a self,
signer: &'a (dyn #subxt::Signer<T, S, E> + Send + Sync),
signer: &'a (dyn #subxt::Signer<T> + Send + Sync),
#args
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<T::Hash, #subxt::Error>> + Send + 'a>> {
let #marker = core::marker::PhantomData::<T>;
@@ -94,7 +90,7 @@ pub fn call(s: Structure) -> TokenStream {
fn #call_and_watch<'a>(
&'a self,
signer: &'a (dyn #subxt::Signer<T, S, E> + Send + Sync),
signer: &'a (dyn #subxt::Signer<T> + Send + Sync),
#args
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<#subxt::ExtrinsicSuccess<T>, #subxt::Error>> + Send + 'a>> {
let #marker = core::marker::PhantomData::<T>;
@@ -130,11 +126,11 @@ mod tests {
}
/// Call extension trait.
pub trait TransferCallExt<T: Balances, S: codec::Encode, E: substrate_subxt::SignedExtra<T>> {
pub trait TransferCallExt<T: substrate_subxt::Runtime + Balances> {
/// Create and submit an extrinsic.
fn transfer<'a>(
&'a self,
signer: &'a (dyn substrate_subxt::Signer<T, S, E> + Send + Sync),
signer: &'a (dyn substrate_subxt::Signer<T> + Send + Sync),
to: &'a <T as System>::Address,
amount: T::Balance,
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<T::Hash, substrate_subxt::Error>> + Send + 'a>>;
@@ -142,22 +138,19 @@ mod tests {
/// Create, submit and watch an extrinsic.
fn transfer_and_watch<'a>(
&'a self,
signer: &'a (dyn substrate_subxt::Signer<T, S, E> + Send + Sync),
signer: &'a (dyn substrate_subxt::Signer<T> + Send + Sync),
to: &'a <T as System>::Address,
amount: T::Balance,
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<substrate_subxt::ExtrinsicSuccess<T>, substrate_subxt::Error>> + Send + 'a>>;
}
impl<T, S, E> TransferCallExt<T, S, E> for substrate_subxt::Client<T, S, E>
impl<T: substrate_subxt::Runtime + Balances> TransferCallExt<T> for substrate_subxt::Client<T>
where
T: Balances + substrate_subxt::system::System + Send + Sync + 'static,
S: codec::Encode + Send + Sync + 'static,
E: substrate_subxt::SignedExtra<T> + substrate_subxt::sp_runtime::traits::SignedExtension + Send + Sync + 'static,
<<E as substrate_subxt::SignedExtra<T>>::Extra as substrate_subxt::sp_runtime::traits::SignedExtension>::AdditionalSigned: Send + Sync,
<<T::Extra as substrate_subxt::SignedExtra<T>>::Extra as substrate_subxt::SignedExtension>::AdditionalSigned: Send + Sync,
{
fn transfer<'a>(
&'a self,
signer: &'a (dyn substrate_subxt::Signer<T, S, E> + Send + Sync),
signer: &'a (dyn substrate_subxt::Signer<T> + Send + Sync),
to: &'a <T as System>::Address,
amount: T::Balance,
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<T::Hash, substrate_subxt::Error>> + Send + 'a>> {
@@ -167,7 +160,7 @@ mod tests {
fn transfer_and_watch<'a>(
&'a self,
signer: &'a (dyn substrate_subxt::Signer<T, S, E> + Send + Sync),
signer: &'a (dyn substrate_subxt::Signer<T> + Send + Sync),
to: &'a <T as System>::Address,
amount: T::Balance,
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<substrate_subxt::ExtrinsicSuccess<T>, substrate_subxt::Error>> + Send + 'a>> {
+2 -4
View File
@@ -36,7 +36,7 @@ pub fn event(s: Structure) -> TokenStream {
let event = format_ident!("{}", event_name.to_snake_case());
let event_trait = format_ident!("{}EventExt", event_name);
let expanded = quote! {
quote! {
impl<T: #module> #subxt::Event<T> for #ident<T> {
const MODULE: &'static str = MODULE;
const EVENT: &'static str = #event_name;
@@ -53,9 +53,7 @@ pub fn event(s: Structure) -> TokenStream {
self.find_event()
}
}
};
TokenStream::from(expanded)
}
}
#[cfg(test)]
+6 -3
View File
@@ -24,32 +24,35 @@ mod test;
mod utils;
use proc_macro::TokenStream;
use proc_macro_error::proc_macro_error;
use synstructure::{
decl_derive,
Structure,
};
#[proc_macro_attribute]
#[proc_macro_error]
pub fn module(args: TokenStream, input: TokenStream) -> TokenStream {
module::module(args.into(), input.into()).into()
}
decl_derive!([Call] => call);
decl_derive!([Call] => #[proc_macro_error] call);
fn call(s: Structure) -> TokenStream {
call::call(s).into()
}
decl_derive!([Event] => event);
decl_derive!([Event] => #[proc_macro_error] event);
fn event(s: Structure) -> TokenStream {
event::event(s).into()
}
decl_derive!([Store, attributes(store)] => store);
decl_derive!([Store, attributes(store)] => #[proc_macro_error] store);
fn store(s: Structure) -> TokenStream {
store::store(s).into()
}
#[proc_macro]
#[proc_macro_error]
pub fn subxt_test(input: TokenStream) -> TokenStream {
test::test(input.into()).into()
}
+10 -5
View File
@@ -17,6 +17,7 @@
use crate::utils;
use heck::SnakeCase;
use proc_macro2::TokenStream;
use proc_macro_error::abort;
use quote::{
format_ident,
quote,
@@ -48,8 +49,10 @@ type ModuleAttrs = utils::Attrs<ModuleAttr>;
fn ignore(attrs: &[syn::Attribute]) -> bool {
for attr in attrs {
if let Some(ident) = attr.path.get_ident() {
if ident.to_string() == "module" {
let attrs: ModuleAttrs = syn::parse2(attr.tokens.clone()).unwrap();
if ident == "module" {
let attrs: ModuleAttrs = syn::parse2(attr.tokens.clone())
.map_err(|err| abort!("{}", err))
.unwrap();
if !attrs.attrs.is_empty() {
return true
}
@@ -69,10 +72,12 @@ fn with_module_ident(module: &syn::Ident) -> syn::Ident {
pub fn module(_args: TokenStream, tokens: TokenStream) -> TokenStream {
let input: Result<syn::ItemTrait, _> = syn::parse2(tokens.clone());
if input.is_err() {
let input = if let Ok(input) = input {
input
} else {
// handle #[module(ignore)] by just returning the tokens
return tokens
}
let input = input.unwrap();
};
let subxt = utils::use_crate("substrate-subxt");
let module = &input.ident;
+16 -15
View File
@@ -20,6 +20,7 @@ use heck::{
SnakeCase,
};
use proc_macro2::TokenStream;
use proc_macro_error::abort;
use quote::{
format_ident,
quote,
@@ -50,7 +51,9 @@ impl Parse for StoreAttr {
type StoreAttrs = utils::Attrs<StoreAttr>;
fn parse_returns_attr(attr: &syn::Attribute) -> Option<(syn::Type, syn::Type, bool)> {
let attrs: StoreAttrs = syn::parse2(attr.tokens.clone()).unwrap();
let attrs: StoreAttrs = syn::parse2(attr.tokens.clone())
.map_err(|err| abort!("{}", err))
.unwrap();
attrs.attrs.into_iter().next().map(|attr| {
let StoreAttr::Returns(attr) = attr;
let ty = attr.value;
@@ -81,7 +84,9 @@ pub fn store(s: Structure) -> TokenStream {
.iter()
.filter_map(|bi| bi.ast().attrs.iter().filter_map(parse_returns_attr).next())
.next()
.expect("#[store(returns = ..)] needs to be specified.");
.unwrap_or_else(|| {
abort!(ident, "#[store(returns = ..)] needs to be specified.")
});
let fetch = if uses_default {
quote!(fetch_or_default)
} else {
@@ -93,7 +98,13 @@ pub fn store(s: Structure) -> TokenStream {
0 => "plain",
1 => "map",
2 => "double_map",
_ => panic!("invalid number of arguments"),
_ => {
abort!(
ident,
"Expected 0-2 fields but found {}",
filtered_fields.len()
);
}
}
);
let keys = filtered_fields
@@ -127,12 +138,7 @@ pub fn store(s: Structure) -> TokenStream {
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<#ret, #subxt::Error>> + Send + 'a>>;
}
impl<T, S, E> #store_trait<T> for #subxt::Client<T, S, E>
where
T: #module + Send + Sync,
S: 'static,
E: Send + Sync + 'static,
{
impl<T: #subxt::Runtime + #module> #store_trait<T> for #subxt::Client<T> {
fn #store<'a>(
&'a self,
#args
@@ -185,12 +191,7 @@ mod tests {
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<AccountData<T::Balance>, substrate_subxt::Error>> + Send + 'a>>;
}
impl<T, S, E> AccountStoreExt<T> for substrate_subxt::Client<T, S, E>
where
T: Balances + Send + Sync,
S: 'static,
E: Send + Sync + 'static,
{
impl<T: substrate_subxt::Runtime + Balances> AccountStoreExt<T> for substrate_subxt::Client<T> {
fn account<'a>(
&'a self,
account_id: &'a <T as System>::AccountId,
+17 -43
View File
@@ -16,6 +16,7 @@
use crate::utils;
use proc_macro2::TokenStream;
use proc_macro_error::abort;
use quote::{
format_ident,
quote,
@@ -34,8 +35,6 @@ mod kw {
custom_keyword!(name);
custom_keyword!(runtime);
custom_keyword!(account);
custom_keyword!(signature);
custom_keyword!(extra);
custom_keyword!(prelude);
custom_keyword!(step);
custom_keyword!(state);
@@ -70,10 +69,9 @@ struct Items<I> {
impl<I: Parse> Parse for Items<I> {
fn parse(input: ParseStream) -> syn::Result<Self> {
let content;
Ok(Self {
brace: syn::braced!(content in input),
items: content.parse_terminated(I::parse)?,
})
let brace = syn::braced!(content in input);
let items = content.parse_terminated(I::parse)?;
Ok(Self { brace, items })
}
}
@@ -82,10 +80,8 @@ type ItemTest = Items<TestItem>;
#[derive(Debug)]
enum TestItem {
Name(Item<kw::name, syn::Ident>),
Runtime(Item<kw::runtime, syn::Type>),
Runtime(Item<kw::runtime, Box<syn::Type>>),
Account(Item<kw::account, syn::Ident>),
Signature(Item<kw::signature, syn::Type>),
Extra(Item<kw::extra, syn::Type>),
State(Item<kw::state, ItemState>),
Prelude(Item<kw::prelude, syn::Block>),
Step(Item<kw::step, ItemStep>),
@@ -99,10 +95,6 @@ impl Parse for TestItem {
Ok(TestItem::Runtime(input.parse()?))
} else if input.peek(kw::account) {
Ok(TestItem::Account(input.parse()?))
} else if input.peek(kw::signature) {
Ok(TestItem::Signature(input.parse()?))
} else if input.peek(kw::extra) {
Ok(TestItem::Extra(input.parse()?))
} else if input.peek(kw::state) {
Ok(TestItem::State(input.parse()?))
} else if input.peek(kw::prelude) {
@@ -142,10 +134,8 @@ type StateItem = Item<syn::Ident, syn::Expr>;
struct Test {
name: syn::Ident,
runtime: syn::Type,
runtime: Box<syn::Type>,
account: syn::Ident,
signature: syn::Type,
extra: syn::Type,
state: Option<State>,
prelude: Option<syn::Block>,
steps: Vec<Step>,
@@ -156,11 +146,11 @@ impl From<ItemTest> for Test {
let mut name = None;
let mut runtime = None;
let mut account = None;
let mut signature = None;
let mut extra = None;
let mut state = None;
let mut prelude = None;
let mut steps = vec![];
let span = test.brace.span;
for test_item in test.items {
match test_item {
TestItem::Name(item) => {
@@ -172,12 +162,6 @@ impl From<ItemTest> for Test {
TestItem::Account(item) => {
account = Some(item.value);
}
TestItem::Signature(item) => {
signature = Some(item.value);
}
TestItem::Extra(item) => {
extra = Some(item.value);
}
TestItem::State(item) => {
state = Some(item.value.into());
}
@@ -193,14 +177,8 @@ impl From<ItemTest> for Test {
let runtime = runtime
.unwrap_or_else(|| syn::parse2(quote!(#subxt::DefaultNodeRuntime)).unwrap());
Self {
name: name.expect("No name specified"),
name: name.unwrap_or_else(|| abort!(span, "No name specified")),
account: account.unwrap_or_else(|| format_ident!("Alice")),
signature: signature.unwrap_or_else(|| {
syn::parse2(quote!(#subxt::sp_runtime::MultiSignature)).unwrap()
}),
extra: extra.unwrap_or_else(|| {
syn::parse2(quote!(#subxt::DefaultExtra<#runtime>)).unwrap()
}),
runtime,
state,
prelude,
@@ -219,8 +197,6 @@ impl Test {
name,
runtime,
account,
signature,
extra,
state,
prelude,
steps,
@@ -234,7 +210,7 @@ impl Test {
#[ignore]
async fn #name() {
#env_logger
let client = #subxt::ClientBuilder::<#runtime, #signature, #extra>::new()
let client = #subxt::ClientBuilder::<#runtime>::new()
.build().await.unwrap();
let signer = #subxt::PairSigner::new(#sp_keyring::AccountKeyring::#account.pair());
@@ -277,6 +253,7 @@ impl From<ItemStep> for Step {
let mut event = vec![];
let mut assert = None;
let span = step.brace.span;
for step_item in step.items {
match step_item {
StepItem::State(item) => {
@@ -297,7 +274,7 @@ impl From<ItemStep> for Step {
Self {
state,
call: call.expect("Step requires a call."),
call: call.unwrap_or_else(|| abort!(span, "Step requires a call.")),
event_name,
event,
assert,
@@ -404,14 +381,15 @@ impl From<ItemState> for State {
fn struct_name(expr: &syn::Expr) -> syn::Path {
if let syn::Expr::Struct(syn::ExprStruct { path, .. }) = expr {
return path.clone()
path.clone()
} else {
panic!("not a struct");
abort!(expr, "Expected a struct");
}
}
pub fn test(input: TokenStream) -> TokenStream {
let item_test: ItemTest = syn::parse2(input).unwrap();
let item_test: ItemTest =
syn::parse2(input).map_err(|err| abort!("{}", err)).unwrap();
Test::from(item_test).into_tokens()
}
@@ -450,11 +428,7 @@ mod tests {
#[ignore]
async fn test_transfer_balance() {
env_logger::try_init().ok();
let client = substrate_subxt::ClientBuilder::<
KusamaRuntime,
substrate_subxt::sp_runtime::MultiSignature,
substrate_subxt::DefaultExtra<KusamaRuntime>
>::new().build().await.unwrap();
let client = substrate_subxt::ClientBuilder::<KusamaRuntime>::new().build().await.unwrap();
let signer = substrate_subxt::PairSigner::new(sp_keyring::AccountKeyring::Alice.pair());
#[allow(unused)]
let alice = sp_keyring::AccountKeyring::Alice.to_account_id();
+9 -10
View File
@@ -56,7 +56,7 @@ pub fn bindings<'a>(s: &'a Structure) -> Vec<&'a BindingInfo<'a>> {
type Field = (syn::Ident, syn::Type);
pub fn fields<'a>(bindings: &'a [&'a BindingInfo<'a>]) -> Vec<Field> {
pub fn fields(bindings: &[&BindingInfo<'_>]) -> Vec<Field> {
bindings
.iter()
.enumerate()
@@ -72,7 +72,7 @@ pub fn fields<'a>(bindings: &'a [&'a BindingInfo<'a>]) -> Vec<Field> {
.collect()
}
pub fn marker_field<'a>(fields: &'a [Field]) -> Option<syn::Ident> {
pub fn marker_field(fields: &[Field]) -> Option<syn::Ident> {
fields
.iter()
.filter_map(|(field, ty)| {
@@ -86,7 +86,7 @@ pub fn marker_field<'a>(fields: &'a [Field]) -> Option<syn::Ident> {
.cloned()
}
pub fn filter_fields<'a>(fields: &'a [Field], field: &'a syn::Ident) -> Vec<Field> {
pub fn filter_fields(fields: &[Field], field: &syn::Ident) -> Vec<Field> {
fields
.iter()
.filter_map(|(field2, ty)| {
@@ -99,12 +99,12 @@ pub fn filter_fields<'a>(fields: &'a [Field], field: &'a syn::Ident) -> Vec<Fiel
.collect()
}
pub fn fields_to_args<'a>(fields: &'a [Field]) -> TokenStream {
pub fn fields_to_args(fields: &[Field]) -> TokenStream {
let args = fields.iter().map(|(field, ty)| quote!(#field: #ty,));
quote!(#(#args)*)
}
pub fn build_struct<'a>(ident: &'a syn::Ident, fields: &'a [Field]) -> TokenStream {
pub fn build_struct(ident: &syn::Ident, fields: &[Field]) -> TokenStream {
let fields = fields.iter().map(|(field, _)| field);
quote!(#ident { #(#fields,)* })
}
@@ -170,7 +170,7 @@ pub fn type_params(generics: &syn::Generics) -> Vec<TokenStream> {
pub fn parse_option(ty: &syn::Type) -> Option<syn::Type> {
if let syn::Type::Path(ty_path) = ty {
if let Some(seg) = ty_path.path.segments.first() {
if seg.ident.to_string() == "Option" {
if &seg.ident == "Option" {
if let syn::PathArguments::AngleBracketed(args) = &seg.arguments {
if let Some(syn::GenericArgument::Type(ty)) = args.args.first() {
return Some(ty.clone())
@@ -191,10 +191,9 @@ pub struct Attrs<A> {
impl<A: Parse> Parse for Attrs<A> {
fn parse(input: ParseStream) -> syn::Result<Self> {
let content;
Ok(Self {
paren: syn::parenthesized!(content in input),
attrs: content.parse_terminated(A::parse)?,
})
let paren = syn::parenthesized!(content in input);
let attrs = content.parse_terminated(A::parse)?;
Ok(Self { paren, attrs })
}
}