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

SNO-624: Charge fee for outbound operations #940

Closed
wants to merge 80 commits into from
Closed

Conversation

yrong
Copy link
Contributor

@yrong yrong commented Aug 29, 2023

Fix for audit issue (1).
Resolves: SNO-624
Requires: Snowfork/cumulus#63

Context

It makes sense to let third-party Parachain who would like to use the bridge know what the fee is in advance, so added the Estimate API into the runtime. The extra fee is related to

  • dispatch gas for each command
  • gas price
  • swap price between Ether and DOT

So we've made all these factors configurable through extrinsic. Basically for most of the commands gas cost should be static, the only exception is maybe the arbitrary transact we'll add later, the dispatch gas should be some dynamic input from third party. Nonetheless we need to ensure all the gas cost in a reasonable range.

Based on that we've added a Configuring Extrinsic for all the factors above. And also there are two smoke tests added accordingly.

  • estimate_fee
  • configure_base_fee

@codecov
Copy link

codecov bot commented Aug 29, 2023

Codecov Report

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

Comparison is base (5ee2f71) 75.23% compared to head (584ba51) 74.46%.
Report is 1 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main     #940      +/-   ##
==========================================
- Coverage   75.23%   74.46%   -0.77%     
==========================================
  Files          41       42       +1     
  Lines        1813     1978     +165     
  Branches       75       75              
==========================================
+ Hits         1364     1473     +109     
- Misses        429      486      +57     
+ Partials       20       19       -1     
Flag Coverage Δ
rust 75.30% <67.31%> (-0.43%) ⬇️
solidity 70.62% <17.85%> (-2.48%) ⬇️

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

Files Coverage Δ
contracts/src/storage/CoreStorage.sol 100.00% <ø> (ø)
...hain/pallets/outbound-queue/runtime-api/src/lib.rs 0.00% <0.00%> (ø)
parachain/pallets/outbound-queue/src/lib.rs 94.81% <97.36%> (+4.40%) ⬆️
parachain/primitives/router/src/outbound/mod.rs 86.76% <60.00%> (-1.21%) ⬇️
parachain/pallets/outbound-queue/src/api.rs 0.00% <0.00%> (ø)
contracts/src/Gateway.sol 77.30% <35.71%> (+1.08%) ⬆️
contracts/src/FundAgent.sol 0.00% <0.00%> (ø)
parachain/pallets/control/src/lib.rs 61.53% <63.15%> (+0.86%) ⬆️
parachain/primitives/core/src/outbound.rs 36.76% <45.12%> (+19.57%) ⬆️

... and 1 file with indirect coverage changes

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

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.

Looks good. Added one comment.

@yrong yrong changed the base branch from ron/oak-security-audit-pick-sno-584 to main August 29, 2023 10:52
@yrong yrong changed the base branch from main to ron/oak-security-audit-pick-sno-584 August 29, 2023 10:55
@yrong yrong changed the base branch from ron/oak-security-audit-pick-sno-584 to main August 29, 2023 11:06
@yrong yrong changed the title Oak fix: issue 1 Requires configurable fee for each control operation Aug 30, 2023
parachain/pallets/control/src/lib.rs Outdated Show resolved Hide resolved
parachain/pallets/control/src/lib.rs Outdated Show resolved Hide resolved
parachain/pallets/control/src/lib.rs Outdated Show resolved Hide resolved
@yrong yrong marked this pull request as ready for review August 31, 2023 03:41
@yrong yrong self-assigned this Sep 1, 2023
@yrong yrong changed the title Requires configurable fee for each control operation SNO-624: Requires configurable fee for each control operation Sep 1, 2023
parachain/pallets/control/src/lib.rs Outdated Show resolved Hide resolved
parachain/primitives/router/src/outbound/mod.rs Outdated Show resolved Hide resolved
parachain/primitives/router/src/outbound/mod.rs Outdated Show resolved Hide resolved
parachain/pallets/control/src/lib.rs Outdated Show resolved Hide resolved
parachain/pallets/control/src/lib.rs Outdated Show resolved Hide resolved
parachain/pallets/control/src/lib.rs Outdated Show resolved Hide resolved
@yrong yrong changed the title SNO-624: Requires configurable fee for each control operation SNO-624: Requires configurable baseFee plus static per-operation fee multiplier Sep 5, 2023
@yrong yrong changed the title SNO-624: Requires configurable baseFee plus static per-operation fee multiplier SNO-624: Charge configurable baseFee plus static per-operation fee multiplier for outbound operations Sep 5, 2023
Copy link
Contributor

@claravanstaden claravanstaden left a comment

Choose a reason for hiding this comment

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

A mammoth task, this one! 😄 🐘 I've added a few comments and questions. I think overall more unit tests would be useful to test the lower level methods like charge_fees and set_outbound_fee_config.

contracts/test/Gateway.t.sol Show resolved Hide resolved
contracts/test/Gateway.t.sol Outdated Show resolved Hide resolved
contracts/src/FundAgent.sol Show resolved Hide resolved
parachain/pallets/outbound-queue/src/lib.rs Outdated Show resolved Hide resolved
parachain/pallets/outbound-queue/src/lib.rs Outdated Show resolved Hide resolved
web/packages/test/config/launch-config.toml Show resolved Hide resolved
web/packages/test/scripts/set-env.sh Outdated Show resolved Hide resolved
web/packages/test/scripts/set-env.sh Show resolved Hide resolved
parachain/pallets/control/src/lib.rs Outdated Show resolved Hide resolved
_typos.toml Outdated Show resolved Hide resolved
parachain/pallets/control/src/lib.rs Outdated Show resolved Hide resolved
parachain/pallets/control/src/lib.rs Outdated Show resolved Hide resolved
T::OutboundQueue::submit(ticket).map_err(|_| Error::<T>::SubmissionFailed)?;
Ok(())
}

pub fn convert_location(
mut location: MultiLocation,
) -> Result<(H256, Option<ParaId>, MultiLocation), DispatchError> {
) -> Result<(H256, ParaId, MultiLocation), DispatchError> {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Lets rather return a struct to make the code self-documenting:

Suggested change
) -> Result<(H256, ParaId, MultiLocation), DispatchError> {
) -> Result<OriginInfo, DispatchError> {
struct OriginInfo {
    agent_id: H256,
    para_id: ParaId,
    location: MultiLocation,
}

Copy link
Contributor Author

Choose a reason for hiding this comment

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

parachain/pallets/control/src/lib.rs Outdated Show resolved Hide resolved
parachain/pallets/control/src/lib.rs Outdated Show resolved Hide resolved
parachain/primitives/core/src/outbound.rs Outdated Show resolved Hide resolved
parachain/primitives/core/src/outbound.rs Outdated Show resolved Hide resolved
/// gas price in Wei from https://etherscan.io/gastracker
pub gas_price: Option<GasPriceInWei>,
/// swap ratio for Ether->DOT from https://www.coingecko.com/en/coins/polkadot/eth with precision difference between Ether->DOT(18->10)
pub swap_ratio: Option<FixedU128>,
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think it's simpler to not use Option. I.e when a new OutboundFeeConfig is updated, it completely replaces the old stored struct.

Copy link
Contributor Author

@yrong yrong Sep 27, 2023

Choose a reason for hiding this comment

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

The reason for the Option is in some cases we may want to configure it partially(e.g. only command_gas_map with the gas cost for some commands), In this case though it's possible to read previous value from storage and feed it into the call but is error prone IMO.

Comment on lines 14 to 16
fn estimate_fee(message: &Message) -> Result<MultiAssets, SubmitError>;

fn estimate_fee_by_command_index(command_index: u8) -> Result<MultiAssets, SubmitError>;
Copy link
Collaborator

Choose a reason for hiding this comment

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

I think these APIs need some more work

For example, the APIs also need to return the reward paid out on the Ethereum side. Origin parachains need this info so that they can make sure their sovereign accounts on Ethereum are topped up.

To cater for this, the APIs could return a tuple:

  1. Fee charged, if any, on BridgeHub (like for CreateAgent), in DOT
  2. Reward paid out on Ethereum, in ETH.

Do we really need both estimate_fee and estimate_fee_by_command_index? Let's try to settle on having one good API that satisfies all use cases.

One idea:

fn compute_fee_reward(Command: &command) -> Result<(u128, u128), SubmitError> 

Copy link
Contributor Author

@yrong yrong Sep 27, 2023

Choose a reason for hiding this comment

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

Some revamp accordingly in a6649d8 for the reward.

Yes, though currently only command is required for computing fees. I'm afraid there is some use case that depends on ParaId(e.g. discounts for a particular parachain). So would prefer the previous Message which includes the original ParaId.

@claravanstaden
Copy link
Contributor

Hey @yrong! I tried checking out your branch together with https://github.com/Snowfork/cumulus/tree/ron/sno-624 but I couldn't get it to build - I came across some compiler errors in the bridge hub rococo runtime xcm config due to the renaming of AgentHashedDescription. Will you let me know when I can checkout your branch and run your branch for a final review? 😄

Co-authored-by: Clara van Staden <claravanstaden64@gmail.com>
@yrong
Copy link
Contributor Author

yrong commented Sep 29, 2023

I tried checking out your branch together with https://github.com/Snowfork/cumulus/tree/ron/sno-624

@claravanstaden Thanks for the review! Actually the cumulus branch is ron/oak-sno-624 and the companion PR Snowfork/cumulus#63

@yrong
Copy link
Contributor Author

yrong commented Oct 4, 2023

Closed in favor of #968

@yrong yrong closed this Oct 4, 2023
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