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

Commit

Permalink
Delegate call test
Browse files Browse the repository at this point in the history
  • Loading branch information
yarikbratashchuk committed Jun 24, 2022
1 parent 5fdc100 commit b656c88
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 0 deletions.
69 changes: 69 additions & 0 deletions frame/contracts/fixtures/reentrant_count_delegated_call.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
(module
(import "seal0" "seal_input" (func $seal_input (param i32 i32)))
(import "seal0" "seal_set_storage" (func $seal_set_storage (param i32 i32 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))

;; [0, 32) buffer where code hash is copied

;; [32, 36) buffer for the call stack high

;; [36, 40) size of the input buffer
(data (i32.const 36) "\24")

(func $assert (param i32)
(block $ok
(br_if $ok
(get_local 0)
)
(unreachable)
)
)
(func (export "call")
(local $callstack_high i32)
(local $delegate_call_exit_code i32)

;; Reading input
(call $seal_input (i32.const 0) (i32.const 36))

;; reading passed callstack high
(set_local $callstack_high (i32.load (i32.const 32)))

;; incrementing callstack high
(i32.store (i32.const 32) (i32.add (i32.load (i32.const 32)) (i32.const 1)))

;; reentrance count stays 0
(call $assert
(i32.eq (call $seal_reentrant_count) (i32.const 0))
)

(i32.eq (get_local $callstack_high) (i32.const 5))
(if
(then) ;; exit recursion case
(else
;; Call to itself
(set_local $delegate_call_exit_code
(call $seal_delegate_call
(i32.const 0) ;; Set no call flags
(i32.const 0) ;; Pointer to "callee" code_hash.
(i32.const 0) ;; Input is ingored
(i32.const 36) ;; Length of the input
(i32.const 4294967295) ;; u32 max sentinel value: do not copy output
(i32.const 0) ;; Length is ignored in this case
)
)

(call $assert
(i32.eq (get_local $delegate_call_exit_code) (i32.const 0))
)
)
)

(call $assert
(i32.le_s (get_local $callstack_high) (i32.const 5))
)
)

(func (export "deploy"))
)
37 changes: 37 additions & 0 deletions frame/contracts/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3261,6 +3261,43 @@ fn reentrant_count_works_with_call() {
.unwrap();
});
}

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

ExtBuilder::default().existential_deposit(100).build().execute_with(|| {
let _ = Balances::deposit_creating(&ALICE, 1_000_000);

assert_ok!(Contracts::instantiate_with_code(
Origin::signed(ALICE),
300_000,
GAS_LIMIT,
None,
wasm1,
vec![],
vec![],
));

let mut input = AsRef::<[u8]>::as_ref(&(code_hash1.clone())).to_vec();
input.push(1); // adding callstack high to the input

Contracts::bare_call(
ALICE,
contract_addr1.clone(),
0,
GAS_LIMIT,
None,
input,
true,
)
.result
.unwrap();
});
}

#[test]
#[cfg(feature = "unstable-interface")]
fn account_entrance_count_works() {
Expand Down

0 comments on commit b656c88

Please sign in to comment.