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

Fix xcmp_fee for automation-time #366

Merged
merged 32 commits into from
Jul 12, 2023
Merged

Fix xcmp_fee for automation-time #366

merged 32 commits into from
Jul 12, 2023

Conversation

imstar15
Copy link
Member

@imstar15 imstar15 commented Jun 9, 2023

No description provided.

@imstar15 imstar15 changed the title [WIP]Fix xcmp_fee for automation-time Fix xcmp_fee for automation-time Jun 30, 2023
@imstar15 imstar15 requested a review from chrisli30 June 30, 2023 08:46
@imstar15 imstar15 marked this pull request as ready for review June 30, 2023 08:47
Copy link
Member

@chrisli30 chrisli30 left a comment

Choose a reason for hiding this comment

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

@imstar15 can you add comments on this webpage pointing out the core changes of this PR?

  1. What is the TransactInfo before change? What are the keys and values?
  2. What is the TransactInfo after? What are the keys and values?
  3. If any extrinsic interface has changed, please provide screenshots for before and after.

Adding @v9n for a look.

@chrisli30
Copy link
Member

In addition, "This branch is out-of-date with the base branch". Please rebase master.

@chrisli30 chrisli30 requested a review from v9n July 1, 2023 00:54
@chrisli30
Copy link
Member

chrisli30 commented Jul 1, 2023

We need to keep optimizing the Flow parameter in the transact_info for better functionality. Rather than specifying the flow for a destination, we should specify the supported flows instead(for example, config a supported array for each chain). Most destination could support both flows.

In practice, this means that we will have separate extrinsics for each flow. The scheduleXcmpTask function would utilize the Normal flow, while scheduleXcmpTaskThroughProxy would utilize the Alternate flow. By distinguishing between the flows this way, we can achieve more efficient and targeted execution based on each task type’s requirements.

Furthermore, it's important to note that the current names, Normal and Alternate, lack clear meaning. The key distinction between them lies in who pays for the fees. In the Normal flow, Turing's sovereign account covers the XCM fee through ReserveAssetDeposited. On the other hand, in the Alternate flow, the user's proxy wallet bears the XCM fee. Another key difference is that the Alternate flow requires the user to pay XCM fees on the receiving end, thereby reducing the required amount of tokens on Turing (we only need to charge for task scheduling). However, whether users truly care about the slight fee difference remains uncertain.

You can find detailed instructions for each flow in the following code links:

Normal flow instructions
https://github.com/OAK-Foundation/OAK-blockchain/blob/67b4f7f5976d58dd35b080cf325eb01dff4921a7/pallets/xcmp-handler/src/lib.rs#L320

Alternate flow instructions
https://github.com/OAK-Foundation/OAK-blockchain/blob/67b4f7f5976d58dd35b080cf325eb01dff4921a7/pallets/xcmp-handler/src/lib.rs#L386C1-L386C1

While the name "Flow" is acceptable, it's worth considering more descriptive options since these sequences, combinations, or templates have distinct characteristics. I propose renaming the Flow selector to XcmTaskSupported, which consists of two types: scheduleWithPrepaidFees for the normal flow and scheduleWithoutPrepaidFees for the alternate flow. In both cases, the PrepaidFeeds refer to both the XCM fee and execution fee at the receiving end.

@chrisli30
Copy link
Member

Discussed the new flow parametes with Charles, we don’t need the chain storage XcmTaskSupported as of now, so after this PR there should be no XCM task relevant parameter left in chain storage.

@imstar15
Copy link
Member Author

imstar15 commented Jul 3, 2023

1, What is the TransactInfo before change? What are the keys and values?
2. What is the TransactInfo after? What are the keys and values?

We have removed TransactInfo storage.
User should input overallWeight and executionFee params for scheduleXcmpTask and scheduleXcmpTaskThroughProxy extrinsic.

overallWeight = encodedCallWeight + instructionWeight * instructionCount
executionFee =  FEE_PER_SECOND * overallWeight / WEIGHT_PER_SECOND

ScheduleXcmpTask run with PayThroughSovereignAccount flow, the execution fee for XCM instructions will be deducted in turing chain.
ScheduleXcmpTaskThroughProxy run with PayThroughRemoteDerivativeAccount flow, the execution fee for XCM instructions will not be deducted in turing chain.

  1. If any extrinsic interface has changed, please provide screenshots for before and after.

Changes:

  1. paraId -> destination: Multilation
  2. currencyId -> scheduleFee
  3. xcmAssetLocation -> executionFee, AssetPayment { asset_location, amount }
  4. overallWeight, new Parameter

Before:
image

After:
image

@imstar15 imstar15 requested review from chrisli30 and v9n July 7, 2023 15:24
Copy link
Member

@chrisli30 chrisli30 left a comment

Choose a reason for hiding this comment

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

A lot to improve for this code change.

pallets/automation-time/src/tests.rs Outdated Show resolved Hide resolved
@@ -50,6 +50,9 @@ pub const NATIVE: CurrencyId = 0;
pub const NATIVE_LOCATION: MultiLocation = MultiLocation { parents: 0, interior: Here };
pub const FOREIGN_CURRENCY_ID: CurrencyId = 1;

pub const FEE_PER_SECOND: u128 = 100_000_000_000;
pub const XCMP_TASK_REF_TIME: u128 = 20_000;
Copy link
Member

Choose a reason for hiding this comment

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

You need to map the fee_per_second to destination, so we have

dest1: {
weight,
refTime,
assets: [ {token 1, fee per second 1}, {token 2, fee per second 2} ]
}

dest2 ..

and test them.

pallets/automation-time/src/tests.rs Show resolved Hide resolved
AutomationTime::calculate_schedule_fee_amount(&action, execution_count)
.expect("Calculate schedule fee amount should work");
let expected_schedule_fee_amount =
FEE_PER_SECOND * XCMP_TASK_REF_TIME * (execution_count as u128) /
Copy link
Member

Choose a reason for hiding this comment

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

action is what you passed in to the function calculate_schedule_fee_amount. You need to use the variable values within action for calculation, instead of using a separate const variable XCMP_TASK_REF_TIME. XCMP_TASK_REF_TIME is not used in mock.

Copy link
Member

Choose a reason for hiding this comment

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

WEIGHT_REF_TIME_PER_SECOND is a global variable for our chain, so that’s the only constant variable which can be used in this line of validation.

Fee per second is in relation to each destination, you need to mock and to get them by destination.fee_per_second.

Copy link
Member Author

@imstar15 imstar15 Jul 10, 2023

Choose a reason for hiding this comment

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

If the task is a xcmp task. the weight of the task is a fixed variable.

It is defined here.

https://github.com/OAK-Foundation/OAK-blockchain/blob/69464509e9dd628485b6633df1b33718ce42cbab/pallets/automation-time/src/weights.rs#L311

It is not calculated based on the data of the task/action.

So, I think it should be fine to define the XCMP_TASK_REF_TIME constant as well.

pallets/automation-time/src/tests.rs Show resolved Hide resolved
pallets/automation-time/src/tests.rs Show resolved Hide resolved
@imstar15 imstar15 changed the title Fix xcmp_fee for automation-time [WIP]Fix xcmp_fee for automation-time Jul 10, 2023
Ok(())
}

/// Withdraw the fee.
fn withdraw_fee(&self) -> Result<(), DispatchError> {
let fee = self.execution_fee.saturating_add(self.xcmp_fee);
let fee = self.schedule_fee_amount.saturating_add(self.execution_fee_amount);
Copy link
Member

@v9n v9n Jul 10, 2023

Choose a reason for hiding this comment

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

let fee = self.schedule_fee_amount.saturating_add(self.execution_fee_amount);

we used this line in 2 places can_pay_fee and withdraw_fee . even though it's one liner I think it increase the chance of us accidently forgetting to keep sync that logic between the two. Instead, we may turn that single line into a method get_final_fee or some similar name, to encapsulate the logic of how we calculate the final fee

Copy link
Member Author

@imstar15 imstar15 Jul 11, 2023

Choose a reason for hiding this comment

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

We will modify the FeeHandler logic in a later version, so don't worry about it in this PR.

@imstar15 imstar15 requested a review from v9n July 11, 2023 14:22
overall_weight: Weight::from_ref_time(200_000),
schedule_as: Some(delegator_account),
flow: InstructionSequence::PayThroughRemoteDerivativeAccount,
let num_of_execution = 2;
Copy link
Member

Choose a reason for hiding this comment

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

This function is important. Could we use an arbitrary number such as 15, instead of 2? 2 is too common.

Copy link
Member

Choose a reason for hiding this comment

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

Same applies to the below num_of_execution values. Maybe just use a random value from 1 to 20.

Copy link
Member

@chrisli30 chrisli30 left a comment

Choose a reason for hiding this comment

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

I have a couple of minor suggestions but apply to many tests.

overall_weight: Weight::from_ref_time(200_000),
schedule_as: Some(delegator_account),
flow: InstructionSequence::PayThroughRemoteDerivativeAccount,
let num_of_execution = 2;
Copy link
Member

Choose a reason for hiding this comment

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

Same applies to the below num_of_execution values. Maybe just use a random value from 1 to 20.

),
..XcmpTaskParams::default()
});
let fee_amount_1 = AutomationTime::calculate_schedule_fee_amount(&action, num_of_execution)
Copy link
Member

@chrisli30 chrisli30 Jul 12, 2023

Choose a reason for hiding this comment

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

Please use fee_amount_absolute instead of fee_amount_1. The same goes to all _1 and _2s. What are 1 and 2?

@imstar15 imstar15 changed the title [WIP]Fix xcmp_fee for automation-time Fix xcmp_fee for automation-time Jul 12, 2023
@imstar15 imstar15 requested a review from chrisli30 July 12, 2023 02:32
para_id: ParaId,
currency_id: T::CurrencyId,
xcm_asset_location: VersionedMultiLocation,
destination: Box<VersionedMultiLocation>,
Copy link
Member

Choose a reason for hiding this comment

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

@imstar15 could you help add a comment on top of this because they are changed now? they used to be this:

 375         /// Schedule a task through XCMP to fire an XCMP message with a provided call.
 376         ///
 377         /// Before the task can be scheduled the task must past validation checks.
 378         /// * The transaction is signed
 379         /// * The provided_id's length > 0
 380         /// * The times are valid
 381         /// * The given asset location is supported
 382         ///
 383         /// # Parameters
 384         /// * `provided_id`: An id provided by the user. This id must be unique for the user.
 385         /// * `execution_times`: The list of unix standard times in seconds for when the task should run.
 386         /// * `para_id`: Parachain id the XCMP call will be sent to.
 387         /// * `currency_id`: The currency in which fees will be paid.
 388         /// * `encoded_call`: Call that will be sent via XCMP to the parachain id provided.
 389         /// * `encoded_call_weight`: Required weight at most the provided call will take.

Copy link
Member Author

Choose a reason for hiding this comment

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

Okay, I will update the comment

Copy link
Member Author

Choose a reason for hiding this comment

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

I have updated the comment. Please review.

@imstar15 imstar15 requested a review from v9n July 12, 2023 02:59
Copy link
Member

@chrisli30 chrisli30 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 to me!

Copy link
Member

@v9n v9n left a comment

Choose a reason for hiding this comment

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

lgtm

@imstar15 imstar15 merged commit 54272be into master Jul 12, 2023
@imstar15 imstar15 deleted the xcmp-fee branch July 12, 2023 13:14
imstar15 added a commit to AvaProtocol/xcm-demo that referenced this pull request Jul 12, 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.

3 participants