Skip to content

Commit

Permalink
Move all imported constants to our code base (#976)
Browse files Browse the repository at this point in the history
* Move SlowAdjustingFeeUpdate

Signed-off-by: Georgi Zlatarev <[email protected]>

* Move weights constants

Signed-off-by: Georgi Zlatarev <[email protected]>

* Fix SlowAdjustingFeeUpdate

Signed-off-by: Georgi Zlatarev <[email protected]>

* Add BlockExecutionWeight

Signed-off-by: Georgi Zlatarev <[email protected]>

* Add ExtrinsicBaseWeight

Signed-off-by: Georgi Zlatarev <[email protected]>

* FIx WEIGHT_PER_SEOCND

Signed-off-by: Georgi Zlatarev <[email protected]>

* Fix rocksdbweight

Signed-off-by: Georgi Zlatarev <[email protected]>

* CLean up

Signed-off-by: Georgi Zlatarev <[email protected]>

* Comments

Signed-off-by: Georgi Zlatarev <[email protected]>

* Add constants tests

Signed-off-by: Georgi Zlatarev <[email protected]>

* ExtrinsicBaseWeight

Signed-off-by: Georgi Zlatarev <[email protected]>

* Fix linters

Signed-off-by: Georgi Zlatarev <[email protected]>

Signed-off-by: Georgi Zlatarev <[email protected]>
  • Loading branch information
ghzlatarev authored Jan 16, 2023
1 parent a69c6e2 commit b0095a5
Show file tree
Hide file tree
Showing 13 changed files with 134 additions and 44 deletions.
3 changes: 2 additions & 1 deletion .github/resources/frame-weight-template.hbs
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,9 @@
#![allow(unused_imports)]
#![allow(clippy::unnecessary_cast)]

use frame_support::{traits::Get, weights::{Weight, constants::RocksDbWeight}};
use frame_support::{traits::Get, weights::Weight};
use sp_std::marker::PhantomData;
use manta_primitives::RocksDbWeight;

/// Weight functions needed for {{pallet}}.
pub trait WeightInfo {
Expand Down
54 changes: 53 additions & 1 deletion primitives/manta/src/constants.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@
//! Manta Protocol Constants
use crate::types::Balance;
use frame_support::PalletId;
use frame_support::{
parameter_types,
weights::{RuntimeDbWeight, Weight},
PalletId,
};

/// Calamari SS58 Prefix
pub const CALAMARI_SS58PREFIX: u8 = 78;
Expand Down Expand Up @@ -96,3 +100,51 @@ pub const MANTA_PAY_PALLET_ID: PalletId = PalletId(*b"mantapay");
///
/// This should only be used for testing and should not be used in production.
pub const TEST_DEFAULT_ASSET_ED: Balance = 1;

/// 1_000_000_000_000
pub const WEIGHT_PER_SECOND: Weight = 1_000_000_000_000;
/// 1_000_000_000
pub const WEIGHT_PER_MILLIS: Weight = WEIGHT_PER_SECOND / 1000;
/// 1_000_000
pub const WEIGHT_PER_MICROS: Weight = WEIGHT_PER_MILLIS / 1000;
/// 1_000
pub const WEIGHT_PER_NANOS: Weight = WEIGHT_PER_MICROS / 1000;

parameter_types! {
/// By default, Substrate uses RocksDB, so this will be the weight used throughout
/// the runtime.
pub const RocksDbWeight: RuntimeDbWeight = RuntimeDbWeight {
read: 25_000 * WEIGHT_PER_NANOS,
write: 100_000 * WEIGHT_PER_NANOS,
};
}

#[cfg(test)]
mod constants_tests {
use super::*;

#[test]
fn sanity_check_rocksdb_weight() {
use frame_support::weights::constants::RocksDbWeight as ImportedRocksDbWeight;
assert_eq!(ImportedRocksDbWeight::get().read, RocksDbWeight::get().read);
assert_eq!(
ImportedRocksDbWeight::get().write,
RocksDbWeight::get().write
);
}

#[test]
fn sanity_check_weight_per_time_constants() {
use frame_support::weights::constants::{
WEIGHT_PER_MICROS as IMPORTED_WEIGHT_PER_MICROS,
WEIGHT_PER_MILLIS as IMPORTED_WEIGHT_PER_MILLIS,
WEIGHT_PER_NANOS as IMPORTED_WEIGHT_PER_NANOS,
WEIGHT_PER_SECOND as IMPORTED_WEIGHT_PER_SECOND,
};

assert_eq!(WEIGHT_PER_SECOND, IMPORTED_WEIGHT_PER_SECOND);
assert_eq!(WEIGHT_PER_MILLIS, IMPORTED_WEIGHT_PER_MILLIS);
assert_eq!(WEIGHT_PER_MICROS, IMPORTED_WEIGHT_PER_MICROS);
assert_eq!(WEIGHT_PER_NANOS, IMPORTED_WEIGHT_PER_NANOS);
}
}
7 changes: 5 additions & 2 deletions primitives/manta/src/xcm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,10 @@

//! XCM primitives and implementations
use super::assets::{AssetConfig, FungibleLedger};
use super::{
assets::{AssetConfig, FungibleLedger},
constants::WEIGHT_PER_SECOND,
};

use sp_runtime::traits::{CheckedConversion, Convert, Zero};
use sp_std::marker::PhantomData;
Expand All @@ -25,7 +28,7 @@ use crate::assets::{AssetIdLocationMap, UnitsPerSecond};
use frame_support::{
pallet_prelude::Get,
traits::{fungibles::Mutate, tokens::ExistenceRequirement},
weights::{constants::WEIGHT_PER_SECOND, Weight},
weights::Weight,
};
use frame_system::Config;
use xcm::{
Expand Down
2 changes: 1 addition & 1 deletion runtime/calamari/src/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ mod multiplier_tests {

let base_fee = max_number_of_remarks_per_block
* <Runtime as pallet_transaction_payment::Config>::WeightToFee::weight_to_fee(
&frame_support::weights::constants::ExtrinsicBaseWeight::get(),
&runtime_common::ExtrinsicBaseWeight::get(),
);

run_with_system_weight(block_weight, || {
Expand Down
11 changes: 5 additions & 6 deletions runtime/calamari/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,23 +46,22 @@ use frame_support::{
ConstU128, ConstU16, ConstU32, ConstU8, Contains, Currency, EitherOfDiverse, IsInVec,
NeverEnsureOrigin, PrivilegeCmp,
},
weights::{
constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight, WEIGHT_PER_SECOND},
ConstantMultiplier, DispatchClass, Weight,
},
weights::{ConstantMultiplier, DispatchClass, Weight},
PalletId,
};
use frame_system::{
limits::{BlockLength, BlockWeights},
EnsureRoot,
};
use manta_primitives::{
constants::{time::*, STAKING_PALLET_ID, TREASURY_PALLET_ID},
constants::{time::*, RocksDbWeight, STAKING_PALLET_ID, TREASURY_PALLET_ID, WEIGHT_PER_SECOND},
types::{AccountId, Balance, BlockNumber, Hash, Header, Index, Signature},
};
pub use pallet_parachain_staking::{InflationInfo, Range};
use pallet_session::ShouldEndSession;
use runtime_common::{prod_or_fast, BlockHashCount, SlowAdjustingFeeUpdate};
use runtime_common::{
prod_or_fast, BlockExecutionWeight, BlockHashCount, ExtrinsicBaseWeight, SlowAdjustingFeeUpdate,
};
use session_key_primitives::{AuraId, NimbusId, VrfId};

#[cfg(any(feature = "std", test))]
Expand Down
11 changes: 0 additions & 11 deletions runtime/calamari/tests/integrations_mock/integration_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ use frame_support::{
codec::Encode,
dispatch::Dispatchable,
traits::{tokens::ExistenceRequirement, PalletInfo, StorageInfo, StorageInfoTrait},
weights::constants::*,
StorageHasher, Twox128,
};
use manta_primitives::{
Expand Down Expand Up @@ -697,16 +696,6 @@ fn collator_with_400k_not_selected_for_block_production() {
});
}

#[test]
fn sanity_check_weight_per_time_constants_are_as_expected() {
// These values comes from Substrate, we want to make sure that if it
// ever changes we don't accidentally break Polkadot
assert_eq!(WEIGHT_PER_SECOND, 1_000_000_000_000);
assert_eq!(WEIGHT_PER_MILLIS, WEIGHT_PER_SECOND / 1000);
assert_eq!(WEIGHT_PER_MICROS, WEIGHT_PER_MILLIS / 1000);
assert_eq!(WEIGHT_PER_NANOS, WEIGHT_PER_MICROS / 1000);
}

#[test]
fn calamari_vesting_works() {
ExtBuilder::default().build().execute_with(|| {
Expand Down
4 changes: 2 additions & 2 deletions runtime/calamari/tests/xcm_mock/parachain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ use frame_support::{
pallet_prelude::DispatchResult,
parameter_types,
traits::{ConstU32, Currency, Everything, Nothing},
weights::{constants::WEIGHT_PER_SECOND, Weight},
weights::Weight,
PalletId,
};
use frame_system::EnsureRoot;
Expand All @@ -42,7 +42,7 @@ use manta_primitives::{
AssetConfig, AssetIdLocationConvert, AssetIdType, AssetLocation, AssetRegistry,
AssetRegistryMetadata, AssetStorageMetadata, BalanceType, LocationType, NativeAndNonNative,
},
constants::{ASSET_MANAGER_PALLET_ID, CALAMARI_DECIMAL},
constants::{ASSET_MANAGER_PALLET_ID, CALAMARI_DECIMAL, WEIGHT_PER_SECOND},
types::{BlockNumber, CalamariAssetId, Header},
xcm::{FirstAssetTrader, IsNativeConcrete, MultiAssetAdapter, MultiNativeAsset},
};
Expand Down
5 changes: 2 additions & 3 deletions runtime/calamari/tests/xcm_mock/xcm_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,9 @@
use crate::xcm_mock::parachain::XcmFeesAccount;
use codec::Encode;
use frame_support::{
assert_err, assert_noop, assert_ok, traits::tokens::fungibles::Mutate,
weights::constants::WEIGHT_PER_SECOND, WeakBoundedVec,
assert_err, assert_noop, assert_ok, traits::tokens::fungibles::Mutate, WeakBoundedVec,
};
use manta_primitives::assets::AssetLocation;
use manta_primitives::{assets::AssetLocation, constants::WEIGHT_PER_SECOND};
use xcm::{latest::prelude::*, v2::Response, VersionedMultiLocation, WrapVersion};
use xcm_executor::traits::{Convert, WeightBounds};
use xcm_simulator::TestExt;
Expand Down
52 changes: 50 additions & 2 deletions runtime/common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@

pub mod migration;

use frame_support::parameter_types;
use manta_primitives::types::BlockNumber;
use frame_support::{parameter_types, weights::Weight};
use manta_primitives::{constants::WEIGHT_PER_NANOS, types::BlockNumber};
use pallet_transaction_payment::{Multiplier, TargetedFeeAdjustment};
use sp_runtime::{FixedPointNumber, Perquintill};

Expand Down Expand Up @@ -77,3 +77,51 @@ parameter_types! {
/// https://research.web3.foundation/en/latest/polkadot/overview/2-token-economics.html#-2.-slow-adjusting-mechanism
pub type SlowAdjustingFeeUpdate<R> =
TargetedFeeAdjustment<R, TargetBlockFullness, AdjustmentVariable, MinimumMultiplier>;

parameter_types! {
/// Time to execute an empty block.
/// Calculated by multiplying the *Average* with `1` and adding `0`.
///
/// Stats nanoseconds:
/// Min, Max: 5_303_128, 5_507_784
/// Average: 5_346_284
/// Median: 5_328_139
/// Std-Dev: 41749.5
///
/// Percentiles nanoseconds:
/// 99th: 5_489_273
/// 95th: 5_433_314
/// 75th: 5_354_812
pub const BlockExecutionWeight: Weight = 5_346_284 * WEIGHT_PER_NANOS;
}

parameter_types! {
/// Time to execute a NO-OP extrinsic, for example `System::remark`.
/// Calculated by multiplying the *Average* with `1` and adding `0`.
///
/// Stats nanoseconds:
/// Min, Max: 86_060, 86_999
/// Average: 86_298
/// Median: 86_248
/// Std-Dev: 207.19
///
/// Percentiles nanoseconds:
/// 99th: 86_924
/// 95th: 86_828
/// 75th: 86_347
pub const ExtrinsicBaseWeight: Weight = 86_298 * WEIGHT_PER_NANOS;
}

#[cfg(test)]
mod sanity_tests {
use super::*;
use frame_support::weights::constants::ExtrinsicBaseWeight as ImportedExtrinsicBaseWeight;

#[test]
fn sanity_check_extrinsic_base_weight() {
assert_eq!(
ExtrinsicBaseWeight::get(),
ImportedExtrinsicBaseWeight::get()
);
}
}
3 changes: 2 additions & 1 deletion runtime/common/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@
#![cfg_attr(not(feature = "std"), no_std)]

use manta_primitives::constants::RocksDbWeight;

use frame_support::{
dispatch::Weight,
migrations::migrate_from_pallet_version_to_storage_version,
traits::{GetStorageVersion, OnRuntimeUpgrade, PalletInfoAccess},
weights::constants::RocksDbWeight,
};
#[cfg(feature = "try-runtime")]
use frame_support::{ensure, traits::StorageVersion};
Expand Down
11 changes: 5 additions & 6 deletions runtime/dolphin/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,21 +45,20 @@ use frame_support::{
ConstU16, ConstU32, ConstU8, Contains, Currency, EitherOfDiverse, IsInVec,
NeverEnsureOrigin, PrivilegeCmp,
},
weights::{
constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight, WEIGHT_PER_SECOND},
ConstantMultiplier, DispatchClass, Weight,
},
weights::{ConstantMultiplier, DispatchClass, Weight},
PalletId,
};
use frame_system::{
limits::{BlockLength, BlockWeights},
EnsureRoot,
};
use manta_primitives::{
constants::{time::*, STAKING_PALLET_ID, TREASURY_PALLET_ID},
constants::{time::*, RocksDbWeight, STAKING_PALLET_ID, TREASURY_PALLET_ID, WEIGHT_PER_SECOND},
types::{AccountId, Balance, BlockNumber, Hash, Header, Index, Signature},
};
use runtime_common::{prod_or_fast, BlockHashCount, SlowAdjustingFeeUpdate};
use runtime_common::{
prod_or_fast, BlockExecutionWeight, BlockHashCount, ExtrinsicBaseWeight, SlowAdjustingFeeUpdate,
};
use session_key_primitives::{AuraId, NimbusId, VrfId};

#[cfg(any(feature = "std", test))]
Expand Down
4 changes: 2 additions & 2 deletions runtime/manta/src/fee.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@

use crate::currency;
use frame_support::weights::{
constants::ExtrinsicBaseWeight, WeightToFeeCoefficient, WeightToFeeCoefficients,
WeightToFeePolynomial,
WeightToFeeCoefficient, WeightToFeeCoefficients, WeightToFeePolynomial,
};
use manta_primitives::types::Balance;
use runtime_common::ExtrinsicBaseWeight;
use smallvec::smallvec;
pub use sp_runtime::Perbill;

Expand Down
11 changes: 5 additions & 6 deletions runtime/manta/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,23 +43,22 @@ use sp_version::RuntimeVersion;
use frame_support::{
construct_runtime, parameter_types,
traits::{ConstU128, ConstU16, ConstU32, ConstU8, Contains, Currency},
weights::{
constants::{BlockExecutionWeight, ExtrinsicBaseWeight, RocksDbWeight, WEIGHT_PER_SECOND},
ConstantMultiplier, DispatchClass, Weight,
},
weights::{ConstantMultiplier, DispatchClass, Weight},
PalletId,
};
use frame_system::{
limits::{BlockLength, BlockWeights},
EnsureRoot,
};
use manta_primitives::{
constants::{time::*, STAKING_PALLET_ID},
constants::{time::*, RocksDbWeight, STAKING_PALLET_ID, WEIGHT_PER_SECOND},
types::{AccountId, Balance, BlockNumber, Hash, Header, Index, Signature},
};
pub use pallet_parachain_staking::{InflationInfo, Range};
use pallet_session::ShouldEndSession;
use runtime_common::{prod_or_fast, BlockHashCount, SlowAdjustingFeeUpdate};
use runtime_common::{
prod_or_fast, BlockExecutionWeight, BlockHashCount, ExtrinsicBaseWeight, SlowAdjustingFeeUpdate,
};
use session_key_primitives::{AuraId, NimbusId, VrfId};

#[cfg(any(feature = "std", test))]
Expand Down

0 comments on commit b0095a5

Please sign in to comment.