Skip to content
This repository was archived by the owner on Nov 15, 2023. It is now read-only.

Commit d6e7b8b

Browse files
committed
Make extension trait.
1 parent 05a5daf commit d6e7b8b

File tree

1 file changed

+33
-31
lines changed

1 file changed

+33
-31
lines changed

frame/support/src/traits.rs

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -1554,26 +1554,12 @@ pub trait OnGenesis {
15541554
}
15551555

15561556
/// Prefix to be used (optionally) for implementing [`OnRuntimeUpgrade::storage_key`].
1557+
#[cfg(feature = "try-runtime")]
15571558
pub const ON_RUNTIME_UPGRADE_PREFIX: &[u8] = b"__ON_RUNTIME_UPGRADE__";
15581559

1559-
/// The runtime upgrade trait.
1560-
///
1561-
/// Implementing this lets you express what should happen when the runtime upgrades,
1562-
/// and changes may need to occur to your module.
1563-
pub trait OnRuntimeUpgrade {
1564-
/// Perform a module upgrade.
1565-
///
1566-
/// # Warning
1567-
///
1568-
/// This function will be called before we initialized any runtime state, aka `on_initialize`
1569-
/// wasn't called yet. So, information like the block number and any other
1570-
/// block local data are not accessible.
1571-
///
1572-
/// Return the non-negotiable weight consumed for runtime upgrade.
1573-
fn on_runtime_upgrade() -> crate::weights::Weight {
1574-
0
1575-
}
1576-
1560+
/// Some helper functions for [`OnRuntimeUpgrade`] during `try-runtime` testing.
1561+
#[cfg(feature = "try-runtime")]
1562+
pub trait OnRuntimeUpgradeHelpersExt {
15771563
/// Generate a storage key unique to this runtime upgrade.
15781564
///
15791565
/// This can be used to communicate data from pre-upgrade to post-upgrade state and check
@@ -1590,6 +1576,17 @@ pub trait OnRuntimeUpgrade {
15901576
final_key
15911577
}
15921578

1579+
/// Get temporary storage data written by [`set_temp_storage`].
1580+
///
1581+
/// Returns `None` if either the data is unavailable or un-decodable.
1582+
///
1583+
/// A `at` storage identifier must be provided to indicate where the storage is being read from.
1584+
#[cfg(feature = "try-runtime")]
1585+
fn get_temp_storage<T: Decode>(at: &str) -> Option<T> {
1586+
sp_io::storage::get(&Self::storage_key(at))
1587+
.and_then(|bytes| Decode::decode(&mut &*bytes).ok())
1588+
}
1589+
15931590
/// Write some temporary data to a specific storage that can be read (potentially in
15941591
/// post-upgrade hook) via [`get_temp_storage`].
15951592
///
@@ -1599,16 +1596,27 @@ pub trait OnRuntimeUpgrade {
15991596
fn set_temp_storage<T: Encode>(data: T, at: &str) {
16001597
sp_io::storage::set(&Self::storage_key(at), &data.encode());
16011598
}
1599+
}
16021600

1603-
/// Get temporary storage data written by [`set_temp_storage`].
1601+
#[cfg(feature = "try-runtime")]
1602+
impl<U: OnRuntimeUpgrade> OnRuntimeUpgradeHelpersExt for U {}
1603+
1604+
/// The runtime upgrade trait.
1605+
///
1606+
/// Implementing this lets you express what should happen when the runtime upgrades,
1607+
/// and changes may need to occur to your module.
1608+
pub trait OnRuntimeUpgrade {
1609+
/// Perform a module upgrade.
16041610
///
1605-
/// Returns `None` if either the data is unavailable or un-decodable.
1611+
/// # Warning
16061612
///
1607-
/// A `at` storage identifier must be provided to indicate where the storage is being read from.
1608-
#[cfg(feature = "try-runtime")]
1609-
fn get_temp_storage<T: Decode>(at: &str) -> Option<T> {
1610-
sp_io::storage::get(&Self::storage_key(at))
1611-
.and_then(|bytes| Decode::decode(&mut &*bytes).ok())
1613+
/// This function will be called before we initialized any runtime state, aka `on_initialize`
1614+
/// wasn't called yet. So, information like the block number and any other
1615+
/// block local data are not accessible.
1616+
///
1617+
/// Return the non-negotiable weight consumed for runtime upgrade.
1618+
fn on_runtime_upgrade() -> crate::weights::Weight {
1619+
0
16121620
}
16131621

16141622
/// Execute some pre-checks prior to a runtime upgrade.
@@ -1649,12 +1657,6 @@ impl OnRuntimeUpgrade for Tuple {
16491657
for_tuples!( #( result = result.and(Tuple::post_upgrade()); )* );
16501658
result
16511659
}
1652-
1653-
#[cfg(feature = "try-runtime")]
1654-
fn storage_key(ident: &str) -> [u8; 32] {
1655-
// We will never use the key on a tuple, effectively _don't care_.
1656-
unreachable!()
1657-
}
16581660
}
16591661

16601662
/// Off-chain computation trait.

0 commit comments

Comments
 (0)