poa-bridge/bridge/src/bridge/withdraw_confirm.rs

156 lines
5.0 KiB
Rust
Raw Normal View History

2017-08-13 07:13:03 -07:00
use std::sync::Arc;
use std::ops;
2017-08-12 11:03:48 -07:00
use futures::{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-02-14 03:13:11 -08:00
use web3::types::{H256, H520, Address, TransactionRequest, 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;
use contracts::foreign;
use util::web3_filter;
2017-08-13 07:15:14 -07:00
use database::Database;
2017-08-31 08:32:34 -07:00
use error::Error;
use message_to_mainnet::{MessageToMainnet, MESSAGE_LENGTH};
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)
}
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-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
/// Signing withdraws.
2017-08-25 07:27:55 -07:00
SignWithdraws {
messages: Vec<Vec<u8>>,
2017-08-31 08:32:34 -07:00
future: JoinAll<Vec<Timeout<ApiCall<H520, T::Out>>>>,
2017-08-13 07:13:03 -07:00
block: u64,
},
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>),
}
2017-08-13 07:15:14 -07:00
pub fn create_withdraw_confirm<T: Transport + Clone>(app: Arc<App<T>>, init: &Database) -> WithdrawConfirm<T> {
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),
foreign_contract: init.foreign_contract_address,
2017-08-13 07:15:14 -07:00
state: WithdrawConfirmState::Wait,
app,
}
}
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,
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> {
2017-08-13 07:13:03 -07:00
loop {
let next_state = match self.state {
WithdrawConfirmState::Wait => {
let item = try_stream!(self.logs.poll());
info!("got {} new withdraws to sign", item.logs.len());
let withdraw_messages = item.logs
2017-08-13 07:13:03 -07:00
.into_iter()
.map(|log| {
info!("withdraw is ready for signature submission. tx hash {}", log.transaction_hash.unwrap());
Ok(MessageToMainnet::from_log(log)?.to_bytes())
})
.collect::<Result<Vec<_>, Error>>()?;
2017-08-13 07:13:03 -07:00
let requests = withdraw_messages.clone()
.into_iter()
.map(|message| {
2017-08-31 08:32:34 -07:00
self.app.timer.timeout(
2018-02-12 01:11:39 -08:00
api::sign(&self.app.connections.foreign, self.app.config.foreign.account, Bytes(message)),
2017-10-10 02:02:46 -07:00
self.app.config.foreign.request_timeout)
2017-08-31 08:32:34 -07:00
})
2017-08-13 07:13:03 -07:00
.collect::<Vec<_>>();
info!("signing");
2017-08-25 07:27:55 -07:00
WithdrawConfirmState::SignWithdraws {
2017-08-13 07:13:03 -07:00
future: join_all(requests),
messages: withdraw_messages,
2017-08-13 07:13:03 -07:00
block: item.to,
}
},
WithdrawConfirmState::SignWithdraws { ref mut future, ref mut messages, block } => {
2017-08-31 08:32:34 -07:00
let signatures = try_ready!(future.poll());
info!("signing complete");
2017-08-13 07:13:03 -07:00
// borrow checker...
let app = &self.app;
2017-10-10 02:02:46 -07:00
let foreign_contract = &self.foreign_contract;
let confirmations = messages
2017-08-13 07:13:03 -07:00
.drain(ops::RangeFull)
.zip(signatures.into_iter())
.map(|(withdraw_message, signature)| {
withdraw_submit_signature_payload(&app.foreign_bridge, withdraw_message, signature)
})
2017-08-13 07:13:03 -07:00
.map(|payload| TransactionRequest {
from: app.config.foreign.account,
2017-10-10 02:02:46 -07:00
to: Some(foreign_contract.clone()),
gas: Some(app.config.txs.withdraw_confirm.gas.into()),
gas_price: Some(app.config.txs.withdraw_confirm.gas_price.into()),
value: None,
2017-08-13 07:13:03 -07:00
data: Some(payload),
nonce: None,
condition: None,
})
2017-08-31 08:32:34 -07:00
.map(|request| {
info!("submitting signature");
2017-08-31 08:32:34 -07:00
app.timer.timeout(
2017-10-10 02:02:46 -07:00
api::send_transaction(&app.connections.foreign, request),
app.config.foreign.request_timeout)
2017-08-31 08:32:34 -07:00
})
2017-08-13 07:13:03 -07:00
.collect::<Vec<_>>();
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());
info!("submitting signatures complete");
2017-08-13 07:13:03 -07:00
WithdrawConfirmState::Yield(Some(block))
},
WithdrawConfirmState::Yield(ref mut block) => match block.take() {
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
}
}