Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add more unit tests #267

Open
wants to merge 4 commits into
base: feat/v3.1
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
175 changes: 174 additions & 1 deletion bridge-proxy/tests/bridge_proxy_blackbox_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use bridge_proxy::config::ProxyTrait as _;
use crowdfunding_esdt::crowdfunding_esdt_proxy;
use multiversx_sc::codec::NestedEncode;
use multiversx_sc::contract_base::ManagedSerializer;
use multiversx_sc::sc_print;
use multiversx_sc::imports::MultiValue2;
use multiversx_sc::types::{
EgldOrEsdtTokenIdentifier, EsdtTokenPayment, ManagedOption, MultiValueEncoded,
ReturnsNewAddress, ReturnsResult, TestAddress, TestSCAddress, TestTokenIdentifier,
Expand All @@ -25,6 +25,7 @@ use multiversx_sc::{
ManagedByteArray, ManagedVec, TokenIdentifier,
},
};
use multiversx_sc::{require, sc_print};
use multiversx_sc_scenario::imports::MxscPath;
use multiversx_sc_scenario::{
api::StaticApi,
Expand Down Expand Up @@ -1105,3 +1106,175 @@ fn bridge_proxy_refund_tx_test() {
.check_account(BRIDGE_PROXY_ADDRESS)
.check_storage("str:highestTxId", "1");
}

#[test]
fn bridge_proxy_double_execute_same_tx_test() {
let mut test = BridgeProxyTestState::new();
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("fund"),
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: BigUint::from(500u64),
tx_nonce: 1u64,
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,
&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)
.gas(200_000_000)
.typed(bridge_proxy_contract_proxy::BridgeProxyContractProxy)
.execute(1u32)
.run();

test.world
.query()
.to(CROWDFUNDING_ADDRESS)
.typed(crowdfunding_esdt_proxy::CrowdfundingProxy)
.get_current_funds()
.returns(ExpectValue(500u64))
.run();

test.world
.tx()
.from(OWNER_ADDRESS)
.to(BRIDGE_PROXY_ADDRESS)
.gas(200_000_000)
.typed(bridge_proxy_contract_proxy::BridgeProxyContractProxy)
.execute(1u32)
.returns(ExpectError(4, "Invalid tx id"))
.run();
}

#[test]
fn bridge_proxy_execute_1000_even_tx_test() {
let mut test = BridgeProxyTestState::new();
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("fund"),
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: BigUint::from(5u64),
tx_nonce: 1u64,
call_data: ManagedOption::some(call_data),
};

for _ in 1..101 {
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,
&BigUint::from(5u64),
)
.run();
}

for i in 1..101u32 {
test.world
.query()
.to(BRIDGE_PROXY_ADDRESS)
.typed(bridge_proxy_contract_proxy::BridgeProxyContractProxy)
.get_pending_transaction_by_id(i)
.returns(ExpectValue(eth_tx.clone()))
.run();
}

for i in 1..101u32 {
if i % 2 == 1 {
continue;
}
test.world
.tx()
.from(OWNER_ADDRESS)
.to(BRIDGE_PROXY_ADDRESS)
.gas(200_000_000)
.typed(bridge_proxy_contract_proxy::BridgeProxyContractProxy)
.execute(i)
.run();
}
test.world
.query()
.to(CROWDFUNDING_ADDRESS)
.typed(crowdfunding_esdt_proxy::CrowdfundingProxy)
.get_current_funds()
.returns(ExpectValue(250u64))
.run();

let pending_txs = test
.world
.query()
.to(BRIDGE_PROXY_ADDRESS)
.typed(bridge_proxy_contract_proxy::BridgeProxyContractProxy)
.get_pending_transactions()
.returns(ReturnsResult)
.run();

for tx in pending_txs {
let (tx_id, actual_tx) = tx.into_tuple();
assert!(tx_id % 2 == 1);
}
}
161 changes: 161 additions & 0 deletions esdt-safe/tests/esdt_safe_blackbox_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1343,3 +1343,164 @@ fn withdraw_transaction_fees_test() {
"accumulated_transaction_fees should be zero after withdrawal"
);
}

#[test]
fn esdt_safe_reduce_max_tx_batch_size_test() {
let mut state = EsdtSafeTestState::new();
state.multisig_deploy();
state.safe_deploy();

state.world.set_esdt_balance(
MULTISIG_ADDRESS,
b"TOKEN-WITH",
BigUint::from(10_000_000u64),
);

state.config_esdtsafe();

// Batch size is default 10
state
.world
.tx()
.from(BRIDGED_TOKENS_WRAPPER_ADDRESS)
.to(ESDT_SAFE_ADDRESS)
.typed(esdt_safe_proxy::EsdtSafeProxy)
.create_transaction(
EthAddress {
raw_addr: ManagedByteArray::new_from_bytes(b"01020304050607080910"),
},
OptionalValue::<BigUint<StaticApi>>::None,
)
.single_esdt(&TOKEN_ID.into(), 0, &BigUint::from(10u64))
.run();

state
.world
.tx()
.from(BRIDGED_TOKENS_WRAPPER_ADDRESS)
.to(ESDT_SAFE_ADDRESS)
.typed(esdt_safe_proxy::EsdtSafeProxy)
.create_transaction(
EthAddress {
raw_addr: ManagedByteArray::new_from_bytes(b"01020304050607080910"),
},
OptionalValue::<BigUint<StaticApi>>::None,
)
.single_esdt(&TOKEN_ID.into(), 0, &BigUint::from(10u64))
.run();

state
.world
.tx()
.from(BRIDGED_TOKENS_WRAPPER_ADDRESS)
.to(ESDT_SAFE_ADDRESS)
.typed(esdt_safe_proxy::EsdtSafeProxy)
.create_transaction(
EthAddress {
raw_addr: ManagedByteArray::new_from_bytes(b"01020304050607080910"),
},
OptionalValue::<BigUint<StaticApi>>::None,
)
.single_esdt(&TOKEN_ID.into(), 0, &BigUint::from(10u64))
.run();

state
.world
.tx()
.from(BRIDGED_TOKENS_WRAPPER_ADDRESS)
.to(ESDT_SAFE_ADDRESS)
.typed(esdt_safe_proxy::EsdtSafeProxy)
.create_transaction(
EthAddress {
raw_addr: ManagedByteArray::new_from_bytes(b"01020304050607080910"),
},
OptionalValue::<BigUint<StaticApi>>::None,
)
.single_esdt(&TOKEN_ID.into(), 0, &BigUint::from(10u64))
.run();

//get_batch_status
let batch_id = 1u64;
let batch_status = state
.world
.tx()
.from(BRIDGED_TOKENS_WRAPPER_ADDRESS)
.to(ESDT_SAFE_ADDRESS)
.typed(esdt_safe_proxy::EsdtSafeProxy)
.get_batch_status(batch_id)
.returns(ReturnsResult)
.run();

assert_eq!(
batch_status,
BatchStatus::PartiallyFull {
end_block_nonce: 100,
tx_ids: ManagedVec::from(vec![1u64, 2u64, 3u64, 4u64])
},
"Incorrect batch status"
);

let new_max_batch_status = 2usize;
state
.world
.tx()
.from(MULTISIG_ADDRESS)
.to(ESDT_SAFE_ADDRESS)
.typed(esdt_safe_proxy::EsdtSafeProxy)
.set_max_tx_batch_size(new_max_batch_status)
.run();

state
.world
.tx()
.from(BRIDGED_TOKENS_WRAPPER_ADDRESS)
.to(ESDT_SAFE_ADDRESS)
.typed(esdt_safe_proxy::EsdtSafeProxy)
.create_transaction(
EthAddress {
raw_addr: ManagedByteArray::new_from_bytes(b"01020304050607080910"),
},
OptionalValue::<BigUint<StaticApi>>::None,
)
.single_esdt(&TOKEN_ID.into(), 0, &BigUint::from(10u64))
.run();

//First batch should be full
let batch_id = 1u64;
let batch_status = state
.world
.tx()
.from(BRIDGED_TOKENS_WRAPPER_ADDRESS)
.to(ESDT_SAFE_ADDRESS)
.typed(esdt_safe_proxy::EsdtSafeProxy)
.get_batch_status(batch_id)
.returns(ReturnsResult)
.run();

assert_eq!(
batch_status,
BatchStatus::WaitingForSignatures,
"Incorrect batch status"
);

//A new batch should be created
let batch_id = 2u64;
let batch_status = state
.world
.tx()
.from(BRIDGED_TOKENS_WRAPPER_ADDRESS)
.to(ESDT_SAFE_ADDRESS)
.typed(esdt_safe_proxy::EsdtSafeProxy)
.get_batch_status(batch_id)
.returns(ReturnsResult)
.run();

assert_eq!(
batch_status,
BatchStatus::PartiallyFull {
end_block_nonce: 100,
tx_ids: ManagedVec::from(vec![5u64])
},
"Incorrect batch status"
);
}
2 changes: 1 addition & 1 deletion multi-transfer-esdt/scenarios/setup_accounts.scen.json
Original file line number Diff line number Diff line change
Expand Up @@ -190,7 +190,7 @@
"str:firstBatchId": "1",
"str:lastBatchId": "1",
"str:maxTxBatchSize": "10",
"str:maxTxBatchBlockDuration": "0xffffffffffffffff"
"str:maxTxBatchBlockDuration": "100_000_000"
}
},
"sc:bridge-proxy": {
Expand Down
2 changes: 1 addition & 1 deletion multi-transfer-esdt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use sc_proxies::{bridge_proxy_contract_proxy, bridged_tokens_wrapper_proxy, esdt
use transaction::{EthTransaction, PaymentsVec, Transaction, TxNonce};

const DEFAULT_MAX_TX_BATCH_SIZE: usize = 10;
const DEFAULT_MAX_TX_BATCH_BLOCK_DURATION: u64 = u64::MAX;
const DEFAULT_MAX_TX_BATCH_BLOCK_DURATION: u64 = 100_000_000u64;
const CHAIN_SPECIFIC_TO_UNIVERSAL_TOKEN_MAPPING: &[u8] = b"chainSpecificToUniversalMapping";

#[multiversx_sc::contract]
Expand Down
Loading
Loading