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

206 lines
7.9 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;
use ethabi::{RawLog, Hash};
2017-08-12 11:03:48 -07:00
use web3::Transport;
use web3::types::{H256, H520, Address, TransactionRequest, Log, 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;
2017-10-10 02:02:46 -07:00
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;
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-10-10 02:02:46 -07:00
fn withdraw_confirm_sign_payload(foreign: &foreign::ForeignBridge, log: Log) -> Result<Bytes, Error> {
let raw_log = RawLog {
topics: log.topics.into_iter().map(|t| t.0.into()).collect(),
data: log.data.0,
};
2017-10-10 02:02:46 -07:00
let withdraw_log = foreign.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(&Hash::from(withdraw_log.value));
result[52..84].copy_from_slice(&hash);
Ok(result.into())
}
fn withdraw_submit_signature_payload(foreign: &foreign::ForeignBridge, withdraw_message: Bytes, signature: H520) -> Bytes {
assert_eq!(signature.0.len(), 65);
assert_eq!(withdraw_message.0.len(), 84, "ForeignBridge never accepts messages with len != 84 bytes; qed");
foreign.functions().submit_signature().input(signature.0.to_vec(), withdraw_message.0).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 {
withdraws: Vec<Bytes>,
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.clone(),
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());
2017-08-13 07:13:03 -07:00
let withdraws = item.logs
.into_iter()
.map(|log| {
info!("withdraw is ready for signature submission. tx hash {}", log.transaction_hash.unwrap());
withdraw_confirm_sign_payload(&self.app.foreign_bridge, log)
})
2017-08-13 07:13:03 -07:00
.collect::<Result<Vec<_>, _>>()?;
let requests = withdraws.clone()
.into_iter()
2017-08-31 08:32:34 -07:00
.map(|bytes| {
self.app.timer.timeout(
2017-10-10 02:02:46 -07:00
api::sign(&self.app.connections.foreign, self.app.config.foreign.account.clone(), bytes),
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),
withdraws: withdraws,
block: item.to,
}
},
2017-08-25 07:27:55 -07:00
WithdrawConfirmState::SignWithdraws { ref mut future, ref mut withdraws, 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;
2017-08-13 07:13:03 -07:00
let confirmations = withdraws
.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
}
}
2017-08-25 08:58:55 -07:00
#[cfg(test)]
mod tests {
use rustc_hex::FromHex;
use web3::types::{Log, Bytes};
2017-10-10 02:02:46 -07:00
use contracts::foreign;
2017-08-25 08:58:55 -07:00
use super::{withdraw_confirm_sign_payload, withdraw_submit_signature_payload};
#[test]
fn test_withdraw_confirm_sign_payload() {
2017-10-10 02:02:46 -07:00
let foreign = foreign::ForeignBridge::default();
2017-08-25 08:58:55 -07:00
let data = "000000000000000000000000aff3454fce5edbc8cca8697c15331677e6ebcccc00000000000000000000000000000000000000000000000000000000000000f0".from_hex().unwrap();
let log = Log {
data: data.into(),
topics: vec!["884edad9ce6fa2440d8a54cc123490eb96d2768479d49ff9c7366125a9424364".into()],
transaction_hash: Some("884edad9ce6fa2440d8a54cc123490eb96d2768479d49ff9c7366125a9424364".into()),
2017-08-25 08:58:55 -07:00
..Default::default()
};
2017-10-10 02:02:46 -07:00
let payload = withdraw_confirm_sign_payload(&foreign, log).unwrap();
2017-08-25 08:58:55 -07:00
let expected: Bytes = "aff3454fce5edbc8cca8697c15331677e6ebcccc00000000000000000000000000000000000000000000000000000000000000f0884edad9ce6fa2440d8a54cc123490eb96d2768479d49ff9c7366125a9424364".from_hex().unwrap().into();
assert_eq!(expected, payload);
}
#[test]
fn test_withdraw_submit_signature_payload() {
2017-10-10 02:02:46 -07:00
let foreign = foreign::ForeignBridge::default();
2017-08-25 08:58:55 -07:00
let message: Bytes = "aff3454fce5edbc8cca8697c15331677e6ebcccc00000000000000000000000000000000000000000000000000000000000000f0884edad9ce6fa2440d8a54cc123490eb96d2768479d49ff9c7366125a9424364".from_hex().unwrap().into();
let signature = "8697c15331677e6ebccccaff3454fce5edbc8cca8697c15331677aff3454fce5edbc8cca8697c15331677e6ebccccaff3454fce5edbc8cca8697c15331677e6ebc".into();
2017-08-25 08:58:55 -07:00
2017-10-10 02:02:46 -07:00
let payload = withdraw_submit_signature_payload(&foreign, message, signature);
2017-08-25 08:58:55 -07:00
let expected: Bytes = "630cea8e000000000000000000000000000000000000000000000000000000000000004000000000000000000000000000000000000000000000000000000000000000c000000000000000000000000000000000000000000000000000000000000000418697c15331677e6ebccccaff3454fce5edbc8cca8697c15331677aff3454fce5edbc8cca8697c15331677e6ebccccaff3454fce5edbc8cca8697c15331677e6ebc000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000054aff3454fce5edbc8cca8697c15331677e6ebcccc00000000000000000000000000000000000000000000000000000000000000f0884edad9ce6fa2440d8a54cc123490eb96d2768479d49ff9c7366125a9424364000000000000000000000000".from_hex().unwrap().into();
assert_eq!(expected, payload);
}
}