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

Add OnFeeCharged trait in fee modules.#1924

Closed
shaunxw wants to merge 9 commits intoparitytech:masterfrom
shaunxw:sw/on-fee-charged
Closed

Add OnFeeCharged trait in fee modules.#1924
shaunxw wants to merge 9 commits intoparitytech:masterfrom
shaunxw:sw/on-fee-charged

Conversation

@shaunxw
Copy link
Contributor

@shaunxw shaunxw commented Mar 5, 2019

PR for issue #1923

This is an improvement to fees module. The OnFeeCharged trait gives a chance to deal with accumulated fees.

@parity-cla-bot
Copy link

It looks like @shaopengw signed our Contributor License Agreement. 👍

Many thanks,

Parity Technologies CLA Bot

1 similar comment
@parity-cla-bot
Copy link

It looks like @shaopengw signed our Contributor License Agreement. 👍

Many thanks,

Parity Technologies CLA Bot

fn on_fee_charged(_: &Amount) {}
}

impl<
Copy link
Member

Choose a reason for hiding this comment

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

Could you do this implementation please like seen here.


fn on_finalise() {
let extrinsic_count = <system::Module<T>>::extrinsic_count();
let block_fee = (0..extrinsic_count).fold(<AssetOf<T>>::sa(0), |total, index| {
Copy link
Member

Choose a reason for hiding this comment

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

Can we not combine this with the loop below? To have this loop just once?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@bkchr all updated :]

Copy link
Member

@bkchr bkchr 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 so far, but still some stuff need to be improved.

@bkchr
Copy link
Member

bkchr commented Mar 6, 2019

Could you please add a test for this as well? Maybe just extend the on_finalise_should_work test and check that on_fee_charged is called with the correct amount.

@@ -47,13 +72,17 @@ decl_module! {

fn on_finalise() {
Copy link
Member

@gavofyork gavofyork Mar 6, 2019

Choose a reason for hiding this comment

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

This could be done more cleanly by adding an event hook OnTransactionFeePaid into executive (or, probably better, system::Trait which would be called from executive) which could then be hooked in to fees::Trait::OnFeeChanged. This way you can avoid a second loop over all the transactions at the end of the block and writes into storage needed for CurrentTransactionFee.

Copy link
Contributor

Choose a reason for hiding this comment

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

I believe that is basically the ChargeFee trait. Storage write (or some in memory write) is necessary to combine multiple fees together and emit a single event with the accumulated fee (#1815). I don't think there is other way to achieve that in the current substrate.

Copy link
Member

Choose a reason for hiding this comment

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

In-memory write is way faster than storage round-trip, so I would prefer it to be donee thtat way, especially since it will affect all chains, even those with no use for this fee mechanism.

Copy link
Contributor

Choose a reason for hiding this comment

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

Make sense. Is there existing in memory store can be used? Or need to crate this new mechanism? which will be out of scope of this PR and shouldn’t prevent this PR from been merged.

@shaunxw
Copy link
Contributor Author

shaunxw commented Mar 8, 2019

@bkchr Could you please suggest a clean way(or existing example in srml) to check if on_fee_charged called with correct amount in unit tests?

The one I could come up with is a mock implementing OnFeeCharged, but then a global variable is needed to hold the amount so it could be checked after on_finalise call, and creating global variables in mock.rs doesn't sound a ideal solution.

@bkchr
Copy link
Member

bkchr commented Mar 8, 2019

Create some type in fees/src/mock.rs that implements OnFeeCharged and add this here.
When on_fee_charged is called, write the value into storage and get that value afterwards.
Checking the value can be done here:

@shaunxw
Copy link
Contributor Author

shaunxw commented Mar 11, 2019

@bkchr Test added. :)

@bkchr
Copy link
Member

bkchr commented Mar 11, 2019

I'm happy now, but we still need to integrate the requested changes by @gavofyork.
@gavofyork I'm not 100% sure on how to implement your requested changes, could you explain it more detailed for us?
Do you want to have 1 storage item for all transaction fees? So, that we don't require to load extrinsic_count items from the storage?

@gavofyork
Copy link
Member

There would be no need for any storage used. Basically, you just call the transaction-fee-paid hook right at the point that it is paid (in executive) rather than putting it all in storage and then re-looping in on_finalize.

@xlc
Copy link
Contributor

xlc commented Mar 12, 2019

That's one possible way to do it but one of the goal of this fees module is to provide accumulated tx fees. Take balances transfer for example, it should charge fee twice (after #1815 is merged), and I don't want it emit fees.Charged event twice. This fees module will sum all the charged fees and emit one fees.Charged event with total fees been charged.

@gavofyork
Copy link
Member

That's not going to fly: Chains that don't care about this (e.g. Polkadot) should not have to pay the extra storage i/o just because other chains decide they wants to amalgamate all this info.

If you have a callback based solution then your chain has the information is needs and can mutate it, store it and act upon it as desired, all without impacting on the performance of other chains that don't care.

@xlc xlc mentioned this pull request Mar 14, 2019
@gavofyork
Copy link
Member

superceded in #2048

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants