Skip to content
This repository has been archived by the owner on May 21, 2024. It is now read-only.

Refactor WeightToFee to include ProofSize #215

Merged
merged 14 commits into from
Jun 15, 2023

Conversation

valentinfernandez1
Copy link
Contributor

@valentinfernandez1 valentinfernandez1 commented Jun 6, 2023

This PR introduces an update to the WeightToFee struct, addressing the changes discussed in issue #169. The update aligns the struct with the latest version included in release v0.9.40, which incorporates the following trait:

pub trait WeightToFee {
/// The type that is returned as result from calculation.
    type Balance: BaseArithmetic + From<u32> + Copy + Unsigned;

    /// Calculates the fee from the passed `weight`.
    fn weight_to_fee(weight: &Weight) -> Self::Balance;
}

The implementation of this functionality draws heavily from the implementation provided by statemine (now called asset-hub). However, the FeePolynomial struct and some Trait functions that are used in statemine implementation are not available for v0.9.40. To address this, a custom trait called WeightCoefficientCalc has been implemented, which facilitates the necessary calculations.

pub trait WeightCoefficientCalc<Balance> {
    fn saturating_eval(&self, result: Balance, x: Balance) -> Balance;
}

impl<Balance> WeightCoefficientCalc<Balance> for WeightToFeeCoefficient<Balance>
where
    Balance: BaseArithmetic + From<u32> + Copy + Unsigned + SaturatedConversion,
{
    fn saturating_eval(&self, mut result: Balance, x: Balance) -> Balance {
	let power = x.saturating_pow(self.degree.into());

	let frac = self.coeff_frac * power; // Overflow safe.
	let integer = self.coeff_integer.saturating_mul(power);
	// Do not add them together here to avoid an underflow.

	if self.negative {
		result = result.saturating_sub(frac);
		result = result.saturating_sub(integer);
	} else {
		result = result.saturating_add(frac);
		result = result.saturating_add(integer);
	}

	result
    }
}

This implementation can be easily switched to use FeePolynomial after upgrading the substrate version to v0.9.42 or above.


Note: I plan to include additional tests to ensure that this works as intended specially on write heavy extrinsics, but feel free to leave your feedback on this change in the mean time.

closes #168
closes #169

@valentinfernandez1 valentinfernandez1 added the enhancement New feature or request label Jun 6, 2023
@valentinfernandez1 valentinfernandez1 changed the title [WIP] WeightToFee refactor to include ProofSize [WIP] Refactor WeightToFee to include ProofSize Jun 6, 2023
@valentinfernandez1 valentinfernandez1 marked this pull request as ready for review June 12, 2023 14:13
@valentinfernandez1 valentinfernandez1 changed the title [WIP] Refactor WeightToFee to include ProofSize Refactor WeightToFee to include ProofSize Jun 12, 2023
runtime/stout/src/constants.rs Outdated Show resolved Hide resolved
runtime/stout/src/xcm_config.rs Show resolved Hide resolved
runtime/stout/src/xcm_config.rs Show resolved Hide resolved
Copy link
Contributor

@kalaninja kalaninja left a comment

Choose a reason for hiding this comment

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

I've noticed a comment referencing kusama and took a closer look. Is it ok that we are using constants from kusama_runtime_constants? I think trappist should be referencing those of polkadot.

runtime/trappist/src/constants.rs Outdated Show resolved Hide resolved
runtime/trappist/src/constants.rs Outdated Show resolved Hide resolved
fn saturating_eval(&self, mut result: Balance, x: Balance) -> Balance {
let power = x.saturating_pow(self.degree.into());

let frac = self.coeff_frac * power; // Overflow safe.
Copy link
Contributor

Choose a reason for hiding this comment

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

I think you can be more specific here and say that it's "Overflow safe" because coeff_frac is strictly less than 1.

@valentinfernandez1
Copy link
Contributor Author

I've noticed a comment referencing kusama and took a closer look. Is it ok that we are using constants from kusama_runtime_constants? I think trappist should be referencing those of polkadot.

This should be discussed since it's also part of trappist v0.9.37 I don't know the reason why it was added initially maybe @stiiifff can clarify the reasoning behind it.

@@ -220,11 +220,13 @@ parameter_types! {
// Rockmine's Assets pallet index
pub RockmineAssetsPalletLocation: MultiLocation =
MultiLocation::new(1, X2(Parachain(1000), PalletInstance(50)));
pub XUsdPerSecond: (xcm::v3::AssetId, u128, u128) = (
MultiLocation::new(1, X3(Parachain(1000), PalletInstance(50), GeneralIndex(1))).into(),
pub RUsdPerSecond: (xcm::v3::AssetId, u128, u128) = (
Copy link
Contributor

Choose a reason for hiding this comment

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

nitpick: changes to this files probably shouldn't be part of this PR.

@stiiifff
Copy link
Contributor

I've noticed a comment referencing kusama and took a closer look. Is it ok that we are using constants from kusama_runtime_constants? I think trappist should be referencing those of polkadot.

This should be discussed since it's also part of trappist v0.9.37 I don't know the reason why it was added initially maybe @stiiifff can clarify the reasoning behind it.

@valentinfernandez1 @kalaninja The intention there was/is to use the same numbers as Kusama for things like existential deposit, deposit fees, and currency-related constants (i.e. the chain's native token, HOP, is mirroring the KSM token for decimals, total emission).

@stiiifff
Copy link
Contributor

Looks good, @valentinfernandez1 let's get the build fixed, and then LGTM.

@valentinfernandez1 valentinfernandez1 merged commit 85b9903 into xcmv3-dev Jun 15, 2023
@valentinfernandez1 valentinfernandez1 deleted the vf-weight-to-fee branch June 15, 2023 15:39
@valentinfernandez1 valentinfernandez1 mentioned this pull request Jun 20, 2023
7 tasks
hbulgarini added a commit that referenced this pull request Jul 31, 2023
* Upgrade to v0.9.40 (#171)

* Upgraded to 9-40 missing some fixes

* Added XCM v3 Traits (Need to configure)

* Added fee per MB

* Added XCM V3 Configs

* Fix XCM_config and node service

* Removed chess pallet

* pallet_contracts and pallet_xcm fix

* rust fmt

* Quick fix to contracts on stout and pallet-dex dependancie

* Added hardware check to service

* Fixed stout service

* Review fixes

* run tests on CI (#174)
(cherry picked from commit d826938)

* fmt

* review fix

* review fix

* fmt

* Fixed xcm-playground error

---------

Co-authored-by: Valentin Fernandez <[email protected]>
Co-authored-by: Alexander Kalankhodzhaev <[email protected]>

* Backport Drop Assets (#189)

Backport PR #165 (cherry picked from commit 3f1c483) in Trappist v0.9.40 and updates it to match the changes included in polkadot v0.9.38 to v0.9.40. Weights were re benchmarked to match weightsV2.
---------

Co-authored-by: Valentin Fernandez <[email protected]>

* asset-registry backport (#193)

Backport PR #173 (cherry picked from commit 24552f3) in Trappist v0.9.40 and updates it to match the changes included in polkadot v0.9.38 to v0.9.40. Weights were re benchmarked to match weightsV2.

Co-authored-by: Valentin Fernandez <[email protected]>

* fix CI for substrate version update (#198)

* Backport Zombienet and CI testing (#194)

* zombienet refactor and ci fix

* swap-storage removed from CI

* ci fix

* CI fix

---------

Co-authored-by: Valentin Fernandez <[email protected]>

* Backport Lockdown-pallet  (#191)

* lockdown-pallet backport

* Add lockdown mode activation to lockdown-mode pallet's `GenesisConfig` (#176)

* add activating lockdown mode to GenesisConfig

* add activating lockdown mode to test ext

* add tests for activating lockdown mode in GenesisConfig

* cargo fmt -p pallet-lockdown-mode

* fix linting

* add activated to benchmark

* removing, as this is already in another test

* setting initial state to true, therefore no longer need to manually change state

* rename activated in genesisConfig to initial_status

---------

Co-authored-by: Valentin Fernandez <[email protected]>
Co-authored-by: Bruno Galvao <[email protected]>

* [Backport] #185  XCM benchmarks from `pallet_xcm_benchmarks` to v0.9.40 (#201)

* backport xcm_benchmarks

* Co-Authored-By: hbulgarini <[email protected]>

* fix

* add profile "production" (#187)

---------

Co-authored-by: Alexander Kalankhodzhaev <[email protected]>

* [Backport] #206 Trappist node and #207 workspace refactor to v0.9.40 (#209)

* cherry-picked changes

* WIP fixing node service¨

* Upgrade fixes, missing stout trait bound error

* workspace refactor

* added dex to stout

* fmt

---------

Co-authored-by: Hector Bulgarini <[email protected]>

* Query account balances using `accounts_common` (#202)

* query_account_balances added

* fmt

* Added assets-common dependency from Cumulus

---------

Co-authored-by: Steve Degosserie <[email protected]>

* [Backport] #220 Apache License and #223 Minor Refactoring (#228)

* backport apache license

* backport minor refactoring PR #223

* fmt

* Refactor `WeightToFee` to include ProofSize (#215)

* Removed FixedRateOfFungibles

* fmt

* added comments

* fmt

* removed unused imports

* fmt

* removed allow unpaid execution from rockmine following PR #211

* Include new traders from PR #221

* minor fixes

* minor fixes

* fmt

* [Backport] Asset-registry fix,  Bump Docker and add preimage to benchmarks (#229)

* Validate `MultiLocation` for asset-registry  (#224)

* Validate location for register_reserve_asset

* fmt

* fmt

* fmt

* validator refactor

* fmt

* fix `pallet_xcm_benchmarks::generic::claim_asset` benchmark (#226)

* Bump docker/login-action from 2.1.0 to 2.2.0 (#219)

Bumps [docker/login-action](https://github.com/docker/login-action) from 2.1.0 to 2.2.0.
- [Release notes](https://github.com/docker/login-action/releases)
- [Commits](docker/login-action@f4ef78c...465a078)

---
updated-dependencies:
- dependency-name: docker/login-action
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump docker/metadata-action from 4.4.0 to 4.5.0 (#218)

Bumps [docker/metadata-action](https://github.com/docker/metadata-action) from 4.4.0 to 4.5.0.
- [Release notes](https://github.com/docker/metadata-action/releases)
- [Commits](docker/metadata-action@c4ee3ad...2c0bd77)

---
updated-dependencies:
- dependency-name: docker/metadata-action
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* fix network id in tests

* fix network id in tests

* Add missing import

* include preimage to benchmarks

* Bump docker/metadata-action from 4.5.0 to 4.6.0 (#233)

Bumps [docker/metadata-action](https://github.com/docker/metadata-action) from 4.5.0 to 4.6.0.
- [Release notes](https://github.com/docker/metadata-action/releases)
- [Commits](docker/metadata-action@2c0bd77...818d4b7)

---
updated-dependencies:
- dependency-name: docker/metadata-action
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* Bump docker/build-push-action from 4.0.0 to 4.1.1 (#232)

Bumps [docker/build-push-action](https://github.com/docker/build-push-action) from 4.0.0 to 4.1.1.
- [Release notes](https://github.com/docker/build-push-action/releases)
- [Commits](docker/build-push-action@3b5e802...2eb1c19)

---
updated-dependencies:
- dependency-name: docker/build-push-action
  dependency-type: direct:production
  update-type: version-update:semver-minor
...

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: Alexander Kalankhodzhaev <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>

* WIP

* WeightToFee updated to use FeePolynomial. Based on the implementation in the asset-hub

* finish upgrade

* update deps

* Addressing check account

* Fix

* fmt

---------

Signed-off-by: dependabot[bot] <[email protected]>
Co-authored-by: valentinfernandez1 <[email protected]>
Co-authored-by: Valentin Fernandez <[email protected]>
Co-authored-by: Alexander Kalankhodzhaev <[email protected]>
Co-authored-by: Bruno Galvao <[email protected]>
Co-authored-by: Steve Degosserie <[email protected]>
Co-authored-by: dependabot[bot] <49699333+dependabot[bot]@users.noreply.github.com>
Co-authored-by: Valentin Fernandez <[email protected]>
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement New feature or request
Projects
None yet
4 participants