Skip to content

Commit

Permalink
Merge branch 'tiago/token-whitelist' (#1755)
Browse files Browse the repository at this point in the history
  • Loading branch information
sug0 committed Aug 30, 2023
2 parents 368d013 + 20044d0 commit 4122caf
Show file tree
Hide file tree
Showing 38 changed files with 1,434 additions and 307 deletions.
2 changes: 2 additions & 0 deletions .changelog/unreleased/features/1290-token-whitelist.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
- Implement Ethereum token whitelist.
([\#1290](https://github.com/anoma/namada/issues/1290))
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.

6 changes: 6 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +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" }
ethers = "2.0.0"
expectrl = "0.7.0"
eyre = "0.6.5"
Expand Down
6 changes: 3 additions & 3 deletions apps/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ derivative.workspace = true
directories.workspace = true
ed25519-consensus.workspace = true
ethabi.workspace = true
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-events = {git = "https://github.com/heliaxdev/ethbridge-rs", tag = "v0.18.0"}
ethbridge-bridge-events.workspace = true
ethbridge-events.workspace = true
ethbridge-governance-events.workspace = true
eyre.workspace = true
fd-lock.workspace = true
ferveo-common.workspace = true
Expand Down
8 changes: 8 additions & 0 deletions apps/src/lib/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2563,6 +2563,7 @@ pub mod args {
pub const NET_ADDRESS: Arg<SocketAddr> = arg("net-address");
pub const NAMADA_START_TIME: ArgOpt<DateTimeUtc> = arg_opt("time");
pub const NO_CONVERSIONS: ArgFlag = flag("no-conversions");
pub const NUT: ArgFlag = flag("nut");
pub const OUT_FILE_PATH_OPT: ArgOpt<PathBuf> = arg_opt("out-file-path");
pub const OUTPUT_FOLDER_PATH: ArgOpt<PathBuf> =
arg_opt("output-folder-path");
Expand Down Expand Up @@ -2838,6 +2839,7 @@ pub mod args {
impl CliToSdk<EthereumBridgePool<SdkTypes>> for EthereumBridgePool<CliTypes> {
fn to_sdk(self, ctx: &mut Context) -> EthereumBridgePool<SdkTypes> {
EthereumBridgePool::<SdkTypes> {
nut: self.nut,
tx: self.tx.to_sdk(ctx),
asset: self.asset,
recipient: self.recipient,
Expand All @@ -2864,6 +2866,7 @@ pub mod args {
});
let fee_payer = BRIDGE_GAS_PAYER.parse(matches);
let code_path = PathBuf::from(TX_BRIDGE_POOL_WASM);
let nut = NUT.parse(matches);
Self {
tx,
asset,
Expand All @@ -2873,6 +2876,7 @@ pub mod args {
fee_amount,
fee_payer,
code_path,
nut,
}
}

Expand Down Expand Up @@ -2906,6 +2910,10 @@ pub mod args {
"The Namada address of the account paying the fee for the \
Ethereum transaction.",
))
.arg(NUT.def().help(
"Add Non Usable Tokens (NUTs) to the Bridge pool. These \
are usually obtained from invalid transfers to Namada.",
))
}
}

Expand Down
12 changes: 11 additions & 1 deletion apps/src/lib/config/genesis.rs
Original file line number Diff line number Diff line change
Expand Up @@ -924,10 +924,13 @@ pub fn genesis(
}
#[cfg(any(test, feature = "dev"))]
pub fn genesis(num_validators: u64) -> Genesis {
use namada::ledger::eth_bridge::{Contracts, UpgradeableContract};
use namada::ledger::eth_bridge::{
Contracts, Erc20WhitelistEntry, UpgradeableContract,
};
use namada::types::address::{
self, apfel, btc, dot, eth, kartoffel, nam, schnitzel, wnam,
};
use namada::types::ethereum_events::testing::DAI_ERC20_ETH_ADDRESS;
use namada::types::ethereum_events::EthAddress;

use crate::wallet;
Expand Down Expand Up @@ -1132,6 +1135,13 @@ pub fn genesis(num_validators: u64) -> Genesis {
gov_params: GovernanceParameters::default(),
pgf_params: PgfParameters::default(),
ethereum_bridge_params: Some(EthereumBridgeConfig {
erc20_whitelist: vec![Erc20WhitelistEntry {
token_address: DAI_ERC20_ETH_ADDRESS,
token_cap: token::DenominatedAmount {
amount: token::Amount::max(),
denom: 18.into(),
},
}],
eth_start_height: Default::default(),
min_confirmations: Default::default(),
contracts: Contracts {
Expand Down
61 changes: 15 additions & 46 deletions apps/src/lib/node/ledger/ethereum_oracle/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,14 @@ pub mod eth_events {
};
use ethbridge_events::{DynEventCodec, Events as RawEvents};
use ethbridge_governance_events::{
GovernanceEvents, NewContractFilter, UpdateBridgeWhitelistFilter,
UpgradedContractFilter, ValidatorSetUpdateFilter,
GovernanceEvents, NewContractFilter, UpgradedContractFilter,
ValidatorSetUpdateFilter,
};
use namada::core::types::ethereum_structs;
use namada::eth_bridge::ethers::contract::EthEvent;
use namada::types::address::Address;
use namada::types::ethereum_events::{
EthAddress, EthereumEvent, TokenWhitelist, TransferToEthereum,
EthAddress, EthereumEvent, TransferToEthereum, TransferToEthereumKind,
TransferToNamada, Uint,
};
use namada::types::keccak::KeccakHash;
Expand Down Expand Up @@ -106,31 +106,6 @@ pub mod eth_events {
NewContractFilter::name().into(),
));
}
RawEvents::Governance(
GovernanceEvents::UpdateBridgeWhitelistFilter(
UpdateBridgeWhitelistFilter {
nonce,
tokens,
token_cap,
},
),
) => {
let mut whitelist = vec![];

for (token, cap) in
tokens.into_iter().zip(token_cap.into_iter())
{
whitelist.push(TokenWhitelist {
token: token.parse_eth_address()?,
cap: cap.parse_amount()?,
});
}

EthereumEvent::UpdateBridgeWhitelist {
nonce: nonce.parse_uint256()?,
whitelist,
}
}
RawEvents::Governance(
GovernanceEvents::UpgradedContractFilter(
UpgradedContractFilter { name: _, addr: _ },
Expand Down Expand Up @@ -180,6 +155,7 @@ 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 }
Expand All @@ -198,6 +174,13 @@ pub mod eth_events {
parse_method! { parse_transfer_to_eth -> TransferToEthereum }
}

impl Parse for u8 {
fn parse_eth_transfer_kind(self) -> Result<TransferToEthereumKind> {
self.try_into()
.map_err(|err| Error::Decode(format!("{:?}", err)))
}
}

impl Parse for ethabi::Address {
fn parse_eth_address(self) -> Result<EthAddress> {
Ok(EthAddress(self.0))
Expand Down Expand Up @@ -296,13 +279,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()?;
Ok(TransferToEthereum {
kind,
asset,
amount,
sender,
Expand Down Expand Up @@ -332,7 +317,7 @@ pub mod eth_events {
use ethabi::ethereum_types::{H160, U256};
use ethbridge_events::{
TRANSFER_TO_ERC_CODEC, TRANSFER_TO_NAMADA_CODEC,
UPDATE_BRIDGE_WHITELIST_CODEC, VALIDATOR_SET_UPDATE_CODEC,
VALIDATOR_SET_UPDATE_CODEC,
};
use namada::eth_bridge::ethers::abi::AbiEncode;

Expand Down Expand Up @@ -524,6 +509,7 @@ 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(),
Expand All @@ -542,11 +528,6 @@ pub mod eth_events {
bridge_validator_set_hash: [1; 32],
governance_validator_set_hash: [2; 32],
};
let whitelist = UpdateBridgeWhitelistFilter {
nonce: 0u64.into(),
tokens: vec![H160([0; 20]); 2],
token_cap: vec![0u64.into(); 2],
};
assert_eq!(
{
let decoded: TransferToNamadaFilter =
Expand Down Expand Up @@ -582,18 +563,6 @@ pub mod eth_events {
},
update
);
assert_eq!(
{
let decoded: UpdateBridgeWhitelistFilter =
UPDATE_BRIDGE_WHITELIST_CODEC
.decode(&get_log(whitelist.clone().encode()))
.expect("Test failed")
.try_into()
.expect("Test failed");
decoded
},
whitelist
);
}

/// Return an Ethereum events log, from the given encoded event
Expand Down
6 changes: 5 additions & 1 deletion apps/src/lib/node/ledger/ethereum_oracle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -569,7 +569,9 @@ 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};
use namada::types::ethereum_events::{
EthAddress, TransferToEthereum, TransferToEthereumKind,
};
use tokio::sync::oneshot::channel;
use tokio::time::timeout;

Expand Down Expand Up @@ -826,6 +828,7 @@ 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(),
Expand Down Expand Up @@ -895,6 +898,7 @@ mod test_oracle {
assert_eq!(
transfer,
TransferToEthereum {
kind: TransferToEthereumKind::Erc20,
amount: Default::default(),
asset: EthAddress([0; 20]),
sender: gas_payer.clone(),
Expand Down
3 changes: 2 additions & 1 deletion apps/src/lib/node/ledger/shell/finalize_block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1100,7 +1100,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, Uint as ethUint,
EthAddress, TransferToEthereum, TransferToEthereumKind, Uint as ethUint,
};
use namada::types::hash::Hash;
use namada::types::keccak::KeccakHash;
Expand Down Expand Up @@ -1786,6 +1786,7 @@ mod test_finalize_block {
let transfer = {
use namada::core::types::eth_bridge_pool::PendingTransfer;
let transfer = TransferToEthereum {
kind: TransferToEthereumKind::Erc20,
amount: 10u64.into(),
asset,
receiver,
Expand Down
23 changes: 6 additions & 17 deletions apps/src/lib/node/ledger/shell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ use std::rc::Rc;
use borsh::{BorshDeserialize, BorshSerialize};
use masp_primitives::transaction::Transaction;
use namada::core::ledger::eth_bridge;
use namada::ledger::eth_bridge::{EthBridgeQueries, EthereumBridgeConfig};
use namada::ledger::eth_bridge::{EthBridgeQueries, EthereumOracleConfig};
use namada::ledger::events::log::EventLog;
use namada::ledger::events::Event;
use namada::ledger::gas::{Gas, TxGasMeter};
Expand Down Expand Up @@ -969,27 +969,16 @@ where
);
return;
}
let Some(config) = EthereumBridgeConfig::read(&self.wl_storage) else {
tracing::info!(
"Not starting oracle as the Ethereum bridge config couldn't be found in storage"
);
return;
};
let config = EthereumOracleConfig::read(&self.wl_storage).expect(
"The oracle config must be present in storage, since the \
bridge is enabled",
);
let start_block = self
.wl_storage
.storage
.ethereum_height
.clone()
.unwrap_or_else(|| {
self.wl_storage
.read(&eth_bridge::storage::eth_start_height_key())
.expect(
"Failed to read Ethereum start height from storage",
)
.expect(
"The Ethereum start height should be in storage",
)
});
.unwrap_or(config.eth_start_height);
tracing::info!(
?start_block,
"Found Ethereum height from which the Ethereum oracle should \
Expand Down
Loading

0 comments on commit 4122caf

Please sign in to comment.