2018-04-03 23:42:16 -07:00
|
|
|
use std::sync::{Arc, RwLock};
|
2017-08-13 07:13:03 -07:00
|
|
|
use std::ops;
|
2018-04-03 23:42:16 -07:00
|
|
|
use futures::{self, Future, Stream, Poll};
|
2017-08-13 07:13:03 -07:00
|
|
|
use futures::future::{JoinAll, join_all};
|
2017-08-31 08:32:34 -07:00
|
|
|
use tokio_timer::Timeout;
|
2017-08-12 11:03:48 -07:00
|
|
|
use web3::Transport;
|
2018-04-25 03:12:39 -07:00
|
|
|
use web3::types::{H256, U256, H520, Address, Bytes, FilterBuilder};
|
2017-08-31 08:32:34 -07:00
|
|
|
use api::{self, LogStream, ApiCall};
|
2017-08-13 07:13:03 -07:00
|
|
|
use app::App;
|
2018-02-02 06:59:54 -08:00
|
|
|
use contracts::foreign;
|
2017-08-24 09:42:19 -07:00
|
|
|
use util::web3_filter;
|
2017-08-13 07:15:14 -07:00
|
|
|
use database::Database;
|
2018-04-03 23:42:16 -07:00
|
|
|
use error::{Error, ErrorKind};
|
2018-02-02 06:59:54 -08:00
|
|
|
use message_to_mainnet::{MessageToMainnet, MESSAGE_LENGTH};
|
2018-04-25 03:12:39 -07:00
|
|
|
use transaction::prepare_raw_transaction;
|
|
|
|
use ethcore_transaction::{Transaction, Action};
|
|
|
|
use itertools::Itertools;
|
2017-08-12 11:03:48 -07:00
|
|
|
|
2017-10-10 02:02:46 -07:00
|
|
|
fn withdraws_filter(foreign: &foreign::ForeignBridge, address: Address) -> FilterBuilder {
|
|
|
|
let filter = foreign.events().withdraw().create_filter();
|
2017-08-23 10:09:51 -07:00
|
|
|
web3_filter(filter, address)
|
2017-08-21 08:32:37 -07:00
|
|
|
}
|
|
|
|
|
2018-02-02 06:59:54 -08:00
|
|
|
fn withdraw_submit_signature_payload(foreign: &foreign::ForeignBridge, withdraw_message: Vec<u8>, signature: H520) -> Bytes {
|
|
|
|
assert_eq!(withdraw_message.len(), MESSAGE_LENGTH, "ForeignBridge never accepts messages with len != {} bytes; qed", MESSAGE_LENGTH);
|
|
|
|
foreign.functions().submit_signature().input(signature.0.to_vec(), withdraw_message).into()
|
2017-08-21 08:32:37 -07:00
|
|
|
}
|
|
|
|
|
2017-08-13 07:15:14 -07:00
|
|
|
/// State of withdraw confirmation.
|
|
|
|
enum WithdrawConfirmState<T: Transport> {
|
|
|
|
/// Withdraw confirm is waiting for logs.
|
2017-08-12 11:03:48 -07:00
|
|
|
Wait,
|
2017-08-13 07:15:14 -07:00
|
|
|
/// Confirming withdraws.
|
2017-08-13 07:13:03 -07:00
|
|
|
ConfirmWithdraws {
|
2017-08-31 08:32:34 -07:00
|
|
|
future: JoinAll<Vec<Timeout<ApiCall<H256, T::Out>>>>,
|
2017-08-12 11:03:48 -07:00
|
|
|
block: u64,
|
|
|
|
},
|
2017-08-13 07:15:14 -07:00
|
|
|
/// All withdraws till given block has been confirmed.
|
2017-08-12 11:03:48 -07:00
|
|
|
Yield(Option<u64>),
|
|
|
|
}
|
|
|
|
|
2018-04-25 03:12:39 -07:00
|
|
|
pub fn create_withdraw_confirm<T: Transport + Clone>(app: Arc<App<T>>, init: &Database, foreign_balance: Arc<RwLock<Option<U256>>>, foreign_chain_id: u64, foreign_nonce: Arc<RwLock<Option<U256>>>) -> WithdrawConfirm<T> {
|
2017-08-13 07:15:14 -07:00
|
|
|
let logs_init = api::LogStreamInit {
|
|
|
|
after: init.checked_withdraw_confirm,
|
2017-10-10 02:02:46 -07:00
|
|
|
request_timeout: app.config.foreign.request_timeout,
|
|
|
|
poll_interval: app.config.foreign.poll_interval,
|
|
|
|
confirmations: app.config.foreign.required_confirmations,
|
|
|
|
filter: withdraws_filter(&app.foreign_bridge, init.foreign_contract_address.clone()),
|
2017-08-13 07:15:14 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
WithdrawConfirm {
|
2017-10-10 02:02:46 -07:00
|
|
|
logs: api::log_stream(app.connections.foreign.clone(), app.timer.clone(), logs_init),
|
2018-02-07 05:58:50 -08:00
|
|
|
foreign_contract: init.foreign_contract_address,
|
2017-08-13 07:15:14 -07:00
|
|
|
state: WithdrawConfirmState::Wait,
|
|
|
|
app,
|
2018-04-03 23:42:16 -07:00
|
|
|
foreign_balance,
|
2018-04-25 03:12:39 -07:00
|
|
|
foreign_nonce,
|
|
|
|
foreign_chain_id,
|
2017-08-13 07:15:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-12 11:03:48 -07:00
|
|
|
pub struct WithdrawConfirm<T: Transport> {
|
2017-08-13 07:13:03 -07:00
|
|
|
app: Arc<App<T>>,
|
|
|
|
logs: LogStream<T>,
|
2017-08-12 11:03:48 -07:00
|
|
|
state: WithdrawConfirmState<T>,
|
2017-10-10 02:02:46 -07:00
|
|
|
foreign_contract: Address,
|
2018-04-03 23:42:16 -07:00
|
|
|
foreign_balance: Arc<RwLock<Option<U256>>>,
|
2018-04-25 03:12:39 -07:00
|
|
|
foreign_nonce: Arc<RwLock<Option<U256>>>,
|
|
|
|
foreign_chain_id: u64,
|
2017-08-12 11:03:48 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
impl<T: Transport> Stream for WithdrawConfirm<T> {
|
|
|
|
type Item = u64;
|
|
|
|
type Error = Error;
|
|
|
|
|
|
|
|
fn poll(&mut self) -> Poll<Option<Self::Item>, Self::Error> {
|
2018-04-25 03:12:39 -07:00
|
|
|
// borrow checker...
|
|
|
|
let app = &self.app;
|
|
|
|
let gas = self.app.config.txs.withdraw_confirm.gas.into();
|
|
|
|
let gas_price = self.app.config.txs.withdraw_confirm.gas_price.into();
|
|
|
|
let contract = self.foreign_contract.clone();
|
|
|
|
let foreign = &self.app.config.foreign;
|
|
|
|
let chain_id = self.foreign_chain_id;
|
2017-08-13 07:13:03 -07:00
|
|
|
loop {
|
|
|
|
let next_state = match self.state {
|
|
|
|
WithdrawConfirmState::Wait => {
|
2018-04-28 10:03:38 -07:00
|
|
|
let foreign_balance = self.foreign_balance.read().unwrap();
|
|
|
|
if foreign_balance.is_none() {
|
|
|
|
warn!("foreign contract balance is unknown");
|
|
|
|
return Ok(futures::Async::NotReady);
|
|
|
|
}
|
|
|
|
let foreign_nonce = self.foreign_nonce.read().unwrap();
|
|
|
|
if foreign_nonce.is_none() {
|
|
|
|
warn!("foreign nonce is unknown");
|
|
|
|
return Ok(futures::Async::NotReady);
|
|
|
|
}
|
|
|
|
|
2017-08-13 07:13:03 -07:00
|
|
|
let item = try_stream!(self.logs.poll());
|
2018-01-19 05:47:13 -08:00
|
|
|
info!("got {} new withdraws to sign", item.logs.len());
|
2018-04-28 10:03:38 -07:00
|
|
|
let mut messages = item.logs
|
2017-08-13 07:13:03 -07:00
|
|
|
.into_iter()
|
2018-01-19 05:47:13 -08:00
|
|
|
.map(|log| {
|
|
|
|
info!("withdraw is ready for signature submission. tx hash {}", log.transaction_hash.unwrap());
|
2018-02-02 06:59:54 -08:00
|
|
|
Ok(MessageToMainnet::from_log(log)?.to_bytes())
|
2018-01-19 05:47:13 -08:00
|
|
|
})
|
2018-02-02 06:59:54 -08:00
|
|
|
.collect::<Result<Vec<_>, Error>>()?;
|
2017-08-13 07:13:03 -07:00
|
|
|
|
2018-04-28 10:03:38 -07:00
|
|
|
info!("signing");
|
|
|
|
|
|
|
|
let signatures = messages.clone()
|
2017-08-21 08:32:37 -07:00
|
|
|
.into_iter()
|
2018-04-28 10:03:38 -07:00
|
|
|
.map(|message|
|
|
|
|
app.keystore.sign(self.app.config.foreign.account, None, api::eth_data_hash(message)))
|
|
|
|
.map_results(|sig| H520::from(sig.into_electrum()))
|
|
|
|
.fold_results(vec![], |mut acc, sig| {
|
|
|
|
acc.push(sig);
|
|
|
|
acc
|
2017-08-31 08:32:34 -07:00
|
|
|
})
|
2018-04-28 10:03:38 -07:00
|
|
|
.map_err(|e| ErrorKind::SignError(e))?;
|
2017-08-13 07:13:03 -07:00
|
|
|
|
2018-04-28 10:03:38 -07:00
|
|
|
let block = item.to;
|
2018-04-03 23:42:16 -07:00
|
|
|
|
|
|
|
let balance_required = U256::from(self.app.config.txs.withdraw_confirm.gas) * U256::from(self.app.config.txs.withdraw_confirm.gas_price) * U256::from(signatures.len());
|
|
|
|
if balance_required > *foreign_balance.as_ref().unwrap() {
|
|
|
|
return Err(ErrorKind::InsufficientFunds.into())
|
|
|
|
}
|
|
|
|
|
2018-01-19 05:47:13 -08:00
|
|
|
info!("signing complete");
|
2018-02-02 06:59:54 -08:00
|
|
|
let confirmations = messages
|
2017-08-13 07:13:03 -07:00
|
|
|
.drain(ops::RangeFull)
|
|
|
|
.zip(signatures.into_iter())
|
2018-02-02 06:59:54 -08:00
|
|
|
.map(|(withdraw_message, signature)| {
|
|
|
|
withdraw_submit_signature_payload(&app.foreign_bridge, withdraw_message, signature)
|
|
|
|
})
|
2018-04-25 03:12:39 -07:00
|
|
|
.map(|payload| {
|
|
|
|
let tx = Transaction {
|
|
|
|
gas, gas_price,
|
|
|
|
value: U256::zero(),
|
|
|
|
data: payload.0,
|
|
|
|
nonce: foreign_nonce.unwrap(),
|
|
|
|
action: Action::Call(contract),
|
|
|
|
};
|
|
|
|
prepare_raw_transaction(tx, app, foreign, chain_id)
|
2017-08-13 07:13:03 -07:00
|
|
|
})
|
2018-04-25 03:12:39 -07:00
|
|
|
.map_results(|tx| {
|
2018-01-19 05:47:13 -08:00
|
|
|
info!("submitting signature");
|
2017-08-31 08:32:34 -07:00
|
|
|
app.timer.timeout(
|
2018-04-25 03:12:39 -07:00
|
|
|
api::send_raw_transaction(&app.connections.foreign, tx),
|
2017-10-10 02:02:46 -07:00
|
|
|
app.config.foreign.request_timeout)
|
2017-08-31 08:32:34 -07:00
|
|
|
})
|
2018-04-25 03:12:39 -07:00
|
|
|
.fold_results(vec![], |mut acc, tx| {
|
|
|
|
acc.push(tx);
|
|
|
|
acc
|
|
|
|
})?;
|
2017-08-13 07:13:03 -07:00
|
|
|
|
2018-01-19 05:47:13 -08:00
|
|
|
info!("submitting {} signatures", confirmations.len());
|
2017-08-13 07:13:03 -07:00
|
|
|
WithdrawConfirmState::ConfirmWithdraws {
|
|
|
|
future: join_all(confirmations),
|
|
|
|
block,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
WithdrawConfirmState::ConfirmWithdraws { ref mut future, block } => {
|
2017-08-31 08:32:34 -07:00
|
|
|
let _ = try_ready!(future.poll());
|
2018-01-19 05:47:13 -08:00
|
|
|
info!("submitting signatures complete");
|
2017-08-13 07:13:03 -07:00
|
|
|
WithdrawConfirmState::Yield(Some(block))
|
|
|
|
},
|
|
|
|
WithdrawConfirmState::Yield(ref mut block) => match block.take() {
|
2018-01-19 05:47:13 -08:00
|
|
|
None => {
|
|
|
|
info!("waiting for new withdraws that should get signed");
|
|
|
|
WithdrawConfirmState::Wait
|
|
|
|
},
|
2017-08-13 07:13:03 -07:00
|
|
|
some => return Ok(some.into()),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
self.state = next_state;
|
|
|
|
}
|
2017-08-12 11:03:48 -07:00
|
|
|
}
|
|
|
|
}
|