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

Contracts: runtime_call and storage_deposit #13990

Merged
merged 21 commits into from
Apr 29, 2023

Conversation

pgherveou
Copy link
Contributor

@pgherveou pgherveou commented Apr 24, 2023

This PR makes the charge function fallible, so that when the the caller is unable to pay for the storage deposit we rollback the whole transaction.

@pgherveou pgherveou requested a review from athei as a code owner April 24, 2023 14:12
@pgherveou pgherveou force-pushed the pg/storage_deposit_runtime branch from 62fc1e3 to 909cbae Compare April 24, 2023 14:40
@pgherveou pgherveou added A0-please_review Pull request needs code review. B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. D5-nicetohaveaudit ⚠️ PR contains trivial changes to logic that should be properly reviewed. labels Apr 24, 2023
data.encode(),
);

assert_err_ignore_postinfo!(result, TokenError::FundsUnavailable);
Copy link
Contributor Author

Choose a reason for hiding this comment

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

would be nice to test that the transaction gas were actually charge to the user, but I am not too sure how this can be done within our test framework

Copy link
Contributor

Choose a reason for hiding this comment

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

can't you just check that Alice's balance became lower than it was before the tx?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Updated the test, so we can validate that the fees are the same whether the transaction fails (because the payment failed) or not.
I don't think we can test Alice's balance because as far as I know, this would be done in the pre/post hook of the transaction_payment pallet that does not exist in the Test config

for reference

fn pre_dispatch(
self,
who: &Self::AccountId,
call: &Self::Call,
info: &DispatchInfoOf<Self::Call>,
len: usize,
) -> Result<Self::Pre, TransactionValidityError> {
let (_fee, imbalance) = self.withdraw_fee(who, call, info, len)?;
Ok((self.0, who.clone(), imbalance))
}
fn post_dispatch(
maybe_pre: Option<Self::Pre>,
info: &DispatchInfoOf<Self::Call>,
post_info: &PostDispatchInfoOf<Self::Call>,
len: usize,
_result: &DispatchResult,
) -> Result<(), TransactionValidityError> {
if let Some((tip, who, imbalance)) = maybe_pre {
let actual_fee = Pallet::<T>::compute_actual_fee(len as u32, info, post_info, tip);
T::OnChargeTransaction::correct_and_deposit_fee(
&who, info, post_info, actual_fee, tip, imbalance,
)?;
Pallet::<T>::deposit_event(Event::<T>::TransactionFeePaid { who, actual_fee, tip });
}
Ok(())
}

Copy link
Member

Choose a reason for hiding this comment

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

You can check the events emitted during execution. We do that in other tests. There is a event for transaction fee payment.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

we would need the transaction_payment pallet for this in test, I don't think we have it 🤔 might have overlooked, will check again

Copy link
Member

Choose a reason for hiding this comment

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

Ahh yeah. You are right. We didn't set this up in tests. It is fine. We don't really need to test this.

Copy link
Contributor

@agryaznov agryaznov left a comment

Choose a reason for hiding this comment

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

Just some nitpicks

frame/contracts/src/tests.rs Outdated Show resolved Hide resolved
frame/contracts/src/tests.rs Outdated Show resolved Hide resolved
frame/contracts/fixtures/call_runtime_and_call.wat Outdated Show resolved Hide resolved
frame/contracts/fixtures/call_runtime_and_call.wat Outdated Show resolved Hide resolved
frame/contracts/fixtures/call_runtime_and_call.wat Outdated Show resolved Hide resolved
data.encode(),
);

assert_err_ignore_postinfo!(result, TokenError::FundsUnavailable);
Copy link
Contributor

Choose a reason for hiding this comment

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

can't you just check that Alice's balance became lower than it was before the tx?

frame/contracts/src/lib.rs Show resolved Hide resolved
@agryaznov
Copy link
Contributor

also some PR description would be great to have

pgherveou and others added 6 commits April 25, 2023 23:01
Co-authored-by: Sasha Gryaznov <hi@agryaznov.com>
Co-authored-by: Sasha Gryaznov <hi@agryaznov.com>
Co-authored-by: Sasha Gryaznov <hi@agryaznov.com>
Co-authored-by: Sasha Gryaznov <hi@agryaznov.com>
@pgherveou pgherveou requested a review from agryaznov April 26, 2023 08:22
@pgherveou
Copy link
Contributor Author

bot help

@command-bot
Copy link

command-bot bot commented Apr 27, 2023

Here's a link to docs

@pgherveou
Copy link
Contributor Author

bot rebase

@paritytech-processbot
Copy link

Rebased

@@ -366,14 +366,14 @@ where
///
Copy link
Contributor

@agryaznov agryaznov Apr 28, 2023

Choose a reason for hiding this comment

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

docs above this line could be improved a bit to reflect the fact this is now a fallible fn

@pgherveou
Copy link
Contributor Author

bot merge

@paritytech-processbot paritytech-processbot bot merged commit bc8f2db into master Apr 29, 2023
@paritytech-processbot paritytech-processbot bot deleted the pg/storage_deposit_runtime branch April 29, 2023 16:07
@jakoblell jakoblell added D1-audited 👍 PR contains changes to fund-managing logic that has been properly reviewed and externally audited and removed D5-nicetohaveaudit ⚠️ PR contains trivial changes to logic that should be properly reviewed. labels May 2, 2023
gpestana pushed a commit that referenced this pull request May 4, 2023
* wip

* add comments

* fix comment

* comments

* comments

* PR comment

* field orders

* Update frame/contracts/src/tests.rs

* Update frame/contracts/fixtures/call_runtime_and_call.wat

Co-authored-by: Sasha Gryaznov <hi@agryaznov.com>

* Apply suggestions from code review

Co-authored-by: Sasha Gryaznov <hi@agryaznov.com>

* Apply suggestions from code review

Co-authored-by: Sasha Gryaznov <hi@agryaznov.com>

* Update frame/contracts/src/tests.rs

Co-authored-by: Sasha Gryaznov <hi@agryaznov.com>

* Validate fees of failed call

* Update frame/contracts/src/tests.rs

* Update frame/contracts/src/tests.rs

* Update frame/contracts/src/tests.rs

* bubble up refund error

* rename fixture file

---------

Co-authored-by: Sasha Gryaznov <hi@agryaznov.com>
Co-authored-by: parity-processbot <>
nathanwhit pushed a commit to nathanwhit/substrate that referenced this pull request Jul 19, 2023
* wip

* add comments

* fix comment

* comments

* comments

* PR comment

* field orders

* Update frame/contracts/src/tests.rs

* Update frame/contracts/fixtures/call_runtime_and_call.wat

Co-authored-by: Sasha Gryaznov <hi@agryaznov.com>

* Apply suggestions from code review

Co-authored-by: Sasha Gryaznov <hi@agryaznov.com>

* Apply suggestions from code review

Co-authored-by: Sasha Gryaznov <hi@agryaznov.com>

* Update frame/contracts/src/tests.rs

Co-authored-by: Sasha Gryaznov <hi@agryaznov.com>

* Validate fees of failed call

* Update frame/contracts/src/tests.rs

* Update frame/contracts/src/tests.rs

* Update frame/contracts/src/tests.rs

* bubble up refund error

* rename fixture file

---------

Co-authored-by: Sasha Gryaznov <hi@agryaznov.com>
Co-authored-by: parity-processbot <>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
A0-please_review Pull request needs code review. B0-silent Changes should not be mentioned in any release notes C1-low PR touches the given topic and has a low impact on builders. D1-audited 👍 PR contains changes to fund-managing logic that has been properly reviewed and externally audited
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants