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

Contracts: seal0::balance should return the free balance #1254

Merged
merged 6 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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 substrate/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 substrate/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))
)
)
Comment on lines +53 to +59
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't it simpler not to modify the fixture but to add ED to the transferred balance?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm, not sure what you mean, this does add the ED to the transferred balance. How would I do that without modifying the fixture?

Copy link
Member Author

@xermicus xermicus Aug 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean the fixture is broken, given seal_balance should only return free balance: It tries to send away all free balance but expects the transfer to fail. Which should never fail. Hence we need to modify it anyways.


;; 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
8 changes: 6 additions & 2 deletions substrate/frame/contracts/src/exec.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ use frame_support::{
storage::{with_transaction, TransactionOutcome},
traits::{
fungible::{Inspect, Mutate},
tokens::Preservation,
tokens::{Fortitude, Preservation},
Contains, OriginTrait, Randomness, Time,
},
weights::Weight,
Expand Down Expand Up @@ -1367,7 +1367,11 @@ where
}

fn balance(&self) -> BalanceOf<T> {
Copy link
Contributor

@pgherveou pgherveou Aug 29, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good catch, looks like this is a regression from paritytech/substrate#14020

 	fn balance(&self) -> BalanceOf<T> {
-		T::Currency::free_balance(&self.top_frame().account_id)
+		T::Currency::balance(&self.top_frame().account_id)
 	}

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Those are different traits. On the Currency trait (before) we had free_balance. But on the new trait Inspect it is just balance which also doesn't include holds or freezes.

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

fn value_transferred(&self) -> BalanceOf<T> {
Expand Down
53 changes: 53 additions & 0 deletions substrate/frame/contracts/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5891,3 +5891,56 @@ 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;

// Check that the BOB contract has been instantiated.
get_contract(&addr);
xermicus marked this conversation as resolved.
Show resolved Hide resolved

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