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

Commit

Permalink
Address @agryaznov's comments
Browse files Browse the repository at this point in the history
  • Loading branch information
yarikbratashchuk committed Jun 1, 2022
1 parent 0a70239 commit 6f3655a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 8 deletions.
6 changes: 3 additions & 3 deletions frame/contracts/fixtures/reentrant_count_call.wat
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
(module
(import "seal0" "seal_input" (func $seal_input (param i32 i32)))
(import "seal0" "seal_call" (func $seal_call (param i32 i32 i64 i32 i32 i32 i32 i32 i32) (result i32)))
(import "seal1" "seal_input" (func $seal_input (param i32 i32)))
(import "seal1" "seal_call" (func $seal_call (param i32 i32 i64 i32 i32 i32 i32 i32 i32) (result i32)))
(import "seal0" "seal_delegate_call" (func $seal_delegate_call (param i32 i32 i32 i32 i32 i32) (result i32)))
(import "__unstable__" "seal_reentrant_count" (func $seal_reentrant_count (result i32)))
(import "env" "memory" (memory 1 1))
Expand Down Expand Up @@ -41,7 +41,7 @@
(i32.const 0) ;; Pointer to "callee" code_hash.
(i32.const 0) ;; Input is ignored
(i32.const 0) ;; Length of the input
(i32.const 4294967295) ;; u32 max sentinel value: do not copy output
(i32.const 0xffffffff) ;; u32 max sentinel value: do not copy output
(i32.const 0) ;; Length is ignored in this case
)
)
Expand Down
2 changes: 1 addition & 1 deletion frame/contracts/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1243,7 +1243,7 @@ where
}

fn account_entrance_count(&self, account_id: &AccountIdOf<Self::T>) -> u32 {
self.frames().filter_map(|f| Some(f.delegate_caller.is_none() && &f.account_id == account_id)).count() as u32
self.frames().filter_map(|f| (f.delegate_caller.is_none() && &f.account_id == account_id).then(|| true)).count() as u32
}
}

Expand Down
2 changes: 1 addition & 1 deletion frame/contracts/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3231,7 +3231,7 @@ fn set_code_hash() {

#[test]
#[cfg(feature = "unstable-interface")]
fn reentrant_count() {
fn reentrant_count_works() {
let (wasm1, code_hash1) = compile_module::<Test>("reentrant_count_call").unwrap();
let contract_addr1 = Contracts::contract_address(&ALICE, &code_hash1, &[]);

Expand Down
2 changes: 1 addition & 1 deletion frame/contracts/src/wasm/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2599,7 +2599,7 @@ mod tests {

#[test]
#[cfg(feature = "unstable-interface")]
fn reentrant_count() {
fn reentrant_count_works() {
const CODE: &str = r#"
(module
(import "__unstable__" "seal_reentrant_count" (func $seal_reentrant_count (result i32)))
Expand Down
15 changes: 13 additions & 2 deletions frame/contracts/src/wasm/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2088,15 +2088,26 @@ define_env!(Env, <E: Ext>,
},

// Returns then number of times currently executing contract exists on the call stack in addition
// to the calling instance. A value of 0 means no reentrancy.
// to the calling instance.
//
// # Return Value
//
// Returns 0 when there is no reentrancy
[__unstable__] seal_reentrant_count(ctx) -> u32 => {
ctx.charge_gas(RuntimeCosts::ReentrantCount)?;
Ok(ctx.ext.reentrant_count() as u32)
},

// Returns the number of times specified contract exists on the call stack. Delegated calls are
// not calculated as separate calls.
// A value of 0 means it does not exist on the stack.
//
// # Parameters
//
// - `account_ptr`: a pointer to the contract address.
//
// # Return Value
//
// Returns 0 when the contract does not exist on the call stack.
[__unstable__] seal_account_entrance_count(ctx, account_ptr: u32) -> u32 => {
ctx.charge_gas(RuntimeCosts::AccountEntranceCount)?;
let account_id: <<E as Ext>::T as frame_system::Config>::AccountId = ctx.read_sandbox_memory_as(account_ptr)?;
Expand Down

0 comments on commit 6f3655a

Please sign in to comment.