From 78157dfcf6dce1b60acffdd0aaa624e4e6cc9986 Mon Sep 17 00:00:00 2001 From: pgherveou Date: Tue, 25 Apr 2023 09:42:03 +0200 Subject: [PATCH 1/3] contracts Add storage_deposit test --- frame/contracts/fixtures/call.wat | 39 ++++++++++++++++++++++++ frame/contracts/src/tests.rs | 50 +++++++++++++++++++++++++++++++ 2 files changed, 89 insertions(+) create mode 100644 frame/contracts/fixtures/call.wat diff --git a/frame/contracts/fixtures/call.wat b/frame/contracts/fixtures/call.wat new file mode 100644 index 0000000000000..5203d60b524c0 --- /dev/null +++ b/frame/contracts/fixtures/call.wat @@ -0,0 +1,39 @@ +;; This calls another contract as passed as its account id. It also creates some storage. +(module + (import "seal0" "seal_input" (func $seal_input (param i32 i32))) + (import "seal1" "seal_call" (func $seal_call (param i32 i32 i64 i32 i32 i32 i32 i32) (result i32))) + (import "env" "memory" (memory 1 1)) + + (func $assert (param i32) + (block $ok + (br_if $ok + (get_local 0) + ) + (unreachable) + ) + ) + + (func (export "deploy")) + + (func (export "call") + ;; store length of input buffer + (i32.store (i32.const 0) (i32.const 512)) + + ;; copy input at address 4 + (call $seal_input (i32.const 4) (i32.const 0)) + + ;; call passed contract + (call $assert (i32.eqz + (call $seal_call + (i32.const 0) ;; No flags + (i32.const 8) ;; Pointer to "callee" address. + (i64.const 0) ;; How much gas to devote for the execution. 0 = all. + (i32.const 512) ;; Pointer to the buffer with value to transfer + (i32.const 4) ;; Pointer to input data buffer address + (i32.const 4) ;; Length of input data buffer + (i32.const 4294967295) ;; u32 max value is the sentinel value: do not copy output + (i32.const 0) ;; Length is ignored in this case + ) + )) + ) +) diff --git a/frame/contracts/src/tests.rs b/frame/contracts/src/tests.rs index 4e6c468f19c81..ee66dd81e7d3c 100644 --- a/frame/contracts/src/tests.rs +++ b/frame/contracts/src/tests.rs @@ -3565,6 +3565,56 @@ fn storage_deposit_works() { }); } +#[test] +fn storage_deposit_callee_works() { + let (wasm_caller, _code_hash_caller) = compile_module::("call").unwrap(); + let (wasm_callee, _code_hash_callee) = compile_module::("store").unwrap(); + ExtBuilder::default().existential_deposit(200).build().execute_with(|| { + let _ = Balances::deposit_creating(&ALICE, 1_000_000); + + // Create both contracts: Constructors do nothing. + let addr_caller = Contracts::bare_instantiate( + ALICE, + 0, + GAS_LIMIT, + None, + Code::Upload(wasm_caller), + vec![], + vec![], + false, + ) + .result + .unwrap() + .account_id; + let addr_callee = Contracts::bare_instantiate( + ALICE, + 0, + GAS_LIMIT, + None, + Code::Upload(wasm_callee), + vec![], + vec![], + false, + ) + .result + .unwrap() + .account_id; + + assert_ok!(Contracts::call( + RuntimeOrigin::signed(ALICE), + addr_caller, + 0, + GAS_LIMIT, + None, + (100u32, &addr_callee).encode() + )); + + let callee = get_contract(&addr_callee); + assert_eq!(test_utils::get_balance(callee.deposit_account()), 302); + assert_eq!(callee.total_deposit(), 302); + }); +} + #[test] fn set_code_extrinsic() { let (wasm, code_hash) = compile_module::("dummy").unwrap(); From 5107680ae3cd75495d8d2d645261e72df122d6d9 Mon Sep 17 00:00:00 2001 From: pgherveou Date: Tue, 25 Apr 2023 10:09:00 +0200 Subject: [PATCH 2/3] fix comments --- frame/contracts/fixtures/call.wat | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/frame/contracts/fixtures/call.wat b/frame/contracts/fixtures/call.wat index 5203d60b524c0..4558b2c6409b9 100644 --- a/frame/contracts/fixtures/call.wat +++ b/frame/contracts/fixtures/call.wat @@ -1,4 +1,4 @@ -;; This calls another contract as passed as its account id. It also creates some storage. +;; This calls another contract as passed as its account id. (module (import "seal0" "seal_input" (func $seal_input (param i32 i32))) (import "seal1" "seal_call" (func $seal_call (param i32 i32 i64 i32 i32 i32 i32 i32) (result i32))) @@ -16,13 +16,13 @@ (func (export "deploy")) (func (export "call") - ;; store length of input buffer + ;; Store length of input buffer. (i32.store (i32.const 0) (i32.const 512)) - ;; copy input at address 4 + ;; Copy input at address 4. (call $seal_input (i32.const 4) (i32.const 0)) - ;; call passed contract + ;; Call passed contract. (call $assert (i32.eqz (call $seal_call (i32.const 0) ;; No flags From 1a77cc10286d30aabacd0d674907f3f3d1b05a31 Mon Sep 17 00:00:00 2001 From: pgherveou Date: Wed, 26 Apr 2023 08:30:43 +0200 Subject: [PATCH 3/3] PR comments --- frame/contracts/src/tests.rs | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/frame/contracts/src/tests.rs b/frame/contracts/src/tests.rs index ee66dd81e7d3c..be34b1cccb6bb 100644 --- a/frame/contracts/src/tests.rs +++ b/frame/contracts/src/tests.rs @@ -3569,7 +3569,8 @@ fn storage_deposit_works() { fn storage_deposit_callee_works() { let (wasm_caller, _code_hash_caller) = compile_module::("call").unwrap(); let (wasm_callee, _code_hash_callee) = compile_module::("store").unwrap(); - ExtBuilder::default().existential_deposit(200).build().execute_with(|| { + const ED: u64 = 200; + ExtBuilder::default().existential_deposit(ED).build().execute_with(|| { let _ = Balances::deposit_creating(&ALICE, 1_000_000); // Create both contracts: Constructors do nothing. @@ -3610,8 +3611,10 @@ fn storage_deposit_callee_works() { )); let callee = get_contract(&addr_callee); - assert_eq!(test_utils::get_balance(callee.deposit_account()), 302); - assert_eq!(callee.total_deposit(), 302); + let deposit = ED + DepositPerByte::get() * 100 + DepositPerItem::get() * 1; + + assert_eq!(test_utils::get_balance(callee.deposit_account()), deposit); + assert_eq!(callee.total_deposit(), deposit); }); }