Skip to content

Commit

Permalink
Merge pull request #1340 from anoma/tiago/ethbridge/warn-bp-transfer
Browse files Browse the repository at this point in the history
Emit warnings for fallible Bridge pool relays
  • Loading branch information
sug0 authored May 10, 2023
2 parents 5c0caf0 + f8a9b02 commit fd0ef46
Show file tree
Hide file tree
Showing 7 changed files with 97 additions and 41 deletions.
35 changes: 21 additions & 14 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 6 additions & 5 deletions apps/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ data-encoding = "2.3.2"
derivative = "2.2.0"
ed25519-consensus = "1.2.0"
ethabi = "18.0.0"
ethbridge-bridge-contract = {git = "https://github.com/heliaxdev/ethbridge-rs", tag = "v0.17.0"}
ethbridge-bridge-events = {git = "https://github.com/heliaxdev/ethbridge-rs", tag = "v0.17.0"}
ethbridge-events = {git = "https://github.com/heliaxdev/ethbridge-rs", tag = "v0.17.0"}
ethbridge-governance-contract = {git = "https://github.com/heliaxdev/ethbridge-rs", tag = "v0.17.0"}
ethbridge-governance-events = {git = "https://github.com/heliaxdev/ethbridge-rs", tag = "v0.17.0"}
ethbridge-bridge-contract = {git = "https://github.com/heliaxdev/ethbridge-rs", tag = "v0.18.0"}
ethbridge-bridge-events = {git = "https://github.com/heliaxdev/ethbridge-rs", tag = "v0.18.0"}
ethbridge-events = {git = "https://github.com/heliaxdev/ethbridge-rs", tag = "v0.18.0"}
ethbridge-governance-contract = {git = "https://github.com/heliaxdev/ethbridge-rs", tag = "v0.18.0"}
ethbridge-governance-events = {git = "https://github.com/heliaxdev/ethbridge-rs", tag = "v0.18.0"}
ferveo = {git = "https://github.com/anoma/ferveo", rev = "9e5e91c954158e7cff45c483fd06cd649a81553f"}
ferveo-common = {git = "https://github.com/anoma/ferveo", rev = "9e5e91c954158e7cff45c483fd06cd649a81553f"}
eyre = "0.6.5"
Expand All @@ -120,6 +120,7 @@ num-traits = "0.2.14"
num_cpus = "1.13.0"
once_cell = "1.8.0"
orion = "0.16.0"
owo-colors = "3.5.0"
parse_duration = "2.1.1"
prost = "0.9.0"
prost-types = "0.9.0"
Expand Down
59 changes: 44 additions & 15 deletions apps/src/lib/client/eth_bridge/bridge_pool.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ use namada::types::eth_bridge_pool::{
};
use namada::types::keccak::KeccakHash;
use namada::types::token::Amount;
use namada::types::voting_power::FractionalVotingPower;
use owo_colors::OwoColorize;
use serde::{Deserialize, Serialize};
use tokio::time::{Duration, Instant};

Expand Down Expand Up @@ -159,20 +161,27 @@ async fn construct_bridge_pool_proof(
.unwrap();

let warnings: Vec<_> = in_progress
.keys()
.filter_map(|k| {
let hash = PendingTransfer::from(k).keccak256();
transfers.contains(&hash).then_some(hash)
.into_iter()
.filter_map(|(ref transfer, voting_power)| {
if voting_power > FractionalVotingPower::ONE_THIRD {
let hash = PendingTransfer::from(transfer).keccak256();
transfers.contains(&hash).then_some(hash)
} else {
None
}
})
.collect();

if !warnings.is_empty() {
let warning = "Warning".on_yellow();
let warning = warning.bold();
let warning = warning.blink();
println!(
"\x1b[93mWarning: The following hashes correspond to transfers \
\nthat have been relayed but do not yet have a quorum of \
\nvalidator signatures; thus they are still in the bridge \
pool:\n\x1b[0m{:?}",
warnings
"{warning}: The following hashes correspond to transfers that \
have surpassed the security threshold in Namada, therefore have \
likely been relayed, but do not yet have a quorum of validator \
signatures behind them; thus they are still in the Bridge \
pool:\n{warnings:?}",
);
print!("\nDo you wish to proceed? (y/n): ");
std::io::stdout().flush().unwrap();
Expand Down Expand Up @@ -278,12 +287,15 @@ pub async fn relay_bridge_pool_proof(args: args::RelayBridgePoolProof) {
.await
{
Ok(address) => Bridge::new(address.address, eth_client),
error => {
Err(err_msg) => {
let error = "Error".on_red();
let error = error.bold();
let error = error.blink();
println!(
"Failed to retreive the Ethereum Bridge smart contract \
address from storage with reason:\n{:?}\n\nPerhaps the \
Ethereum bridge is not active.",
error
"{error}: Failed to retrieve the Ethereum Bridge smart \
contract address from storage with \
reason:\n{err_msg}\n\nPerhaps the Ethereum bridge is not \
active.",
);
safe_exit(1)
}
Expand All @@ -297,6 +309,24 @@ pub async fn relay_bridge_pool_proof(args: args::RelayBridgePoolProof) {
}
};

// NOTE: this operation costs no gas on Ethereum
let contract_nonce =
bridge.transfer_to_erc_20_nonce().call().await.unwrap();

if contract_nonce != bp_proof.batch_nonce {
let warning = "Warning".on_yellow();
let warning = warning.bold();
let warning = warning.blink();
println!(
"{warning}: The Bridge pool nonce in the smart contract is \
{contract_nonce}, while the nonce in Namada is still {}. A relay \
of the former one has already happened, but a proof has yet to \
be crafted in Namada.",
bp_proof.batch_nonce
);
safe_exit(1)
}

let mut relay_op = bridge.transfer_to_erc(bp_proof);
if let Some(gas) = args.gas {
relay_op.tx.set_gas(gas);
Expand Down Expand Up @@ -325,7 +355,6 @@ mod recommendations {
use namada::types::vote_extensions::validator_set_update::{
EthAddrBook, VotingPowersMap, VotingPowersMapExt,
};
use namada::types::voting_power::FractionalVotingPower;

use super::*;
const TRANSFER_FEE: i64 = 37_500;
Expand Down
2 changes: 1 addition & 1 deletion core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ data-encoding = "2.3.2"
derivative = "2.2.0"
ed25519-consensus = "1.2.0"
ethabi = "18.0.0"
ethbridge-structs = { git = "https://github.com/heliaxdev/ethbridge-rs", tag = "v0.17.0" }
ethbridge-structs = { git = "https://github.com/heliaxdev/ethbridge-rs", tag = "v0.18.0" }
eyre = "0.6.8"
ferveo = {optional = true, git = "https://github.com/anoma/ferveo", rev = "9e5e91c954158e7cff45c483fd06cd649a81553f"}
ferveo-common = {git = "https://github.com/anoma/ferveo", rev = "9e5e91c954158e7cff45c483fd06cd649a81553f"}
Expand Down
23 changes: 21 additions & 2 deletions shared/src/ledger/queries/shell/eth_bridge.rs
Original file line number Diff line number Diff line change
Expand Up @@ -365,8 +365,8 @@ where
..Default::default()
})
}
Ok(_) => unreachable!(),
Err(e) => Err(storage_api::Error::new(e)),
_ => unreachable!(),
}
} else {
Err(storage_api::Error::SimpleMessage(
Expand Down Expand Up @@ -404,10 +404,25 @@ where
}
})
{
// we checked above that key is not empty, so this write is fine
*key.segments.last_mut().unwrap() =
DbKeySeg::StringSeg(Keys::segments().seen.into());
// check if the event has been seen
let is_seen = ctx
.wl_storage
.read::<bool>(&key)
.into_storage_result()?
.expect(
"Iterating over storage should not yield keys without values.",
);
if is_seen {
continue;
}

if let Ok(EthereumEvent::TransfersToEthereum { transfers, .. }) =
EthereumEvent::try_from_slice(&value)
{
// We checked above that key is not empty
// read the voting power behind the event
*key.segments.last_mut().unwrap() =
DbKeySeg::StringSeg(Keys::segments().voting_power.into());
let voting_power = ctx
Expand Down Expand Up @@ -1206,6 +1221,10 @@ mod test_ethbridge_router {
.expect("Test failed"),
)
.expect("Test failed");
client
.wl_storage
.write(&eth_msg_key.seen(), false)
.expect("Test failed");
// commit the changes and increase block height
client
.wl_storage
Expand Down
4 changes: 2 additions & 2 deletions wasm/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions wasm_for_tests/wasm_source/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit fd0ef46

Please sign in to comment.