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

Commit f65a281

Browse files
author
Ross Bulat
committed
Revert "contracts: Don't rely on reserved balances keeping an account alive (#13369)"
This reverts commit c23c308.
1 parent 06a8a94 commit f65a281

File tree

11 files changed

+2173
-2098
lines changed

11 files changed

+2173
-2098
lines changed

frame/contracts/fixtures/caller_contract.wat

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,11 +16,27 @@
1616
)
1717
)
1818

19+
(func $current_balance (param $sp i32) (result i64)
20+
(i32.store
21+
(i32.sub (get_local $sp) (i32.const 16))
22+
(i32.const 8)
23+
)
24+
(call $seal_balance
25+
(i32.sub (get_local $sp) (i32.const 8))
26+
(i32.sub (get_local $sp) (i32.const 16))
27+
)
28+
(call $assert
29+
(i32.eq (i32.load (i32.sub (get_local $sp) (i32.const 16))) (i32.const 8))
30+
)
31+
(i64.load (i32.sub (get_local $sp) (i32.const 8)))
32+
)
33+
1934
(func (export "deploy"))
2035

2136
(func (export "call")
2237
(local $sp i32)
2338
(local $exit_code i32)
39+
(local $balance i64)
2440

2541
;; Length of the buffer
2642
(i32.store (i32.const 20) (i32.const 32))
@@ -38,6 +54,9 @@
3854

3955
;; Read current balance into local variable.
4056
(set_local $sp (i32.const 1024))
57+
(set_local $balance
58+
(call $current_balance (get_local $sp))
59+
)
4160

4261
;; Fail to deploy the contract since it returns a non-zero exit status.
4362
(set_local $exit_code
@@ -63,6 +82,11 @@
6382
(i32.eq (get_local $exit_code) (i32.const 2)) ;; ReturnCode::CalleeReverted
6483
)
6584

85+
;; Check that balance has not changed.
86+
(call $assert
87+
(i64.eq (get_local $balance) (call $current_balance (get_local $sp)))
88+
)
89+
6690
;; Fail to deploy the contract due to insufficient gas.
6791
(set_local $exit_code
6892
(call $seal_instantiate
@@ -88,6 +112,11 @@
88112
(i32.eq (get_local $exit_code) (i32.const 1)) ;; ReturnCode::CalleeTrapped
89113
)
90114

115+
;; Check that balance has not changed.
116+
(call $assert
117+
(i64.eq (get_local $balance) (call $current_balance (get_local $sp)))
118+
)
119+
91120
;; Length of the output buffer
92121
(i32.store
93122
(i32.sub (get_local $sp) (i32.const 4))
@@ -124,6 +153,14 @@
124153
(i32.eq (i32.load (i32.sub (get_local $sp) (i32.const 4))) (i32.const 32))
125154
)
126155

156+
;; Check that balance has been deducted.
157+
(set_local $balance
158+
(i64.sub (get_local $balance) (i64.load (i32.const 0)))
159+
)
160+
(call $assert
161+
(i64.eq (get_local $balance) (call $current_balance (get_local $sp)))
162+
)
163+
127164
;; Zero out destination buffer of output
128165
(i32.store
129166
(i32.sub (get_local $sp) (i32.const 4))
@@ -167,6 +204,11 @@
167204
)
168205
)
169206

207+
;; Check that balance has not changed.
208+
(call $assert
209+
(i64.eq (get_local $balance) (call $current_balance (get_local $sp)))
210+
)
211+
170212
;; Fail to call the contract due to insufficient gas.
171213
(set_local $exit_code
172214
(call $seal_call
@@ -187,6 +229,11 @@
187229
(i32.eq (get_local $exit_code) (i32.const 1)) ;; ReturnCode::CalleeTrapped
188230
)
189231

232+
;; Check that balance has not changed.
233+
(call $assert
234+
(i64.eq (get_local $balance) (call $current_balance (get_local $sp)))
235+
)
236+
190237
;; Zero out destination buffer of output
191238
(i32.store
192239
(i32.sub (get_local $sp) (i32.const 4))
@@ -229,6 +276,14 @@
229276
(i32.const 0x77665544)
230277
)
231278
)
279+
280+
;; Check that balance has been deducted.
281+
(set_local $balance
282+
(i64.sub (get_local $balance) (i64.load (i32.const 0)))
283+
)
284+
(call $assert
285+
(i64.eq (get_local $balance) (call $current_balance (get_local $sp)))
286+
)
232287
)
233288

234289
(data (i32.const 0) "\00\80") ;; The value to transfer on instantiation and calls.

frame/contracts/fixtures/drain.wat

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,8 @@
3434
)
3535

3636
;; Try to self-destruct by sending full balance to the 0 address.
37-
;; The call will fail because a contract transfer has a keep alive requirement
37+
;; All the *free* balance will be send away, which is a valid thing to do
38+
;; because the storage deposits will keep the account alive.
3839
(call $assert
3940
(i32.eq
4041
(call $seal_transfer
@@ -43,7 +44,7 @@
4344
(i32.const 0) ;; Pointer to the buffer with value to transfer
4445
(i32.const 8) ;; Length of the buffer with value to transfer
4546
)
46-
(i32.const 5) ;; ReturnCode::TransferFailed
47+
(i32.const 0) ;; ReturnCode::Success
4748
)
4849
)
4950
)

frame/contracts/primitives/src/lib.rs

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -47,12 +47,10 @@ pub struct ContractResult<R, Balance> {
4747
/// Additionally, any `seal_call` or `seal_instantiate` makes use of pre-charging
4848
/// when a non-zero `gas_limit` argument is supplied.
4949
pub gas_required: Weight,
50-
/// How much balance was paid by the origin into the contract's deposit account in order to
51-
/// pay for storage.
50+
/// How much balance was deposited and reserved during execution in order to pay for storage.
5251
///
53-
/// The storage deposit is never actually charged from the origin in case of [`Self::result`]
54-
/// is `Err`. This is because on error all storage changes are rolled back including the
55-
/// payment of the deposit.
52+
/// The storage deposit is never actually charged from the caller in case of [`Self::result`]
53+
/// is `Err`. This is because on error all storage changes are rolled back.
5654
pub storage_deposit: StorageDeposit<Balance>,
5755
/// An optional debug message. This message is only filled when explicitly requested
5856
/// by the code that calls into the contract. Otherwise it is empty.
@@ -161,12 +159,12 @@ pub enum StorageDeposit<Balance> {
161159
/// The transaction reduced storage consumption.
162160
///
163161
/// This means that the specified amount of balance was transferred from the involved
164-
/// deposit accounts to the origin.
162+
/// contracts to the call origin.
165163
Refund(Balance),
166-
/// The transaction increased storage consumption.
164+
/// The transaction increased overall storage usage.
167165
///
168-
/// This means that the specified amount of balance was transferred from the origin
169-
/// to the involved deposit accounts.
166+
/// This means that the specified amount of balance was transferred from the call origin
167+
/// to the contracts involved.
170168
Charge(Balance),
171169
}
172170

frame/contracts/src/address.rs

Lines changed: 0 additions & 81 deletions
This file was deleted.

0 commit comments

Comments
 (0)