Skip to content

Commit

Permalink
Merge pull request #82 from yrashk/required-signatures
Browse files Browse the repository at this point in the history
Problem: required_signatures is static
  • Loading branch information
akolotov authored Jul 5, 2018
2 parents 69474ce + d4de1c8 commit 7c42979
Show file tree
Hide file tree
Showing 8 changed files with 43 additions and 36 deletions.
1 change: 0 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ withdraw_confirm = { gas = 3000000 }
#### authorities options

- `authorities.account` - all authorities (**required**)
- `authorities.required_signatures` - number of authorities signatures required to consider action final (**required**)

#### transaction options

Expand Down
2 changes: 1 addition & 1 deletion bridge/src/bridge/deposit_relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use itertools::Itertools;

fn deposits_filter(home: &home::HomeBridge, address: Address) -> FilterBuilder {
let filter = home.events().deposit().create_filter();
web3_filter(filter, address)
web3_filter(filter, ::std::iter::once(address))
}

fn deposit_relay_payload(home: &home::HomeBridge, foreign: &foreign::ForeignBridge, log: Log) -> Result<Bytes> {
Expand Down
2 changes: 1 addition & 1 deletion bridge/src/bridge/withdraw_confirm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use super::BridgeChecked;

fn withdraws_filter(foreign: &foreign::ForeignBridge, address: Address) -> FilterBuilder {
let filter = foreign.events().withdraw().create_filter();
web3_filter(filter, address)
web3_filter(filter, ::std::iter::once(address))
}

fn withdraw_submit_signature_payload(foreign: &foreign::ForeignBridge, withdraw_message: Vec<u8>, signature: H520) -> Bytes {
Expand Down
54 changes: 29 additions & 25 deletions bridge/src/bridge/withdraw_relay.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,9 @@ use super::BridgeChecked;
use itertools::Itertools;

/// returns a filter for `ForeignBridge.CollectedSignatures` events
fn collected_signatures_filter(foreign: &foreign::ForeignBridge, address: Address) -> FilterBuilder {
fn collected_signatures_filter<I: IntoIterator<Item = Address>>(foreign: &foreign::ForeignBridge, addresses: I) -> FilterBuilder {
let filter = foreign.events().collected_signatures().create_filter();
web3_filter(filter, address)
web3_filter(filter, addresses)
}

/// payloads for calls to `ForeignBridge.signature` and `ForeignBridge.message`
Expand All @@ -34,7 +34,7 @@ struct RelayAssignment {
message_payload: Bytes,
}

fn signatures_payload(foreign: &foreign::ForeignBridge, required_signatures: u32, my_address: Address, log: Log) -> error::Result<Option<RelayAssignment>> {
fn signatures_payload(foreign: &foreign::ForeignBridge, my_address: Address, log: Log) -> error::Result<Option<RelayAssignment>> {
// convert web3::Log to ethabi::RawLog since ethabi events can
// only be parsed from the latter
let raw_log = RawLog {
Expand All @@ -48,7 +48,9 @@ fn signatures_payload(foreign: &foreign::ForeignBridge, required_signatures: u32
// someone else will relay this transaction to home.
return Ok(None);
}
let signature_payloads = (0..required_signatures).into_iter()

let required_signatures: U256 = (&foreign.functions().message().input(collected_signatures.number_of_collected_signatures)[4..]).into();
let signature_payloads = (0..required_signatures.low_u32()).into_iter()
.map(|index| foreign.functions().signature().input(collected_signatures.message_hash, index))
.map(Into::into)
.collect();
Expand Down Expand Up @@ -83,7 +85,7 @@ 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, vec![init.foreign_contract_address]),
};

WithdrawRelay {
Expand Down Expand Up @@ -120,7 +122,13 @@ impl<T: Transport> Stream for WithdrawRelay<T> {
let contract = self.home_contract.clone();
let home = &self.app.config.home;
let t = &self.app.connections.home;
let foreign = &self.app.connections.foreign;
let chain_id = self.home_chain_id;
let foreign_bridge = &self.app.foreign_bridge;
let foreign_account = self.app.config.foreign.account;
let timer = &self.app.timer;
let foreign_contract = self.foreign_contract;
let foreign_request_timeout = self.app.config.foreign.request_timeout;

loop {
let next_state = match self.state {
Expand All @@ -129,14 +137,10 @@ impl<T: Transport> Stream for WithdrawRelay<T> {
info!("got {} new signed withdraws to relay", item.logs.len());
let assignments = item.logs
.into_iter()
.map(|log| {
info!("collected signature is ready for relay: tx hash: {}", log.transaction_hash.unwrap());
signatures_payload(
&self.app.foreign_bridge,
self.app.config.authorities.required_signatures,
self.app.config.foreign.account,
log)
})
.map(|log| signatures_payload(
foreign_bridge,
foreign_account,
log))
.collect::<error::Result<Vec<_>>>()?;

let (signatures, messages): (Vec<_>, Vec<_>) = assignments.into_iter()
Expand All @@ -146,19 +150,19 @@ impl<T: Transport> Stream for WithdrawRelay<T> {

let message_calls = messages.into_iter()
.map(|payload| {
self.app.timer.timeout(
api::call(&self.app.connections.foreign, self.foreign_contract.clone(), payload),
self.app.config.foreign.request_timeout)
timer.timeout(
api::call(foreign, foreign_contract.clone(), payload),
foreign_request_timeout)
})
.collect::<Vec<_>>();

let signature_calls = signatures.into_iter()
.map(|payloads| {
payloads.into_iter()
.map(|payload| {
self.app.timer.timeout(
api::call(&self.app.connections.foreign, self.foreign_contract.clone(), payload),
self.app.config.foreign.request_timeout)
timer.timeout(
api::call(foreign, foreign_contract.clone(), payload),
foreign_request_timeout)
})
.collect::<Vec<_>>()
})
Expand Down Expand Up @@ -271,11 +275,11 @@ mod tests {
let foreign = foreign::ForeignBridge::default();
let my_address = "aff3454fce5edbc8cca8697c15331677e6ebcccc".into();

let data = "000000000000000000000000aff3454fce5edbc8cca8697c15331677e6ebcccc00000000000000000000000000000000000000000000000000000000000000f0".from_hex().unwrap();
let data = "000000000000000000000000aff3454fce5edbc8cca8697c15331677e6ebcccc00000000000000000000000000000000000000000000000000000000000000f00000000000000000000000000000000000000000000000000000000000000002".from_hex().unwrap();

let log = Log {
data: data.into(),
topics: vec!["eb043d149eedb81369bec43d4c3a3a53087debc88d2525f13bfaa3eecda28b5c".into()],
topics: vec!["415557404d88a0c0b8e3b16967cafffc511213fd9c465c16832ee17ed57d7237".into()],
transaction_hash: Some("884edad9ce6fa2440d8a54cc123490eb96d2768479d49ff9c7366125a9424364".into()),
address: Address::zero(),
block_hash: None,
Expand All @@ -287,7 +291,7 @@ mod tests {
removed: None,
};

let assignment = signatures_payload(&foreign, 2, my_address, log).unwrap().unwrap();
let assignment = signatures_payload(&foreign, my_address, log).unwrap().unwrap();
let expected_message: Bytes = "490a32c600000000000000000000000000000000000000000000000000000000000000f0".from_hex().unwrap().into();
let expected_signatures: Vec<Bytes> = vec![
"1812d99600000000000000000000000000000000000000000000000000000000000000f00000000000000000000000000000000000000000000000000000000000000000".from_hex().unwrap().into(),
Expand All @@ -302,11 +306,11 @@ mod tests {
let foreign = foreign::ForeignBridge::default();
let my_address = "aff3454fce5edbc8cca8697c15331677e6ebcccd".into();

let data = "000000000000000000000000aff3454fce5edbc8cca8697c15331677e6ebcccc00000000000000000000000000000000000000000000000000000000000000f0".from_hex().unwrap();
let data = "000000000000000000000000aff3454fce5edbc8cca8697c15331677e6ebcccc00000000000000000000000000000000000000000000000000000000000000f00000000000000000000000000000000000000000000000000000000000000002".from_hex().unwrap();

let log = Log {
data: data.into(),
topics: vec!["eb043d149eedb81369bec43d4c3a3a53087debc88d2525f13bfaa3eecda28b5c".into()],
topics: vec!["415557404d88a0c0b8e3b16967cafffc511213fd9c465c16832ee17ed57d7237".into()],
transaction_hash: Some("884edad9ce6fa2440d8a54cc123490eb96d2768479d49ff9c7366125a9424364".into()),
address: Address::zero(),
block_hash: None,
Expand All @@ -318,7 +322,7 @@ mod tests {
removed: None,
};

let assignment = signatures_payload(&foreign, 2, my_address, log).unwrap();
let assignment = signatures_payload(&foreign, my_address, log).unwrap();
assert_eq!(None, assignment);
}
}
5 changes: 3 additions & 2 deletions bridge/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ impl Config {
authorities: Authorities {
#[cfg(feature = "deploy")]
accounts: config.authorities.accounts,
#[cfg(feature = "deploy")]
required_signatures: config.authorities.required_signatures,
},
txs: config.transactions.map(Transactions::from_load_struct).unwrap_or_default(),
Expand Down Expand Up @@ -220,6 +221,7 @@ pub struct ContractConfig {
pub struct Authorities {
#[cfg(feature = "deploy")]
pub accounts: Vec<Address>,
#[cfg(feature = "deploy")]
pub required_signatures: u32,
}

Expand Down Expand Up @@ -325,6 +327,7 @@ mod load {
#[cfg(feature = "deploy")]
#[serde(default)]
pub accounts: Vec<Address>,
#[cfg(feature = "deploy")]
pub required_signatures: u32,
}
}
Expand Down Expand Up @@ -403,7 +406,6 @@ required_signatures = 2
#[cfg(feature = "deploy")]
accounts: vec![
],
required_signatures: 2,
},
keystore: "/keys/".into(),
};
Expand Down Expand Up @@ -466,7 +468,6 @@ required_signatures = 2
#[cfg(feature = "deploy")]
accounts: vec![
],
required_signatures: 2,
},
keystore: "/keys/".into(),
};
Expand Down
3 changes: 3 additions & 0 deletions bridge/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@ error_chain! {
errors {
ShutdownRequested
InsufficientFunds
NoRequiredSignaturesChanged {
description("No RequiredSignaturesChanged has been observed")
}
// api timeout
Timeout(request: &'static str) {
description("Request timeout"),
Expand Down
4 changes: 2 additions & 2 deletions bridge/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,12 @@ fn web3_topic(topic: ethabi::Topic<ethabi::Hash>) -> Option<Vec<H256>> {
}
}

pub fn web3_filter(filter: ethabi::TopicFilter, address: Address) -> FilterBuilder {
pub fn web3_filter<I: IntoIterator<Item = Address>>(filter: ethabi::TopicFilter, addresses: I) -> FilterBuilder {
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])
.address(addresses.into_iter().collect())
.topics(t0, t1, t2, t3)
}
8 changes: 4 additions & 4 deletions contracts/bridge.sol
Original file line number Diff line number Diff line change
Expand Up @@ -400,7 +400,7 @@ contract ForeignBridge is BridgeDeploymentAddressStorage,
event Withdraw(address recipient, uint256 value, uint256 homeGasPrice);

/// Collected signatures which should be relayed to home chain.
event CollectedSignatures(address authorityResponsibleForRelay, bytes32 messageHash);
event CollectedSignatures(address authorityResponsibleForRelay, bytes32 messageHash, uint256 NumberOfCollectedSignatures);

/// Event created when new token address is set up.
event TokenAddress(address token);
Expand All @@ -415,11 +415,11 @@ contract ForeignBridge is BridgeDeploymentAddressStorage,
require(_requiredSignatures != 0);
require(_requiredSignatures <= _authorities.length);
requiredSignatures = _requiredSignatures;

for (uint i = 0; i < _authorities.length; i++) {
authorities[_authorities[i]] = true;
}

estimatedGasCostOfWithdraw = _estimatedGasCostOfWithdraw;
}

Expand Down Expand Up @@ -551,7 +551,7 @@ contract ForeignBridge is BridgeDeploymentAddressStorage,

// TODO: this may cause troubles if requiredSignatures len is changed
if (signed == requiredSignatures) {
CollectedSignatures(msg.sender, hash);
CollectedSignatures(msg.sender, hash, signed);
}
}

Expand Down

0 comments on commit 7c42979

Please sign in to comment.