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

Commit fb22763

Browse files
committed
Use ExtBuilder in tests
1 parent 4f367f2 commit fb22763

File tree

1 file changed

+50
-52
lines changed

1 file changed

+50
-52
lines changed

substrate/runtime/contract/src/tests.rs

+50-52
Original file line numberDiff line numberDiff line change
@@ -127,6 +127,7 @@ impl ExtBuilder {
127127
balances: vec![],
128128
intentions: vec![],
129129
validator_count: 2,
130+
minimum_validator_count: 0,
130131
bonding_duration: 0,
131132
transaction_base_fee: 0,
132133
transaction_byte_fee: 0,
@@ -136,6 +137,7 @@ impl ExtBuilder {
136137
reclaim_rebate: 0,
137138
early_era_slash: 0,
138139
session_reward: 0,
140+
offline_slash_grace: 0,
139141
}.build_storage()
140142
.unwrap(),
141143
);
@@ -159,13 +161,6 @@ impl ExtBuilder {
159161
}
160162
}
161163

162-
fn new_test_ext(existential_deposit: u64, gas_price: u64) -> runtime_io::TestExternalities<KeccakHasher> {
163-
ExtBuilder::default()
164-
.existential_deposit(existential_deposit)
165-
.gas_price(gas_price)
166-
.build()
167-
}
168-
169164
const CODE_TRANSFER: &str = r#"
170165
(module
171166
;; ext_transfer(transfer_to: u32, transfer_to_len: u32, value_ptr: u32, value_len: u32)
@@ -195,7 +190,7 @@ fn contract_transfer() {
195190

196191
let code_transfer = wabt::wat2wasm(CODE_TRANSFER).unwrap();
197192

198-
with_externalities(&mut new_test_ext(0, 2), || {
193+
with_externalities(&mut ExtBuilder::default().build(), || {
199194
<CodeOf<Test>>::insert(1, code_transfer.to_vec());
200195

201196
Staking::set_free_balance(&0, 100_000_000);
@@ -230,7 +225,7 @@ fn contract_transfer_oog() {
230225

231226
let code_transfer = wabt::wat2wasm(CODE_TRANSFER).unwrap();
232227

233-
with_externalities(&mut new_test_ext(0, 2), || {
228+
with_externalities(&mut ExtBuilder::default().build(), || {
234229
<CodeOf<Test>>::insert(1, code_transfer.to_vec());
235230

236231
Staking::set_free_balance(&0, 100_000_000);
@@ -262,7 +257,7 @@ fn contract_transfer_max_depth() {
262257

263258
let code_transfer = wabt::wat2wasm(CODE_TRANSFER).unwrap();
264259

265-
with_externalities(&mut new_test_ext(0, 2), || {
260+
with_externalities(&mut ExtBuilder::default().build(), || {
266261
<CodeOf<Test>>::insert(CONTRACT_SHOULD_TRANSFER_TO, code_transfer.to_vec());
267262

268263
Staking::set_free_balance(&0, 100_000_000);
@@ -362,7 +357,7 @@ fn contract_create() {
362357
let code_ctor_transfer = wabt::wat2wasm(&code_ctor(&code_transfer)).unwrap();
363358
let code_create = wabt::wat2wasm(&code_create(&code_ctor_transfer)).unwrap();
364359

365-
with_externalities(&mut new_test_ext(0, 2), || {
360+
with_externalities(&mut ExtBuilder::default().build(), || {
366361
Staking::set_free_balance(&0, 100_000_000);
367362
Staking::increase_total_stake_by(100_000_000);
368363
Staking::set_free_balance(&1, 0);
@@ -411,7 +406,7 @@ fn top_level_create() {
411406
let code_transfer = wabt::wat2wasm(CODE_TRANSFER).unwrap();
412407
let code_ctor_transfer = wabt::wat2wasm(&code_ctor(&code_transfer)).unwrap();
413408

414-
with_externalities(&mut new_test_ext(0, 3), || {
409+
with_externalities(&mut ExtBuilder::default().gas_price(3).build(), || {
415410
let derived_address = <Test as Trait>::DetermineContractAddress::contract_address_for(
416411
&code_ctor_transfer,
417412
&0,
@@ -456,7 +451,7 @@ const CODE_NOP: &'static str = r#"
456451
fn refunds_unused_gas() {
457452
let code_nop = wabt::wat2wasm(CODE_NOP).unwrap();
458453

459-
with_externalities(&mut new_test_ext(0, 2), || {
454+
with_externalities(&mut ExtBuilder::default().build(), || {
460455
<CodeOf<Test>>::insert(1, code_nop.to_vec());
461456

462457
Staking::set_free_balance(&0, 100_000_000);
@@ -470,7 +465,7 @@ fn refunds_unused_gas() {
470465

471466
#[test]
472467
fn call_with_zero_value() {
473-
with_externalities(&mut new_test_ext(0, 2), || {
468+
with_externalities(&mut ExtBuilder::default().build(), || {
474469
<CodeOf<Test>>::insert(1, vec![]);
475470

476471
Staking::set_free_balance(&0, 100_000_000);
@@ -486,7 +481,7 @@ fn call_with_zero_value() {
486481
fn create_with_zero_endowment() {
487482
let code_nop = wabt::wat2wasm(CODE_NOP).unwrap();
488483

489-
with_externalities(&mut new_test_ext(0, 2), || {
484+
with_externalities(&mut ExtBuilder::default().build(), || {
490485
Staking::set_free_balance(&0, 100_000_000);
491486
Staking::increase_total_stake_by(100_000_000);
492487

@@ -503,42 +498,45 @@ fn create_with_zero_endowment() {
503498

504499
#[test]
505500
fn account_removal_removes_storage() {
506-
with_externalities(&mut new_test_ext(100, 2), || {
507-
// Setup two accounts with free balance above than exsistential threshold.
508-
{
509-
Staking::set_free_balance(&1, 110);
510-
Staking::increase_total_stake_by(110);
511-
<StorageOf<Test>>::insert(1, b"foo".to_vec(), b"1".to_vec());
512-
<StorageOf<Test>>::insert(1, b"bar".to_vec(), b"2".to_vec());
513-
514-
Staking::set_free_balance(&2, 110);
515-
Staking::increase_total_stake_by(110);
516-
<StorageOf<Test>>::insert(2, b"hello".to_vec(), b"3".to_vec());
517-
<StorageOf<Test>>::insert(2, b"world".to_vec(), b"4".to_vec());
518-
}
519-
520-
// Transfer funds from account 1 of such amount that after this transfer
521-
// the balance of account 1 is will be below than exsistential threshold.
522-
//
523-
// This should lead to the removal of all storage associated with this account.
524-
assert_ok!(Staking::transfer(&1, 2.into(), 20));
525-
526-
// Verify that all entries from account 1 is removed, while
527-
// entries from account 2 is in place.
528-
{
529-
assert_eq!(<StorageOf<Test>>::get(1, b"foo".to_vec()), None);
530-
assert_eq!(<StorageOf<Test>>::get(1, b"bar".to_vec()), None);
531-
532-
assert_eq!(
533-
<StorageOf<Test>>::get(2, b"hello".to_vec()),
534-
Some(b"3".to_vec())
535-
);
536-
assert_eq!(
537-
<StorageOf<Test>>::get(2, b"world".to_vec()),
538-
Some(b"4".to_vec())
539-
);
540-
}
541-
});
501+
with_externalities(
502+
&mut ExtBuilder::default().existential_deposit(100).build(),
503+
|| {
504+
// Setup two accounts with free balance above than exsistential threshold.
505+
{
506+
Staking::set_free_balance(&1, 110);
507+
Staking::increase_total_stake_by(110);
508+
<StorageOf<Test>>::insert(1, b"foo".to_vec(), b"1".to_vec());
509+
<StorageOf<Test>>::insert(1, b"bar".to_vec(), b"2".to_vec());
510+
511+
Staking::set_free_balance(&2, 110);
512+
Staking::increase_total_stake_by(110);
513+
<StorageOf<Test>>::insert(2, b"hello".to_vec(), b"3".to_vec());
514+
<StorageOf<Test>>::insert(2, b"world".to_vec(), b"4".to_vec());
515+
}
516+
517+
// Transfer funds from account 1 of such amount that after this transfer
518+
// the balance of account 1 is will be below than exsistential threshold.
519+
//
520+
// This should lead to the removal of all storage associated with this account.
521+
assert_ok!(Staking::transfer(&1, 2.into(), 20));
522+
523+
// Verify that all entries from account 1 is removed, while
524+
// entries from account 2 is in place.
525+
{
526+
assert_eq!(<StorageOf<Test>>::get(1, b"foo".to_vec()), None);
527+
assert_eq!(<StorageOf<Test>>::get(1, b"bar".to_vec()), None);
528+
529+
assert_eq!(
530+
<StorageOf<Test>>::get(2, b"hello".to_vec()),
531+
Some(b"3".to_vec())
532+
);
533+
assert_eq!(
534+
<StorageOf<Test>>::get(2, b"world".to_vec()),
535+
Some(b"4".to_vec())
536+
);
537+
}
538+
},
539+
);
542540
}
543541

544542
const CODE_UNREACHABLE: &'static str = r#"
@@ -553,7 +551,7 @@ const CODE_UNREACHABLE: &'static str = r#"
553551
#[test]
554552
fn top_level_call_refunds_even_if_fails() {
555553
let code_unreachable = wabt::wat2wasm(CODE_UNREACHABLE).unwrap();
556-
with_externalities(&mut new_test_ext(0, 4), || {
554+
with_externalities(&mut ExtBuilder::default().gas_price(4).build(), || {
557555
<CodeOf<Test>>::insert(1, code_unreachable.to_vec());
558556

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

0 commit comments

Comments
 (0)