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

View File

@ -82,36 +82,29 @@ impl<D: Dispatcher + 'static> SigningQueueClient<D> {
} }
fn dispatch(&self, payload: RpcConfirmationPayload, default_account: DefaultAccount) -> BoxFuture<DispatchResult, Error> { fn dispatch(&self, payload: RpcConfirmationPayload, default_account: DefaultAccount) -> BoxFuture<DispatchResult, Error> {
let setup = move || { let accounts = take_weakf!(self.accounts);
let accounts = take_weak!(self.accounts); let default_account = match default_account {
let default_account = default_account; DefaultAccount::Provided(acc) => acc,
let default_account = match default_account { DefaultAccount::ForDapp(dapp) => accounts.default_address(dapp).ok().unwrap_or_default(),
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(); let dispatcher = self.dispatcher.clone();
future::done(setup()) let signer = take_weakf!(self.signer);
.and_then(move |(dispatcher, accounts, default_account)| { dispatch::from_rpc(payload, default_account, &dispatcher)
dispatch::from_rpc(payload, default_account, &dispatcher) .and_then(move |payload| {
.and_then(move |payload| { let sender = payload.sender();
let sender = payload.sender(); if accounts.is_unlocked(sender) {
if accounts.is_unlocked(sender) { dispatch::execute(dispatcher, &accounts, payload, dispatch::SignWith::Nothing)
dispatch::execute(dispatcher, &accounts, payload, dispatch::SignWith::Nothing) .map(|v| v.into_value())
.map(|v| v.into_value()) .map(DispatchResult::Value)
.map(DispatchResult::Value) .boxed()
.boxed() } else {
} else { future::done(
future::lazy(move || signer.add_request(payload)
take_weak!(weak_signer).add_request(payload) .map(DispatchResult::Promise)
.map(DispatchResult::Promise) .map_err(|_| errors::request_rejected_limit())
.map_err(|_| errors::request_rejected_limit()) ).boxed()
).boxed() }
}
})
}) })
.boxed() .boxed()
} }

View File

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

View File

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