Skip to content

Commit

Permalink
Fix/runtime-upgrade-supports-wasm (#4088)
Browse files Browse the repository at this point in the history
Co-authored-by: Martin Rieke <121793148+martin-chainflip@users.noreply.github.com>
  • Loading branch information
dandanlen and martin-chainflip authored Oct 17, 2023
1 parent 07fed4d commit 779c859
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 91 deletions.
30 changes: 12 additions & 18 deletions state-chain/TROUBLESHOOTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,26 +12,21 @@ As of yet there is no real structure - this isn't intended to be a document to r

First, build the runtime node with `try-runtime` enabled.

The `try-runtime` features for the Chainflip runtime upgrade utilities are currently incompatible with WASM builds, so use `SKIP_WASM_BUILD` to avoid compiler errors.

> *Note: you need to tweak the `spec_version` of the local runtime to match that of the remote chain.*
```sh
SKIP_WASM_BUILD=1 cargo build --release --features=try-runtime
```bash copy
cargo build --release --features=try-runtime
```

Now you can run your tests like so (using an appropriate public rpc node):
Now you can run your tests aginst using an appropriate public rpc node. For example for perseverance:

```sh
./target/release/chainflip-node try-runtime --execution native \
```bash copy
./target/release/chainflip-node try-runtime \
--runtime ./target/release/wbuild/state-chain-runtime/state_chain_runtime.wasm \
on-runtime-upgrade live --uri wss://perseverance.chainflip.xyz:443
```

Sometimes this doesn't work. In this case you run a local rpc node and connect to that instead.
If you have trouble connecting to the remote rpc node, you can run a local rpc node and connect to that instead. First connect a local node to the network with some rpc optimisations:

First connect a local node to the network with some rpc optimisations:

```sh
```bash copy
./target/release/chainflip-node \
--chain ./state-chain/node/chainspecs/perseverance.chainspec.raw.json
--sync warp \
Expand All @@ -41,15 +36,14 @@ First connect a local node to the network with some rpc optimisations:
--rpc-cors all
```

Once the node has synced, in another terminal window, run the checks:
Once the node has synced, in another terminal window, run the checks as above:

```sh
./target/release/chainflip-node try-runtime --execution native \
```bash copy
./target/release/chainflip-node try-runtime \
--runtime ./target/release/wbuild/state-chain-runtime/state_chain_runtime.wasm \
on-runtime-upgrade live --uri ws://localhost:9944
```

> *Note: Using `--execution native` ensures faster execution and also prevents log messages from being scrubbed.
### General tips and guidelines

- There are some useful storage conversion utilities in `frame_support::storage::migration`.
Expand Down
119 changes: 46 additions & 73 deletions state-chain/runtime-upgrade-utilities/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,83 +31,56 @@ pub struct VersionedMigration<

#[cfg(feature = "try-runtime")]
mod try_runtime_helpers {
use frame_support::traits::PalletInfoAccess;
use sp_std::vec::Vec;

#[cfg(feature = "std")]
pub use with_std::*;

#[cfg(not(feature = "std"))]
pub use without_std::*;

#[cfg(feature = "std")]
mod with_std {
use super::*;
use core::cell::RefCell;
use sp_std::{
cmp::{max, min},
collections::btree_map::BTreeMap,
};

thread_local! {
pub static MIGRATION_BOUNDS: RefCell<BTreeMap<&'static str, (u16, u16)>> = Default::default();
#[allow(clippy::type_complexity)]
pub static MIGRATION_STATE: RefCell<BTreeMap<&'static str, BTreeMap<(u16, u16), Vec<u8>>>> = Default::default();
}

pub fn update_migration_bounds<T: PalletInfoAccess, const FROM: u16, const TO: u16>() {
MIGRATION_BOUNDS.with(|cell| {
cell.borrow_mut()
.entry(T::name())
.and_modify(|(from, to)| {
*from = min(*from, FROM);
*to = max(*to, TO);
})
.or_insert((FROM, TO));
});
}

pub fn get_migration_bounds<T: PalletInfoAccess>() -> Option<(u16, u16)> {
MIGRATION_BOUNDS.with(|cell| cell.borrow().get(T::name()).copied())
}

pub fn save_state<T: PalletInfoAccess, const FROM: u16, const TO: u16>(s: Vec<u8>) {
MIGRATION_STATE
.with(|cell| cell.borrow_mut().entry(T::name()).or_default().insert((FROM, TO), s));
}

pub fn restore_state<T: PalletInfoAccess, const FROM: u16, const TO: u16>() -> Vec<u8> {
MIGRATION_STATE.with(|cell| {
cell.borrow()
.get(T::name())
.cloned()
.unwrap_or_default()
.get(&(FROM, TO))
.cloned()
.unwrap_or_default()
})
}
use frame_support::{pallet_prelude::ValueQuery, storage_alias, traits::PalletInfoAccess};
use sp_std::{
cmp::{max, min},
collections::btree_map::BTreeMap,
vec::Vec,
};

#[storage_alias]
pub type MigrationBounds =
StorageValue<CfUpgradeUtilities, BTreeMap<Vec<u8>, (u16, u16)>, ValueQuery>;

#[storage_alias]
pub type MigrationState = StorageValue<
CfUpgradeUtilities,
BTreeMap<Vec<u8>, BTreeMap<(u16, u16), Vec<u8>>>,
ValueQuery,
>;

pub fn update_migration_bounds<T: PalletInfoAccess, const FROM: u16, const TO: u16>() {
MigrationBounds::mutate(|bounds| {
bounds
.entry(T::name().as_bytes().to_vec())
.and_modify(|(from, to)| {
*from = min(*from, FROM);
*to = max(*to, TO);
})
.or_insert((FROM, TO));
});
}

#[cfg(not(feature = "std"))]
mod without_std {
use super::*;

pub fn update_migration_bounds<T: PalletInfoAccess, const FROM: u16, const TO: u16>() {
log::warn!("❗️ Runtime upgrade utilities are not supported in no-std.");
}

pub fn get_migration_bounds<T: PalletInfoAccess>() -> Option<(u16, u16)> {
Default::default()
}
pub fn get_migration_bounds<T: PalletInfoAccess>() -> Option<(u16, u16)> {
MigrationBounds::get().get(T::name().as_bytes()).copied()
}

pub fn save_state<T: PalletInfoAccess, const FROM: u16, const TO: u16>(s: Vec<u8>) {
log::warn!("❗️ Runtime upgrade utilities are not supported in no-std.");
}
pub fn save_state<T: PalletInfoAccess, const FROM: u16, const TO: u16>(s: Vec<u8>) {
MigrationState::mutate(|state| {
state.entry(T::name().as_bytes().to_vec()).or_default().insert((FROM, TO), s)
});
}

pub fn restore_state<T: PalletInfoAccess, const FROM: u16, const TO: u16>() -> Vec<u8> {
Default::default()
}
pub fn restore_state<T: PalletInfoAccess, const FROM: u16, const TO: u16>() -> Vec<u8> {
MigrationState::mutate(|state| {
state
.get(T::name().as_bytes())
.cloned()
.unwrap_or_default()
.get(&(FROM, TO))
.cloned()
.unwrap_or_default()
})
}
}

Expand Down

0 comments on commit 779c859

Please sign in to comment.