Skip to content
This repository has been archived by the owner on Sep 22, 2022. It is now read-only.

Free calls pallet #181

Closed
wants to merge 79 commits into from
Closed

Conversation

TarekkMA
Copy link

Rename branch from #177

@F3Joule F3Joule requested review from F3Joule and siman and removed request for F3Joule January 19, 2022 09:00
runtime/Cargo.toml Outdated Show resolved Hide resolved
runtime/src/lib.rs Outdated Show resolved Hide resolved
@TarekkMA TarekkMA force-pushed the tarekkma/free-calls branch from 816a306 to d236787 Compare January 25, 2022 21:39
Comment on lines +190 to +194
(
total_weight,
boxed_call_info.class,
Pays::No,
)

Choose a reason for hiding this comment

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

please put a note that you have a signed extension doing a pre-validation check here

Comment on lines 207 to 210
actual_weight = actual_weight.saturating_add(call.get_dispatch_info().weight);

// Dispatch the call
let result = call.dispatch(origin);

Choose a reason for hiding this comment

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

this doesn't make sense to me. You are just doing the same math here as you did above. you will get the same results.

what you want to do is actually get the result from let result = call.dispatch(origin); and from that get the real weight, and use that for the math

Choose a reason for hiding this comment

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

Copy link
Author

Choose a reason for hiding this comment

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

Right, that makes more sense. Thank you.

Comment on lines 283 to 287
/// Determine if `consumer` can have a free call and optionally update its window usage.
///
/// Window usage for the caller `consumer` will only update if there is a quota and all of the
/// previous window usages doesn't exceed the defined windows config.
pub fn can_make_free_call(consumer: &T::AccountId, should_update_consumer_stats: ShouldUpdateConsumerStats) -> bool {

Choose a reason for hiding this comment

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

i generally do not like combining a check function with also a state modification function, but that is just up to you

Copy link
Author

Choose a reason for hiding this comment

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

Yeah I planned to refactor this, which will make it easier for tests, and also it will be less complex.

_ => return false,
};

let mut windows: Vec<Window<T>> = Vec::new();

Choose a reason for hiding this comment

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

you know the size of this vec will be at most WindowType or something, so you should create this vec with the known bound so it doesnt reallocate

Choose a reason for hiding this comment

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

Ideally you use bounded vecs everywhere for this kind of thing

Copy link
Author

@TarekkMA TarekkMA Feb 7, 2022

Choose a reason for hiding this comment

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

Now all vecs are bounded to the size of the passed in config.

    /// A `BoundedVec` that can hold a list of `ConsumerStats` objects bounded by the size of WindowConfigs.
    pub type ConsumerStatsVec<T> = BoundedVec<ConsumerStats<<T as frame_system::Config>::BlockNumber>, WindowsConfigSize<T>>;


    /// Retrieves the size of `T::WindowsConfig` to be used for `BoundedVec` declaration.
    pub struct WindowsConfigSize<T: Config>(PhantomData<T>);

    impl<T: Config> Default for WindowsConfigSize<T> {
        fn default() -> Self {
            Self(PhantomData)
        }
    }

    impl<T: Config> Get<u32> for WindowsConfigSize<T> {
        fn get() -> u32 {
            T::WindowsConfig::get().len().try_into().unwrap()
        }
    }

runtime/src/lib.rs Outdated Show resolved Hide resolved
Comment on lines 166 to 168
WindowType,
ConsumerStats<T::BlockNumber>,
>;

Choose a reason for hiding this comment

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

using a double map here does not seem as efficient as a bounded vec.

from what i read, you are updating all the windows each time a tx is going though, so instead of modifying one storage item, you are modifying N storage items.

@TarekkMA TarekkMA force-pushed the tarekkma/free-calls branch from 23a2c6e to 955c734 Compare February 9, 2022 21:02
@TarekkMA TarekkMA force-pushed the tarekkma/free-calls branch from 955c734 to 9979464 Compare February 9, 2022 21:13
@TarekkMA
Copy link
Author

This PR is obsolete, it's now split into 4 PRs

@TarekkMA TarekkMA closed this Feb 15, 2022
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.

4 participants