seal_reentrant_count returns contract reentrant count (#12695)

* Add logic, test, broken benchmark

* account_entrance_count

* Addressing comments

* Address @agryaznov's comments

* Add test for account_entrance_count, fix ci

* Cargo fmt

* Fix tests

* Fix tests

* Remove delegated call from test, address comments

* Minor fixes and indentation in wat files

* Update test for account_entrance_count

* Update reentrant_count_call test

* Delegate call test

* Cargo +nightly fmt

* Address comments

* Update reentrant_count_works test

* Apply weights diff

* Add fixture descriptions

* Update comments as suggested

* Update reentrant_count_call test to use seal_address

* add missing code

* cargo fmt

* account_entrance_count -> account_reentrance_count

* fix tests

* fmt

* normalize signatures

Co-authored-by: yarikbratashchuk <yarik.bratashchuk@gmail.com>
This commit is contained in:
Artemka374
2022-11-15 15:12:08 +02:00
committed by GitHub
parent 679d2dcd25
commit 103ea38f95
10 changed files with 569 additions and 1 deletions
+139
View File
@@ -4383,3 +4383,142 @@ fn delegate_call_indeterministic_code() {
);
});
}
#[test]
#[cfg(feature = "unstable-interface")]
fn reentrant_count_works_with_call() {
let (wasm, code_hash) = compile_module::<Test>("reentrant_count_call").unwrap();
let contract_addr = Contracts::contract_address(&ALICE, &code_hash, &[]);
ExtBuilder::default().existential_deposit(100).build().execute_with(|| {
let _ = Balances::deposit_creating(&ALICE, 1_000_000);
assert_ok!(Contracts::instantiate_with_code(
RuntimeOrigin::signed(ALICE),
300_000,
GAS_LIMIT,
None,
wasm,
vec![],
vec![],
));
// passing reentrant count to the input
let input = 0.encode();
Contracts::bare_call(
ALICE,
contract_addr,
0,
GAS_LIMIT,
None,
input,
true,
Determinism::Deterministic,
)
.result
.unwrap();
});
}
#[test]
#[cfg(feature = "unstable-interface")]
fn reentrant_count_works_with_delegated_call() {
let (wasm, code_hash) = compile_module::<Test>("reentrant_count_delegated_call").unwrap();
let contract_addr = Contracts::contract_address(&ALICE, &code_hash, &[]);
ExtBuilder::default().existential_deposit(100).build().execute_with(|| {
let _ = Balances::deposit_creating(&ALICE, 1_000_000);
assert_ok!(Contracts::instantiate_with_code(
RuntimeOrigin::signed(ALICE),
300_000,
GAS_LIMIT,
None,
wasm,
vec![],
vec![],
));
// adding a callstack height to the input
let input = (code_hash, 1).encode();
Contracts::bare_call(
ALICE,
contract_addr.clone(),
0,
GAS_LIMIT,
None,
input,
true,
Determinism::Deterministic,
)
.result
.unwrap();
});
}
#[test]
#[cfg(feature = "unstable-interface")]
fn account_reentrance_count_works() {
let (wasm, code_hash) = compile_module::<Test>("account_reentrance_count_call").unwrap();
let (wasm_reentrant_count, code_hash_reentrant_count) =
compile_module::<Test>("reentrant_count_call").unwrap();
ExtBuilder::default().existential_deposit(100).build().execute_with(|| {
let _ = Balances::deposit_creating(&ALICE, 1_000_000);
assert_ok!(Contracts::instantiate_with_code(
RuntimeOrigin::signed(ALICE),
300_000,
GAS_LIMIT,
None,
wasm,
vec![],
vec![],
));
assert_ok!(Contracts::instantiate_with_code(
RuntimeOrigin::signed(ALICE),
300_000,
GAS_LIMIT,
None,
wasm_reentrant_count,
vec![],
vec![]
));
let contract_addr = Contracts::contract_address(&ALICE, &code_hash, &[]);
let another_contract_addr =
Contracts::contract_address(&ALICE, &code_hash_reentrant_count, &[]);
let result1 = Contracts::bare_call(
ALICE,
contract_addr.clone(),
0,
GAS_LIMIT,
None,
contract_addr.encode(),
true,
Determinism::Deterministic,
)
.result
.unwrap();
let result2 = Contracts::bare_call(
ALICE,
contract_addr.clone(),
0,
GAS_LIMIT,
None,
another_contract_addr.encode(),
true,
Determinism::Deterministic,
)
.result
.unwrap();
assert_eq!(result1.data, 1.encode());
assert_eq!(result2.data, 0.encode());
});
}