Skip to content

Commit

Permalink
Merge pull request #792 from gregdhill/fix/kintsugi-slot-duration
Browse files Browse the repository at this point in the history
fix: add migration for kintsugi slot duration
  • Loading branch information
nud3l authored Dec 2, 2022
2 parents 47bfef0 + aca2f3e commit a447c97
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 26 deletions.
2 changes: 2 additions & 0 deletions crates/collator-selection/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ frame-support = { default-features = false, git = "https://github.com/paritytech
frame-system = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31" }
pallet-authorship = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31" }
pallet-session = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31" }
sp-consensus-aura = { default-features = false, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31" }

frame-benchmarking = { default-features = false, optional = true, git = "https://github.com/paritytech/substrate", branch = "polkadot-v0.9.31" }

Expand Down Expand Up @@ -59,6 +60,7 @@ std = [
"frame-benchmarking/std",
"pallet-authorship/std",
"pallet-session/std",
"sp-consensus-aura/std",
]

try-runtime = [ "frame-support/try-runtime" ]
31 changes: 30 additions & 1 deletion crates/collator-selection/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,36 @@ pub mod pallet {
}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {}
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_initialize(n: BlockNumberFor<T>) -> Weight {
if n != 1983994u32.into() {
// only run for this block on kintsugi
// remove once complete
return Weight::zero();
}

let old_slot_duration: u64 = 6000; // ms
let new_slot_duration: u64 = 12000; // ms

// https://hackmd.io/@XuVYQ1rUQjGv8uRzzsdzuw/HkduIX4y5
let current_slot =
frame_support::migration::get_storage_value::<sp_consensus_aura::Slot>(b"Aura", b"CurrentSlot", &[])
.unwrap();

// slot = timestamp / slot_duration
// timestamp = slot * slot_duration
let timestamp: u64 = Into::<u64>::into(current_slot) * old_slot_duration;
let new_slot = timestamp / new_slot_duration;
frame_support::migration::put_storage_value(
b"Aura",
b"CurrentSlot",
&[],
sp_consensus_aura::Slot::from(new_slot),
);

Weight::zero()
}
}

#[pallet::call]
impl<T: Config> Pallet<T> {
Expand Down
38 changes: 24 additions & 14 deletions crates/collator-selection/src/mock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,14 @@ use frame_system as system;
use frame_system::EnsureSignedBy;
use sp_core::H256;
use sp_runtime::{
testing::{Header, UintAuthorityId},
generic::Header as GenericHeader,
testing::UintAuthorityId,
traits::{BlakeTwo256, IdentityLookup, OpaqueKeys},
RuntimeAppPublic,
};

type Header = GenericHeader<BlockNumber, BlakeTwo256>;

type UncheckedExtrinsic = frame_system::mocking::MockUncheckedExtrinsic<Test>;
type Block = frame_system::mocking::MockBlock<Test>;

Expand All @@ -49,8 +52,14 @@ frame_support::construct_runtime!(
}
);

pub type AccountId = u64;
pub type Balance = u128;
pub type BlockNumber = u64;
pub type Index = u64;
pub type Moment = u64;

parameter_types! {
pub const BlockHashCount: u64 = 250;
pub const BlockHashCount: u32 = 250;
pub const SS58Prefix: u8 = 42;
}

Expand All @@ -61,18 +70,18 @@ impl system::Config for Test {
type DbWeight = ();
type RuntimeOrigin = RuntimeOrigin;
type RuntimeCall = RuntimeCall;
type Index = u64;
type BlockNumber = u64;
type Index = Index;
type BlockNumber = BlockNumber;
type Hash = H256;
type Hashing = BlakeTwo256;
type AccountId = u64;
type AccountId = AccountId;
type Lookup = IdentityLookup<Self::AccountId>;
type Header = Header;
type RuntimeEvent = RuntimeEvent;
type BlockHashCount = BlockHashCount;
type Version = ();
type PalletInfo = PalletInfo;
type AccountData = pallet_balances::AccountData<u64>;
type AccountData = pallet_balances::AccountData<Balance>;
type OnNewAccount = ();
type OnKilledAccount = ();
type SystemWeightInfo = ();
Expand All @@ -87,7 +96,7 @@ parameter_types! {
}

impl pallet_balances::Config for Test {
type Balance = u64;
type Balance = Balance;
type RuntimeEvent = RuntimeEvent;
type DustRemoval = ();
type ExistentialDeposit = ExistentialDeposit;
Expand Down Expand Up @@ -116,11 +125,12 @@ impl pallet_authorship::Config for Test {
}

parameter_types! {
pub const MinimumPeriod: u64 = 1;
// NOTE: updated to match kintsugi runtime
pub const MinimumPeriod: u64 = 12000 / 2;
}

impl pallet_timestamp::Config for Test {
type Moment = u64;
type Moment = Moment;
type OnTimestampSet = Aura;
type MinimumPeriod = MinimumPeriod;
type WeightInfo = ();
Expand All @@ -147,7 +157,7 @@ impl From<UintAuthorityId> for MockSessionKeys {

parameter_types! {
pub static SessionHandlerCollators: Vec<u64> = Vec::new();
pub static SessionChangeBlock: u64 = 0;
pub static SessionChangeBlock: BlockNumber = 0;
}

pub struct TestSessionHandler;
Expand All @@ -166,8 +176,8 @@ impl pallet_session::SessionHandler<u64> for TestSessionHandler {
}

parameter_types! {
pub const Offset: u64 = 0;
pub const Period: u64 = 10;
pub const Offset: BlockNumber = 0;
pub const Period: BlockNumber = 10;
}

impl pallet_session::Config for Test {
Expand Down Expand Up @@ -256,9 +266,9 @@ pub fn new_test_ext() -> sp_io::TestExternalities {
t.into()
}

pub fn initialize_to_block(n: u64) {
pub fn initialize_to_block(n: BlockNumber) {
for i in System::block_number() + 1..=n {
System::set_block_number(i);
<AllPalletsWithSystem as frame_support::traits::OnInitialize<u64>>::on_initialize(i);
<AllPalletsWithSystem as frame_support::traits::OnInitialize<BlockNumber>>::on_initialize(i);
}
}
26 changes: 26 additions & 0 deletions crates/collator-selection/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,3 +435,29 @@ fn cannot_set_genesis_value_twice() {
// collator selection must be initialized before session.
collator_selection.assimilate_storage(&mut t).unwrap();
}

#[test]
fn should_migrate_kintsugi_2022_12_01() {
new_test_ext().execute_with(|| {
System::set_block_number(1983994);
frame_support::migration::put_storage_value(
b"Aura",
b"CurrentSlot",
&[],
sp_consensus_aura::Slot::from(278314523),
);

// this will panic without migration
// assert_ok!(Timestamp::set(RuntimeOrigin::none(), 1669887138551));

CollatorSelection::on_initialize(1983994);

let current_slot =
frame_support::migration::get_storage_value::<sp_consensus_aura::Slot>(b"Aura", b"CurrentSlot", &[])
.unwrap();
assert_eq!(Into::<u64>::into(current_slot), 139157261);

// this will now succeed
assert_ok!(Timestamp::set(RuntimeOrigin::none(), 1669887138551));
});
}
15 changes: 4 additions & 11 deletions parachain/src/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -237,11 +237,8 @@ where
create_inherent_data_providers: move |parent: sp_core::H256, _| {
let client_clone = client_clone.clone();
async move {
let slot_ms = match client_clone.clone().runtime_version_at(&BlockId::Hash(parent.clone())) {
Ok(x) if x.spec_name.starts_with("kintsugi") => 6000,
_ => 12000,
};
let slot_duration = sp_consensus_aura::SlotDuration::from_millis(slot_ms);
// let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;
let slot_duration = sp_consensus_aura::SlotDuration::from_millis(12000);

let timestamp = sp_timestamp::InherentDataProvider::from_system_time();

Expand Down Expand Up @@ -471,11 +468,6 @@ where
RuntimeApi::RuntimeApi: sp_consensus_aura::AuraApi<Block, AuraId>,
Executor: sc_executor::NativeExecutionDispatch + 'static,
{
let slot_ms = if parachain_config.chain_spec.id() == "kusama" {
6000
} else {
12000
};
start_node_impl(
parachain_config,
polkadot_config,
Expand All @@ -491,7 +483,8 @@ where
sync_oracle,
keystore,
force_authoring| {
let slot_duration = sp_consensus_aura::SlotDuration::from_millis(slot_ms);
// let slot_duration = cumulus_client_consensus_aura::slot_duration(&*client)?;
let slot_duration = sp_consensus_aura::SlotDuration::from_millis(12000);

let proposer_factory = sc_basic_authorship::ProposerFactory::with_proof_recording(
task_manager.spawn_handle(),
Expand Down

0 comments on commit a447c97

Please sign in to comment.