Skip to content

Commit

Permalink
Merge pull request #226 from multiversx/bridge-proxy-more-unit-tests
Browse files Browse the repository at this point in the history
Bridge proxy & multisig more unit tests
  • Loading branch information
evelinemolnar authored Nov 8, 2024
2 parents a1d8ef6 + cb4c3a7 commit 4bb7379
Show file tree
Hide file tree
Showing 5 changed files with 375 additions and 16 deletions.
3 changes: 2 additions & 1 deletion Cargo.lock

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

190 changes: 183 additions & 7 deletions bridge-proxy/tests/bridge_proxy_blackbox_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ const BRIDGE_TOKEN_ID: TestTokenIdentifier = TestTokenIdentifier::new("BRIDGE-12
const WBRIDGE_TOKEN_ID: TestTokenIdentifier = TestTokenIdentifier::new("WBRIDGE-123456");

const GAS_LIMIT: u64 = 10_000_000;
const TOO_SMALL_GAS_LIMIT: u64 = 1_000_000;

const CF_DEADLINE: u64 = 7 * 24 * 60 * 60; // 1 week in seconds

const OWNER_ADDRESS: TestAddress = TestAddress::new("owner");
Expand Down Expand Up @@ -544,10 +546,115 @@ fn bridge_proxy_wrong_formatting_sc_call_test() {
test.deploy_crowdfunding();
test.config_bridge();

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(),
};

let amount = BigUint::from(500u64);
// Destination is not an initialized contract
test.world
.tx()
.from(MULTI_TRANSFER_ADDRESS)
.to(BRIDGE_PROXY_ADDRESS)
.typed(bridge_proxy_contract_proxy::BridgeProxyContractProxy)
.deposit(&eth_tx, 1u64)
.egld_or_single_esdt(
&EgldOrEsdtTokenIdentifier::esdt(BRIDGE_TOKEN_ID),
0,
&amount,
)
.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, amount.clone());

// Wrong endpoint for callData
let mut args = ManagedVec::new();
let call_data: CallData<StaticApi> = CallData {
endpoint: ManagedBuffer::from(b"nofunc"),
gas_limit: GAS_LIMIT,
args: ManagedOption::some(args),
};

let call_data: ManagedBuffer<StaticApi> =
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(CROWDFUNDING_ADDRESS.eval_to_array()),
token_id: BRIDGE_TOKEN_ID.into(),
amount: amount.clone(),
tx_nonce: 2u64,
call_data: ManagedOption::some(call_data),
};

test.world
.tx()
.from(MULTI_TRANSFER_ADDRESS)
.to(BRIDGE_PROXY_ADDRESS)
.typed(bridge_proxy_contract_proxy::BridgeProxyContractProxy)
.deposit(&eth_tx, 1u64)
.egld_or_single_esdt(
&EgldOrEsdtTokenIdentifier::esdt(BRIDGE_TOKEN_ID),
0,
&amount,
)
.run();

test.world
.query()
.to(BRIDGE_PROXY_ADDRESS)
.typed(bridge_proxy_contract_proxy::BridgeProxyContractProxy)
.get_pending_transaction_by_id(2u32)
.returns(ExpectValue(eth_tx))
.run();

test.world
.tx()
.from(OWNER_ADDRESS)
.to(BRIDGE_PROXY_ADDRESS)
.typed(bridge_proxy_contract_proxy::BridgeProxyContractProxy)
.execute(2u32)
.run();

// Refund: Funds are transfered to BridgedTokensWrapper
test.world
.check_account(BRIDGED_TOKENS_WRAPPER_ADDRESS)
.esdt_balance(BRIDGE_TOKEN_ID, amount.clone() * 2u64);

// Wrong args
let mut args = ManagedVec::new();
args.push(ManagedBuffer::from(b"wrongargs"));

let call_data: CallData<StaticApi> = CallData {
endpoint: ManagedBuffer::from("fund"),
endpoint: ManagedBuffer::from(b"fund"),
gas_limit: GAS_LIMIT,
args: ManagedOption::some(args),
};
Expand All @@ -559,13 +666,82 @@ fn bridge_proxy_wrong_formatting_sc_call_test() {
from: EthAddress {
raw_addr: ManagedByteArray::new_from_bytes(b"01020304050607080910"),
},
to: ManagedAddress::from(NO_INIT_SC_ADDRESS.eval_to_array()),
to: ManagedAddress::from(CROWDFUNDING_ADDRESS.eval_to_array()),
token_id: BRIDGE_TOKEN_ID.into(),
amount: amount.clone(),
tx_nonce: 3u64,
call_data: ManagedOption::some(call_data),
};

test.world
.tx()
.from(MULTI_TRANSFER_ADDRESS)
.to(BRIDGE_PROXY_ADDRESS)
.typed(bridge_proxy_contract_proxy::BridgeProxyContractProxy)
.deposit(&eth_tx, 1u64)
.egld_or_single_esdt(
&EgldOrEsdtTokenIdentifier::esdt(BRIDGE_TOKEN_ID),
0,
&amount,
)
.run();

test.world
.query()
.to(BRIDGE_PROXY_ADDRESS)
.typed(bridge_proxy_contract_proxy::BridgeProxyContractProxy)
.get_pending_transaction_by_id(3u32)
.returns(ExpectValue(eth_tx))
.run();

test.world
.tx()
.from(OWNER_ADDRESS)
.to(BRIDGE_PROXY_ADDRESS)
.typed(bridge_proxy_contract_proxy::BridgeProxyContractProxy)
.execute(3u32)
.run();

// Refund: Funds are transfered to BridgedTokensWrapper
test.world
.check_account(BRIDGED_TOKENS_WRAPPER_ADDRESS)
.esdt_balance(BRIDGE_TOKEN_ID, amount * 3u64);
}

#[test]
fn bridge_proxy_too_small_gas_sc_call_test() {
let mut test = BridgeProxyTestState::new();

test.world.start_trace();

test.multisig_deploy();
test.deploy_bridge_proxy();
test.deploy_crowdfunding();
test.config_bridge();

let mut args = ManagedVec::new();
let call_data: CallData<StaticApi> = CallData {
endpoint: ManagedBuffer::from(b"fund"),
gas_limit: TOO_SMALL_GAS_LIMIT,
args: ManagedOption::some(args),
};

let call_data: ManagedBuffer<StaticApi> =
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(CROWDFUNDING_ADDRESS.eval_to_array()),
token_id: BRIDGE_TOKEN_ID.into(),
amount: BigUint::from(500u64),
tx_nonce: 1u64,
call_data: ManagedOption::none(),
call_data: ManagedOption::some(call_data),
};

let amount = BigUint::from(500u64);
// Destination is not an initialized contract
test.world
.tx()
.from(MULTI_TRANSFER_ADDRESS)
Expand All @@ -575,7 +751,7 @@ fn bridge_proxy_wrong_formatting_sc_call_test() {
.egld_or_single_esdt(
&EgldOrEsdtTokenIdentifier::esdt(BRIDGE_TOKEN_ID),
0,
&BigUint::from(500u64),
&amount,
)
.run();

Expand All @@ -595,8 +771,8 @@ fn bridge_proxy_wrong_formatting_sc_call_test() {
.execute(1u32)
.run();

// Refund: Funds are transfered to BridgedTokensWrapper
// Refund: Funds are transfered to EsdtSafe
test.world
.check_account(BRIDGED_TOKENS_WRAPPER_ADDRESS)
.esdt_balance(BRIDGE_TOKEN_ID, BigUint::from(500u64));
.check_account(ESDT_SAFE_ADDRESS)
.esdt_balance(BRIDGE_TOKEN_ID, amount.clone());
}
11 changes: 7 additions & 4 deletions common/mock-contracts/mock-esdt-safe/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,13 @@ authors = ["you"]
path = "src/mock_esdt_safe.rs"

[dependencies.multiversx-sc]
version = "0.53.2"
version = "=0.53.2"

[dev-dependencies]
num-bigint = "0.4"
[dependencies.multiversx-sc-modules]
version = "=0.53.2"

[dev-dependencies.multiversx-sc-scenario]
version = "0.53.2"
version = "=0.53.2"

[dependencies.eth-address]
path = "../../eth-address"
22 changes: 20 additions & 2 deletions common/mock-contracts/mock-esdt-safe/src/mock_esdt_safe.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,16 @@
#![no_std]
multiversx_sc::imports!();
multiversx_sc::derive_imports!();

#[allow(unused_imports)]
use multiversx_sc::imports::*;
use eth_address::EthAddress;

#[type_abi]
#[derive(TopEncode, TopDecode, NestedEncode, NestedDecode, Clone, ManagedVecItem, PartialEq)]
pub struct RefundInfo<M: ManagedTypeApi> {
pub address: ManagedAddress<M>,
pub initial_batch_id: u64,
pub initial_nonce: u64,
}

/// An empty contract. To be used as a template when starting a new contract from scratch.
#[multiversx_sc::contract]
Expand All @@ -17,4 +26,13 @@ pub trait MockEsdtSafe {

#[upgrade]
fn upgrade(&self) {}

#[payable("*")]
#[endpoint(createTransaction)]
fn create_transaction(
&self,
_to: EthAddress<Self::Api>,
_opt_refund_info: OptionalValue<RefundInfo<Self::Api>>,
) {
}
}
Loading

0 comments on commit 4bb7379

Please sign in to comment.