-
Notifications
You must be signed in to change notification settings - Fork 110
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[AHM] Asset rate pallet migration (#581)
meant to be merged into AHM dev branch Integrates asset rate pallet into Polkadot Asset Hub and introduces the data migration for the pallet. No user impact, no security concerns. --------- Signed-off-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io> Co-authored-by: Oliver Tale-Yazdi <oliver.tale-yazdi@parity.io>
- Loading branch information
Showing
12 changed files
with
261 additions
and
20 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
// Copyright (C) Parity Technologies (UK) Ltd. | ||
// This file is part of Polkadot. | ||
|
||
// Polkadot is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// Polkadot is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
use crate::*; | ||
use pallet_asset_rate::ConversionRateToNative; | ||
|
||
impl<T: Config> Pallet<T> { | ||
pub fn do_receive_asset_rates( | ||
rates: Vec<(<T as pallet_asset_rate::Config>::AssetKind, FixedU128)>, | ||
) -> Result<(), Error<T>> { | ||
log::info!(target: LOG_TARGET, "Processing {} asset rates", rates.len()); | ||
|
||
let count = rates.len() as u32; | ||
Self::deposit_event(Event::AssetRatesReceived { count }); | ||
|
||
for rate in rates { | ||
Self::do_receive_asset_rate(rate)?; | ||
} | ||
|
||
log::info!(target: LOG_TARGET, "Processed {} asset rates", count); | ||
Self::deposit_event(Event::AssetRatesProcessed { count_good: count, count_bad: 0 }); | ||
|
||
Ok(()) | ||
} | ||
|
||
pub fn do_receive_asset_rate( | ||
rate: (<T as pallet_asset_rate::Config>::AssetKind, FixedU128), | ||
) -> Result<(), Error<T>> { | ||
let (asset_kind, rate) = rate; | ||
log::debug!(target: LOG_TARGET, "Inserting asset rate for {:?}: {}", asset_kind, rate); | ||
ConversionRateToNative::<T>::insert(asset_kind, rate); | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright (C) Parity Technologies (UK) Ltd. | ||
// This file is part of Polkadot. | ||
|
||
// Polkadot is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// Polkadot is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with Polkadot. If not, see <http://www.gnu.org/licenses/>. | ||
|
||
use crate::*; | ||
use pallet_asset_rate::ConversionRateToNative; | ||
|
||
pub struct AssetRateMigrator<T> { | ||
pub _phantom: PhantomData<T>, | ||
} | ||
|
||
impl<T: Config> PalletMigration for AssetRateMigrator<T> { | ||
type Key = <T as pallet_asset_rate::Config>::AssetKind; | ||
type Error = Error<T>; | ||
|
||
fn migrate_many( | ||
mut last_key: Option<Self::Key>, | ||
weight_counter: &mut WeightMeter, | ||
) -> Result<Option<Self::Key>, Self::Error> { | ||
log::info!(target: LOG_TARGET, "Migrating asset rates"); | ||
let mut messages = Vec::new(); | ||
|
||
loop { | ||
if weight_counter | ||
.try_consume(<T as frame_system::Config>::DbWeight::get().reads_writes(1, 1)) | ||
.is_err() | ||
{ | ||
if messages.is_empty() { | ||
return Err(Error::OutOfWeight); | ||
} else { | ||
break; | ||
} | ||
} | ||
if messages.len() > 10_000 { | ||
log::warn!(target: LOG_TARGET, "Weight allowed very big batch, stopping"); | ||
break; | ||
} | ||
|
||
let mut iter = if let Some(last_key) = last_key { | ||
ConversionRateToNative::<T>::iter_from_key(last_key) | ||
} else { | ||
ConversionRateToNative::<T>::iter() | ||
}; | ||
|
||
match iter.next() { | ||
Some((key, value)) => { | ||
log::debug!(target: LOG_TARGET, "Extracting asset rate for {:?}", &key); | ||
ConversionRateToNative::<T>::remove(&key); | ||
messages.push((key.clone(), value)); | ||
last_key = Some(key); | ||
}, | ||
None => { | ||
log::debug!(target: LOG_TARGET, "No more asset rates to migrate"); | ||
last_key = None; | ||
break; | ||
}, | ||
} | ||
} | ||
|
||
if !messages.is_empty() { | ||
Pallet::<T>::send_chunked_xcm(messages, |messages| { | ||
types::AhMigratorCall::<T>::ReceiveAssetRates { asset_rates: messages } | ||
})?; | ||
} | ||
|
||
Ok(last_key) | ||
} | ||
} |
Oops, something went wrong.