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-21 08:32:37 -07:00
|
|
|
use ethabi::RawLog;
|
2017-08-12 11:03:48 -07:00
|
|
|
use web3::Transport;
|
|
|
|
use web3::helpers::CallResult;
|
2017-08-21 08:32:37 -07:00
|
|
|
use web3::types::{H256, H520, Address, TransactionRequest, Log, Bytes, FilterBuilder};
|
2017-08-13 07:13:03 -07:00
|
|
|
use api::{self, LogStream};
|
|
|
|
use app::App;
|
2017-08-21 08:32:37 -07:00
|
|
|
use contracts::{testnet, web3_topic};
|
2017-08-13 07:15:14 -07:00
|
|
|
use database::Database;
|
2017-08-13 07:13:03 -07:00
|
|
|
use error::{Error, ErrorKind};
|
2017-08-12 11:03:48 -07:00
|
|
|
|
2017-08-21 08:32:37 -07:00
|
|
|
fn withdraws_filter(testnet: &testnet::KovanBridge, address: Address) -> FilterBuilder {
|
|
|
|
let filter = testnet.events().withdraw().create_filter();
|
|
|
|
let t0 = web3_topic(filter.topic0);
|
|
|
|
let t1 = web3_topic(filter.topic1);
|
|
|
|
let t2 = web3_topic(filter.topic2);
|
|
|
|
let t3 = web3_topic(filter.topic3);
|
|
|
|
FilterBuilder::default()
|
|
|
|
.address(vec![address])
|
|
|
|
.topics(t0, t1, t2, t3)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn withdraw_confirm_payload(testnet: &testnet::KovanBridge, log: Log) -> Result<Bytes, Error> {
|
|
|
|
let raw_log = RawLog {
|
|
|
|
topics: log.topics.into_iter().map(|t| t.0).collect(),
|
|
|
|
data: log.data.0,
|
|
|
|
};
|
|
|
|
let withdraw_log = testnet.events().withdraw().parse_log(raw_log)?;
|
|
|
|
let hash = log.transaction_hash.expect("log to be mined and contain `transaction_hash`");
|
|
|
|
let mut result = vec![0u8; 84];
|
|
|
|
result[0..20].copy_from_slice(&withdraw_log.recipient);
|
|
|
|
result[20..52].copy_from_slice(&withdraw_log.value);
|
|
|
|
result[52..84].copy_from_slice(&hash);
|
|
|
|
Ok(result.into())
|
|
|
|
}
|
|
|
|
|
|
|
|
fn withdraw_collect_signatures_payload(_testnet: &testnet::KovanBridge, _withdraw_payload: Bytes, _signature: H520) -> Bytes {
|
|
|
|
// TODO: this is likely to change
|
|
|
|
unimplemented!();
|
|
|
|
}
|
|
|
|
|
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-13 07:13:03 -07:00
|
|
|
SignWithraws {
|
2017-08-21 08:32:37 -07:00
|
|
|
withdraws: Vec<Bytes>,
|
2017-08-13 07:13:03 -07:00
|
|
|
future: JoinAll<Vec<CallResult<H520, T::Out>>>,
|
|
|
|
block: u64,
|
|
|
|
},
|
2017-08-13 07:15:14 -07:00
|
|
|
/// Confirming withdraws.
|
2017-08-13 07:13:03 -07:00
|
|
|
ConfirmWithdraws {
|
2017-08-12 11:03:48 -07:00
|
|
|
future: JoinAll<Vec<CallResult<H256, T::Out>>>,
|
|
|
|
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,
|
|
|
|
poll_interval: app.config.testnet.poll_interval,
|
|
|
|
confirmations: app.config.testnet.required_confirmations,
|
2017-08-21 08:32:37 -07:00
|
|
|
filter: withdraws_filter(&app.testnet_bridge, init.testnet_contract_address.clone()),
|
2017-08-13 07:15:14 -07:00
|
|
|
};
|
|
|
|
|
|
|
|
WithdrawConfirm {
|
|
|
|
logs: api::log_stream(app.connections.testnet.clone(), logs_init),
|
|
|
|
testnet_contract: init.testnet_contract_address.clone(),
|
|
|
|
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-08-13 07:13:03 -07:00
|
|
|
testnet_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());
|
|
|
|
let withdraws = item.logs
|
|
|
|
.into_iter()
|
2017-08-21 08:32:37 -07:00
|
|
|
.map(|log| withdraw_confirm_payload(&self.app.testnet_bridge, log))
|
2017-08-13 07:13:03 -07:00
|
|
|
.collect::<Result<Vec<_>, _>>()?;
|
|
|
|
|
2017-08-21 08:32:37 -07:00
|
|
|
let requests = withdraws.clone()
|
|
|
|
.into_iter()
|
2017-08-13 07:13:03 -07:00
|
|
|
.map(|bytes| api::sign(&self.app.connections.testnet, self.app.config.testnet.account.clone(), bytes))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
WithdrawConfirmState::SignWithraws {
|
|
|
|
future: join_all(requests),
|
|
|
|
withdraws: withdraws,
|
|
|
|
block: item.to,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
WithdrawConfirmState::SignWithraws { ref mut future, ref mut withdraws, block } => {
|
|
|
|
let signatures = try_ready!(future.poll().map_err(ErrorKind::Web3));
|
|
|
|
// borrow checker...
|
|
|
|
let app = &self.app;
|
|
|
|
let testnet_contract = &self.testnet_contract;
|
|
|
|
let confirmations = withdraws
|
|
|
|
.drain(ops::RangeFull)
|
|
|
|
.zip(signatures.into_iter())
|
2017-08-21 08:32:37 -07:00
|
|
|
.map(|(withdraw, signature)| withdraw_collect_signatures_payload(&app.testnet_bridge, withdraw, signature))
|
2017-08-13 07:13:03 -07:00
|
|
|
.map(|payload| TransactionRequest {
|
|
|
|
// TODO: gas pricing should be taken from correct config option!!!
|
|
|
|
from: app.config.testnet.account.clone(),
|
|
|
|
to: Some(testnet_contract.clone()),
|
|
|
|
gas: Some(app.config.testnet.txs.deposit.gas.into()),
|
|
|
|
gas_price: Some(app.config.testnet.txs.deposit.gas_price.into()),
|
|
|
|
value: Some(app.config.testnet.txs.deposit.value.into()),
|
|
|
|
data: Some(payload),
|
|
|
|
nonce: None,
|
|
|
|
condition: None,
|
|
|
|
})
|
|
|
|
.map(|request| api::send_transaction(&app.connections.testnet, request))
|
|
|
|
.collect::<Vec<_>>();
|
|
|
|
|
|
|
|
WithdrawConfirmState::ConfirmWithdraws {
|
|
|
|
future: join_all(confirmations),
|
|
|
|
block,
|
|
|
|
}
|
|
|
|
},
|
|
|
|
WithdrawConfirmState::ConfirmWithdraws { ref mut future, block } => {
|
|
|
|
let _ = try_ready!(future.poll().map_err(ErrorKind::Web3));
|
|
|
|
WithdrawConfirmState::Yield(Some(block))
|
|
|
|
},
|
|
|
|
WithdrawConfirmState::Yield(ref mut block) => match block.take() {
|
|
|
|
None => WithdrawConfirmState::Wait,
|
|
|
|
some => return Ok(some.into()),
|
|
|
|
}
|
|
|
|
};
|
|
|
|
self.state = next_state;
|
|
|
|
}
|
2017-08-12 11:03:48 -07:00
|
|
|
}
|
|
|
|
}
|