Skip to content

Commit

Permalink
Merge branch 'tiago/update-ethbridge-rs' (#1789)
Browse files Browse the repository at this point in the history
* origin/tiago/update-ethbridge-rs:
  Changelog for #1789
  Test pending transfers and events have the same ABI encoding
  Fix compilation errors to support new `ethbridge-rs`
  Update ethbridge-rs to v0.22.0
  • Loading branch information
Fraccaman committed Sep 6, 2023
2 parents ac5b5be + 5678ee7 commit 583b334
Show file tree
Hide file tree
Showing 17 changed files with 521 additions and 342 deletions.
2 changes: 2 additions & 0 deletions .changelog/unreleased/features/1789-update-ethbridge-rs.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Update ethbridge-rs to v0.22.0
([\#1789](https://github.com/anoma/namada/pull/1789))
24 changes: 12 additions & 12 deletions Cargo.lock

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

12 changes: 6 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,12 @@ directories = "4.0.1"
ed25519-consensus = "1.2.0"
escargot = "0.5.7"
ethabi = "18.0.0"
ethbridge-bridge-contract = {git = "https://github.com/heliaxdev/ethbridge-rs", tag = "v0.21.0"}
ethbridge-bridge-events = {git = "https://github.com/heliaxdev/ethbridge-rs", tag = "v0.21.0"}
ethbridge-events = {git = "https://github.com/heliaxdev/ethbridge-rs", tag = "v0.21.0"}
ethbridge-governance-contract = {git = "https://github.com/heliaxdev/ethbridge-rs", tag = "v0.21.0"}
ethbridge-governance-events = {git = "https://github.com/heliaxdev/ethbridge-rs", tag = "v0.21.0"}
ethbridge-structs = { git = "https://github.com/heliaxdev/ethbridge-rs", tag = "v0.21.0" }
ethbridge-bridge-contract = {git = "https://github.com/heliaxdev/ethbridge-rs", tag = "v0.22.0"}
ethbridge-bridge-events = {git = "https://github.com/heliaxdev/ethbridge-rs", tag = "v0.22.0"}
ethbridge-events = {git = "https://github.com/heliaxdev/ethbridge-rs", tag = "v0.22.0"}
ethbridge-governance-contract = {git = "https://github.com/heliaxdev/ethbridge-rs", tag = "v0.22.0"}
ethbridge-governance-events = {git = "https://github.com/heliaxdev/ethbridge-rs", tag = "v0.22.0"}
ethbridge-structs = { git = "https://github.com/heliaxdev/ethbridge-rs", tag = "v0.22.0" }
ethers = "2.0.0"
expectrl = "0.7.0"
eyre = "0.6.5"
Expand Down
80 changes: 41 additions & 39 deletions apps/src/lib/node/ledger/ethereum_oracle/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ pub mod eth_events {
use namada::eth_bridge::ethers::contract::EthEvent;
use namada::types::address::Address;
use namada::types::ethereum_events::{
EthAddress, EthereumEvent, TransferToEthereum, TransferToEthereumKind,
TransferToNamada, Uint,
EthAddress, EthereumEvent, TransferToEthereum, TransferToNamada, Uint,
};
use namada::types::hash::Hash;
use namada::types::keccak::KeccakHash;
use namada::types::token::Amount;
use num256::Uint256;
Expand Down Expand Up @@ -153,32 +153,33 @@ pub mod eth_events {
};
}

/// Trait to add parsing methods to foreign types.
trait Parse: Sized {
parse_method! { parse_eth_transfer_kind -> TransferToEthereumKind }
parse_method! { parse_eth_address -> EthAddress }
parse_method! { parse_address -> Address }
parse_method! { parse_amount -> Amount }
parse_method! { parse_u32 -> u32 }
parse_method! { parse_uint256 -> Uint }
parse_method! { parse_bool -> bool }
parse_method! { parse_string -> String }
parse_method! { parse_keccak -> KeccakHash }
parse_method! { parse_amount_array -> Vec<Amount> }
parse_method! { parse_eth_address_array -> Vec<EthAddress> }
parse_method! { parse_address_array -> Vec<Address> }
parse_method! { parse_string_array -> Vec<String> }
parse_method! { parse_transfer_to_namada_array -> Vec<TransferToNamada> }
parse_method! { parse_transfer_to_namada -> TransferToNamada }
parse_method! { parse_transfer_to_eth_array -> Vec<TransferToEthereum> }
parse_method! { parse_transfer_to_eth -> TransferToEthereum }
macro_rules! trait_parse_def {
($($name:ident -> $type:ty;)*) => {
/// Trait to add parsing methods to foreign types.
trait Parse: Sized {
$( parse_method!($name -> $type); )*
}
}
}

impl Parse for u8 {
fn parse_eth_transfer_kind(self) -> Result<TransferToEthereumKind> {
self.try_into()
.map_err(|err| Error::Decode(format!("{:?}", err)))
}
trait_parse_def! {
parse_address -> Address;
parse_address_array -> Vec<Address>;
parse_amount -> Amount;
parse_amount_array -> Vec<Amount>;
parse_bool -> bool;
parse_eth_address -> EthAddress;
parse_eth_address_array -> Vec<EthAddress>;
parse_hash -> Hash;
parse_keccak -> KeccakHash;
parse_string -> String;
parse_string_array -> Vec<String>;
parse_transfer_to_eth -> TransferToEthereum;
parse_transfer_to_eth_array -> Vec<TransferToEthereum>;
parse_transfer_to_namada -> TransferToNamada;
parse_transfer_to_namada_array -> Vec<TransferToNamada>;
parse_u32 -> u32;
parse_uint256 -> Uint;
}

impl Parse for ethabi::Address {
Expand All @@ -200,7 +201,13 @@ pub mod eth_events {

impl Parse for ethabi::Uint {
fn parse_amount(self) -> Result<Amount> {
Ok(Amount::from(self.as_u64()))
let uint = {
use namada::core::types::uint::Uint as NamadaUint;
let mut num_buf = [0; 32];
self.to_little_endian(&mut num_buf);
NamadaUint::from_little_endian(&num_buf)
};
Amount::from_uint(uint, 0).map_err(|e| Error::Decode(e.to_string()))
}

fn parse_u32(self) -> Result<u32> {
Expand All @@ -222,6 +229,10 @@ pub mod eth_events {
fn parse_keccak(self) -> Result<KeccakHash> {
Ok(KeccakHash(self))
}

fn parse_hash(self) -> Result<Hash> {
Ok(Hash(self))
}
}

impl Parse for Vec<ethabi::Uint> {
Expand Down Expand Up @@ -279,21 +290,15 @@ pub mod eth_events {

impl Parse for ethereum_structs::Erc20Transfer {
fn parse_transfer_to_eth(self) -> Result<TransferToEthereum> {
let kind = self.kind.parse_eth_transfer_kind()?;
let asset = self.from.parse_eth_address()?;
let receiver = self.to.parse_eth_address()?;
let sender = self.sender.parse_address()?;
let amount = self.amount.parse_amount()?;
let gas_payer = self.fee_from.parse_address()?;
let gas_amount = self.fee.parse_amount()?;
let checksum = self.namada_data_digest.parse_hash()?;
Ok(TransferToEthereum {
kind,
asset,
amount,
sender,
receiver,
gas_amount,
gas_payer,
checksum,
})
}
}
Expand Down Expand Up @@ -509,13 +514,10 @@ pub mod eth_events {
let eth_transfers = TransferToErcFilter {
transfers: vec![
ethereum_structs::Erc20Transfer {
kind: TransferToEthereumKind::Erc20 as u8,
from: H160([1; 20]),
to: H160([2; 20]),
sender: address.clone(),
amount: 0u64.into(),
fee_from: address.clone(),
fee: 0u64.into(),
namada_data_digest: [0; 32],
};
2
],
Expand Down
15 changes: 4 additions & 11 deletions apps/src/lib/node/ledger/ethereum_oracle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,9 +569,8 @@ mod test_oracle {
use namada::eth_bridge::ethers::types::H160;
use namada::eth_bridge::structs::Erc20Transfer;
use namada::types::address::testing::gen_established_address;
use namada::types::ethereum_events::{
EthAddress, TransferToEthereum, TransferToEthereumKind,
};
use namada::types::ethereum_events::{EthAddress, TransferToEthereum};
use namada::types::hash::Hash;
use tokio::sync::oneshot::channel;
use tokio::time::timeout;

Expand Down Expand Up @@ -828,13 +827,10 @@ mod test_oracle {
let gas_payer = gen_established_address();
let second_event = TransferToErcFilter {
transfers: vec![Erc20Transfer {
kind: TransferToEthereumKind::Erc20 as u8,
amount: 0.into(),
from: H160([0; 20]),
sender: gas_payer.to_string(),
to: H160([1; 20]),
fee: 0.into(),
fee_from: gas_payer.to_string(),
namada_data_digest: [0; 32],
}],
valid_map: vec![true],
relayer_address: gas_payer.to_string(),
Expand Down Expand Up @@ -898,13 +894,10 @@ mod test_oracle {
assert_eq!(
transfer,
TransferToEthereum {
kind: TransferToEthereumKind::Erc20,
amount: Default::default(),
asset: EthAddress([0; 20]),
sender: gas_payer.clone(),
receiver: EthAddress([1; 20]),
gas_amount: Default::default(),
gas_payer: gas_payer.clone(),
checksum: Hash::default(),
}
);
} else {
Expand Down
31 changes: 18 additions & 13 deletions apps/src/lib/node/ledger/shell/finalize_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1099,9 +1099,7 @@ mod test_finalize_block {
};
use namada::proto::{Code, Data, Section, Signature};
use namada::types::dec::POS_DECIMAL_PRECISION;
use namada::types::ethereum_events::{
EthAddress, TransferToEthereum, TransferToEthereumKind, Uint as ethUint,
};
use namada::types::ethereum_events::{EthAddress, Uint as ethUint};
use namada::types::hash::Hash;
use namada::types::keccak::KeccakHash;
use namada::types::key::tm_consensus_key_raw_hash;
Expand Down Expand Up @@ -1784,17 +1782,24 @@ mod test_finalize_block {
}
// write transfer to storage
let transfer = {
use namada::core::types::eth_bridge_pool::PendingTransfer;
let transfer = TransferToEthereum {
kind: TransferToEthereumKind::Erc20,
amount: 10u64.into(),
asset,
receiver,
gas_amount: 10u64.into(),
sender: bertha.clone(),
gas_payer: bertha.clone(),
use namada::core::types::eth_bridge_pool::{
GasFee, PendingTransfer, TransferToEthereum,
TransferToEthereumKind,
};
let pending = PendingTransfer {
transfer: TransferToEthereum {
kind: TransferToEthereumKind::Erc20,
amount: 10u64.into(),
asset,
recipient: receiver,
sender: bertha.clone(),
},
gas_fee: GasFee {
amount: 10u64.into(),
payer: bertha.clone(),
},
};
let pending = PendingTransfer::from(&transfer);
let transfer = (&pending).into();
shell
.wl_storage
.write(&bridge_pool::get_pending_key(&pending), pending)
Expand Down
Loading

0 comments on commit 583b334

Please sign in to comment.