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

Refactoring XcmExportFeeToSibling and more tests for outbound fee #1109

Merged
merged 27 commits into from
Jan 19, 2024

Conversation

yrong
Copy link
Contributor

@yrong yrong commented Jan 11, 2024

Resolves: SNO-820, SNO-812

This is the sibling PR for #1107 without the change required for the xcm primitives in Snowfork/polkadot-sdk#95.

In this PR we focus on

  • adding more sanity checks for the params and more tests accordingly
  • improving the error log for XcmExportFeeToSibling

Copy link

codecov bot commented Jan 11, 2024

Codecov Report

Attention: 27 lines in your changes are missing coverage. Please review.

Comparison is base (e0506f7) 76.36% compared to head (15132aa) 76.25%.
Report is 7 commits behind head on main.

Files Patch % Lines
parachain/runtime/runtime-common/src/lib.rs 62.16% 14 Missing ⚠️
parachain/pallets/ethereum-client/src/impls.rs 0.00% 6 Missing ⚠️
parachain/runtime/test-common/src/lib.rs 0.00% 6 Missing ⚠️
parachain/pallets/system/src/lib.rs 66.66% 1 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #1109      +/-   ##
==========================================
- Coverage   76.36%   76.25%   -0.12%     
==========================================
  Files          56       58       +2     
  Lines        2344     2400      +56     
  Branches       72       72              
==========================================
+ Hits         1790     1830      +40     
- Misses        537      553      +16     
  Partials       17       17              
Flag Coverage Δ
rust 75.37% <49.05%> (-0.12%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@yrong yrong marked this pull request as ready for review January 11, 2024 12:00
Copy link
Contributor

@alistair-singh alistair-singh left a comment

Choose a reason for hiding this comment

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

Looks good. Added minor comments.

parachain/primitives/core/src/lib.rs Show resolved Hide resolved
parachain/runtime/runtime-common/src/lib.rs Outdated Show resolved Hide resolved
parachain/runtime/runtime-common/src/lib.rs Outdated Show resolved Hide resolved
parachain/runtime/runtime-common/src/lib.rs Outdated Show resolved Hide resolved
parachain/runtime/runtime-common/src/lib.rs Outdated Show resolved Hide resolved
@yrong yrong changed the title Improve handle fee with more tests and logs Sanity checks for the pricing params & Improve XcmExportFeeToSibling with more tests and logs Jan 16, 2024
Comment on lines 61 to 68
ensure!(
gas_used_at_most > T::GasMeter::MAXIMUM_BASE_GAS &&
gas_used_at_most < T::GasMeter::MAXIMUM_GAS_CAP,
SendError::MessageInvalidGasFees
);
let fee = Self::calculate_fee(gas_used_at_most, T::PricingParameters::get());
let zero_fee = <Self as SendMessageFeeProvider>::Balance::zero();
ensure!(fee.local > zero_fee && fee.remote > zero_fee, SendError::MessageInvalidGasFees);
Copy link
Collaborator

@vgeddes vgeddes Jan 16, 2024

Choose a reason for hiding this comment

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

These checks don't make sense to me. The expectation is that T::GasMeter::maximum_gas_used_at_most and Self::calculate_fee always return correct values for any arbitrary Command.

This behavior is important, as we can't reject commands because for some reason we could not calculate fees. To do otherwise would lead to a loss of user funds.

It seems these checks are acting as unit tests, and so we should convert them to actual unit tests.

Copy link
Contributor Author

@yrong yrong Jan 17, 2024

Choose a reason for hiding this comment

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

This check fee.remote > 0 is for the test case as I mentioned here in #1109 (comment) in which the remote_fee calculated being zero(though pricing parameters are all non-zero and valid). It will lead to the sovereign account of the sibling chain not being properly refunded in XcmExportFeeToSibling and finally the agent on the Ethereum side can't be funded.

That's also the reason why I introduced the sanity check of the pricing params, prefer to reject commands because for some reason we could not calculate fees rather than silently ignoring.

Copy link
Contributor Author

@yrong yrong Jan 17, 2024

Choose a reason for hiding this comment

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

MAXIMUM_GAS_CAP added here is a hard limit that applies to all commands. It's mainly for command Upgrade with a sanity initializer_max_gas. It also applies to the future arbitrary transact for which the gas is dynamic from the input of the end-user.

MAXIMUM_GAS_CAP should be consistent with the default relayer config otherwise the check in Gateway will always fail and the bridge stuck.

Copy link
Collaborator

@vgeddes vgeddes Jan 17, 2024

Choose a reason for hiding this comment

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

This check fee.remote > 0 is for the test case as I mentioned here in #1109 (comment) in which the remote_fee calculated being zero(though pricing parameters are all non-zero and valid). It will lead to the sovereign account of the sibling chain not being properly refunded in XcmExportFeeToSibling and finally the agent on the Ethereum side can't be funded.

Thanks Ron, that makes sense. But it seems there is a bug in our code if the calculated remote fee ends up being zero. Can you investigate why this happens? We should try and fix it so that we don't need to have these checks.

Copy link
Collaborator

Choose a reason for hiding this comment

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

I don't think we should introduce MAXIMUM_GAS_CAP, for the same reasons I outlined in #1109 (comment).

Copy link
Collaborator

Choose a reason for hiding this comment

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

See #1109 (comment). I think its fine if calculated remote fee is zero. It just means the fee is too low to be represented using DOT decimals.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Checks reverted in af15053

parachain/primitives/core/src/outbound.rs Outdated Show resolved Hide resolved
parachain/primitives/core/src/pricing.rs Outdated Show resolved Hide resolved
Comment on lines 35 to 37
if self.exchange_rate < FixedU128::from_rational(1, 10000) ||
self.exchange_rate > FixedU128::from_rational(1, 1)
{
Copy link
Collaborator

Choose a reason for hiding this comment

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

I prefer not to change the validation logic. I also thought about this approach, but ultimately decided it wasn't worth it.

  1. If the exchange rate does actually mistakenly get set to 1/9999 (slightly less than the limit), then that practically still ends up blocking the bridge anyway.
  2. The upper limit is arbitrary, and if the real-world exchange rate breaches the limit (quite possible), then a runtime upgrade would be required to fix the validation code.

Copy link
Contributor Author

@yrong yrong Jan 16, 2024

Choose a reason for hiding this comment

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

Reverted in f43fce3

My only concern is #1109 (comment) but can be avoided by double check before sending the governance call so should be fine.

parachain/primitives/core/src/pricing.rs Outdated Show resolved Hide resolved
parachain/runtime/runtime-common/src/tests.rs Outdated Show resolved Hide resolved
parachain/runtime/runtime-common/src/tests.rs Outdated Show resolved Hide resolved
parachain/runtime/runtime-common/src/tests.rs Outdated Show resolved Hide resolved
parachain/runtime/runtime-common/src/tests.rs Outdated Show resolved Hide resolved
parachain/runtime/test-common/src/lib.rs Outdated Show resolved Hide resolved
@yrong yrong changed the title Sanity checks for the pricing params & Improve XcmExportFeeToSibling with more tests and logs Improve XcmExportFeeToSibling with more tests and logs Jan 17, 2024
@yrong yrong changed the title Improve XcmExportFeeToSibling with more tests and logs Refactoring XcmExportFeeToSibling with logs/tests and add some more tests for calculating the outbound fee Jan 18, 2024
@yrong yrong changed the title Refactoring XcmExportFeeToSibling with logs/tests and add some more tests for calculating the outbound fee Refactoring XcmExportFeeToSibling with logs/tests and more tests for the outbound fee Jan 18, 2024
@yrong yrong changed the title Refactoring XcmExportFeeToSibling with logs/tests and more tests for the outbound fee Refactoring XcmExportFeeToSibling and more tests for outbound fee Jan 18, 2024
Copy link
Collaborator

@vgeddes vgeddes left a comment

Choose a reason for hiding this comment

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

+1

@yrong yrong merged commit 4e24732 into main Jan 19, 2024
7 checks passed
@yrong yrong deleted the ron/handle-fee branch January 19, 2024 02:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants