Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Drop legacy CurrencyId::AUSD variant #1433

Closed
wants to merge 14 commits into from
Closed
73 changes: 1 addition & 72 deletions libs/types/src/tokens.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,6 @@ pub enum CurrencyId {
#[codec(index = 1)]
Tranche(PoolId, TrancheId),

#[codec(index = 3)]
/// DEPRECATED - Will be removed in the following up Runtime Upgrade once
/// the orml_tokens' balances are migrated to the new CurrencyId for AUSD.
AUSD,

/// A foreign asset
#[codec(index = 4)]
ForeignAsset(ForeignAssetId),
Expand Down Expand Up @@ -412,7 +407,7 @@ mod tests {
use frame_support::parameter_types;

use super::*;
use crate::tokens::CurrencyId::{ForeignAsset, Native, Staking, Tranche, AUSD};
use crate::tokens::CurrencyId::{ForeignAsset, Native, Staking, Tranche};

const FOREIGN: CurrencyId = ForeignAsset(1u32);

Expand Down Expand Up @@ -464,70 +459,6 @@ mod tests {
);
}

#[cfg(test)]
mod tests {
use cfg_primitives::TrancheId;
use codec::Encode;
use hex::FromHex;

use super::StakingCurrency;
use crate::{tokens as after, tokens::before};

#[test]
fn currency_id_refactor_encode_equality() {
// Native
assert_eq!(
before::CurrencyId::Native.encode(),
after::CurrencyId::Native.encode()
);
assert_eq!(after::CurrencyId::Native.encode(), vec![0]);

// Tranche
assert_eq!(
before::CurrencyId::Tranche(33, default_tranche_id()).encode(),
after::CurrencyId::Tranche(33, default_tranche_id()).encode()
);
assert_eq!(
after::CurrencyId::Tranche(33, default_tranche_id()).encode(),
vec![
1, 33, 0, 0, 0, 0, 0, 0, 0, 129, 26, 205, 91, 63, 23, 192, 104, 65, 199, 228,
30, 158, 4, 203, 27
]
);

// KSM - deprecated
assert_eq!(before::CurrencyId::KSM.encode(), vec![2]);

// AUSD - deprecated
assert_eq!(before::CurrencyId::AUSD.encode(), vec![3]);

// ForeignAsset
assert_eq!(
before::CurrencyId::ForeignAsset(91).encode(),
after::CurrencyId::ForeignAsset(91).encode()
);
assert_eq!(
after::CurrencyId::ForeignAsset(91).encode(),
vec![4, 91, 0, 0, 0]
);

// Staking
assert_eq!(
before::CurrencyId::Staking(StakingCurrency::BlockRewards).encode(),
after::CurrencyId::Staking(StakingCurrency::BlockRewards).encode()
);
assert_eq!(
after::CurrencyId::Staking(StakingCurrency::BlockRewards).encode(),
vec![5, 0]
);
}

fn default_tranche_id() -> TrancheId {
<[u8; 16]>::from_hex("811acd5b3f17c06841c7e41e9e04cb1b")
.expect("Should be valid tranche id")
}
}

/// Sanity check for every CurrencyId variant's encoding value.
/// This will stop us from accidentally moving or dropping variants
/// around which could have silent but serious negative consequences.
Expand All @@ -539,7 +470,6 @@ mod tests {
vec![
Native,
Tranche(42, [42; 16]),
AUSD,
ForeignAsset(89),
Staking(StakingCurrency::BlockRewards),
]
Expand All @@ -558,7 +488,6 @@ mod tests {
r.append(&mut tranche_id.to_vec());
r
}
AUSD => vec![3],
ForeignAsset(id) => {
let mut r: Vec<u8> = vec![4];
r.append(&mut id.encode());
Expand Down
128 changes: 0 additions & 128 deletions runtime/altair/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,6 @@ use frame_support::{traits::OnRuntimeUpgrade, weights::Weight};
/// already been executed on Algol (1028 & 1029).
#[cfg(not(feature = "testnet-runtime"))]
pub type UpgradeAltair1034 = (
// FIXME: This migration fails to decode 4 entries against Altair
// orml_tokens_migration::CurrencyIdRefactorMigration,
// At minimum, bumps storage version from 1 to 2
runtime_common::migrations::nuke::Migration<crate::Loans, crate::RocksDbWeight, 1>,
// At minimum, bumps storage version from 0 to 3
Expand Down Expand Up @@ -207,132 +205,6 @@ mod asset_registry {
}
}

mod orml_tokens_migration {
use cfg_primitives::{AccountId, Balance};
use cfg_types::tokens::CurrencyId;
use codec::{Decode, Encode};
#[cfg(feature = "try-runtime")]
use frame_support::ensure;
use orml_tokens::AccountData;
use sp_std::vec::Vec;

use super::*;
use crate::Runtime;

const DEPRECATED_AUSD_CURRENCY_ID: CurrencyId = CurrencyId::AUSD;
const NEW_AUSD_CURRENCY_ID: CurrencyId = CurrencyId::ForeignAsset(2);

/// As we dropped `CurrencyId::KSM` and `CurrencyId::AUSD`, we need to
/// migrate the balances under the dropped variants in favour of the new,
/// corresponding `CurrencyId::ForeignAsset`. We have never transferred KSM
/// so we only need to deal with AUSD.
pub struct CurrencyIdRefactorMigration;

#[derive(Clone, PartialEq, Eq, Debug, Encode, Decode)]
pub struct OldState {
pub total_issuance: Balance,
pub entries: Vec<(AccountId, AccountData<Balance>)>,
}

impl OnRuntimeUpgrade for CurrencyIdRefactorMigration {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<Vec<u8>, &'static str> {
let total_issuance =
orml_tokens::TotalIssuance::<Runtime>::get(DEPRECATED_AUSD_CURRENCY_ID);
let entries: Vec<(AccountId, AccountData<Balance>)> =
orml_tokens::Accounts::<Runtime>::iter()
.filter(|(_, old_currency_id, _)| {
*old_currency_id == DEPRECATED_AUSD_CURRENCY_ID
})
.map(|(account, _, account_data)| (account, account_data))
.collect::<_>();

Ok(OldState {
total_issuance,
entries,
}
.encode())
}

#[cfg(feature = "try-runtime")]
fn post_upgrade(state: Vec<u8>) -> Result<(), &'static str> {
use crate::OrmlTokens;

let old_state = OldState::decode(&mut state.as_ref())
.map_err(|_| "Error decoding pre-upgrade state")?;

let new_total_issuance =
orml_tokens::TotalIssuance::<Runtime>::get(NEW_AUSD_CURRENCY_ID);

ensure!(
old_state.total_issuance == new_total_issuance,
"The old AUSD issuance differs from the new one"
);

for (account, account_data) in old_state.entries {
ensure!(
OrmlTokens::accounts(&account, NEW_AUSD_CURRENCY_ID) == account_data.clone(),
"The account data under the new AUSD Currency does NOT match the old one"
);
}

Ok(())
}

fn on_runtime_upgrade() -> Weight {
use frame_support::traits::tokens::fungibles::Mutate;

let mut migrated_entries: u64 = 0;

// Burn all AUSD tokens under the old CurrencyId and mint them under the new one
orml_tokens::Accounts::<Runtime>::iter()
.filter(|(_, old_currency_id, _)| *old_currency_id == DEPRECATED_AUSD_CURRENCY_ID)
.for_each(|(account, _, account_data)| {
let balance = account_data.free;
// Burn the amount under the old, hardcoded CurrencyId
<orml_tokens::Pallet<Runtime> as Mutate<AccountId>>::burn_from(
DEPRECATED_AUSD_CURRENCY_ID,
&account,
balance,
)
.map_err(|e| {
log::error!(
"Failed to call burn_from({:?}, {:?}, {balance}): {:?}",
DEPRECATED_AUSD_CURRENCY_ID,
account,
e
)
})
.ok();
// Now mint the amount under the new CurrencyID
<orml_tokens::Pallet<Runtime> as Mutate<AccountId>>::mint_into(
NEW_AUSD_CURRENCY_ID,
&account,
balance,
)
.map_err(|e| {
log::error!(
"Failed to mint_into burn_from({:?}, {:?}, {balance}): {:?}",
NEW_AUSD_CURRENCY_ID,
account,
e
)
})
.ok();

migrated_entries += 1;
});

// Approximate weight given for every entry migration there are two calls being
// made, so counting the reads and writes for each call.
<Runtime as frame_system::Config>::DbWeight::get().reads_writes(
migrated_entries.saturating_mul(5),
migrated_entries.saturating_mul(4),
)
}
}
}

mod xcm_v2_to_v3 {
use super::*;
use crate::{PolkadotXcm, RuntimeOrigin};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -600,34 +600,6 @@ fn allow_pool_should_fail() {
),
pallet_liquidity_pools::Error::<DevelopmentRuntime>::AssetNotLiquidityPoolsWrappedToken
);

// Create new pool for non foreign asset
// NOTE: Can be removed after merging https://github.com/centrifuge/centrifuge-chain/pull/1343
assert_ok!(OrmlAssetRegistry::register_asset(
RuntimeOrigin::root(),
asset_metadata(
"Acala Dollar".into(),
"AUSD".into(),
12,
true,
None,
Default::default()
),
Some(CurrencyId::AUSD)
));
create_currency_pool(pool_id + 1, CurrencyId::AUSD, 10_000 * dollar(12));
// Should fail if currency is not foreign asset
assert_noop!(
LiquidityPools::allow_pool_currency(
RuntimeOrigin::signed(BOB.into()),
pool_id + 1,
// Tranche id is arbitrary in this case, so we don't need to check for the exact
// pool_id
default_tranche_id(pool_id + 1),
CurrencyId::AUSD,
),
DispatchError::Token(sp_runtime::TokenError::Unsupported)
);
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ use crate::{
test_net::{Development, Moonbeam, RelayChain, TestNet},
tests::routers::axelar_evm::TEST_EVM_CHAIN,
},
utils::accounts::Keyring,
utils::{accounts::Keyring, AUSD_CURRENCY_ID},
};

#[test]
Expand Down Expand Up @@ -200,6 +200,6 @@ fn setup(router_creation_fn: RouterCreationFn) {
assert_ok!(OrmlAssetRegistry::register_asset(
RuntimeOrigin::root(),
ausd_meta,
Some(CurrencyId::AUSD)
Some(AUSD_CURRENCY_ID)
));
}
Loading
Loading