remove let inner pattern with take_weakf and try_bf

This commit is contained in:
Robert Habermeier 2017-02-09 20:38:43 +01:00
parent c83d27420c
commit 0d09a473a7
3 changed files with 101 additions and 122 deletions

View File

@ -92,10 +92,9 @@ impl<C: MiningBlockChainClient, M: MinerService> Dispatcher for FullDispatcher<C
fn fill_optional_fields(&self, request: TransactionRequest, default_sender: Address)
-> BoxFuture<FilledTransactionRequest, Error>
{
let inner = move || {
let (client, miner) = (take_weak!(self.client), take_weak!(self.miner));
let (client, miner) = (take_weakf!(self.client), take_weakf!(self.miner));
let request = request;
Ok(FilledTransactionRequest {
future::ok(FilledTransactionRequest {
from: request.from.unwrap_or(default_sender),
used_default_from: request.from.is_none(),
to: request.to,
@ -105,19 +104,16 @@ impl<C: MiningBlockChainClient, M: MinerService> Dispatcher for FullDispatcher<C
value: request.value.unwrap_or_else(|| 0.into()),
data: request.data.unwrap_or_else(Vec::new),
condition: request.condition,
})
};
future::done(inner()).boxed()
}).boxed()
}
fn sign(&self, accounts: &AccountProvider, filled: FilledTransactionRequest, password: SignWith)
-> BoxFuture<WithToken<SignedTransaction>, Error>
{
let inner = move || {
let (client, miner) = (take_weak!(self.client), take_weak!(self.miner));
let (client, miner) = (take_weakf!(self.client), take_weakf!(self.miner));
let network_id = client.signing_network_id();
let address = filled.from;
let signed_transaction = {
future::ok({
let t = Transaction {
nonce: filled.nonce
.or_else(|| miner
@ -133,16 +129,13 @@ impl<C: MiningBlockChainClient, M: MinerService> Dispatcher for FullDispatcher<C
};
let hash = t.hash(network_id);
let signature = signature(accounts, address, hash, password)?;
let signature = try_bf!(signature(accounts, address, hash, password));
signature.map(|sig| {
SignedTransaction::new(t.with_signature(sig, network_id))
.expect("Transaction was signed by AccountsProvider; it never produces invalid signatures; qed")
})
};
Ok(signed_transaction)
};
future::done(inner()).boxed()
}).boxed()
}
fn dispatch_transaction(&self, signed_transaction: PendingTransaction) -> Result<H256, Error> {

View File

@ -359,133 +359,108 @@ impl<C, SN: ?Sized, S: ?Sized, M, EM> Eth for EthClient<C, SN, S, M, EM> where
fn balance(&self, address: RpcH160, num: Trailing<BlockNumber>) -> BoxFuture<RpcU256, Error> {
let address = address.into();
let inner = || {
match num.0.clone() {
BlockNumber::Pending => Ok(take_weak!(self.miner).balance(&*take_weak!(self.client), &address).into()),
let res = match num.0.clone() {
BlockNumber::Pending => Ok(take_weakf!(self.miner).balance(&*take_weakf!(self.client), &address).into()),
id => {
let client = take_weak!(self.client);
let client = take_weakf!(self.client);
check_known(&*client, id.clone())?;
try_bf!(check_known(&*client, id.clone()));
match client.balance(&address, id.into()) {
Some(balance) => Ok(balance.into()),
None => Err(errors::state_pruned()),
}
}
}
};
future::done(inner()).boxed()
future::done(res).boxed()
}
fn storage_at(&self, address: RpcH160, pos: RpcU256, num: Trailing<BlockNumber>) -> BoxFuture<RpcH256, Error> {
let address: Address = RpcH160::into(address);
let position: U256 = RpcU256::into(pos);
let inner = || {
match num.0.clone() {
BlockNumber::Pending => Ok(take_weak!(self.miner).storage_at(&*take_weak!(self.client), &address, &H256::from(position)).into()),
let res = match num.0.clone() {
BlockNumber::Pending => Ok(take_weakf!(self.miner).storage_at(&*take_weakf!(self.client), &address, &H256::from(position)).into()),
id => {
let client = take_weak!(self.client);
let client = take_weakf!(self.client);
check_known(&*client, id.clone())?;
try_bf!(check_known(&*client, id.clone()));
match client.storage_at(&address, &H256::from(position), id.into()) {
Some(s) => Ok(s.into()),
None => Err(errors::state_pruned()),
}
}
}
};
future::done(inner()).boxed()
future::done(res).boxed()
}
fn transaction_count(&self, address: RpcH160, num: Trailing<BlockNumber>) -> BoxFuture<RpcU256, Error> {
let address: Address = RpcH160::into(address);
let inner = move || {
match num.0.clone() {
BlockNumber::Pending => Ok(take_weak!(self.miner).nonce(&*take_weak!(self.client), &address).into()),
let res = match num.0.clone() {
BlockNumber::Pending => Ok(take_weakf!(self.miner).nonce(&*take_weakf!(self.client), &address).into()),
id => {
let client = take_weak!(self.client);
let client = take_weakf!(self.client);
check_known(&*client, id.clone())?;
try_bf!(check_known(&*client, id.clone()));
match client.nonce(&address, id.into()) {
Some(nonce) => Ok(nonce.into()),
None => Err(errors::state_pruned()),
}
}
}
};
future::done(inner()).boxed()
future::done(res).boxed()
}
fn block_transaction_count_by_hash(&self, hash: RpcH256) -> BoxFuture<Option<RpcU256>, Error> {
let inner = || {
Ok(take_weak!(self.client).block(BlockId::Hash(hash.into()))
.map(|block| block.transactions_count().into()))
};
future::done(inner()).boxed()
future::ok(take_weakf!(self.client).block(BlockId::Hash(hash.into()))
.map(|block| block.transactions_count().into())).boxed()
}
fn block_transaction_count_by_number(&self, num: BlockNumber) -> BoxFuture<Option<RpcU256>, Error> {
let inner = || {
match num {
BlockNumber::Pending => Ok(Some(
take_weak!(self.miner).status().transactions_in_pending_block.into()
)),
_ => Ok(
take_weak!(self.client).block(num.into())
future::ok(match num {
BlockNumber::Pending => Some(
take_weakf!(self.miner).status().transactions_in_pending_block.into()
),
_ =>
take_weakf!(self.client).block(num.into())
.map(|block| block.transactions_count().into())
)
}
};
future::done(inner()).boxed()
}).boxed()
}
fn block_uncles_count_by_hash(&self, hash: RpcH256) -> BoxFuture<Option<RpcU256>, Error> {
let inner = || {
Ok(take_weak!(self.client).block(BlockId::Hash(hash.into()))
future::ok(take_weakf!(self.client).block(BlockId::Hash(hash.into()))
.map(|block| block.uncles_count().into()))
};
future::done(inner()).boxed()
.boxed()
}
fn block_uncles_count_by_number(&self, num: BlockNumber) -> BoxFuture<Option<RpcU256>, Error> {
let inner = || {
match num {
BlockNumber::Pending => Ok(Some(0.into())),
_ => Ok(
take_weak!(self.client).block(num.into())
.map(|block| block.uncles_count().into())
future::ok(match num {
BlockNumber::Pending => Some(0.into()),
_ => take_weakf!(self.client).block(num.into())
.map(|block| block.uncles_count().into()
),
}
};
future::done(inner()).boxed()
}).boxed()
}
fn code_at(&self, address: RpcH160, num: Trailing<BlockNumber>) -> BoxFuture<Bytes, Error> {
let address: Address = RpcH160::into(address);
let inner = || {
match num.0.clone() {
BlockNumber::Pending => Ok(take_weak!(self.miner).code(&*take_weak!(self.client), &address).map_or_else(Bytes::default, Bytes::new)),
let res = match num.0.clone() {
BlockNumber::Pending => Ok(take_weakf!(self.miner).code(&*take_weakf!(self.client), &address).map_or_else(Bytes::default, Bytes::new)),
id => {
let client = take_weak!(self.client);
let client = take_weakf!(self.client);
check_known(&*client, id.clone())?;
try_bf!(check_known(&*client, id.clone()));
match client.code(&address, id.into()) {
Some(code) => Ok(code.map_or_else(Bytes::default, Bytes::new)),
None => Err(errors::state_pruned()),
}
}
}
};
future::done(inner()).boxed()
future::done(res).boxed()
}
fn block_by_hash(&self, hash: RpcH256, include_txs: bool) -> BoxFuture<Option<RichBlock>, Error> {

View File

@ -38,6 +38,17 @@ macro_rules! take_weakf {
}
}
// short for "try_boxfuture"
// unwrap a result, returning a BoxFuture<_, Err> on failure.
macro_rules! try_bf {
($res: expr) => {
match $res {
Ok(val) => val,
Err(e) => return ::futures::future::err(e.into()).boxed(),
}
}
}
#[macro_use]
mod helpers;
mod impls;