simplify code

This commit is contained in:
Robert Habermeier 2017-02-09 15:01:15 +01:00
parent bcf0e23a4b
commit bce6bf92d9
4 changed files with 46 additions and 64 deletions

View File

@ -95,15 +95,9 @@ impl<D: Dispatcher + 'static> Personal for PersonalClient<D> {
}
fn send_transaction(&self, meta: Metadata, request: TransactionRequest, password: String) -> BoxFuture<RpcH256, Error> {
let setup = || {
let dispatcher = self.dispatcher.clone();
let accounts = take_weak!(self.accounts);
let accounts = take_weakf!(self.accounts);
Ok((accounts, dispatcher))
};
future::done(setup())
.and_then(move |(accounts, dispatcher)| {
let default = match request.from.as_ref() {
Some(account) => Ok(account.clone().into()),
None => accounts
@ -111,19 +105,20 @@ impl<D: Dispatcher + 'static> Personal for PersonalClient<D> {
.map_err(|e| errors::account("Cannot find default account.", e)),
};
let dis = dispatcher.clone();
future::done(default)
.and_then(move |default| dis.fill_optional_fields(request.into(), default))
.map(move |tx| (tx, accounts, dispatcher))
})
.and_then(move |(filled, accounts, dispatcher)| {
let default = match default {
Ok(default) => default,
Err(e) => return future::err(e).boxed(),
};
dispatcher.fill_optional_fields(request.into(), default)
.and_then(move |filled| {
let condition = filled.condition.clone().map(Into::into);
dispatcher.sign(&accounts, filled, SignWith::Password(password))
.map(|tx| tx.into_value())
.map(move |tx| PendingTransaction::new(tx, condition))
.map(move |tx| (tx, dispatcher))
})
.and_then(move |(pending_tx, dispatcher)| {
.and_then(|(pending_tx, dispatcher)| {
let network_id = pending_tx.network_id();
trace!(target: "miner", "send_transaction: dispatching tx: {} for network ID {:?}",
::rlp::encode(&*pending_tx).to_vec().pretty(), network_id);

View File

@ -82,20 +82,14 @@ impl<D: Dispatcher + 'static> SigningQueueClient<D> {
}
fn dispatch(&self, payload: RpcConfirmationPayload, default_account: DefaultAccount) -> BoxFuture<DispatchResult, Error> {
let setup = move || {
let accounts = take_weak!(self.accounts);
let default_account = default_account;
let accounts = take_weakf!(self.accounts);
let default_account = match default_account {
DefaultAccount::Provided(acc) => acc,
DefaultAccount::ForDapp(dapp) => accounts.default_address(dapp).ok().unwrap_or_default(),
};
Ok((self.dispatcher.clone(), accounts, default_account))
};
let weak_signer = self.signer.clone();
future::done(setup())
.and_then(move |(dispatcher, accounts, default_account)| {
let dispatcher = self.dispatcher.clone();
let signer = take_weakf!(self.signer);
dispatch::from_rpc(payload, default_account, &dispatcher)
.and_then(move |payload| {
let sender = payload.sender();
@ -105,14 +99,13 @@ impl<D: Dispatcher + 'static> SigningQueueClient<D> {
.map(DispatchResult::Value)
.boxed()
} else {
future::lazy(move ||
take_weak!(weak_signer).add_request(payload)
future::done(
signer.add_request(payload)
.map(DispatchResult::Promise)
.map_err(|_| errors::request_rejected_limit())
).boxed()
}
})
})
.boxed()
}
}

View File

@ -52,26 +52,18 @@ impl<D: Dispatcher + 'static> SigningUnsafeClient<D> {
}
fn handle(&self, payload: RpcConfirmationPayload, account: DefaultAccount) -> BoxFuture<RpcConfirmationResponse, Error> {
let setup = move || {
let accounts = take_weak!(self.accounts);
let default_account = account;
let default_account = match default_account {
let accounts = take_weakf!(self.accounts);
let default = match account {
DefaultAccount::Provided(acc) => acc,
DefaultAccount::ForDapp(dapp) => accounts.default_address(dapp).ok().unwrap_or_default(),
};
Ok((accounts, default_account))
};
let dis = self.dispatcher.clone();
future::done(setup())
.and_then(move |(accounts, default)| {
dispatch::from_rpc(payload, default, &dis)
.and_then(move |payload| {
dispatch::execute(dis, &accounts, payload, dispatch::SignWith::Nothing)
})
.map(|v| v.into_value())
})
.boxed()
}
}

View File

@ -18,6 +18,7 @@
//!
//! Compliant with ethereum rpc.
// Upgrade a weak pointer, returning an error on failure.
macro_rules! take_weak {
($weak: expr) => {
match $weak.upgrade() {
@ -27,11 +28,12 @@ macro_rules! take_weak {
}
}
// Upgrade a weak pointer, returning an error leaf-future on failure.
macro_rules! take_weakf {
($weak: expr) => {
match $weak.upgrade() {
Some(arc) => arc,
None => return ::futures::future::err(Error::internal_error()),
None => return ::futures::future::err(Error::internal_error()).boxed(),
}
}
}