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

Commit a4d1efd

Browse files
committed
Use ExtBuilder in tests
1 parent 56d6fc1 commit a4d1efd

File tree

1 file changed

+46
-50
lines changed

1 file changed

+46
-50
lines changed

substrate/runtime/contract/src/tests.rs

+46-50
Original file line numberDiff line numberDiff line change
@@ -157,13 +157,6 @@ impl ExtBuilder {
157157
}
158158
}
159159

160-
fn new_test_ext(existential_deposit: u64, gas_price: u64) -> runtime_io::TestExternalities {
161-
ExtBuilder::default()
162-
.existential_deposit(existential_deposit)
163-
.gas_price(gas_price)
164-
.build()
165-
}
166-
167160
const CODE_TRANSFER: &str = r#"
168161
(module
169162
;; ext_transfer(transfer_to: u32, transfer_to_len: u32, value_ptr: u32, value_len: u32)
@@ -193,7 +186,7 @@ fn contract_transfer() {
193186

194187
let code_transfer = wabt::wat2wasm(CODE_TRANSFER).unwrap();
195188

196-
with_externalities(&mut new_test_ext(0, 2), || {
189+
with_externalities(&mut ExtBuilder::default().build(), || {
197190
<CodeOf<Test>>::insert(1, code_transfer.to_vec());
198191

199192
Staking::set_free_balance(&0, 100_000_000);
@@ -226,7 +219,7 @@ fn contract_transfer_oog() {
226219

227220
let code_transfer = wabt::wat2wasm(CODE_TRANSFER).unwrap();
228221

229-
with_externalities(&mut new_test_ext(0, 2), || {
222+
with_externalities(&mut ExtBuilder::default().build(), || {
230223
<CodeOf<Test>>::insert(1, code_transfer.to_vec());
231224

232225
Staking::set_free_balance(&0, 100_000_000);
@@ -256,7 +249,7 @@ fn contract_transfer_max_depth() {
256249

257250
let code_transfer = wabt::wat2wasm(CODE_TRANSFER).unwrap();
258251

259-
with_externalities(&mut new_test_ext(0, 2), || {
252+
with_externalities(&mut ExtBuilder::default().build(), || {
260253
<CodeOf<Test>>::insert(CONTRACT_SHOULD_TRANSFER_TO, code_transfer.to_vec());
261254

262255
Staking::set_free_balance(&0, 100_000_000);
@@ -354,7 +347,7 @@ fn contract_create() {
354347
let code_ctor_transfer = wabt::wat2wasm(&code_ctor(&code_transfer)).unwrap();
355348
let code_create = wabt::wat2wasm(&code_create(&code_ctor_transfer)).unwrap();
356349

357-
with_externalities(&mut new_test_ext(0, 2), || {
350+
with_externalities(&mut ExtBuilder::default().build(), || {
358351
Staking::set_free_balance(&0, 100_000_000);
359352
Staking::set_free_balance(&1, 0);
360353
Staking::set_free_balance(&9, 30);
@@ -401,7 +394,7 @@ fn top_level_create() {
401394
let code_transfer = wabt::wat2wasm(CODE_TRANSFER).unwrap();
402395
let code_ctor_transfer = wabt::wat2wasm(&code_ctor(&code_transfer)).unwrap();
403396

404-
with_externalities(&mut new_test_ext(0, 3), || {
397+
with_externalities(&mut ExtBuilder::default().gas_price(3).build(), || {
405398
let derived_address = <Test as Trait>::DetermineContractAddress::contract_address_for(
406399
&code_ctor_transfer,
407400
&0,
@@ -444,7 +437,7 @@ const CODE_NOP: &'static str = r#"
444437
fn refunds_unused_gas() {
445438
let code_nop = wabt::wat2wasm(CODE_NOP).unwrap();
446439

447-
with_externalities(&mut new_test_ext(0, 2), || {
440+
with_externalities(&mut ExtBuilder::default().build(), || {
448441
<CodeOf<Test>>::insert(1, code_nop.to_vec());
449442

450443
Staking::set_free_balance(&0, 100_000_000);
@@ -457,7 +450,7 @@ fn refunds_unused_gas() {
457450

458451
#[test]
459452
fn call_with_zero_value() {
460-
with_externalities(&mut new_test_ext(0, 2), || {
453+
with_externalities(&mut ExtBuilder::default().build(), || {
461454
<CodeOf<Test>>::insert(1, vec![]);
462455

463456
Staking::set_free_balance(&0, 100_000_000);
@@ -472,7 +465,7 @@ fn call_with_zero_value() {
472465
fn create_with_zero_endowment() {
473466
let code_nop = wabt::wat2wasm(CODE_NOP).unwrap();
474467

475-
with_externalities(&mut new_test_ext(0, 2), || {
468+
with_externalities(&mut ExtBuilder::default().build(), || {
476469
Staking::set_free_balance(&0, 100_000_000);
477470

478471
assert_ok!(Contract::create(&0, 0, 100_000, code_nop, Vec::new()));
@@ -488,40 +481,43 @@ fn create_with_zero_endowment() {
488481

489482
#[test]
490483
fn account_removal_removes_storage() {
491-
with_externalities(&mut new_test_ext(100, 2), || {
492-
// Setup two accounts with free balance above than exsistential threshold.
493-
{
494-
Staking::set_free_balance(&1, 110);
495-
<StorageOf<Test>>::insert(1, b"foo".to_vec(), b"1".to_vec());
496-
<StorageOf<Test>>::insert(1, b"bar".to_vec(), b"2".to_vec());
497-
498-
Staking::set_free_balance(&2, 110);
499-
<StorageOf<Test>>::insert(2, b"hello".to_vec(), b"3".to_vec());
500-
<StorageOf<Test>>::insert(2, b"world".to_vec(), b"4".to_vec());
501-
}
502-
503-
// Transfer funds from account 1 of such amount that after this transfer
504-
// the balance of account 1 is will be below than exsistential threshold.
505-
//
506-
// This should lead to the removal of all storage associated with this account.
507-
assert_ok!(Staking::transfer(&1, 2.into(), 20));
508-
509-
// Verify that all entries from account 1 is removed, while
510-
// entries from account 2 is in place.
511-
{
512-
assert_eq!(<StorageOf<Test>>::get(1, b"foo".to_vec()), None);
513-
assert_eq!(<StorageOf<Test>>::get(1, b"bar".to_vec()), None);
514-
515-
assert_eq!(
516-
<StorageOf<Test>>::get(2, b"hello".to_vec()),
517-
Some(b"3".to_vec())
518-
);
519-
assert_eq!(
520-
<StorageOf<Test>>::get(2, b"world".to_vec()),
521-
Some(b"4".to_vec())
522-
);
523-
}
524-
});
484+
with_externalities(
485+
&mut ExtBuilder::default().existential_deposit(100).build(),
486+
|| {
487+
// Setup two accounts with free balance above than exsistential threshold.
488+
{
489+
Staking::set_free_balance(&1, 110);
490+
<StorageOf<Test>>::insert(1, b"foo".to_vec(), b"1".to_vec());
491+
<StorageOf<Test>>::insert(1, b"bar".to_vec(), b"2".to_vec());
492+
493+
Staking::set_free_balance(&2, 110);
494+
<StorageOf<Test>>::insert(2, b"hello".to_vec(), b"3".to_vec());
495+
<StorageOf<Test>>::insert(2, b"world".to_vec(), b"4".to_vec());
496+
}
497+
498+
// Transfer funds from account 1 of such amount that after this transfer
499+
// the balance of account 1 is will be below than exsistential threshold.
500+
//
501+
// This should lead to the removal of all storage associated with this account.
502+
assert_ok!(Staking::transfer(&1, 2.into(), 20));
503+
504+
// Verify that all entries from account 1 is removed, while
505+
// entries from account 2 is in place.
506+
{
507+
assert_eq!(<StorageOf<Test>>::get(1, b"foo".to_vec()), None);
508+
assert_eq!(<StorageOf<Test>>::get(1, b"bar".to_vec()), None);
509+
510+
assert_eq!(
511+
<StorageOf<Test>>::get(2, b"hello".to_vec()),
512+
Some(b"3".to_vec())
513+
);
514+
assert_eq!(
515+
<StorageOf<Test>>::get(2, b"world".to_vec()),
516+
Some(b"4".to_vec())
517+
);
518+
}
519+
},
520+
);
525521
}
526522

527523
const CODE_UNREACHABLE: &'static str = r#"
@@ -536,7 +532,7 @@ const CODE_UNREACHABLE: &'static str = r#"
536532
#[test]
537533
fn top_level_call_refunds_even_if_fails() {
538534
let code_unreachable = wabt::wat2wasm(CODE_UNREACHABLE).unwrap();
539-
with_externalities(&mut new_test_ext(0, 4), || {
535+
with_externalities(&mut ExtBuilder::default().gas_price(4).build(), || {
540536
<CodeOf<Test>>::insert(1, code_unreachable.to_vec());
541537

542538
Staking::set_free_balance(&0, 100_000_000);

0 commit comments

Comments
 (0)