diff --git a/bridge/src/bridge/deploy.rs b/bridge/src/bridge/deploy.rs index bdf975c..2cdadc4 100644 --- a/bridge/src/bridge/deploy.rs +++ b/bridge/src/bridge/deploy.rs @@ -66,15 +66,7 @@ impl Future for Deploy { loop { let _next_state = match self.state { DeployState::CheckIfNeeded => { - let db = Database::load_user_defined( - &self.app.database_path, - self.app.config.home.contract_address.expect("Home contract \ - address must be specified within config toml."), - self.app.config.foreign.contract_address.expect("Foreign contract \ - address must be specified within config toml.")) - .map_err(ErrorKind::from); - - match db { + match Database::load(&self.app.database_path).map_err(ErrorKind::from) { Ok(database) => return Ok(Deployed::Existing(database).into()), Err(ErrorKind::MissingFile(_e)) => { #[cfg(feature = "deploy")] { @@ -131,9 +123,15 @@ impl Future for Deploy { DeployState::Deploying(ref mut future) => { let (main_receipt, test_receipt) = try_ready!(future.poll()); + // The `deploy` feature is being removed so this shouldn't + // matter but the following lines have been left here as a + // reminder that contract addresses are no longer stored + // within `Database`. + // + // let _ = main_receipt.contract_address.expect("contract creation receipt must have an address; qed") + // let _ = test_receipt.contract_address.expect("contract creation receipt must have an address; qed") + let database = Database { - home_contract_address: main_receipt.contract_address.expect("contract creation receipt must have an address; qed"), - foreign_contract_address: test_receipt.contract_address.expect("contract creation receipt must have an address; qed"), home_deploy: Some(main_receipt.block_number.low_u64()), foreign_deploy: Some(test_receipt.block_number.low_u64()), checked_deposit_relay: main_receipt.block_number.low_u64(), diff --git a/bridge/src/bridge/deposit_relay.rs b/bridge/src/bridge/deposit_relay.rs index dd78966..ed545ff 100644 --- a/bridge/src/bridge/deposit_relay.rs +++ b/bridge/src/bridge/deposit_relay.rs @@ -49,11 +49,11 @@ pub fn create_deposit_relay(app: Arc>, init: &Datab request_timeout: app.config.home.request_timeout, poll_interval: app.config.home.poll_interval, confirmations: app.config.home.required_confirmations, - filter: deposits_filter(&app.home_bridge, init.home_contract_address), + filter: deposits_filter(&app.home_bridge, app.config.home.contract_address), }; DepositRelay { logs: api::log_stream(app.connections.home.clone(), app.timer.clone(), logs_init), - foreign_contract: init.foreign_contract_address, + foreign_contract: app.config.foreign.contract_address, state: DepositRelayState::Wait, app, foreign_balance, @@ -92,7 +92,7 @@ impl Stream for DepositRelay { let gas = U256::from(self.app.config.txs.deposit_relay.gas); let gas_price = U256::from(*self.foreign_gas_price.read().unwrap()); let balance_required = gas * gas_price * U256::from(item.logs.len()); - + if balance_required > *foreign_balance.as_ref().unwrap() { return Err(ErrorKind::InsufficientFunds.into()) } diff --git a/bridge/src/bridge/gas_price.rs b/bridge/src/bridge/gas_price.rs index 4ceda4f..2918997 100644 --- a/bridge/src/bridge/gas_price.rs +++ b/bridge/src/bridge/gas_price.rs @@ -165,7 +165,7 @@ mod tests { fn errored_request() { let node = Node { account: Address::new(), - contract_address: Some("49edf201c1e139282643d5e7c6fb0c7219ad1db7".into()), + contract_address: "49edf201c1e139282643d5e7c6fb0c7219ad1db7".into(), request_timeout: Duration::from_secs(5), poll_interval: Duration::from_secs(1), required_confirmations: 0, @@ -209,7 +209,7 @@ mod tests { fn bad_json() { let node = Node { account: Address::new(), - contract_address: Some("49edf201c1e139282643d5e7c6fb0c7219ad1db7".into()), + contract_address: "49edf201c1e139282643d5e7c6fb0c7219ad1db7".into(), request_timeout: Duration::from_secs(5), poll_interval: Duration::from_secs(1), required_confirmations: 0, @@ -253,7 +253,7 @@ mod tests { fn unexpected_json() { let node = Node { account: Address::new(), - contract_address: Some("49edf201c1e139282643d5e7c6fb0c7219ad1db7".into()), + contract_address: "49edf201c1e139282643d5e7c6fb0c7219ad1db7".into(), request_timeout: Duration::from_secs(5), poll_interval: Duration::from_secs(1), required_confirmations: 0, @@ -296,7 +296,7 @@ mod tests { fn non_object_json() { let node = Node { account: Address::new(), - contract_address: Some("49edf201c1e139282643d5e7c6fb0c7219ad1db7".into()), + contract_address: "49edf201c1e139282643d5e7c6fb0c7219ad1db7".into(), request_timeout: Duration::from_secs(5), poll_interval: Duration::from_secs(1), required_confirmations: 0, @@ -339,7 +339,7 @@ mod tests { fn correct_json() { let node = Node { account: Address::new(), - contract_address: Some("49edf201c1e139282643d5e7c6fb0c7219ad1db7".into()), + contract_address: "49edf201c1e139282643d5e7c6fb0c7219ad1db7".into(), request_timeout: Duration::from_secs(5), poll_interval: Duration::from_secs(1), required_confirmations: 0, diff --git a/bridge/src/bridge/mod.rs b/bridge/src/bridge/mod.rs index 764fe19..ef84639 100644 --- a/bridge/src/bridge/mod.rs +++ b/bridge/src/bridge/mod.rs @@ -62,7 +62,7 @@ impl> Stream for Bridge { .create(true) .open(&self.path)?; - self.database.store(file)?; + self.database.save(file)?; Ok(Async::Ready(Some(()))) } } @@ -244,7 +244,7 @@ mod tests { let mut event_loop = Core::new().unwrap(); let _ = event_loop.run(bridge.collect()); - let db = Database::load_stored(&path).unwrap(); + let db = Database::load(&path).unwrap(); assert_eq!(1, db.checked_deposit_relay); assert_eq!(0, db.checked_withdraw_confirm); @@ -260,7 +260,7 @@ mod tests { let mut event_loop = Core::new().unwrap(); let _ = event_loop.run(bridge.collect()); - let db = Database::load_stored(&path).unwrap(); + let db = Database::load(&path).unwrap(); assert_eq!(2, db.checked_deposit_relay); assert_eq!(3, db.checked_withdraw_confirm); diff --git a/bridge/src/bridge/withdraw_confirm.rs b/bridge/src/bridge/withdraw_confirm.rs index a287a55..53b36ef 100644 --- a/bridge/src/bridge/withdraw_confirm.rs +++ b/bridge/src/bridge/withdraw_confirm.rs @@ -44,12 +44,12 @@ pub fn create_withdraw_confirm(app: Arc>, init: &Da 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()), + filter: withdraws_filter(&app.foreign_bridge, app.config.foreign.contract_address.clone()), }; WithdrawConfirm { logs: api::log_stream(app.connections.foreign.clone(), app.timer.clone(), logs_init), - foreign_contract: init.foreign_contract_address, + foreign_contract: app.config.foreign.contract_address, state: WithdrawConfirmState::Wait, app, foreign_balance, diff --git a/bridge/src/bridge/withdraw_relay.rs b/bridge/src/bridge/withdraw_relay.rs index e79819b..a36505f 100644 --- a/bridge/src/bridge/withdraw_relay.rs +++ b/bridge/src/bridge/withdraw_relay.rs @@ -83,13 +83,13 @@ pub fn create_withdraw_relay(app: Arc>, init: &Data request_timeout: app.config.foreign.request_timeout, poll_interval: app.config.foreign.poll_interval, confirmations: app.config.foreign.required_confirmations, - filter: collected_signatures_filter(&app.foreign_bridge, init.foreign_contract_address), + filter: collected_signatures_filter(&app.foreign_bridge, app.config.foreign.contract_address), }; WithdrawRelay { logs: api::log_stream(app.connections.foreign.clone(), app.timer.clone(), logs_init), - home_contract: init.home_contract_address, - foreign_contract: init.foreign_contract_address, + home_contract: app.config.home.contract_address, + foreign_contract: app.config.foreign.contract_address, state: WithdrawRelayState::Wait, app, home_balance, diff --git a/bridge/src/config.rs b/bridge/src/config.rs index 541949b..3750554 100644 --- a/bridge/src/config.rs +++ b/bridge/src/config.rs @@ -69,7 +69,7 @@ pub struct Node { pub account: Address, #[cfg(feature = "deploy")] pub contract: ContractConfig, - pub contract_address: Option
, + pub contract_address: Address, pub request_timeout: Duration, pub poll_interval: Duration, pub required_confirmations: usize, @@ -133,6 +133,12 @@ impl Node { } } + let contract_address = node.contract_address.ok_or(ErrorKind::ConfigError( + "Contract address not specified. Please define the 'contract_address' key \ + within both the '[home]' and '[foreign]' tables in the toml config file. See \ + 'https://github.com/poanetwork/poa-bridge/blob/master/README.md' \ + for more.".to_owned()))?; + let node = Node { account: node.account, #[cfg(feature = "deploy")] @@ -144,7 +150,7 @@ impl Node { Bytes(read.from_hex()?) } }, - contract_address: node.contract_address, + contract_address: contract_address, request_timeout: Duration::from_secs(node.request_timeout.unwrap_or(DEFAULT_TIMEOUT)), poll_interval: Duration::from_secs(node.poll_interval.unwrap_or(DEFAULT_POLL_INTERVAL)), required_confirmations: node.required_confirmations.unwrap_or(DEFAULT_CONFIRMATIONS), @@ -159,14 +165,6 @@ impl Node { concurrent_http_requests, }; - // Ensure that the contract address is specified for non-deploy builds: - if cfg!(not(feature = "deploy")) && node.contract_address.is_none() { - return Err(ErrorKind::ConfigError("Contract address not specified. Please define the \ - 'contract_address' key within both the '[home]' and '[foreign]' tables in the \ - toml config file. See 'https://github.com/poanetwork/poa-bridge/blob/master/README.md' \ - for more.".into()).into()); - } - Ok(node) } @@ -384,7 +382,7 @@ required_signatures = 2 txs: Transactions::default(), home: Node { account: "1B68Cb0B50181FC4006Ce572cF346e596E51818b".into(), - contract_address: Some("49edf201c1e139282643d5e7c6fb0c7219ad1db7".into()), + contract_address: "49edf201c1e139282643d5e7c6fb0c7219ad1db7".into(), poll_interval: Duration::from_secs(2), request_timeout: Duration::from_secs(DEFAULT_TIMEOUT), required_confirmations: 100, @@ -400,7 +398,7 @@ required_signatures = 2 }, foreign: Node { account: "0000000000000000000000000000000000000001".into(), - contract_address: Some("49edf201c1e139282643d5e7c6fb0c7219ad1db8".into()), + contract_address: "49edf201c1e139282643d5e7c6fb0c7219ad1db8".into(), poll_interval: Duration::from_secs(1), request_timeout: Duration::from_secs(DEFAULT_TIMEOUT), required_confirmations: 12, @@ -451,7 +449,7 @@ required_signatures = 2 txs: Transactions::default(), home: Node { account: "1B68Cb0B50181FC4006Ce572cF346e596E51818b".into(), - contract_address: Some("49edf201c1e139282643d5e7c6fb0c7219ad1db7".into()), + contract_address: "49edf201c1e139282643d5e7c6fb0c7219ad1db7".into(), poll_interval: Duration::from_secs(1), request_timeout: Duration::from_secs(DEFAULT_TIMEOUT), required_confirmations: 12, @@ -467,7 +465,7 @@ required_signatures = 2 }, foreign: Node { account: "0000000000000000000000000000000000000001".into(), - contract_address: Some("49edf201c1e139282643d5e7c6fb0c7219ad1db8".into()), + contract_address: "49edf201c1e139282643d5e7c6fb0c7219ad1db8".into(), poll_interval: Duration::from_secs(1), request_timeout: Duration::from_secs(DEFAULT_TIMEOUT), required_confirmations: 12, diff --git a/bridge/src/database.rs b/bridge/src/database.rs index bd6ea41..d85b249 100644 --- a/bridge/src/database.rs +++ b/bridge/src/database.rs @@ -1,7 +1,6 @@ use std::path::Path; use std::{io, str, fs, fmt}; use std::io::{Read, Write}; -use web3::types::Address; use toml; use error::{Error, ResultExt, ErrorKind}; @@ -9,10 +8,6 @@ use error::{Error, ResultExt, ErrorKind}; /// Application "database". #[derive(Debug, PartialEq, Deserialize, Serialize, Default, Clone)] pub struct Database { - /// Address of home contract. - pub home_contract_address: Address, - /// Address of foreign contract. - pub foreign_contract_address: Address, /// Number of block at which home contract has been deployed. pub home_deploy: Option, /// Number of block at which foreign contract has been deployed. @@ -25,20 +20,6 @@ pub struct Database { pub checked_withdraw_confirm: u64, } -impl From for Database { - fn from(db_parsed: parsed::StoredDatabase) -> Database { - Database { - home_contract_address: db_parsed.home_contract_address, - foreign_contract_address: db_parsed.foreign_contract_address, - home_deploy: db_parsed.home_deploy, - foreign_deploy: db_parsed.foreign_deploy, - checked_deposit_relay: db_parsed.checked_deposit_relay, - checked_withdraw_relay: db_parsed.checked_withdraw_relay, - checked_withdraw_confirm: db_parsed.checked_withdraw_confirm, - } - } -} - impl fmt::Display for Database { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.write_str(&toml::to_string(self).expect("serialization can't fail; qed")) @@ -46,33 +27,9 @@ impl fmt::Display for Database { } impl Database { - /// Loads the toml file specified by `path` and returns a new `Database` - /// containing its parsed contents. - #[deprecated(note = "Use '::load_stored' instead.")] - pub fn load>(path: P) -> Result { - Self::load_stored(path) - } - - /// Loads the toml file specified by `path` and returns a new `Database` - /// containing its parsed contents. - pub fn load_stored>(path: P) -> Result { - let mut file = match fs::File::open(&path) { - Ok(file) => file, - Err(ref err) if err.kind() == io::ErrorKind::NotFound => - return Err(ErrorKind::MissingFile(format!("{:?}", path.as_ref())).into()), - Err(err) => return Err(err).chain_err(|| "Cannot open database file"), - }; - - let mut buffer = String::new(); - file.read_to_string(&mut buffer)?; - Ok(toml::from_str::(&buffer)?.into()) - - } - /// Loads a user defined toml file specified by `path` and returns a new /// `Database` containing its parsed contents. - pub fn load_user_defined>(path: P, home_contract_address: Address, - foreign_contract_address: Address) + pub fn load>(path: P) -> Result { let mut file = match fs::File::open(&path) { Ok(file) => file, @@ -83,17 +40,7 @@ impl Database { let mut buffer = String::new(); file.read_to_string(&mut buffer)?; - Database::from_str_user_defined(&buffer, home_contract_address, foreign_contract_address) - } - - /// Returns a new `Database` constructed from a stored toml string - /// containing keys for 'home_contract_address' and - /// 'foreign_contract_address'. - #[cfg(test)] - fn from_str_stored>(s: S) -> Result { - toml::from_str::(s.as_ref()) - .map(Database::from) - .map_err(Error::from) + Database::from_str(&buffer) } /// Returns a new `Database` constructed from the parsed string `s` and @@ -101,14 +48,11 @@ impl Database { /// /// An error will be returned if the `s` contains keys for /// 'home_contract_address' or 'foreign_contract_address'. - fn from_str_user_defined>(s: S, home_contract_address: Address, - foreign_contract_address: Address) -> Result { - let db_parsed: parsed::UserDefinedDatabase = toml::from_str(s.as_ref()) + fn from_str>(s: S) -> Result { + let db_parsed: parsed::Database = toml::from_str(s.as_ref()) .chain_err(|| "Cannot parse database file")?; Ok(Database { - home_contract_address, - foreign_contract_address, home_deploy: db_parsed.home_deploy, foreign_deploy: db_parsed.foreign_deploy, checked_deposit_relay: db_parsed.checked_deposit_relay, @@ -118,13 +62,7 @@ impl Database { } /// Writes a serialized `Database` to a writer. - #[deprecated(note = "Use '::store' instead.")] - pub fn save(&self, writer: W) -> Result<(), Error> { - self.store(writer) - } - - /// Writes a serialized `Database` to a writer. - pub fn store(&self, mut writer: W) -> Result<(), Error> { + pub fn save(&self, mut writer: W) -> Result<(), Error> { writer.write_all(self.to_string().as_bytes())?; Ok(()) } @@ -136,39 +74,10 @@ mod parsed { #[cfg(test)] use toml; - use super::Address; - - /// Parsed application "database". - #[derive(Debug, Deserialize, Serialize)] - #[serde(deny_unknown_fields)] - pub struct StoredDatabase { - /// Address of home contract. - pub home_contract_address: Address, - /// Address of foreign contract. - pub foreign_contract_address: Address, - /// Number of block at which home contract has been deployed. - pub home_deploy: Option, - /// Number of block at which foreign contract has been deployed. - pub foreign_deploy: Option, - /// Number of last block which has been checked for deposit relays. - pub checked_deposit_relay: u64, - /// Number of last block which has been checked for withdraw relays. - pub checked_withdraw_relay: u64, - /// Number of last block which has been checked for withdraw confirms. - pub checked_withdraw_confirm: u64, - } - - #[cfg(test)] - impl fmt::Display for StoredDatabase { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - f.write_str(&toml::to_string(self).expect("serialization can't fail; qed")) - } - } - /// Parsed application "database". #[derive(Debug, Deserialize, Serialize)] #[serde(deny_unknown_fields)] - pub struct UserDefinedDatabase { + pub struct Database { /// Number of block at which home contract has been deployed. pub home_deploy: Option, /// Number of block at which foreign contract has been deployed. @@ -182,7 +91,7 @@ mod parsed { } #[cfg(test)] - impl fmt::Display for UserDefinedDatabase { + impl fmt::Display for Database { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { f.write_str(&toml::to_string(self).expect("serialization can't fail; qed")) } @@ -195,35 +104,7 @@ mod tests { use super::Database; #[test] - fn database_to_and_from_str_stored() { - let toml = -r#"home_contract_address = "0x49edf201c1e139282643d5e7c6fb0c7219ad1db7" -foreign_contract_address = "0x49edf201c1e139282643d5e7c6fb0c7219ad1db8" -home_deploy = 100 -foreign_deploy = 101 -checked_deposit_relay = 120 -checked_withdraw_relay = 121 -checked_withdraw_confirm = 121 -"#; - - let expected = Database { - home_contract_address: "49edf201c1e139282643d5e7c6fb0c7219ad1db7".into(), - foreign_contract_address: "49edf201c1e139282643d5e7c6fb0c7219ad1db8".into(), - home_deploy: Some(100), - foreign_deploy: Some(101), - checked_deposit_relay: 120, - checked_withdraw_relay: 121, - checked_withdraw_confirm: 121, - }; - - let database = Database::from_str_stored(toml).unwrap(); - assert_eq!(expected, database); - let s = database.to_string(); - assert_eq!(s, toml); - } - - #[test] - fn database_to_and_from_str_user_defined() { + fn database_to_and_from_str() { let toml = r#"home_deploy = 100 foreign_deploy = 101 @@ -232,12 +113,7 @@ checked_withdraw_relay = 121 checked_withdraw_confirm = 121 "#; - let home_contract_address = "49edf201c1e139282643d5e7c6fb0c7219ad1db7".into(); - let foreign_contract_address = "49edf201c1e139282643d5e7c6fb0c7219ad1db8".into(); - let expected = Database { - home_contract_address, - foreign_contract_address, home_deploy: Some(100), foreign_deploy: Some(101), checked_deposit_relay: 120, @@ -245,8 +121,7 @@ checked_withdraw_confirm = 121 checked_withdraw_confirm: 121, }; - let database = Database::from_str_user_defined(toml, - home_contract_address, foreign_contract_address).unwrap(); + let database = Database::from_str(toml).unwrap(); assert_eq!(expected, database); let s = database.to_string(); assert!(s.contains(toml)); diff --git a/cli/src/main.rs b/cli/src/main.rs index 9448395..c539ae6 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -176,7 +176,7 @@ fn execute(command: I, running: Arc) -> Result { info!(target: "bridge", "Deployed new bridge contracts"); info!(target: "bridge", "\n\n{}\n", database); - database.store(fs::File::create(&app.database_path)?)?; + database.save(fs::File::create(&app.database_path)?)?; database }, Deployed::Existing(database) => {