Skip to content
This repository has been archived by the owner on Nov 15, 2023. It is now read-only.

Commit

Permalink
update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
pgherveou committed Jun 5, 2023
1 parent fe940ac commit 7be68c2
Showing 1 changed file with 74 additions and 62 deletions.
136 changes: 74 additions & 62 deletions frame/contracts/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::<Test>::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::<Test>::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
<Pallet<Test>>::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
<Pallet<Test>>::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]
Expand Down

0 comments on commit 7be68c2

Please sign in to comment.