From 6e909a29b3174923be92e302ed9540f79860aa58 Mon Sep 17 00:00:00 2001 From: Tiago Carvalho Date: Wed, 2 Aug 2023 15:48:27 +0100 Subject: [PATCH] wip: new bridge pool tests ( S Q U A S H M E P L S ) --- .../ethereum_bridge/bridge_pool_vp.rs | 61 ++++++++++++++++++- 1 file changed, 59 insertions(+), 2 deletions(-) diff --git a/shared/src/ledger/native_vp/ethereum_bridge/bridge_pool_vp.rs b/shared/src/ledger/native_vp/ethereum_bridge/bridge_pool_vp.rs index aa177181c04..dcb2570aae1 100644 --- a/shared/src/ledger/native_vp/ethereum_bridge/bridge_pool_vp.rs +++ b/shared/src/ledger/native_vp/ethereum_bridge/bridge_pool_vp.rs @@ -518,6 +518,8 @@ mod test_bridge_pool_vp { /// A set of balances for an address struct Balance { + /// The address of the Ethereum asset. + asset: EthAddress, /// NUT or ERC20 Ethereum asset kind. kind: TransferToEthereumKind, /// The owner of the ERC20 assets. @@ -533,6 +535,7 @@ mod test_bridge_pool_vp { fn new(kind: TransferToEthereumKind, address: Address) -> Self { Self { kind, + asset: ASSET, owner: address, gas: 0.into(), token: 0.into(), @@ -655,8 +658,12 @@ mod test_bridge_pool_vp { // get the balance keys let token_key = balance_key( &match balance.kind { - TransferToEthereumKind::Erc20 => wrapped_erc20s::token(&ASSET), - TransferToEthereumKind::Nut => wrapped_erc20s::nut(&ASSET), + TransferToEthereumKind::Erc20 => { + wrapped_erc20s::token(&balance.asset) + } + TransferToEthereumKind::Nut => { + wrapped_erc20s::nut(&balance.asset) + } }, &balance.owner, ); @@ -791,6 +798,7 @@ mod test_bridge_pool_vp { let mut new_keys_changed = update_balances( &mut wl_storage.write_log, Balance { + asset: transfer.transfer.asset, kind: TransferToEthereumKind::Erc20, owner: bertha_address(), gas: BERTHA_WEALTH.into(), @@ -805,6 +813,7 @@ mod test_bridge_pool_vp { let mut new_keys_changed = update_balances( &mut wl_storage.write_log, Balance { + asset: transfer.transfer.asset, kind: TransferToEthereumKind::Erc20, owner: BRIDGE_POOL_ADDRESS, gas: ESCROWED_AMOUNT.into(), @@ -1132,6 +1141,7 @@ mod test_bridge_pool_vp { let mut new_keys_changed = update_balances( &mut wl_storage.write_log, Balance { + asset: ASSET, kind: TransferToEthereumKind::Erc20, owner: bertha_address(), gas: BERTHA_WEALTH.into(), @@ -1146,6 +1156,7 @@ mod test_bridge_pool_vp { let mut new_keys_changed = update_balances( &mut wl_storage.write_log, Balance { + asset: ASSET, kind: TransferToEthereumKind::Erc20, owner: BRIDGE_POOL_ADDRESS, gas: ESCROWED_AMOUNT.into(), @@ -1569,6 +1580,7 @@ mod test_bridge_pool_vp { &mut wl_storage.write_log, Balance { kind, + asset: ASSET, owner: daewon_address(), gas: DAEWONS_GAS.into(), token: DAES_NUTS.into(), @@ -1583,6 +1595,7 @@ mod test_bridge_pool_vp { &mut wl_storage.write_log, Balance { kind, + asset: ASSET, owner: BRIDGE_POOL_ADDRESS, gas: ESCROWED_AMOUNT.into(), token: ESCROWED_NUTS.into(), @@ -1628,4 +1641,48 @@ mod test_bridge_pool_vp { fn test_escrowing_nuts_happy_flow() { test_nut_aux(TransferToEthereumKind::Nut, Expect::True) } + + /// Test that the Bridge pool VP rejects a wNAM NUT transfer. + #[test] + fn test_bridge_pool_vp_rejects_wnam_nut() { + assert_bridge_pool( + SignedAmount::Negative(GAS_FEE.into()), + SignedAmount::Positive(GAS_FEE.into()), + SignedAmount::Negative(TOKENS.into()), + SignedAmount::Positive(TOKENS.into()), + |transfer, log| { + transfer.transfer.kind = TransferToEthereumKind::Nut; + transfer.transfer.asset = wnam(); + log.write( + &get_pending_key(transfer), + transfer.try_to_vec().unwrap(), + ) + .unwrap(); + BTreeSet::from([get_pending_key(transfer)]) + }, + Expect::False, + ); + } + + /// Test that the Bridge pool VP accepts a wNAM ERC20 transfer. + #[test] + fn test_bridge_pool_vp_accepts_wnam_erc20() { + assert_bridge_pool( + SignedAmount::Negative(GAS_FEE.into()), + SignedAmount::Positive(GAS_FEE.into()), + SignedAmount::Negative(TOKENS.into()), + SignedAmount::Positive(TOKENS.into()), + |transfer, log| { + transfer.transfer.kind = TransferToEthereumKind::Erc20; + transfer.transfer.asset = wnam(); + log.write( + &get_pending_key(transfer), + transfer.try_to_vec().unwrap(), + ) + .unwrap(); + BTreeSet::from([get_pending_key(transfer)]) + }, + Expect::True, + ); + } }