diff --git a/frame/contracts/src/tests.rs b/frame/contracts/src/tests.rs index 1776e787fc2aa..ccc3dcc6da663 100644 --- a/frame/contracts/src/tests.rs +++ b/frame/contracts/src/tests.rs @@ -5360,75 +5360,87 @@ fn native_dependency_deposit_works() { // storage deposit rather than the existential deposit. const ED: u64 = 10; - ExtBuilder::default().existential_deposit(ED).build().execute_with(|| { - let _ = Balances::deposit_creating(&ALICE, 1_000_000); - let lockup_deposit_percent = CodeHashLockupDepositPercent::get(); - let per_byte = DepositPerByte::get(); - let per_item = DepositPerItem::get(); + // Test with both existing and uploaded code + for code in [Code::Upload(wasm.clone()), Code::Existing(code_hash)] { + ExtBuilder::default().existential_deposit(ED).build().execute_with(|| { + let _ = Balances::deposit_creating(&ALICE, 1_000_000); + let lockup_deposit_percent = CodeHashLockupDepositPercent::get(); + let per_byte = DepositPerByte::get(); + let per_item = DepositPerItem::get(); - // Upload set_code_hash contracts. - Contracts::upload_code( - RuntimeOrigin::signed(ALICE), - wasm.clone(), - None, - Determinism::Enforced, - ) - .unwrap(); + // Upload the dummy contract, + Contracts::upload_code( + RuntimeOrigin::signed(ALICE), + dummy_wasm.clone(), + None, + Determinism::Enforced, + ) + .unwrap(); - // Upload the dummy contract, - Contracts::upload_code( - RuntimeOrigin::signed(ALICE), - dummy_wasm.clone(), - None, - Determinism::Enforced, - ) - .unwrap(); + // Upload set_code_hash contracts if using Code::Existing. + let add_upload_deposit = match code { + Code::Existing(_) => { + Contracts::upload_code( + RuntimeOrigin::signed(ALICE), + wasm.clone(), + None, + Determinism::Enforced, + ) + .unwrap(); + false + }, + Code::Upload(_) => true, + }; - // Instantiate the set_code_hash contract. - let res = Contracts::bare_instantiate( - ALICE, - 0, - GAS_LIMIT, - None, - Code::Existing(code_hash), - vec![], - vec![], - DebugInfo::Skip, - CollectEvents::Skip, - ); + // Instantiate the set_code_hash contract. + let res = Contracts::bare_instantiate( + ALICE, + 0, + GAS_LIMIT, + None, + code, + vec![], + vec![], + DebugInfo::Skip, + CollectEvents::Skip, + ); - let addr = res.result.unwrap().account_id; - let info = ContractInfoOf::::get(&addr).unwrap(); - let info_len = info.encoded_size() as u64; - let base_deposit = ED + per_byte * info_len + per_item * 1; - let upload_deposit = test_utils::get_owner(&code_hash).deposit(); + let addr = res.result.unwrap().account_id; + let info = ContractInfoOf::::get(&addr).unwrap(); + let info_len = info.encoded_size() as u64; + let base_deposit = ED + per_byte * info_len + per_item * 1; + let upload_deposit = test_utils::get_owner(&code_hash).deposit(); + let extra_deposit = add_upload_deposit.then(|| upload_deposit).unwrap_or_default(); - // Check initial storage_deposit - // The base deposit should be: ED + info_len * per_byte + 1 * per_item + 30% * deposit - let deposit = base_deposit + lockup_deposit_percent.mul_ceil(upload_deposit); - assert_eq!(res.storage_deposit.charge_or_zero(), deposit); + // Check initial storage_deposit + // The base deposit should be: ED + info_len * per_byte + 1 * per_item + 30% * deposit + let deposit = + extra_deposit + base_deposit + lockup_deposit_percent.mul_ceil(upload_deposit); - // call set_code_hash - >::bare_call( - ALICE, - addr.clone(), - 0, - GAS_LIMIT, - None, - dummy_code_hash.encode(), - DebugInfo::Skip, - CollectEvents::Skip, - Determinism::Enforced, - ) - .result - .unwrap(); + assert_eq!(res.storage_deposit.charge_or_zero(), deposit); - // Check updated storage_deposit - let upload_deposit = test_utils::get_owner(&dummy_code_hash).deposit(); - let deposit = base_deposit + lockup_deposit_percent.mul_ceil(upload_deposit); - assert_eq!(test_utils::get_contract(&addr).storage_base_deposit(), deposit); - assert_eq!(test_utils::get_balance(&info.deposit_account()), deposit - ED); - }); + // call set_code_hash + >::bare_call( + ALICE, + addr.clone(), + 0, + GAS_LIMIT, + None, + dummy_code_hash.encode(), + DebugInfo::Skip, + CollectEvents::Skip, + Determinism::Enforced, + ) + .result + .unwrap(); + + // Check updated storage_deposit + let upload_deposit = test_utils::get_owner(&dummy_code_hash).deposit(); + let deposit = base_deposit + lockup_deposit_percent.mul_ceil(upload_deposit); + assert_eq!(test_utils::get_contract(&addr).storage_base_deposit(), deposit); + assert_eq!(test_utils::get_balance(&info.deposit_account()), deposit - ED); + }); + } } #[test]