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
+70 -26
View File
@@ -110,36 +110,56 @@ pub struct TransferEvent<T: Balances> {
mod tests {
use super::*;
use crate::{
system::{
AccountStore,
AccountStoreExt,
error::{
Error,
RuntimeError,
},
tests::test_client,
signer::{
PairSigner,
Signer,
},
system::AccountStoreExt,
tests::{
test_client,
TestRuntime,
},
};
use sp_core::{
sr25519::Pair,
Pair as _,
};
use sp_keyring::AccountKeyring;
subxt_test!({
name: test_transfer,
step: {
state: {
alice: AccountStore { account_id: &alice },
bob: AccountStore { account_id: &bob },
},
call: TransferCall {
to: &bob.clone().into(),
amount: 10_000,
},
event: TransferEvent {
from: alice.clone(),
to: bob.clone(),
amount: 10_000,
},
assert: {
assert!(pre.alice.data.free - 10_000 >= post.alice.data.free);
assert_eq!(pre.bob.data.free + 10_000, post.bob.data.free);
},
},
});
#[async_std::test]
async fn test_transfer() {
env_logger::try_init().ok();
let alice = PairSigner::<TestRuntime, _>::new(AccountKeyring::Alice.pair());
let bob = PairSigner::<TestRuntime, _>::new(AccountKeyring::Bob.pair());
let (client, _) = test_client().await;
let alice_pre = client.account(alice.account_id(), None).await.unwrap();
let bob_pre = client.account(bob.account_id(), None).await.unwrap();
let event = client
.transfer_and_watch(&alice, &bob.account_id(), 10_000)
.await
.unwrap()
.transfer()
.unwrap()
.unwrap();
let expected_event = TransferEvent {
from: alice.account_id().clone(),
to: bob.account_id().clone(),
amount: 10_000,
};
assert_eq!(event, expected_event);
let alice_post = client.account(alice.account_id(), None).await.unwrap();
let bob_post = client.account(bob.account_id(), None).await.unwrap();
assert!(alice_pre.data.free - 10_000 >= alice_post.data.free);
assert_eq!(bob_pre.data.free + 10_000, bob_post.data.free);
}
#[async_std::test]
async fn test_state_total_issuance() {
@@ -157,4 +177,28 @@ mod tests {
let info = client.account(&account, None).await.unwrap();
assert_ne!(info.data.free, 0);
}
#[async_std::test]
async fn test_transfer_error() {
env_logger::try_init().ok();
let alice = PairSigner::new(AccountKeyring::Alice.pair());
let hans = PairSigner::new(Pair::generate().0);
let (client, _) = test_client().await;
client
.transfer_and_watch(&alice, hans.account_id(), 100_000_000_000)
.await
.unwrap();
let res = client
.transfer_and_watch(&hans, alice.account_id(), 100_000_000_000)
.await;
if let Err(Error::Runtime(error)) = res {
let error2 = RuntimeError {
module: "Balances".into(),
error: "InsufficientBalance".into(),
};
assert_eq!(error, error2);
} else {
panic!("expected an error");
}
}
}
+1
View File
@@ -31,6 +31,7 @@ use sp_core::storage::StorageKey;
pub mod balances;
pub mod contracts;
pub mod sudo;
pub mod system;
/// Store trait.
+78
View File
@@ -0,0 +1,78 @@
// Copyright 2019-2020 Parity Technologies (UK) Ltd.
// This file is part of substrate-subxt.
//
// subxt is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// subxt is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with substrate-subxt. If not, see <http://www.gnu.org/licenses/>.
//! Implements support for the frame_sudo module.
use crate::{
frame::system::{
System,
SystemEventsDecoder,
},
Encoded,
};
use codec::Encode;
use core::marker::PhantomData;
/// The subset of the `frame_sudo::Trait` that a client must implement.
#[module]
pub trait Sudo: System {}
/// Execute a transaction with sudo permissions.
#[derive(Clone, Debug, Eq, PartialEq, Call, Encode)]
pub struct SudoCall<'a, T: Sudo> {
/// Runtime marker.
pub _runtime: PhantomData<T>,
/// Encoded transaction.
pub call: &'a Encoded,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::{
error::Error,
frame::balances::TransferCall,
signer::PairSigner,
tests::{
test_client,
TestRuntime,
},
};
use sp_keyring::AccountKeyring;
#[async_std::test]
async fn test_sudo() {
env_logger::try_init().ok();
let alice = PairSigner::<TestRuntime, _>::new(AccountKeyring::Alice.pair());
let (client, _) = test_client().await;
let call = client
.encode(TransferCall {
to: &AccountKeyring::Bob.to_account_id(),
amount: 10_000,
})
.unwrap();
let res = client.sudo_and_watch(&alice, &call).await;
assert!(
if let Err(Error::BadOrigin) = res {
true
} else {
false
}
);
}
}
+41 -15
View File
@@ -155,21 +155,6 @@ pub struct SetCodeCall<'a, T: System> {
pub code: &'a [u8],
}
/// Event for the System module.
#[derive(Clone, Debug, Eq, PartialEq, Decode)]
pub enum SystemEvent<T: System> {
/// An extrinsic completed successfully.
ExtrinsicSuccess(DispatchInfo),
/// An extrinsic failed.
ExtrinsicFailed(DispatchError, DispatchInfo),
/// `:code` was updated.
CodeUpdated,
/// A new account was created.
NewAccount(T::AccountId),
/// An account was reaped.
KilledAccount(T::AccountId),
}
/// A phase of a block's execution.
#[derive(Clone, Debug, Eq, PartialEq, Decode)]
pub enum Phase {
@@ -178,3 +163,44 @@ pub enum Phase {
/// The end.
Finalization,
}
/// An extrinsic completed successfully.
#[derive(Clone, Debug, Eq, PartialEq, Event, Decode)]
pub struct ExtrinsicSuccessEvent<T: System> {
/// Runtime marker.
pub _runtime: PhantomData<T>,
/// The dispatch info.
pub info: DispatchInfo,
}
/// An extrinsic failed.
#[derive(Clone, Debug, Eq, PartialEq, Event, Decode)]
pub struct ExtrinsicFailedEvent<T: System> {
/// Runtime marker.
pub _runtime: PhantomData<T>,
/// The dispatch error.
pub error: DispatchError,
/// The dispatch info.
pub info: DispatchInfo,
}
/// `:code` was updated.
#[derive(Clone, Debug, Eq, PartialEq, Event, Decode)]
pub struct CodeUpdatedEvent<T: System> {
/// Runtime marker.
pub _runtime: PhantomData<T>,
}
/// A new account was created.
#[derive(Clone, Debug, Eq, PartialEq, Event, Decode)]
pub struct NewAccountEvent<T: System> {
/// Created account id.
pub account: T::AccountId,
}
/// An account was reaped.
#[derive(Clone, Debug, Eq, PartialEq, Event, Decode)]
pub struct KilledAccountEvent<T: System> {
/// Killed account id.
pub account: T::AccountId,
}