Skip to content

Commit

Permalink
fix: get try-runtime to work again
Browse files Browse the repository at this point in the history
  • Loading branch information
sander2 committed Dec 6, 2022
1 parent 63b485b commit b4a788e
Show file tree
Hide file tree
Showing 28 changed files with 333 additions and 447 deletions.
283 changes: 61 additions & 222 deletions Cargo.lock

Large diffs are not rendered by default.

17 changes: 16 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,15 @@ pallet-election-provider-support-benchmarking = { git = "https://github.com/pari
pallet-nomination-pools = { git = "https://github.com/paritytech//substrate", branch = "polkadot-v0.9.31" }
pallet-nomination-pools-benchmarking = { git = "https://github.com/paritytech//substrate", branch = "polkadot-v0.9.31" }
pallet-nomination-pools-runtime-api = { git = "https://github.com/paritytech//substrate", branch = "polkadot-v0.9.31" }
pallet-preimage = { git = "https://github.com/paritytech//substrate", branch = "polkadot-v0.9.31" }
pallet-preimage = { git = "https://github.com/paritytech//substrate", branch = "polkadot-v0.9.31" }
pallet-conviction-voting = { git = "https://github.com/paritytech//substrate", branch = "polkadot-v0.9.31" }
pallet-fast-unstake = { git = "https://github.com/paritytech//substrate", branch = "polkadot-v0.9.31" }
pallet-ranked-collective = { git = "https://github.com/paritytech//substrate", branch = "polkadot-v0.9.31" }
pallet-referenda = { git = "https://github.com/paritytech//substrate", branch = "polkadot-v0.9.31" }
pallet-whitelist = { git = "https://github.com/paritytech//substrate", branch = "polkadot-v0.9.31" }
sc-network-light = { git = "https://github.com/paritytech//substrate", branch = "polkadot-v0.9.31" }
sc-network-sync = { git = "https://github.com/paritytech//substrate", branch = "polkadot-v0.9.31" }
fork-tree = { git = "https://github.com/paritytech//substrate", branch = "polkadot-v0.9.31" }

[patch."https://github.com/paritytech/polkadot"]
kusama-runtime = { git = "https://github.com/paritytech//polkadot", branch = "release-v0.9.31" }
Expand Down Expand Up @@ -199,6 +207,12 @@ xcm = { git = "https://github.com/paritytech//polkadot", branch = "release-v0.9.
xcm-builder = { git = "https://github.com/paritytech//polkadot", branch = "release-v0.9.31" }
xcm-executor = { git = "https://github.com/paritytech//polkadot", branch = "release-v0.9.31" }
xcm-simulator = { git = "https://github.com/paritytech//polkadot", branch = "release-v0.9.31" }
polkadot-availability-distribution = { git = "https://github.com/paritytech//polkadot", branch = "release-v0.9.31" }
polkadot-erasure-coding = { git = "https://github.com/paritytech//polkadot", branch = "release-v0.9.31" }
polkadot-network-bridge = { git = "https://github.com/paritytech//polkadot", branch = "release-v0.9.31" }
polkadot-node-jaeger = { git = "https://github.com/paritytech//polkadot", branch = "release-v0.9.31" }
tracing-gum = { git = "https://github.com/paritytech//polkadot", branch = "release-v0.9.31" }
tracing-gum-proc-macro = { git = "https://github.com/paritytech//polkadot", branch = "release-v0.9.31" }

[patch."https://github.com/paritytech/cumulus"]
cumulus-client-cli = { git = "https://github.com/paritytech//cumulus", branch = "polkadot-v0.9.31" }
Expand All @@ -222,3 +236,4 @@ parachain-info = { git = "https://github.com/paritytech//cumulus", branch = "po
cumulus-relay-chain-inprocess-interface = { git = "https://github.com/paritytech//cumulus", branch = "polkadot-v0.9.31" }
cumulus-relay-chain-rpc-interface = { git = "https://github.com/paritytech//cumulus", branch = "polkadot-v0.9.31" }
cumulus-client-collator = { git = "https://github.com/paritytech//cumulus", branch = "polkadot-v0.9.31" }
cumulus-relay-chain-minimal-node = { git = "https://github.com/paritytech//cumulus", branch = "polkadot-v0.9.31" }
3 changes: 2 additions & 1 deletion crates/annuity/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,5 @@ runtime-benchmarks = [
"frame-benchmarking",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
]
]
try-runtime = [ "frame-support/try-runtime" ]
1 change: 1 addition & 0 deletions crates/btc-relay/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@ runtime-benchmarks = [
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
]
try-runtime = [ "frame-support/try-runtime" ]
3 changes: 2 additions & 1 deletion crates/clients-info/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -42,4 +42,5 @@ std = [
"frame-support/std",
"frame-system/std",
"frame-benchmarking/std",
]
]
try-runtime = [ "frame-support/try-runtime" ]
1 change: 1 addition & 0 deletions crates/currency/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -55,3 +55,4 @@ std = [
testing-utils = [
"mocktopus"
]
try-runtime = [ "frame-support/try-runtime" ]
3 changes: 2 additions & 1 deletion crates/escrow/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,5 @@ runtime-benchmarks = [
"frame-benchmarking",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
]
]
try-runtime = [ "frame-support/try-runtime" ]
3 changes: 2 additions & 1 deletion crates/fee/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,5 @@ runtime-benchmarks = [
"frame-benchmarking",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
]
]
try-runtime = [ "frame-support/try-runtime" ]
27 changes: 0 additions & 27 deletions crates/issue/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,33 +125,6 @@ pub mod pallet {
AmountBelowDustAmount,
}

#[pallet::hooks]
impl<T: Config> Hooks<T::BlockNumber> for Pallet<T> {
#[cfg(feature = "try-runtime")]
fn pre_upgrade() -> Result<(), &'static str> {
ensure!(Self::storage_version() == Version::V0, "Wrong storage version.");
let issue_count_before = crate::IssueRequests::<T>::iter().count() as u32;
log::info!("issue count before: {:?}", issue_count_before);
crate::types::v4::IssueCountBefore::<T>::put(issue_count_before);

Ok(())
}

fn on_runtime_upgrade() -> frame_support::weights::Weight {
crate::types::v4::migrate_v0_to_v4::<T>()
}

#[cfg(feature = "try-runtime")]
fn post_upgrade() -> Result<(), &'static str> {
ensure!(Self::storage_version() == Version::V4, "Wrong storage version.");
let issue_count_before = crate::types::v4::IssueCountBefore::<T>::take();
let issue_count_after = crate::IssueRequests::<T>::iter().count() as u32;
log::info!("issue count after: {:?}", issue_count_after,);
ensure!(issue_count_before == issue_count_after, "Not all issues were migrated.");
Ok(())
}
}

/// Users create issue requests to issue tokens. This mapping provides access
/// from a unique hash `IssueId` to an `IssueRequest` struct.
#[pallet::storage]
Expand Down
152 changes: 0 additions & 152 deletions crates/issue/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,158 +23,6 @@ pub enum Version {
V4,
}

pub mod v4 {
use super::*;
use crate::{BtcAddress, BtcPublicKey, H256};
use frame_support::pallet_prelude::*;

#[frame_support::storage_alias]
pub type IssueCountBefore<T: crate::Config> = StorageValue<crate::Pallet<T>, u32, ValueQuery>;

#[derive(Encode, Decode, Clone, PartialEq, TypeInfo, MaxEncodedLen)]
pub enum IssueRequestStatusV3 {
Pending,
Completed(Option<H256>),
Cancelled,
}

impl Into<IssueRequestStatus> for IssueRequestStatusV3 {
fn into(self) -> IssueRequestStatus {
match self {
IssueRequestStatusV3::Pending => IssueRequestStatus::Pending,
IssueRequestStatusV3::Completed(_) => IssueRequestStatus::Completed,
IssueRequestStatusV3::Cancelled => IssueRequestStatus::Cancelled,
}
}
}

#[derive(Encode, Decode, Clone, PartialEq, TypeInfo, MaxEncodedLen)]
pub struct IssueRequestV3<AccountId, BlockNumber, Balance, CurrencyId: Copy> {
pub vault: VaultId<AccountId, CurrencyId>,
pub opentime: BlockNumber,
pub period: BlockNumber,
pub griefing_collateral: Balance,
pub amount: Balance,
pub fee: Balance,
pub requester: AccountId,
pub btc_address: BtcAddress,
pub btc_public_key: BtcPublicKey,
pub btc_height: u32,
pub status: IssueRequestStatusV3,
}

pub type DefaultIssueRequestV3<T> = IssueRequestV3<
<T as frame_system::Config>::AccountId,
<T as frame_system::Config>::BlockNumber,
BalanceOf<T>,
CurrencyId<T>,
>;

pub fn migrate_v0_to_v4<T: Config>() -> frame_support::weights::Weight {
use sp_runtime::traits::Saturating;

// NOTE: kintsugi & interlay still on version 0
if !matches!(crate::StorageVersion::<T>::get(), Version::V0) {
log::info!("Not running issue storage migration");
return T::DbWeight::get().reads(1); // already upgraded; don't run migration
}
let mut num_migrated_issues = 0u64;

crate::IssueRequests::<T>::translate::<DefaultIssueRequestV3<T>, _>(|_key, issue_v3| {
num_migrated_issues.saturating_inc();

Some(IssueRequest {
vault: issue_v3.vault,
opentime: issue_v3.opentime,
period: issue_v3.period,
griefing_collateral: issue_v3.griefing_collateral,
amount: issue_v3.amount,
fee: issue_v3.fee,
requester: issue_v3.requester,
btc_address: issue_v3.btc_address,
btc_public_key: issue_v3.btc_public_key,
btc_height: issue_v3.btc_height,
status: issue_v3.status.into(),
})
});
crate::StorageVersion::<T>::put(Version::V4);

log::info!("Migrated {num_migrated_issues} issues");

T::DbWeight::get().reads_writes(num_migrated_issues, num_migrated_issues)
}

#[cfg(test)]
#[test]
fn test_migration() {
use crate::mock::Test;
use frame_support::{storage::migration, Blake2_128Concat, StorageHasher};
use primitives::{CurrencyId::Token, VaultCurrencyPair, KBTC, KSM};
use sp_runtime::traits::TrailingZeroInput;

crate::mock::run_test(|| {
crate::StorageVersion::<Test>::put(Version::V0);

let issue_v3: DefaultIssueRequestV3<Test> = IssueRequestV3 {
vault: VaultId {
account_id: <Test as frame_system::Config>::AccountId::decode(&mut TrailingZeroInput::zeroes())
.unwrap(),
currencies: VaultCurrencyPair {
collateral: Token(KSM),
wrapped: Token(KBTC),
},
},
opentime: 1_501_896,
period: 14_400,
griefing_collateral: 20,
amount: 100,
fee: 10,
requester: <Test as frame_system::Config>::AccountId::decode(&mut TrailingZeroInput::zeroes()).unwrap(),
btc_address: BtcAddress::P2PKH(sp_core::H160::from([1; 20])),
btc_public_key: BtcPublicKey::from([1; 33]),
btc_height: 754_190,
status: IssueRequestStatusV3::Completed(None),
};

let issue_id = crate::ext::security::get_secure_id::<Test>(
&<Test as frame_system::Config>::AccountId::decode(&mut TrailingZeroInput::zeroes()).unwrap(),
);

migration::put_storage_value(
b"Issue",
b"IssueRequests",
&Blake2_128Concat::hash(&issue_id.encode()),
&issue_v3,
);

migrate_v0_to_v4::<Test>();

let issue_v4 = IssueRequest {
vault: issue_v3.vault,
opentime: issue_v3.opentime,
period: issue_v3.period,
griefing_collateral: issue_v3.griefing_collateral,
amount: issue_v3.amount,
fee: issue_v3.fee,
requester: issue_v3.requester,
btc_address: issue_v3.btc_address,
btc_public_key: issue_v3.btc_public_key,
btc_height: issue_v3.btc_height,
status: issue_v3.status.into(),
};

// check that migration was applied correctly
assert_eq!(
crate::IssueRequests::<Test>::iter().collect::<Vec<_>>(),
vec![(issue_id.clone(), issue_v4)]
);

// check that storage version is bumped
assert!(crate::StorageVersion::<Test>::get() == Version::V4);
});
}
}

pub(crate) type BalanceOf<T> = <T as vault_registry::Config>::Balance;

pub(crate) type DefaultVaultId<T> = VaultId<<T as frame_system::Config>::AccountId, CurrencyId<T>>;
Expand Down
1 change: 1 addition & 0 deletions crates/nomination/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,4 @@ runtime-benchmarks = [
"orml-tokens",
"orml-traits",
]
try-runtime = [ "frame-support/try-runtime" ]
1 change: 1 addition & 0 deletions crates/oracle/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,3 +63,4 @@ runtime-benchmarks = [

"pallet-timestamp/runtime-benchmarks",
]
try-runtime = [ "frame-support/try-runtime" ]
1 change: 1 addition & 0 deletions crates/redeem/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -90,3 +90,4 @@ runtime-benchmarks = [
"orml-tokens",
"orml-traits",
]
try-runtime = [ "frame-support/try-runtime" ]
3 changes: 2 additions & 1 deletion crates/replace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,5 @@ runtime-benchmarks = [

"orml-tokens",
"orml-traits",
]
]
try-runtime = [ "frame-support/try-runtime" ]
3 changes: 2 additions & 1 deletion crates/reward/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@ runtime-benchmarks = [
"frame-benchmarking",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
]
]
try-runtime = [ "frame-support/try-runtime" ]
3 changes: 2 additions & 1 deletion crates/security/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@ std = [
"sp-runtime/std",
"frame-support/std",
"frame-system/std",
]
]
try-runtime = [ "frame-support/try-runtime" ]
3 changes: 2 additions & 1 deletion crates/staking/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,5 @@ runtime-benchmarks = [
"frame-benchmarking",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
]
]
try-runtime = [ "frame-support/try-runtime" ]
3 changes: 2 additions & 1 deletion crates/supply/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@ runtime-benchmarks = [
"frame-benchmarking",
"frame-support/runtime-benchmarks",
"frame-system/runtime-benchmarks",
]
]
try-runtime = [ "frame-support/try-runtime" ]
1 change: 1 addition & 0 deletions crates/vault-registry/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -88,3 +88,4 @@ integration-tests = [
"visibility"
]
skip-slow-tests = []
try-runtime = [ "frame-support/try-runtime" ]
Loading

0 comments on commit b4a788e

Please sign in to comment.