Skip to content

Commit

Permalink
Merge branch 'open-web3-stack:master' into master
Browse files Browse the repository at this point in the history
  • Loading branch information
hqwangningbo authored Mar 8, 2023
2 parents 2acce9f + 6df51b3 commit ae28667
Show file tree
Hide file tree
Showing 90 changed files with 1,726 additions and 705 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/coverage.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
- name: Install toolchain
uses: dtolnay/rust-toolchain@nightly
with:
toolchain: nightly-2022-08-05
toolchain: nightly-2022-10-30
components: rustfmt
target: wasm32-unknown-unknown
- name: Generate code coverage
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/master.yml.disabled
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
- name: Install toolchain
uses: dtolnay/rust-toolchain@nightly
with:
toolchain: nightly-2022-08-05
toolchain: nightly-2022-10-30
- name: Install cargo-unleash
run: cargo install cargo-unleash --git https://github.com/xlc/cargo-unleash.git # https://github.com/paritytech/cargo-unleash/pull/38
- name: Prepare
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ jobs:
- name: Install toolchain
uses: dtolnay/rust-toolchain@nightly
with:
toolchain: nightly-2022-08-05
toolchain: nightly-2022-10-30
components: rustfmt
target: wasm32-unknown-unknown
- name: Install Wasm toolchain
Expand Down
133 changes: 69 additions & 64 deletions Cargo.dev.toml

Large diffs are not rendered by default.

4 changes: 3 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,11 @@ dev-check: Cargo.toml check

dev-check-tests: Cargo.toml
cargo check --tests --all
cargo check --tests --features=bench --package=orml-weight-meter --package=orml-bencher-test

dev-test: Cargo.toml
cargo test --all --features runtime-benchmarks
cargo test --all --features=runtime-benchmarks
cargo test --features=bench --package=orml-weight-meter --package=orml-bencher-test

# run benchmarks via Acala node
benchmark-all:
Expand Down
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ ORML is part of the bigger `Open-Web3-Stack` initiative, that is currently under
_In alphabetical order_

- [Acala Network](https://github.com/AcalaNetwork/Acala)
- [Ajuna Network](https://github.com/ajuna-network/Ajuna)
- [Astar Network](https://github.com/AstarNetwork)
- [Bifrost Finance](https://github.com/bifrost-finance/bifrost)
- [Bit.Country](https://github.com/bit-country/Bit-Country-Blockchain)
Expand All @@ -102,8 +103,11 @@ _In alphabetical order_
- [Libra](https://github.com/atscaletech/libra)
- [Listen](https://github.com/listenofficial)
- [Manta Network](https://github.com/Manta-Network)
- [Mangata Finance](https://github.com/mangata-finance)
- [Minterest](https://github.com/minterest-finance/minterest-chain-node)
- [Moonbeam](https://github.com/PureStake/moonbeam/)
- [OAK](https://github.com/OAK-Foundation/OAK-blockchain)
- [Parallel Finance](https://github.com/parallel-finance/)
- [PolkaFoundry Network](https://github.com/PolkaFoundry)
- [Setheum Network](https://github.com/Setheum-Labs/Setheum)
- [Valiu Liquidity Network](https://github.com/valibre-org/vln-node)
Expand Down
38 changes: 19 additions & 19 deletions asset-registry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,37 @@ serde = { version = "1.0.136", optional = true }
codec = { package = "parity-scale-codec", version = "3.0.0", default-features = false, features = ["max-encoded-len"] }

# substrate
frame-support = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.33" }
frame-system = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.33" }
sp-runtime = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.33" }
sp-std = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.33" }
frame-support = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.38" }
frame-system = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.38" }
sp-runtime = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.38" }
sp-std = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.38" }

# polkadot
pallet-xcm = { git = "https://github.com/paritytech/polkadot", default-features = false , branch = "release-v0.9.33" }
xcm = { git = "https://github.com/paritytech/polkadot", default-features = false , branch = "release-v0.9.33" }
xcm-builder = { git = "https://github.com/paritytech/polkadot", default-features = false , branch = "release-v0.9.33" }
xcm-executor = { git = "https://github.com/paritytech/polkadot", default-features = false , branch = "release-v0.9.33" }
pallet-xcm = { git = "https://github.com/paritytech/polkadot", default-features = false , branch = "release-v0.9.38" }
xcm = { git = "https://github.com/paritytech/polkadot", default-features = false , branch = "release-v0.9.38" }
xcm-builder = { git = "https://github.com/paritytech/polkadot", default-features = false , branch = "release-v0.9.38" }
xcm-executor = { git = "https://github.com/paritytech/polkadot", default-features = false , branch = "release-v0.9.38" }

# orml
orml-traits = { path = "../traits", version = "0.4.1-dev", default-features = false }

[dev-dependencies]
# substrate
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33" }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.33" }
sp-io = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.33" }
pallet-balances = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38" }
sp-core = { git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.38" }
sp-io = { git = "https://github.com/paritytech/substrate", default-features = false , branch = "polkadot-v0.9.38" }

# cumulus
cumulus-primitives-core = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.33" }
cumulus-pallet-dmp-queue = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.33" }
cumulus-pallet-xcmp-queue = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.33" }
cumulus-pallet-xcm = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.33" }
parachain-info = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.33" }
cumulus-primitives-core = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.38" }
cumulus-pallet-dmp-queue = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.38" }
cumulus-pallet-xcmp-queue = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.38" }
cumulus-pallet-xcm = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.38" }
parachain-info = { git = "https://github.com/paritytech/cumulus", branch = "polkadot-v0.9.38" }

# polkadot
polkadot-parachain = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.33" }
polkadot-runtime-parachains = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.33" }
xcm-simulator = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.33" }
polkadot-parachain = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.38" }
polkadot-runtime-parachains = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.38" }
xcm-simulator = { git = "https://github.com/paritytech/polkadot", branch = "release-v0.9.38" }

# orml
orml-tokens = { path = "../tokens" }
Expand Down
22 changes: 10 additions & 12 deletions asset-registry/src/impls.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::module::*;
use frame_support::{log, pallet_prelude::*, weights::constants::WEIGHT_PER_SECOND};
use frame_support::{log, pallet_prelude::*, weights::constants::WEIGHT_REF_TIME_PER_SECOND};
use orml_traits::{
asset_registry::{
AssetMetadata, AssetProcessor, FixedConversionRateProvider, Inspect, Mutate, WeightToFeeConverter,
Expand All @@ -12,7 +12,7 @@ use sp_runtime::{
ArithmeticError, FixedU128,
};
use sp_std::prelude::*;
use xcm::latest::{prelude::*, Weight as XcmWeight};
use xcm::v3::{prelude::*, Weight as XcmWeight};
use xcm::VersionedMultiLocation;
use xcm_builder::TakeRevenue;
use xcm_executor::{traits::WeightTrader, Assets};
Expand Down Expand Up @@ -55,7 +55,7 @@ pub struct FixedRateAssetRegistryTrader<P: FixedConversionRateProvider>(PhantomD
impl<P: FixedConversionRateProvider> WeightToFeeConverter for FixedRateAssetRegistryTrader<P> {
fn convert_weight_to_fee(location: &MultiLocation, weight: Weight) -> Option<u128> {
let fee_per_second = P::get_fee_per_second(location)?;
let weight_ratio = FixedU128::saturating_from_rational(weight.ref_time(), WEIGHT_PER_SECOND.ref_time() as u128);
let weight_ratio = FixedU128::saturating_from_rational(weight.ref_time(), WEIGHT_REF_TIME_PER_SECOND);
let amount = weight_ratio.saturating_mul_int(fee_per_second);
Some(amount)
}
Expand Down Expand Up @@ -104,26 +104,24 @@ impl<W: WeightToFeeConverter, R: TakeRevenue> WeightTrader for AssetRegistryTrad
continue;
}

if let Some(fee_increase) = W::convert_weight_to_fee(location, Weight::from_ref_time(weight)) {
if let Some(fee_increase) = W::convert_weight_to_fee(location, weight) {
if fee_increase == 0 {
// if the fee is set very low it could lead to zero fees, in which case
// constructing the fee asset item to subtract from payment would fail.
// Therefore, provide early exit
return Ok(payment);
}

if let Ok(unused) = payment.clone().checked_sub((asset.clone(), fee_increase).into()) {
if let Ok(unused) = payment.clone().checked_sub((*asset, fee_increase).into()) {
let (existing_weight, existing_fee) = match self.bought_weight {
Some(ref x) => (x.weight, x.amount),
None => (Weight::zero(), 0),
};

self.bought_weight = Some(BoughtWeight {
amount: existing_fee.checked_add(fee_increase).ok_or(XcmError::Overflow)?,
weight: existing_weight
.checked_add(&Weight::from_ref_time(weight))
.ok_or(XcmError::Overflow)?,
asset_location: location.clone(),
weight: existing_weight.checked_add(&weight).ok_or(XcmError::Overflow)?,
asset_location: *location,
});
return Ok(unused);
}
Expand All @@ -138,14 +136,14 @@ impl<W: WeightToFeeConverter, R: TakeRevenue> WeightTrader for AssetRegistryTrad

match self.bought_weight {
Some(ref mut bought) => {
let new_weight = bought.weight.saturating_sub(Weight::from_ref_time(weight));
let new_weight = bought.weight.saturating_sub(weight);
let new_amount = W::convert_weight_to_fee(&bought.asset_location, new_weight)?;
let refunded_amount = bought.amount.saturating_sub(new_amount);

bought.weight = new_weight;
bought.amount = new_amount;

Some((AssetId::Concrete(bought.asset_location.clone()), refunded_amount).into())
Some((AssetId::Concrete(bought.asset_location), refunded_amount).into())
}
None => None, // nothing to refund
}
Expand All @@ -155,7 +153,7 @@ impl<W: WeightToFeeConverter, R: TakeRevenue> WeightTrader for AssetRegistryTrad
impl<W: WeightToFeeConverter, R: TakeRevenue> Drop for AssetRegistryTrader<W, R> {
fn drop(&mut self) {
if let Some(ref bought) = self.bought_weight {
R::take_revenue((AssetId::Concrete(bought.asset_location.clone()), bought.amount).into());
R::take_revenue((AssetId::Concrete(bought.asset_location), bought.amount).into());
}
}
}
Expand Down
12 changes: 10 additions & 2 deletions asset-registry/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use sp_runtime::{
DispatchResult,
};
use sp_std::prelude::*;
use xcm::{v2::prelude::*, VersionedMultiLocation};
use xcm::{v3::prelude::*, VersionedMultiLocation};

pub use impls::*;
pub use module::*;
Expand All @@ -28,6 +28,9 @@ mod mock;
#[cfg(test)]
mod tests;

mod migrations;
pub use migrations::Migration;

#[frame_support::pallet]
pub mod module {
use super::*;
Expand Down Expand Up @@ -130,8 +133,11 @@ pub mod module {
}
}

const STORAGE_VERSION: StorageVersion = StorageVersion::new(2);

#[pallet::pallet]
#[pallet::generate_store(pub(super) trait Store)]
#[pallet::storage_version(STORAGE_VERSION)]
#[pallet::without_storage_info]
pub struct Pallet<T>(_);

Expand All @@ -140,6 +146,7 @@ pub mod module {

#[pallet::call]
impl<T: Config> Pallet<T> {
#[pallet::call_index(0)]
#[pallet::weight(T::WeightInfo::register_asset())]
pub fn register_asset(
origin: OriginFor<T>,
Expand All @@ -151,6 +158,7 @@ pub mod module {
Self::do_register_asset(metadata, asset_id)
}

#[pallet::call_index(1)]
#[pallet::weight(T::WeightInfo::update_asset())]
pub fn update_asset(
origin: OriginFor<T>,
Expand Down Expand Up @@ -311,7 +319,7 @@ impl<T: Config> Pallet<T> {
fn do_insert_location(asset_id: T::AssetId, location: VersionedMultiLocation) -> DispatchResult {
// if the metadata contains a location, set the LocationToAssetId
let location: MultiLocation = location.try_into().map_err(|()| Error::<T>::BadVersion)?;
LocationToAssetId::<T>::try_mutate(&location, |maybe_asset_id| {
LocationToAssetId::<T>::try_mutate(location, |maybe_asset_id| {
ensure!(maybe_asset_id.is_none(), Error::<T>::ConflictingLocation);
*maybe_asset_id = Some(asset_id);
Ok(())
Expand Down
43 changes: 43 additions & 0 deletions asset-registry/src/migrations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
use crate::{Config, LocationToAssetId, Pallet, Weight};
use frame_support::pallet_prelude::*;
use frame_support::{migration::storage_key_iter, traits::OnRuntimeUpgrade, StoragePrefixedMap};

use xcm::v3::prelude::*;

pub struct Migration<T>(PhantomData<T>);
impl<T: Config> OnRuntimeUpgrade for Migration<T> {
fn on_runtime_upgrade() -> Weight {
let mut weight: Weight = Weight::zero();
let onchain_version = Pallet::<T>::on_chain_storage_version();
if onchain_version < 2 {
let inner_weight = v2::migrate::<T>();
weight.saturating_accrue(inner_weight);
}
weight
}
}

mod v2 {
use super::*;

pub(crate) fn migrate<T: Config>() -> Weight {
let mut weight: Weight = Weight::zero();
let module_prefix = LocationToAssetId::<T>::module_prefix();
let storage_prefix = LocationToAssetId::<T>::storage_prefix();

weight.saturating_accrue(T::DbWeight::get().reads(1));
let old_data =
storage_key_iter::<xcm::v2::MultiLocation, T::AssetId, Blake2_128Concat>(module_prefix, storage_prefix)
.drain();

for (old_key, value) in old_data {
weight.saturating_accrue(T::DbWeight::get().writes(1));
let new_key: MultiLocation = old_key.try_into().expect("Stored xcm::v2::MultiLocation");
LocationToAssetId::<T>::insert(new_key, value);
}

StorageVersion::new(2).put::<Pallet<T>>();
weight.saturating_accrue(T::DbWeight::get().writes(1));
weight
}
}
Loading

0 comments on commit ae28667

Please sign in to comment.