From d98f79049015594d20b210338c53c777501d73c8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Costin=20Caraba=C8=99?= Date: Fri, 11 Oct 2024 09:20:59 +0300 Subject: [PATCH 1/3] Remove "workspace" from mock-contracts --- .../mock-contracts/mock-bridged-tokens-wrapper/Cargo.toml | 8 +------- common/mock-contracts/mock-esdt-safe/Cargo.toml | 8 +------- .../mock-contracts/mock-esdt-safe/src/mock_esdt_safe.rs | 8 +++++++- common/mock-contracts/mock-multi-transfer-esdt/Cargo.toml | 8 +------- common/mock-contracts/mock-multisig/Cargo.toml | 8 +------- common/mock-contracts/mock-price-aggregator/Cargo.toml | 8 +------- 6 files changed, 12 insertions(+), 36 deletions(-) diff --git a/common/mock-contracts/mock-bridged-tokens-wrapper/Cargo.toml b/common/mock-contracts/mock-bridged-tokens-wrapper/Cargo.toml index ea69765a..29bc0f87 100644 --- a/common/mock-contracts/mock-bridged-tokens-wrapper/Cargo.toml +++ b/common/mock-contracts/mock-bridged-tokens-wrapper/Cargo.toml @@ -18,10 +18,4 @@ version = "0.53.2" num-bigint = "0.4" [dev-dependencies.multiversx-sc-scenario] -version = "0.53.2" - -[workspace] -members = [ - ".", - "meta", -] +version = "0.53.2" \ No newline at end of file diff --git a/common/mock-contracts/mock-esdt-safe/Cargo.toml b/common/mock-contracts/mock-esdt-safe/Cargo.toml index fd39629f..68932d4a 100644 --- a/common/mock-contracts/mock-esdt-safe/Cargo.toml +++ b/common/mock-contracts/mock-esdt-safe/Cargo.toml @@ -15,10 +15,4 @@ version = "0.53.2" num-bigint = "0.4" [dev-dependencies.multiversx-sc-scenario] -version = "0.53.2" - -[workspace] -members = [ - ".", - "meta", -] +version = "0.53.2" \ No newline at end of file diff --git a/common/mock-contracts/mock-esdt-safe/src/mock_esdt_safe.rs b/common/mock-contracts/mock-esdt-safe/src/mock_esdt_safe.rs index 383421b4..8f39ae0f 100644 --- a/common/mock-contracts/mock-esdt-safe/src/mock_esdt_safe.rs +++ b/common/mock-contracts/mock-esdt-safe/src/mock_esdt_safe.rs @@ -7,7 +7,13 @@ use multiversx_sc::imports::*; #[multiversx_sc::contract] pub trait MockEsdtSafe { #[init] - fn init(&self) {} + fn init( + &self, + fee_estimator_contract_address: ManagedAddress, + multi_transfer_contract_address: ManagedAddress, + eth_tx_gas_limit: BigUint, + ) { + } #[upgrade] fn upgrade(&self) {} diff --git a/common/mock-contracts/mock-multi-transfer-esdt/Cargo.toml b/common/mock-contracts/mock-multi-transfer-esdt/Cargo.toml index c458d5d0..c14230ea 100644 --- a/common/mock-contracts/mock-multi-transfer-esdt/Cargo.toml +++ b/common/mock-contracts/mock-multi-transfer-esdt/Cargo.toml @@ -15,10 +15,4 @@ version = "0.53.2" num-bigint = "0.4" [dev-dependencies.multiversx-sc-scenario] -version = "0.53.2" - -[workspace] -members = [ - ".", - "meta", -] +version = "0.53.2" \ No newline at end of file diff --git a/common/mock-contracts/mock-multisig/Cargo.toml b/common/mock-contracts/mock-multisig/Cargo.toml index 9a9544df..c4273f9a 100644 --- a/common/mock-contracts/mock-multisig/Cargo.toml +++ b/common/mock-contracts/mock-multisig/Cargo.toml @@ -15,10 +15,4 @@ version = "0.53.2" num-bigint = "0.4" [dev-dependencies.multiversx-sc-scenario] -version = "0.53.2" - -[workspace] -members = [ - ".", - "meta", -] +version = "0.53.2" \ No newline at end of file diff --git a/common/mock-contracts/mock-price-aggregator/Cargo.toml b/common/mock-contracts/mock-price-aggregator/Cargo.toml index 411df769..b13d5ab3 100644 --- a/common/mock-contracts/mock-price-aggregator/Cargo.toml +++ b/common/mock-contracts/mock-price-aggregator/Cargo.toml @@ -15,10 +15,4 @@ version = "0.53.2" num-bigint = "0.4" [dev-dependencies.multiversx-sc-scenario] -version = "0.53.2" - -[workspace] -members = [ - ".", - "meta", -] +version = "0.53.2" \ No newline at end of file From 1e7e3fa38d56f7a3196734b359fa728b488d537e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Costin=20Caraba=C8=99?= Date: Fri, 11 Oct 2024 09:21:12 +0300 Subject: [PATCH 2/3] bridge-proxy: use mock-contracts for blackbox --- Cargo.lock | 11 + bridge-proxy/Cargo.toml | 3 + .../tests/bridge_proxy_blackbox_test.rs | 240 +++++++++++++----- 3 files changed, 189 insertions(+), 65 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 3586125a..f95faf71 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -122,6 +122,7 @@ dependencies = [ "crowdfunding-esdt", "esdt-safe", "eth-address", + "mock-bridged-tokens-wrapper", "multiversx-sc", "multiversx-sc-modules", "multiversx-sc-scenario", @@ -546,6 +547,16 @@ version = "2.7.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "78ca9ab1a0babb1e7d5695e3530886289c18cf2f87ec19a575a0abdce112e3a3" +[[package]] +name = "mock-bridged-tokens-wrapper" +version = "0.0.0" +dependencies = [ + "eth-address", + "multiversx-sc", + "multiversx-sc-scenario", + "num-bigint", +] + [[package]] name = "multi-transfer-esdt" version = "0.0.0" diff --git a/bridge-proxy/Cargo.toml b/bridge-proxy/Cargo.toml index d9293895..56a92acd 100644 --- a/bridge-proxy/Cargo.toml +++ b/bridge-proxy/Cargo.toml @@ -29,6 +29,9 @@ path = "../esdt-safe" [dependencies.bridged-tokens-wrapper] path = "../bridged-tokens-wrapper" +[dependencies.mock-bridged-tokens-wrapper] +path = "../common/mock-contracts/mock-bridged-tokens-wrapper" + [dependencies.multiversx-sc] version = "=0.53.2" diff --git a/bridge-proxy/tests/bridge_proxy_blackbox_test.rs b/bridge-proxy/tests/bridge_proxy_blackbox_test.rs index bcfdd710..78ea0f84 100644 --- a/bridge-proxy/tests/bridge_proxy_blackbox_test.rs +++ b/bridge-proxy/tests/bridge_proxy_blackbox_test.rs @@ -37,7 +37,7 @@ use multiversx_sc_scenario::{ use multiversx_sc_scenario::{ExpectValue, ScenarioTxRun}; use eth_address::*; -use sc_proxies::{bridge_proxy_contract_proxy, bridged_tokens_wrapper_proxy}; +use sc_proxies::{bridge_proxy_contract_proxy, bridged_tokens_wrapper_proxy, esdt_safe_proxy}; use transaction::{CallData, EthTransaction}; const BRIDGE_TOKEN_ID: TestTokenIdentifier = TestTokenIdentifier::new("BRIDGE-123456"); @@ -52,14 +52,15 @@ const CROWDFUNDING_ADDRESS: TestSCAddress = TestSCAddress::new("crowfunding"); const MULTI_TRANSFER_ADDRESS: TestSCAddress = TestSCAddress::new("multi-transfer"); const ESDT_SAFE_ADDRESS: TestSCAddress = TestSCAddress::new("esdt-safe"); const BRIDGED_TOKENS_WRAPPER_ADDRESS: TestSCAddress = TestSCAddress::new("bridged-tokens-wrapper"); +const NO_INIT_SC_ADDRESS: TestSCAddress = TestSCAddress::new("no-init-sc"); const BRIDGE_PROXY_PATH_EXPR: MxscPath = MxscPath::new("output/bridge-proxy.mxsc.json"); const CROWDFUNDING_PATH_EXPR: MxscPath = MxscPath::new("tests/test-contract/crowdfunding-esdt.mxsc.json"); -const MULTI_TRANSFER_PATH_EXPR: &str = - "mxsc:../multi-transfer-esdt/output/multi-transfer-esdt.mxsc.json"; -const ESDT_SAFE_PATH_EXPR: &str = "mxsc:../esdt-safe/output/esdt-safe.mxsc.json"; -const BRIDGED_TOKENS_WRAPPER_CODE_PATH_EXPR: MxscPath = +const MOCK_MULTI_TRANSFER_PATH_EXPR: MxscPath = MxscPath::new("mxsc:../common/mock-contracts/mock-multi-transfer-esdt/output/mock-multi-transfer-esdt.mxsc.json"); +const MOCK_ESDT_SAFE_PATH_EXPR: MxscPath = + MxscPath::new("mxsc:../common/mock-contrats/mock-esdt-safe/output/mock-esdt-safe.mxsc.json"); +const MOCK_BRIDGED_TOKENS_WRAPPER_CODE_PATH_EXPR: MxscPath = MxscPath::new("../common/mock-contracts/mock-bridged-tokens-wrapper/output/mock-bridged-tokens-wrapper.mxsc.json"); fn world() -> ScenarioWorld { @@ -68,17 +69,16 @@ fn world() -> ScenarioWorld { blockchain.register_contract(BRIDGE_PROXY_PATH_EXPR, bridge_proxy::ContractBuilder); blockchain.register_contract(CROWDFUNDING_PATH_EXPR, crowdfunding_esdt::ContractBuilder); blockchain.register_contract( - BRIDGED_TOKENS_WRAPPER_CODE_PATH_EXPR, + MOCK_BRIDGED_TOKENS_WRAPPER_CODE_PATH_EXPR, mock_bridged_tokens_wrapper::ContractBuilder, ); - blockchain.register_contract(ESDT_SAFE_PATH_EXPR, esdt_safe::ContractBuilder); + // blockchain.register_contract(MOCK_ESDT_SAFE_PATH_EXPR, mock_esdt_safe::ContractBuilder); blockchain } type BridgeProxyContract = ContractInfo>; type CrowdfundingContract = ContractInfo>; -type BridgedTokensWrapperContract = ContractInfo>; struct BridgeProxyTestState { world: ScenarioWorld, @@ -87,8 +87,10 @@ struct BridgeProxyTestState { impl BridgeProxyTestState { fn new() -> Self { let mut world = world(); - let multi_transfer_code = world.code_expression(MULTI_TRANSFER_PATH_EXPR); - let esdt_safe_code = world.code_expression(ESDT_SAFE_PATH_EXPR); + let multi_transfer_code = + world.code_expression(MOCK_MULTI_TRANSFER_PATH_EXPR.eval_to_expr().as_str()); + let esdt_safe_code = + world.code_expression(MOCK_ESDT_SAFE_PATH_EXPR.eval_to_expr().as_str()); world .account(OWNER_ADDRESS) @@ -109,15 +111,13 @@ impl BridgeProxyTestState { .account(BRIDGED_TOKENS_WRAPPER_ADDRESS) .esdt_roles(WBRIDGE_TOKEN_ID, roles.clone()) .esdt_roles(BRIDGE_TOKEN_ID, roles) - .esdt_balance(TokenIdentifier::from(WBRIDGE_TOKEN_ID), 10_000u64) - .esdt_balance(TokenIdentifier::from(BRIDGE_TOKEN_ID), 10_000u64) - .code(BRIDGED_TOKENS_WRAPPER_CODE_PATH_EXPR) + .code(MOCK_BRIDGED_TOKENS_WRAPPER_CODE_PATH_EXPR) .owner(OWNER_ADDRESS); Self { world } } - fn bridge_proxy_deploy(&mut self) -> &mut Self { + fn deploy_bridge_proxy(&mut self) -> &mut Self { self.world .tx() .from(OWNER_ADDRESS) @@ -130,13 +130,30 @@ impl BridgeProxyTestState { self } - fn bridged_tokens_wrapper_deploy(&mut self) -> &mut Self { + fn deploy_esdt_safe(&mut self) -> &mut Self { + self.world + .tx() + .from(OWNER_ADDRESS) + .typed(esdt_safe_proxy::EsdtSafeProxy) + .init( + ManagedAddress::zero(), + ManagedAddress::zero(), + BigUint::zero(), + ) + .code(MOCK_ESDT_SAFE_PATH_EXPR) + .new_address(BRIDGE_PROXY_ADDRESS) + .run(); + + self + } + + fn deploy_bridged_tokens_wrapper(&mut self) -> &mut Self { self.world .tx() .from(OWNER_ADDRESS) .typed(bridged_tokens_wrapper_proxy::BridgedTokensWrapperProxy) .init() - .code(BRIDGED_TOKENS_WRAPPER_CODE_PATH_EXPR) + .code(MOCK_BRIDGED_TOKENS_WRAPPER_CODE_PATH_EXPR) .new_address(BRIDGED_TOKENS_WRAPPER_ADDRESS) .run(); @@ -168,13 +185,13 @@ impl BridgeProxyTestState { .unpause_endpoint() .run(); - self.world - .tx() - .from(OWNER_ADDRESS) - .to(BRIDGED_TOKENS_WRAPPER_ADDRESS) - .typed(bridged_tokens_wrapper_proxy::BridgedTokensWrapperProxy) - .unpause_endpoint() - .run(); + // self.world + // .tx() + // .from(OWNER_ADDRESS) + // .to(BRIDGED_TOKENS_WRAPPER_ADDRESS) + // .typed(bridged_tokens_wrapper_proxy::BridgedTokensWrapperProxy) + // .unpause_endpoint() + // .run(); self.world .tx() @@ -186,42 +203,68 @@ impl BridgeProxyTestState { )) .run(); - self.world - .tx() - .from(OWNER_ADDRESS) - .to(BRIDGED_TOKENS_WRAPPER_ADDRESS) - .typed(bridged_tokens_wrapper_proxy::BridgedTokensWrapperProxy) - .whitelist_token(BRIDGE_TOKEN_ID, 18u32, WBRIDGE_TOKEN_ID) - .run(); - - self.world - .tx() - .from(OWNER_ADDRESS) - .to(BRIDGED_TOKENS_WRAPPER_ADDRESS) - .typed(bridged_tokens_wrapper_proxy::BridgedTokensWrapperProxy) - .add_wrapped_token(WBRIDGE_TOKEN_ID, 18u32) - .run(); - - self.world - .tx() - .from(OWNER_ADDRESS) - .to(BRIDGED_TOKENS_WRAPPER_ADDRESS) - .typed(bridged_tokens_wrapper_proxy::BridgedTokensWrapperProxy) - .deposit_liquidity() - .single_esdt( - &TokenIdentifier::from(BRIDGE_TOKEN_ID), - 0u64, - &BigUint::from(5_000u64), - ) - .run(); - - self.world - .tx() - .from(OWNER_ADDRESS) - .to(BRIDGED_TOKENS_WRAPPER_ADDRESS) - .typed(bridged_tokens_wrapper_proxy::BridgedTokensWrapperProxy) - .set_esdt_safe_contract_address(OptionalValue::Some(ESDT_SAFE_ADDRESS)) - .run(); + // self.world + // .tx() + // .from(OWNER_ADDRESS) + // .to(BRIDGED_TOKENS_WRAPPER_ADDRESS) + // .typed(bridged_tokens_wrapper_proxy::BridgedTokensWrapperProxy) + // .whitelist_token(BRIDGE_TOKEN_ID, 18u32, WBRIDGE_TOKEN_ID) + // .run(); + + // let default_price_nonce: OptionalValue> = OptionalValue::None; + // self.world + // .tx() + // .from(OWNER_ADDRESS) + // .to(BRIDGED_TOKENS_WRAPPER_ADDRESS) + // .typed(esdt_safe_proxy::EsdtSafeProxy) + // .add_token_to_whitelist( + // BRIDGE_TOKEN_ID, + // "BRIDGE", + // true, + // false, + // BigUint::zero(), + // BigUint::zero(), + // BigUint::zero(), + // default_price_nonce, + // ) + // .run(); + + // self.world + // .tx() + // .from(OWNER_ADDRESS) + // .to(BRIDGED_TOKENS_WRAPPER_ADDRESS) + // .typed(bridged_tokens_wrapper_proxy::BridgedTokensWrapperProxy) + // .set_bridge_proxy_contract_address(OptionalValue::Some(BRIDGE_PROXY_ADDRESS)) + // .run(); + + // self.world + // .tx() + // .from(OWNER_ADDRESS) + // .to(BRIDGED_TOKENS_WRAPPER_ADDRESS) + // .typed(bridged_tokens_wrapper_proxy::BridgedTokensWrapperProxy) + // .add_wrapped_token(WBRIDGE_TOKEN_ID, 18u32) + // .run(); + + // self.world + // .tx() + // .from(OWNER_ADDRESS) + // .to(BRIDGED_TOKENS_WRAPPER_ADDRESS) + // .typed(bridged_tokens_wrapper_proxy::BridgedTokensWrapperProxy) + // .deposit_liquidity() + // .single_esdt( + // &TokenIdentifier::from(BRIDGE_TOKEN_ID), + // 0u64, + // &BigUint::from(5_000u64), + // ) + // .run(); + + // self.world + // .tx() + // .from(OWNER_ADDRESS) + // .to(BRIDGED_TOKENS_WRAPPER_ADDRESS) + // .typed(bridged_tokens_wrapper_proxy::BridgedTokensWrapperProxy) + // .set_esdt_safe_contract_address(OptionalValue::Some(ESDT_SAFE_ADDRESS)) + // .run(); self } } @@ -230,7 +273,7 @@ impl BridgeProxyTestState { fn deploy_test() { let mut test = BridgeProxyTestState::new(); - test.bridge_proxy_deploy(); + test.deploy_bridge_proxy(); test.deploy_crowdfunding(); test.config_bridge(); } @@ -241,7 +284,7 @@ fn bridge_proxy_execute_crowdfunding_test() { test.world.start_trace(); - test.bridge_proxy_deploy(); + test.deploy_bridge_proxy(); test.deploy_crowdfunding(); test.config_bridge(); @@ -304,15 +347,15 @@ fn bridge_proxy_execute_crowdfunding_test() { .returns(ExpectValue(500u64)) .run(); - test.world - .write_scenario_trace("scenarios/bridge_proxy_execute_crowdfunding.scen.json"); + // test.world + // .write_scenario_trace("scenarios/bridge_proxy_execute_crowdfunding.scen.json"); } #[test] fn multiple_deposit_test() { let mut test = BridgeProxyTestState::new(); - test.bridge_proxy_deploy(); + test.deploy_bridge_proxy(); test.deploy_crowdfunding(); test.config_bridge(); @@ -426,7 +469,7 @@ fn multiple_deposit_test() { fn test_lowest_tx_id() { let mut test = BridgeProxyTestState::new(); - test.bridge_proxy_deploy(); + test.deploy_bridge_proxy(); test.deploy_crowdfunding(); test.config_bridge(); @@ -520,3 +563,70 @@ fn test_lowest_tx_id() { .returns(ExpectValue(51usize)) .run(); } + +#[test] +fn bridge_proxy_wrong_formatting_sc_call_test() { + let mut test = BridgeProxyTestState::new(); + + test.world.start_trace(); + + test.deploy_bridge_proxy(); + test.deploy_crowdfunding(); + test.config_bridge(); + + let mut args = ManagedVec::new(); + + let call_data: CallData = CallData { + endpoint: ManagedBuffer::from("fund"), + gas_limit: GAS_LIMIT, + args: ManagedOption::some(args), + }; + + let call_data: ManagedBuffer = + ManagedSerializer::new().top_encode_to_managed_buffer(&call_data); + + let eth_tx = EthTransaction { + from: EthAddress { + raw_addr: ManagedByteArray::new_from_bytes(b"01020304050607080910"), + }, + to: ManagedAddress::from(NO_INIT_SC_ADDRESS.eval_to_array()), + token_id: BRIDGE_TOKEN_ID.into(), + amount: BigUint::from(500u64), + tx_nonce: 1u64, + call_data: ManagedOption::none(), + }; + + test.world + .tx() + .from(MULTI_TRANSFER_ADDRESS) + .to(BRIDGE_PROXY_ADDRESS) + .typed(bridge_proxy_contract_proxy::BridgeProxyContractProxy) + .deposit(ð_tx) + .egld_or_single_esdt( + &EgldOrEsdtTokenIdentifier::esdt(BRIDGE_TOKEN_ID), + 0, + &BigUint::from(500u64), + ) + .run(); + + test.world + .query() + .to(BRIDGE_PROXY_ADDRESS) + .typed(bridge_proxy_contract_proxy::BridgeProxyContractProxy) + .get_pending_transaction_by_id(1u32) + .returns(ExpectValue(eth_tx)) + .run(); + + test.world + .tx() + .from(OWNER_ADDRESS) + .to(BRIDGE_PROXY_ADDRESS) + .typed(bridge_proxy_contract_proxy::BridgeProxyContractProxy) + .execute(1u32) + .run(); + + // Refund: Funds are transfered to BridgedTokensWrapper + test.world + .check_account(BRIDGED_TOKENS_WRAPPER_ADDRESS) + .esdt_balance(BRIDGE_TOKEN_ID, BigUint::from(500u64)); +} From eb2ac0599ed28af63ef231f0d8a138dc98b0dc27 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Costin=20Caraba=C8=99?= Date: Fri, 11 Oct 2024 09:49:08 +0300 Subject: [PATCH 3/3] Fix compilation issues --- common/mock-contracts/mock-bridge-proxy/Cargo.toml | 6 ------ 1 file changed, 6 deletions(-) diff --git a/common/mock-contracts/mock-bridge-proxy/Cargo.toml b/common/mock-contracts/mock-bridge-proxy/Cargo.toml index 9cc84ad6..a3416287 100644 --- a/common/mock-contracts/mock-bridge-proxy/Cargo.toml +++ b/common/mock-contracts/mock-bridge-proxy/Cargo.toml @@ -16,9 +16,3 @@ num-bigint = "0.4" [dev-dependencies.multiversx-sc-scenario] version = "0.53.2" - -[workspace] -members = [ - ".", - "meta", -]