Skip to content

Commit

Permalink
Second attempt at cleaning up OldWeights
Browse files Browse the repository at this point in the history
  • Loading branch information
notlesh committed Sep 29, 2022
1 parent e43f9f2 commit 18b3ebd
Show file tree
Hide file tree
Showing 21 changed files with 82 additions and 84 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 21 additions & 21 deletions pallets/xcm-transactor/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,13 +83,12 @@ pub mod migrations;
pub mod weights;

type CurrencyIdOf<T> = <T as Config>::CurrencyId;
pub type OldWeight = u64;

#[pallet]
pub mod pallet {

use crate::weights::WeightInfo;
use crate::{CurrencyIdOf, OldWeight};
use crate::CurrencyIdOf;
use frame_support::{pallet_prelude::*, weights::constants::WEIGHT_PER_SECOND};
use frame_system::{ensure_signed, pallet_prelude::*};
use orml_traits::location::{Parse, Reserve};
Expand All @@ -100,6 +99,7 @@ pub mod pallet {
use xcm::{latest::prelude::*, VersionedMultiLocation};
use xcm_executor::traits::{InvertLocation, TransactAsset, WeightBounds};
use xcm_primitives::{UtilityAvailableCalls, UtilityEncodeCall, XcmTransact};
pub(crate) use xcm_primitives::XcmV2Weight;

#[pallet::pallet]
#[pallet::without_storage_info]
Expand Down Expand Up @@ -158,7 +158,7 @@ pub mod pallet {
/// The actual weight for an XCM message is `T::BaseXcmWeight +
/// T::Weigher::weight(&msg)`.
#[pallet::constant]
type BaseXcmWeight: Get<OldWeight>;
type BaseXcmWeight: Get<XcmV2Weight>;

/// The way to retrieve the reserve of a MultiAsset. This can be
/// configured to accept absolute or relative paths for self tokens
Expand All @@ -175,15 +175,15 @@ pub mod pallet {
/// Extra weight involved when transacting without DescendOrigin
/// This should always be possible in a destination chain, since
/// it involves going through the sovereign account
pub transact_extra_weight: OldWeight,
pub transact_extra_weight: XcmV2Weight,
/// Max destination weight
pub max_weight: OldWeight,
pub max_weight: XcmV2Weight,
/// Whether we allow transacting through signed origins in another chain, and
/// how much extra cost implies
/// Extra weight involved when transacting with DescendOrigin
/// The reason for it being an option is because the destination chain
/// might not support constructing origins based on generic MultiLocations
pub transact_extra_weight_signed: Option<OldWeight>,
pub transact_extra_weight_signed: Option<XcmV2Weight>,
}

/// Enum defining the way to express a Currency.
Expand Down Expand Up @@ -237,11 +237,11 @@ pub mod pallet {
/// If None, then this amount will be tried to be derived from storage. If the storage item
pub struct TransactWeights {
// the amount of weight the Transact instruction should consume at most
pub transact_required_weight_at_most: OldWeight,
pub transact_required_weight_at_most: XcmV2Weight,
// the overall weight to be used for the whole XCM message execution. If None,
// then this amount will be tried to be derived from storage. If the storage item
// for the chain is not populated, then it fails
pub overall_weight: Option<OldWeight>,
pub overall_weight: Option<XcmV2Weight>,
}

/// Since we are using pallet-utility for account derivation (through AsDerivative),
Expand Down Expand Up @@ -511,9 +511,9 @@ pub mod pallet {
pub fn set_transact_info(
origin: OriginFor<T>,
location: Box<VersionedMultiLocation>,
transact_extra_weight: OldWeight,
transact_extra_weight: XcmV2Weight,
max_weight: u64,
transact_extra_weight_signed: Option<OldWeight>,
transact_extra_weight_signed: Option<XcmV2Weight>,
) -> DispatchResult {
T::DerivativeAddressRegistrationOrigin::ensure_origin(origin)?;
let location =
Expand Down Expand Up @@ -751,7 +751,7 @@ pub mod pallet {
fee_location: MultiLocation,
fee_amount: Option<u128>,
destination: MultiLocation,
total_weight: OldWeight,
total_weight: XcmV2Weight,
) -> Result<MultiAsset, DispatchError> {
// If amount is provided, just use it
// Else, multiply weight*destination_units_per_second to see how much we should charge for
Expand All @@ -778,9 +778,9 @@ pub mod pallet {
fn transact_message(
dest: MultiLocation,
asset: MultiAsset,
dest_weight: OldWeight,
dest_weight: XcmV2Weight,
call: Vec<u8>,
dispatch_weight: OldWeight,
dispatch_weight: XcmV2Weight,
origin_kind: OriginKind,
) -> Result<Xcm<()>, DispatchError> {
Ok(Xcm(vec![
Expand Down Expand Up @@ -856,7 +856,7 @@ pub mod pallet {
}

/// Returns weight of `weight_of_initiate_reserve_withdraw` call.
fn weight_of_initiate_reserve_withdraw() -> OldWeight {
fn weight_of_initiate_reserve_withdraw() -> XcmV2Weight {
let dest = MultiLocation::parent();

// We can use whatever asset here
Expand All @@ -876,14 +876,14 @@ pub mod pallet {
xcm: Xcm(vec![]),
},
]);
T::Weigher::weight(&mut xcm.into()).map_or(OldWeight::max_value(), |w| {
T::Weigher::weight(&mut xcm.into()).map_or(XcmV2Weight::max_value(), |w| {
T::BaseXcmWeight::get().saturating_add(w)
})
}

/// Returns the fee for a given set of parameters
/// We always round up in case of fractional division
pub fn calculate_fee_per_second(weight: OldWeight, fee_per_second: u128) -> u128 {
pub fn calculate_fee_per_second(weight: XcmV2Weight, fee_per_second: u128) -> u128 {
// grab WEIGHT_PER_SECOND as u128
let weight_per_second_u128 = WEIGHT_PER_SECOND.ref_time() as u128;

Expand All @@ -899,8 +899,8 @@ pub mod pallet {
/// it returns the weight to be used in non-signed cases
pub fn take_weight_from_transact_info(
dest: MultiLocation,
dest_weight: OldWeight,
) -> Result<OldWeight, DispatchError> {
dest_weight: XcmV2Weight,
) -> Result<XcmV2Weight, DispatchError> {
// Grab transact info for the destination provided
let transactor_info = TransactInfoWithWeightLimit::<T>::get(&dest)
.ok_or(Error::<T>::TransactorInfoNotSet)?;
Expand All @@ -920,8 +920,8 @@ pub mod pallet {
/// it returns the weight to be used in signed cases
pub fn take_weight_from_transact_info_signed(
dest: MultiLocation,
dest_weight: OldWeight,
) -> Result<OldWeight, DispatchError> {
dest_weight: XcmV2Weight,
) -> Result<XcmV2Weight, DispatchError> {
// Grab transact info for the destination provided
let transactor_info = TransactInfoWithWeightLimit::<T>::get(&dest)
.ok_or(Error::<T>::TransactorInfoNotSet)?;
Expand All @@ -948,7 +948,7 @@ pub mod pallet {
pub fn take_fee_per_second_from_storage(
fee_location: MultiLocation,
destination: MultiLocation,
total_weight: OldWeight,
total_weight: XcmV2Weight,
) -> Result<u128, DispatchError> {
let fee_per_second = DestinationAssetFeePerSecond::<T>::get(&fee_location)
.ok_or(Error::<T>::FeePerSecondNotSet)?;
Expand Down
6 changes: 3 additions & 3 deletions pallets/xcm-transactor/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
// along with Moonbeam. If not, see <http://www.gnu.org/licenses/>.

use crate::{
Config, DestinationAssetFeePerSecond, OldWeight, RemoteTransactInfoWithMaxWeight,
Config, DestinationAssetFeePerSecond, XcmV2Weight, RemoteTransactInfoWithMaxWeight,
TransactInfoWithWeightLimit,
};
use frame_support::{
Expand All @@ -41,7 +41,7 @@ pub struct OldRemoteTransactInfo {
/// Size of the tx metadata of a transaction in the destination chain
pub metadata_size: u64,
/// Minimum weight the destination chain charges for a transaction
pub base_weight: OldWeight,
pub base_weight: XcmV2Weight,
/// Fee per weight in the destination chain
pub fee_per_weight: u128,
}
Expand All @@ -50,7 +50,7 @@ pub struct OldRemoteTransactInfo {
#[derive(Default, Clone, Encode, Decode, RuntimeDebug, PartialEq, scale_info::TypeInfo)]
pub struct OldRemoteTransactInfoWithFeePerSecond {
/// Extra weight that transacting a call in a destination chain adds
pub transact_extra_weight: OldWeight,
pub transact_extra_weight: XcmV2Weight,
/// Fee per second
pub fee_per_second: u128,
/// MaxWeight
Expand Down
10 changes: 5 additions & 5 deletions pallets/xcm-transactor/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ use crate as pallet_xcm_transactor;
use frame_support::traits::PalletInfo as PalletInfoTrait;
use frame_support::{construct_runtime, parameter_types, weights::Weight};
use frame_system::EnsureRoot;
use pallet_xcm_transactor::OldWeight;
use parity_scale_codec::{Decode, Encode};

use sp_core::{H160, H256};
Expand All @@ -33,6 +32,7 @@ use xcm::latest::{
Junctions, MultiAsset, MultiLocation, NetworkId, Result as XcmResult, SendResult, SendXcm, Xcm,
};
use xcm_primitives::{UtilityAvailableCalls, UtilityEncodeCall, XcmTransact};
pub use xcm_primitives::XcmV2Weight;

use sp_std::cell::RefCell;
use xcm_executor::{
Expand Down Expand Up @@ -143,7 +143,7 @@ impl WeightTrader for DummyWeightTrader {
DummyWeightTrader
}

fn buy_weight(&mut self, _weight: OldWeight, _payment: Assets) -> Result<Assets, XcmError> {
fn buy_weight(&mut self, _weight: XcmV2Weight, _payment: Assets) -> Result<Assets, XcmError> {
Ok(Assets::default())
}
}
Expand All @@ -162,10 +162,10 @@ use sp_std::marker::PhantomData;
pub struct DummyWeigher<C>(PhantomData<C>);

impl<C: Decode> WeightBounds<C> for DummyWeigher<C> {
fn weight(_message: &mut Xcm<C>) -> Result<OldWeight, ()> {
fn weight(_message: &mut Xcm<C>) -> Result<XcmV2Weight, ()> {
Ok(0)
}
fn instr_weight(_instruction: &Instruction<C>) -> Result<OldWeight, ()> {
fn instr_weight(_instruction: &Instruction<C>) -> Result<XcmV2Weight, ()> {
Ok(0)
}
}
Expand All @@ -187,7 +187,7 @@ impl sp_runtime::traits::Convert<u64, MultiLocation> for AccountIdToMultiLocatio
parameter_types! {
pub Ancestry: MultiLocation = Parachain(ParachainId::get().into()).into();

pub const BaseXcmWeight: OldWeight = 1000;
pub const BaseXcmWeight: XcmV2Weight = 1000;
pub const RelayNetwork: NetworkId = NetworkId::Polkadot;

pub SelfLocation: MultiLocation = (1, Junctions::X1(Parachain(ParachainId::get().into()))).into();
Expand Down
7 changes: 3 additions & 4 deletions precompiles/xcm-transactor/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ use pallet_evm::{
AddressMapping, EnsureAddressNever, EnsureAddressRoot, GasWeightMapping, Precompile,
PrecompileSet,
};
use pallet_xcm_transactor::OldWeight;
use precompile_utils::prelude::*;
use scale_info::TypeInfo;
use serde::{Deserialize, Serialize};
Expand All @@ -46,7 +45,7 @@ use xcm_executor::{
traits::{InvertLocation, TransactAsset, WeightTrader},
Assets,
};
use xcm_primitives::AccountIdToCurrencyId;
use xcm_primitives::{AccountIdToCurrencyId, XcmV2Weight};

pub type AccountId = TestAccount;
pub type Balance = u128;
Expand Down Expand Up @@ -343,7 +342,7 @@ impl WeightTrader for DummyWeightTrader {
DummyWeightTrader
}

fn buy_weight(&mut self, _weight: OldWeight, _payment: Assets) -> Result<Assets, XcmError> {
fn buy_weight(&mut self, _weight: XcmV2Weight, _payment: Assets) -> Result<Assets, XcmError> {
Ok(Assets::default())
}
}
Expand All @@ -368,7 +367,7 @@ pub enum CurrencyId {
parameter_types! {
pub Ancestry: MultiLocation = Parachain(ParachainId::get().into()).into();

pub const BaseXcmWeight: OldWeight = 1000;
pub const BaseXcmWeight: XcmV2Weight = 1000;
pub const RelayNetwork: NetworkId = NetworkId::Polkadot;

pub SelfLocation: MultiLocation = (1, Junctions::X1(Parachain(ParachainId::get().into()))).into();
Expand Down
2 changes: 2 additions & 0 deletions precompiles/xcm-utils/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ num_enum = { version = "0.5.3", default-features = false }

# Moonbeam
precompile-utils = { path = "../utils", default-features = false }
xcm-primitives = { path = "../../primitives/xcm", default-features = false }

# Substrate
frame-support = { git = "https://github.com/purestake/substrate", branch = "moonbeam-polkadot-v0.9.29", default-features = false }
Expand Down Expand Up @@ -58,6 +59,7 @@ std = [
"frame-system/std",
"pallet-evm/std",
"precompile-utils/std",
"xcm-primitives/std",
"sp-core/std",
"sp-std/std",
"xcm-executor/std",
Expand Down
6 changes: 3 additions & 3 deletions precompiles/xcm-utils/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ use pallet_evm::{
AddressMapping, EnsureAddressNever, EnsureAddressRoot, GasWeightMapping, Precompile,
PrecompileOutput, PrecompileSet,
};
use pallet_xcm_transactor::OldWeight;
use scale_info::TypeInfo;
use serde::{Deserialize, Serialize};
use sp_core::{H256, U256};
Expand Down Expand Up @@ -389,7 +388,7 @@ impl WeightTrader for DummyWeightTrader {
DummyWeightTrader
}

fn buy_weight(&mut self, _weight: OldWeight, _payment: Assets) -> Result<Assets, XcmError> {
fn buy_weight(&mut self, _weight: XcmV2Weight, _payment: Assets) -> Result<Assets, XcmError> {
Ok(Assets::default())
}
}
Expand All @@ -408,7 +407,7 @@ impl InvertLocation for InvertNothing {
parameter_types! {
pub Ancestry: MultiLocation = Parachain(ParachainId::get().into()).into();

pub const BaseXcmWeight: OldWeight = 1000;
pub const BaseXcmWeight: XcmV2Weight = 1000;
pub const RelayNetwork: NetworkId = NetworkId::Polkadot;

pub SelfLocation: MultiLocation = (1, Junctions::X1(Parachain(ParachainId::get().into()))).into();
Expand All @@ -423,6 +422,7 @@ parameter_types! {
}

use xcm_builder::{ParentIsPreset, SiblingParachainConvertsVia};
use xcm_primitives::XcmV2Weight;

pub type XcmOriginToTransactDispatchOrigin = (
// Sovereign account converter; this attempts to derive an `AccountId` from the origin location
Expand Down
11 changes: 4 additions & 7 deletions precompiles/xtokens/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,10 @@
use super::*;
use codec::{Decode, Encode, MaxEncodedLen};
use fp_evm::Precompile;
use frame_support::traits::{EnsureOrigin, Everything, OriginTrait, PalletInfo as PalletInfoTrait};
use frame_support::{construct_runtime, parameter_types};
use frame_support::{
traits::{EnsureOrigin, Everything, OriginTrait, PalletInfo as PalletInfoTrait},
weights::Weight,
};
use orml_traits::{location::AbsoluteReserveProvider, parameter_type_with_key};
use pallet_evm::{AddressMapping, EnsureAddressNever, EnsureAddressRoot, PrecompileSet};
use pallet_xcm_transactor::OldWeight;
use serde::{Deserialize, Serialize};
use sp_core::H256;
use sp_io;
Expand All @@ -44,6 +40,7 @@ use xcm_executor::{
traits::{InvertLocation, TransactAsset, WeightTrader},
Assets, XcmExecutor,
};
use xcm_primitives::XcmV2Weight;

pub type AccountId = TestAccount;
pub type Balance = u128;
Expand Down Expand Up @@ -307,7 +304,7 @@ impl WeightTrader for DummyWeightTrader {
DummyWeightTrader
}

fn buy_weight(&mut self, _weight: OldWeight, _payment: Assets) -> Result<Assets, XcmError> {
fn buy_weight(&mut self, _weight: XcmV2Weight, _payment: Assets) -> Result<Assets, XcmError> {
Ok(Assets::default())
}
}
Expand Down Expand Up @@ -431,7 +428,7 @@ impl sp_runtime::traits::Convert<TestAccount, MultiLocation> for AccountIdToMult
parameter_types! {
pub Ancestry: MultiLocation = Parachain(ParachainId::get().into()).into();

pub const BaseXcmWeight: OldWeight = 1000;
pub const BaseXcmWeight: XcmV2Weight = 1000;
pub const RelayNetwork: NetworkId = NetworkId::Polkadot;
pub const MaxAssetsForTransfer: usize = 2;

Expand Down
2 changes: 2 additions & 0 deletions primitives/xcm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,5 @@ pub use transactor_traits::*;

mod ethereum_xcm;
pub use ethereum_xcm::*;

pub type XcmV2Weight = xcm::v2::Weight;
Loading

0 comments on commit 18b3ebd

Please sign in to comment.