Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pick fix from 24c41f10bb29857cc7fcb316d47ac055c15537f7 #46

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 42 additions & 0 deletions frame/contracts/fixtures/balance.wat
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
(module
(import "seal0" "seal_balance" (func $seal_balance (param i32 i32)))
(import "env" "memory" (memory 1 1))

;; [0, 8) reserved for $seal_balance output

;; [8, 16) length of the buffer for $seal_balance
(data (i32.const 8) "\08")

;; [16, inf) zero initialized

(func $assert (param i32)
(block $ok
(br_if $ok
(get_local 0)
)
(unreachable)
)
)

(func (export "deploy"))

(func (export "call")
(call $seal_balance (i32.const 0) (i32.const 8))

;; Balance should be encoded as a u64.
(call $assert
(i32.eq
(i32.load (i32.const 8))
(i32.const 8)
)
)

;; Assert the free balance to be zero.
(call $assert
(i64.eq
(i64.load (i32.const 0))
(i64.const 0)
)
)
)
)
35 changes: 30 additions & 5 deletions frame/contracts/fixtures/drain.wat
Original file line number Diff line number Diff line change
@@ -1,14 +1,20 @@
(module
(import "seal0" "seal_balance" (func $seal_balance (param i32 i32)))
(import "seal0" "seal_minimum_balance" (func $seal_minimum_balance (param i32 i32)))
(import "seal0" "seal_transfer" (func $seal_transfer (param i32 i32 i32 i32) (result i32)))
(import "env" "memory" (memory 1 1))

;; [0, 8) reserved for $seal_balance output

;; [8, 16) length of the buffer
;; [8, 16) length of the buffer for $seal_balance
(data (i32.const 8) "\08")

;; [16, inf) zero initialized
;; [16, 24) reserved for $seal_minimum_balance

;; [24, 32) length of the buffer for $seal_minimum_balance
(data (i32.const 24) "\08")

;; [32, inf) zero initialized

(func $assert (param i32)
(block $ok
Expand All @@ -33,13 +39,32 @@
)
)

;; Try to self-destruct by sending full balance to the 0 address.
;; Get the minimum balance.
(call $seal_minimum_balance (i32.const 16) (i32.const 24))

;; Minimum balance should be encoded as a u64.
(call $assert
(i32.eq
(i32.load (i32.const 24))
(i32.const 8)
)
)

;; Make the transferred value exceed the balance by adding the minimum balance.
(i64.store (i32.const 0)
(i64.add
(i64.load (i32.const 0))
(i64.load (i32.const 16))
)
)

;; Try to self-destruct by sending more balance to the 0 address.
;; The call will fail because a contract transfer has a keep alive requirement
(call $assert
(i32.eq
(call $seal_transfer
(i32.const 16) ;; Pointer to destination address
(i32.const 32) ;; Length of destination address
(i32.const 32) ;; Pointer to destination address
(i32.const 48) ;; Length of destination address
(i32.const 0) ;; Pointer to the buffer with value to transfer
(i32.const 8) ;; Length of the buffer with value to transfer
)
Expand Down
10 changes: 8 additions & 2 deletions frame/contracts/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ use frame_support::{
},
storage::{with_transaction, TransactionOutcome},
traits::{
tokens::{Fortitude::Polite, Preservation::Expendable},
tokens::{Fortitude::Polite, Preservation::Expendable, Preservation},
Contains, Currency, ExistenceRequirement, OriginTrait, Randomness, Time,
},
weights::Weight,
Expand Down Expand Up @@ -1333,7 +1333,13 @@ where
}

fn balance(&self) -> BalanceOf<T> {
T::Currency::free_balance(&self.top_frame().account_id)
use frame_support::traits::fungible::Inspect;

T::Currency::reducible_balance(
&self.top_frame().account_id,
Preservation::Preserve,
Polite,
)
}

fn value_transferred(&self) -> BalanceOf<T> {
Expand Down
52 changes: 51 additions & 1 deletion frame/contracts/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ use frame_support::{
storage::child,
traits::{
ConstU32, ConstU64, Contains, Currency, ExistenceRequirement, LockableCurrency, OnIdle,
OnInitialize, StorageVersion, WithdrawReasons,
OnInitialize, StorageVersion, WithdrawReasons, fungible::Mutate
},
weights::{constants::WEIGHT_REF_TIME_PER_SECOND, Weight},
};
Expand Down Expand Up @@ -5628,3 +5628,53 @@ fn root_cannot_instantiate() {
);
});
}

#[test]
fn balance_api_returns_free_balance() {
let (wasm, _code_hash) = compile_module::<Test>("balance").unwrap();
ExtBuilder::default().existential_deposit(200).build().execute_with(|| {
let _ = <Test as Config>::Currency::set_balance(&ALICE, 1_000_000);

// Instantiate the BOB contract without any extra balance.
let addr = Contracts::bare_instantiate(
ALICE,
0,
GAS_LIMIT,
None,
Code::Upload(wasm.to_vec()),
vec![],
vec![],
DebugInfo::Skip,
CollectEvents::Skip,
)
.result
.unwrap()
.account_id;

let value = 0;
// Call BOB which makes it call the balance runtime API.
// The contract code asserts that the returned balance is 0.
assert_ok!(Contracts::call(
RuntimeOrigin::signed(ALICE),
addr.clone(),
value,
GAS_LIMIT,
None,
vec![]
));

let value = 1;
// Calling with value will trap the contract.
assert_err_ignore_postinfo!(
Contracts::call(
RuntimeOrigin::signed(ALICE),
addr.clone(),
value,
GAS_LIMIT,
None,
vec![]
),
<Error<Test>>::ContractTrapped
);
});
}
Loading