Skip to content

Commit 8a289fa

Browse files
authored
feat(*): upgrade Frequency Mainnet to 6-Second Block Time (#2175)
This update reduces block time to 6 seconds. As a result, the Capacity Epoch length has been adjusted to account for the reduced latency. Additionally, the default interval sealing time has been changed to 6 seconds. A migration has also been added to update the StorageVersion of the CollatorSelection pallet. Commands to test the runtime migration: Paseo ``` make try-runtime-create-snapshot-paseo-testnet make try-runtime-use-snapshot-paseo-testnet ``` Mainnet ``` cargo build --features frequency,try-runtime&& \ try-runtime --runtime ./target/release/wbuild/frequency-runtime/frequency_runtime.wasm on-runtime-upgrade live --uri wss://1.rpc.frequency.xyz:443 --pallet Capacity --pallet System ``` Or Paseo ``` cargo build --features frequency,try-runtime && \ try-runtime --runtime ./target/release/wbuild/frequency-runtime/frequency_runtime.wasm on-runtime-upgrade live --uri wss://1.rpc.frequency.xyz:443 --pallet Capacity --pallet System ``` ``` cargo build --features frequency-testnet,try-runtime && \ try-runtime --runtime ./target/debug/wbuild/frequency-runtime/frequency_runtime.wasm on-runtime-upgrade live --uri wss://rpc.testnet.amplica.io:443 -pallet Capacity --pallet ``` <img width="1286" alt="Screenshot 2024-10-07 at 3 13 25 PM" src="https://github.com/user-attachments/assets/045f2bf1-cafc-41a1-b7f6-0baf12795b87"> #1920 #2141 --------- Co-authored-by: enddynayn <[email protected]>
1 parent 2525cd3 commit 8a289fa

29 files changed

+859
-939
lines changed

node/cli/src/cli.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ pub struct Cli {
9393

9494
/// Interval in seconds for interval sealing.
9595
#[cfg(feature = "frequency-no-relay")]
96-
#[clap(long, help = "The interval in seconds", default_value = "12", value_name = "SECONDS")]
96+
#[clap(long, help = "The interval in seconds", default_value = "6", value_name = "SECONDS")]
9797
pub sealing_interval: NonZeroU16,
9898

9999
/// Whether to create empty blocks in manual and interval sealing modes.

pallets/capacity/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ pub mod pallet {
8888
}
8989

9090
/// the storage version for this pallet
91-
pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(3);
91+
pub const STORAGE_VERSION: StorageVersion = StorageVersion::new(4);
9292

9393
#[pallet::config]
9494
pub trait Config: frame_system::Config {

pallets/capacity/src/migration/mod.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
1+
/// Migration logic for 6 second block updates sideffects to capacity pallet
2+
pub mod v4;

pallets/capacity/src/migration/v3.rs

Lines changed: 0 additions & 215 deletions
This file was deleted.

pallets/capacity/src/migration/v4.rs

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
use crate::{Config, EpochLength, Pallet};
2+
use frame_support::{
3+
pallet_prelude::{GetStorageVersion, Weight},
4+
traits::{Get, OnRuntimeUpgrade, StorageVersion},
5+
};
6+
use frame_system::pallet_prelude::BlockNumberFor;
7+
8+
const LOG_TARGET: &str = "runtime::capacity";
9+
10+
#[cfg(feature = "try-runtime")]
11+
use sp_std::vec::Vec;
12+
13+
/// The OnRuntimeUpgrade implementation for this storage migration
14+
pub struct MigrationToV4<T>(sp_std::marker::PhantomData<T>);
15+
impl<T> MigrationToV4<T>
16+
where
17+
T: Config,
18+
{
19+
/// Update the epoch length to double the current value
20+
pub fn update_epoch_length() -> Weight {
21+
let current_epoch_length = EpochLength::<T>::get();
22+
let new_epoch_length: BlockNumberFor<T> = current_epoch_length * 2u32.into();
23+
log::info!(target: LOG_TARGET, "🔄 Capacity EpochLength update from {:?} to {:?}", current_epoch_length, new_epoch_length);
24+
25+
EpochLength::<T>::put(new_epoch_length);
26+
27+
T::DbWeight::get().reads_writes(1, 1)
28+
}
29+
}
30+
31+
impl<T: Config> OnRuntimeUpgrade for MigrationToV4<T>
32+
where
33+
T: Config,
34+
{
35+
fn on_runtime_upgrade() -> Weight {
36+
let on_chain_version = Pallet::<T>::on_chain_storage_version(); // 1r
37+
38+
if on_chain_version.ge(&4) {
39+
log::info!(target: LOG_TARGET, "Old Capacity EpochLength migration attempted to run. Please remove");
40+
return T::DbWeight::get().reads(1);
41+
}
42+
43+
log::info!(target: LOG_TARGET, "🔄 Capacity EpochLength update migration started");
44+
// The migration started with 1r to get the STORAGE_VERSION
45+
let mut total_weight = T::DbWeight::get().reads_writes(1, 0);
46+
47+
total_weight += Self::update_epoch_length();
48+
49+
StorageVersion::new(4).put::<Pallet<T>>(); // 1 w
50+
51+
total_weight = total_weight.saturating_add(T::DbWeight::get().reads_writes(0, 1));
52+
53+
log::info!(target: LOG_TARGET, "🔄 Capacity EpochLength second update migration finished");
54+
55+
total_weight
56+
}
57+
58+
#[cfg(feature = "try-runtime")]
59+
fn pre_upgrade() -> Result<Vec<u8>, sp_runtime::TryRuntimeError> {
60+
use frame_support::storage::generator::StorageValue;
61+
use sp_std::vec;
62+
let on_chain_version = Pallet::<T>::on_chain_storage_version();
63+
if on_chain_version >= 4 {
64+
return Ok(Vec::new());
65+
}
66+
67+
let pallet_prefix = EpochLength::<T>::pallet_prefix();
68+
let storage_prefix = EpochLength::<T>::storage_prefix();
69+
assert_eq!(&b"Capacity"[..], pallet_prefix);
70+
assert_eq!(&b"EpochLength"[..], storage_prefix);
71+
log::info!(target: LOG_TARGET, "Running pre_upgrade...");
72+
73+
let current_epoch_length = EpochLength::<T>::get();
74+
75+
#[cfg(feature = "frequency")]
76+
assert_eq!(current_epoch_length, 7_200u32.into());
77+
#[cfg(feature = "frequency-testnet")]
78+
assert_eq!(current_epoch_length, 100u32.into());
79+
80+
log::info!(target: LOG_TARGET, "Finish pre_upgrade for with current epoch length {:?} to ", current_epoch_length,);
81+
Ok(vec![])
82+
}
83+
84+
#[cfg(feature = "try-runtime")]
85+
fn post_upgrade(_state: Vec<u8>) -> Result<(), sp_runtime::TryRuntimeError> {
86+
let on_chain_version = Pallet::<T>::on_chain_storage_version();
87+
if on_chain_version >= 4 {
88+
return Ok(());
89+
}
90+
91+
assert_eq!(on_chain_version, crate::pallet::STORAGE_VERSION);
92+
93+
#[cfg(feature = "frequency")]
94+
{
95+
let post_upgrade_epoch_length = EpochLength::<T>::get();
96+
assert_eq!(post_upgrade_epoch_length, 14_400u32.into());
97+
}
98+
99+
#[cfg(feature = "frequency-testnet")]
100+
{
101+
let post_upgrade_epoch_length = EpochLength::<T>::get();
102+
assert_eq!(post_upgrade_epoch_length, 200u32.into());
103+
}
104+
105+
log::info!(target: LOG_TARGET, "✅ migration post_upgrade checks passed");
106+
Ok(())
107+
}
108+
}
109+
110+
#[cfg(test)]
111+
mod test {
112+
use super::*;
113+
use crate::tests::mock::{Test as T, *};
114+
115+
type MigrationOf<T> = MigrationToV4<T>;
116+
117+
#[test]
118+
fn migration_works() {
119+
new_test_ext().execute_with(|| {
120+
EpochLength::<T>::put(7_200u32);
121+
122+
assert_eq!(EpochLength::<T>::get(), 7_200u32);
123+
124+
MigrationOf::<T>::on_runtime_upgrade();
125+
126+
let on_chain_version = Pallet::<T>::on_chain_storage_version();
127+
assert_eq!(on_chain_version, crate::pallet::STORAGE_VERSION);
128+
129+
assert_eq!(EpochLength::<T>::get(), 14_400u32);
130+
})
131+
}
132+
}

0 commit comments

Comments
 (0)