Skip to content

Commit

Permalink
Fetch doesn't fail.
Browse files Browse the repository at this point in the history
  • Loading branch information
dvc94ch committed May 12, 2020
1 parent 3356baa commit b3d2f2e
Show file tree
Hide file tree
Showing 7 changed files with 31 additions and 37 deletions.
8 changes: 4 additions & 4 deletions proc-macro/src/store.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,7 +113,7 @@ pub fn store(s: Structure) -> TokenStream {
fn #store<'a>(
&'a self,
#args
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<Option<#ret>, #subxt::Error>> + Send + 'a>>;
) -> 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>
Expand All @@ -125,7 +125,7 @@ pub fn store(s: Structure) -> TokenStream {
fn #store<'a>(
&'a self,
#args
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<Option<#ret>, #subxt::Error>> + Send + 'a>> {
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<#ret, #subxt::Error>> + Send + 'a>> {
let #marker = core::marker::PhantomData::<T>;
Box::pin(self.fetch(#build_struct, None))
}
Expand Down Expand Up @@ -169,7 +169,7 @@ mod tests {
fn account<'a>(
&'a self,
account_id: &'a <T as System>::AccountId,
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<Option<AccountData<T::Balance> >, substrate_subxt::Error>> + Send + 'a>>;
) -> 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>
Expand All @@ -181,7 +181,7 @@ mod tests {
fn account<'a>(
&'a self,
account_id: &'a <T as System>::AccountId,
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<Option<AccountData<T::Balance> >, substrate_subxt::Error>> + Send + 'a>>
) -> core::pin::Pin<Box<dyn core::future::Future<Output = Result<AccountData<T::Balance>, substrate_subxt::Error>> + Send + 'a>>
{
let _ = core::marker::PhantomData::<T>;
Box::pin(self.fetch(AccountStore { account_id, }, None))
Expand Down
23 changes: 8 additions & 15 deletions proc-macro/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,21 +322,18 @@ impl Step {
.or(test_state)
.map(|state| {
let State {
state_name,
state,
state_param,
state_name,
state,
state_param,
} = state;
let expect_state = state_name
.iter()
.map(|state| format!("failed to fetch state {}", state.to_string()));
let state_struct = quote! {
struct State<#(#state_param),*> {
#(#state_name: #state_param,)*
}
};
let build_struct = quote! {
#(
let #state_name = client.fetch(#state, None).await.unwrap().expect(#expect_state);
let #state_name = client.fetch(#state, None).await.unwrap();
)*
State { #(#state_name),* }
};
Expand Down Expand Up @@ -489,13 +486,11 @@ mod tests {
let alice = client
.fetch(AccountStore { account_id: &alice }, None)
.await
.unwrap()
.expect("failed to fetch state alice");
.unwrap();
let bob = client
.fetch(AccountStore { account_id: &bob }, None)
.await
.unwrap()
.expect("failed to fetch state bob");
.unwrap();
State { alice, bob }
};

Expand Down Expand Up @@ -525,13 +520,11 @@ mod tests {
let alice = client
.fetch(AccountStore { account_id: &alice }, None)
.await
.unwrap()
.expect("failed to fetch state alice");
.unwrap();
let bob = client
.fetch(AccountStore { account_id: &bob }, None)
.await
.unwrap()
.expect("failed to fetch state bob");
.unwrap();
State { alice, bob }
};

Expand Down
8 changes: 4 additions & 4 deletions proc-macro/tests/balances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ async fn transfer_balance_example() -> Result<(), Box<dyn std::error::Error>> {
let alice = AccountKeyring::Alice.to_account_id();
let bob = AccountKeyring::Bob.to_account_id();

let alice_account = client.account(&alice).await?.unwrap_or_default();
let bob_account = client.account(&bob).await?.unwrap_or_default();
let alice_account = client.account(&alice).await?;
let bob_account = client.account(&bob).await?;
let pre = (alice_account, bob_account);

let builder = client.xt(AccountKeyring::Alice.pair(), None).await?;
Expand All @@ -137,8 +137,8 @@ async fn transfer_balance_example() -> Result<(), Box<dyn std::error::Error>> {
})
);

let alice_account = client.account(&alice).await?.unwrap_or_default();
let bob_account = client.account(&bob).await?.unwrap_or_default();
let alice_account = client.account(&alice).await?;
let bob_account = client.account(&bob).await?;
let post = (alice_account, bob_account);

assert_eq!(pre.0.free, post.0.free - 10_000);
Expand Down
4 changes: 2 additions & 2 deletions src/frame/balances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ mod tests {
async fn test_state_total_issuance() {
env_logger::try_init().ok();
let client = test_client().await;
let total_issuance = client.total_issuance().await.unwrap().unwrap();
let total_issuance = client.total_issuance().await.unwrap();
assert_ne!(total_issuance, 0);
}

Expand All @@ -156,7 +156,7 @@ mod tests {
env_logger::try_init().ok();
let client = test_client().await;
let account = AccountKeyring::Alice.to_account_id();
let info = client.account(&account).await.unwrap().unwrap();
let info = client.account(&account).await.unwrap();
assert_ne!(info.data.free, 0);
}
}
7 changes: 2 additions & 5 deletions src/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,14 +47,11 @@ pub trait Store<T>: Encode {
/// Returns the `StorageKey`.
fn key(&self, metadata: &Metadata) -> Result<StorageKey, MetadataError>;
/// Returns the default value.
fn default(
&self,
metadata: &Metadata,
) -> Result<Option<Self::Returns>, MetadataError> {
fn default(&self, metadata: &Metadata) -> Result<Self::Returns, MetadataError> {
Ok(metadata
.module(Self::MODULE)?
.storage(Self::FIELD)?
.default())
.default()?)
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,11 +217,11 @@ impl<T: System, S, E> Client<T, S, E> {
&self,
store: F,
hash: Option<T::Hash>,
) -> Result<Option<F::Returns>, Error> {
) -> Result<F::Returns, Error> {
let key = store.key(&self.metadata)?;
let value = self.rpc.storage::<F::Returns>(key, hash).await?;
if let Some(v) = value {
Ok(Some(v))
Ok(v)
} else {
Ok(store.default(&self.metadata)?)
}
Expand Down Expand Up @@ -340,7 +340,7 @@ where
account_id: &<T as System>::AccountId,
call: C,
) -> Result<SignedPayload<Encoded, <E as SignedExtra<T>>::Extra>, Error> {
let account_nonce = self.account(account_id).await?.unwrap().nonce;
let account_nonce = self.account(account_id).await?.nonce;
let version = self.runtime_version.spec_version;
let genesis_hash = self.genesis_hash;
let call = self
Expand All @@ -367,7 +367,7 @@ where
let account_id = S::Signer::from(signer.public()).into_account();
let nonce = match nonce {
Some(nonce) => nonce,
None => self.account(&account_id).await?.unwrap().nonce,
None => self.account(&account_id).await?.nonce,
};

let genesis_hash = self.genesis_hash;
Expand Down
10 changes: 7 additions & 3 deletions src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use std::{
use codec::{
Decode,
Encode,
Error as CodecError,
};

use frame_metadata::{
Expand Down Expand Up @@ -63,6 +64,9 @@ pub enum MetadataError {
/// Storage type does not match requested type.
#[error("Storage type error")]
StorageTypeError,
/// Default error.
#[error("Failed to decode default: {0}")]
DefaultError(CodecError),
}

/// Runtime metadata.
Expand Down Expand Up @@ -218,9 +222,9 @@ impl StorageMetadata {
bytes
}

pub fn default<V: Decode>(&self) -> Option<V> {
// substrate handles the default different for A => B vs A => Option<B>
Decode::decode(&mut &self.default[..]).ok()
pub fn default<V: Decode>(&self) -> Result<V, MetadataError> {
Decode::decode(&mut &self.default[..])
.map_err(|err| MetadataError::DefaultError(err))
}

pub fn hash(hasher: &StorageHasher, bytes: &[u8]) -> Vec<u8> {
Expand Down

0 comments on commit b3d2f2e

Please sign in to comment.