Skip to content

Commit

Permalink
Clean up Database et. al.
Browse files Browse the repository at this point in the history
* Remove `home/foreign_contract_address` from Database.
* Remove `Option` from `Node::contract_address`.
* Remove unnecessary extra `parsed::...Database` types.
* Remove unnecessary extra `Database::from...` and `::load` methods.
  • Loading branch information
c0gent committed Jun 21, 2018
1 parent 56e9ce1 commit c41913f
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 176 deletions.
20 changes: 9 additions & 11 deletions bridge/src/bridge/deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,7 @@ impl<T: Transport + Clone> Future for Deploy<T> {
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")] {
Expand Down Expand Up @@ -131,9 +123,15 @@ impl<T: Transport + Clone> Future for Deploy<T> {
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(),
Expand Down
6 changes: 3 additions & 3 deletions bridge/src/bridge/deposit_relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ pub fn create_deposit_relay<T: Transport + Clone>(app: Arc<App<T>>, 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,
Expand Down Expand Up @@ -92,7 +92,7 @@ impl<T: Transport> Stream for DepositRelay<T> {
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())
}
Expand Down
10 changes: 5 additions & 5 deletions bridge/src/bridge/gas_price.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 3 additions & 3 deletions bridge/src/bridge/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl<ES: Stream<Item = BridgeChecked, Error = Error>> Stream for Bridge<ES> {
.create(true)
.open(&self.path)?;

self.database.store(file)?;
self.database.save(file)?;
Ok(Async::Ready(Some(())))
}
}
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions bridge/src/bridge/withdraw_confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,12 +44,12 @@ pub fn create_withdraw_confirm<T: Transport + Clone>(app: Arc<App<T>>, 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,
Expand Down
6 changes: 3 additions & 3 deletions bridge/src/bridge/withdraw_relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,13 @@ pub fn create_withdraw_relay<T: Transport + Clone>(app: Arc<App<T>>, 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,
Expand Down
26 changes: 12 additions & 14 deletions bridge/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ pub struct Node {
pub account: Address,
#[cfg(feature = "deploy")]
pub contract: ContractConfig,
pub contract_address: Option<Address>,
pub contract_address: Address,
pub request_timeout: Duration,
pub poll_interval: Duration,
pub required_confirmations: usize,
Expand Down Expand Up @@ -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")]
Expand All @@ -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),
Expand All @@ -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)
}

Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down Expand Up @@ -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,
Expand All @@ -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,
Expand Down
Loading

0 comments on commit c41913f

Please sign in to comment.