simplify code
This commit is contained in:
parent
bcf0e23a4b
commit
bce6bf92d9
@ -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> {
|
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_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() {
|
let default = match request.from.as_ref() {
|
||||||
Some(account) => Ok(account.clone().into()),
|
Some(account) => Ok(account.clone().into()),
|
||||||
None => accounts
|
None => accounts
|
||||||
@ -111,19 +105,20 @@ impl<D: Dispatcher + 'static> Personal for PersonalClient<D> {
|
|||||||
.map_err(|e| errors::account("Cannot find default account.", e)),
|
.map_err(|e| errors::account("Cannot find default account.", e)),
|
||||||
};
|
};
|
||||||
|
|
||||||
let dis = dispatcher.clone();
|
let default = match default {
|
||||||
future::done(default)
|
Ok(default) => default,
|
||||||
.and_then(move |default| dis.fill_optional_fields(request.into(), default))
|
Err(e) => return future::err(e).boxed(),
|
||||||
.map(move |tx| (tx, accounts, dispatcher))
|
};
|
||||||
})
|
|
||||||
.and_then(move |(filled, accounts, dispatcher)| {
|
dispatcher.fill_optional_fields(request.into(), default)
|
||||||
|
.and_then(move |filled| {
|
||||||
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);
|
||||||
|
@ -82,20 +82,14 @@ 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 = default_account;
|
|
||||||
let default_account = match default_account {
|
let default_account = match default_account {
|
||||||
DefaultAccount::Provided(acc) => acc,
|
DefaultAccount::Provided(acc) => acc,
|
||||||
DefaultAccount::ForDapp(dapp) => accounts.default_address(dapp).ok().unwrap_or_default(),
|
DefaultAccount::ForDapp(dapp) => accounts.default_address(dapp).ok().unwrap_or_default(),
|
||||||
};
|
};
|
||||||
|
|
||||||
Ok((self.dispatcher.clone(), accounts, default_account))
|
let dispatcher = self.dispatcher.clone();
|
||||||
};
|
let signer = take_weakf!(self.signer);
|
||||||
|
|
||||||
let weak_signer = self.signer.clone();
|
|
||||||
future::done(setup())
|
|
||||||
.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();
|
||||||
@ -105,14 +99,13 @@ impl<D: Dispatcher + 'static> SigningQueueClient<D> {
|
|||||||
.map(DispatchResult::Value)
|
.map(DispatchResult::Value)
|
||||||
.boxed()
|
.boxed()
|
||||||
} else {
|
} else {
|
||||||
future::lazy(move ||
|
future::done(
|
||||||
take_weak!(weak_signer).add_request(payload)
|
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()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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;
|
|
||||||
let default_account = match default_account {
|
|
||||||
DefaultAccount::Provided(acc) => acc,
|
DefaultAccount::Provided(acc) => acc,
|
||||||
DefaultAccount::ForDapp(dapp) => accounts.default_address(dapp).ok().unwrap_or_default(),
|
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())
|
|
||||||
.and_then(move |(accounts, default)| {
|
|
||||||
dispatch::from_rpc(payload, default, &dis)
|
dispatch::from_rpc(payload, default, &dis)
|
||||||
.and_then(move |payload| {
|
.and_then(move |payload| {
|
||||||
dispatch::execute(dis, &accounts, payload, dispatch::SignWith::Nothing)
|
dispatch::execute(dis, &accounts, payload, dispatch::SignWith::Nothing)
|
||||||
})
|
})
|
||||||
.map(|v| v.into_value())
|
.map(|v| v.into_value())
|
||||||
})
|
|
||||||
.boxed()
|
.boxed()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -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(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user