From 07eff0a02de680b476ef24ad9929e3052bc9417f Mon Sep 17 00:00:00 2001 From: Honza Date: Fri, 26 Jun 2026 19:08:31 +0200 Subject: [PATCH] Migrate allocator role to sudo key --- CHANGELOG.md | 13 ++++ docs/changes/spec-29.md | 25 +++++++ pallets/governance/src/lib.rs | 2 +- runtime/src/lib.rs | 5 +- runtime/src/migrations.rs | 118 ++++++++++++++++++++++++++++++++++ 5 files changed, 160 insertions(+), 3 deletions(-) create mode 100644 docs/changes/spec-29.md create mode 100644 runtime/src/migrations.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 7108e03..4a4dd24 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,18 @@ # Changelog +## Spec 29 + +This release migrates the allocator role to the current sudo key. + +### Allocator Migration + +- Runtime `spec_version` is bumped from `28` to `29`. +- Governance storage version is bumped from `5` to `6`. +- The migration reads the current sudo key and makes it the only governance allocator. +- The sudo allocator is whitelisted, funded with the current registration burn from the DAO treasury, and registered through the normal `torus0` registration path with the fixed name `new_allocator`. +- Existing stake to prior allocators is moved to the sudo allocator before prior allocator agents are deregistered; prior allocator outbound stake is unstaked back to free balance first. +- Prior allocator agents are deregistered through the `torus0` deregistration path after their inbound stake has moved. + ## Spec 27 This release introduces an explicit admin-controlled authority rotation path for the solo-chain runtime, replacing ad hoc emergency storage mutation workflows with a dedicated runtime extrinsic. diff --git a/docs/changes/spec-29.md b/docs/changes/spec-29.md new file mode 100644 index 0000000..b939cec --- /dev/null +++ b/docs/changes/spec-29.md @@ -0,0 +1,25 @@ +# Spec 29 Changelog + +This release migrates the allocator role to the current sudo key. + +## 1. Extrinsics + +No extrinsics were added, changed, or removed. + +## 2. Events + +No events were added, changed, or removed. + +## 3. Storage Items + +No storage items were added, changed, or removed. + +## 4. Runtime Migration + +- Runtime `spec_version` is bumped from `28` to `29`. +- `governance` storage version is bumped from `5` to `6`. +- The migration reads the current sudo key and makes it the only governance allocator. +- The governance allocator set is replaced with the sudo key. +- The sudo allocator is whitelisted, funded with the current registration burn from the DAO treasury, and registered through the normal torus0 registration path with the fixed name `new_allocator`. +- Stake to prior allocators is moved to the sudo allocator before prior allocator agents are deregistered; prior allocator outbound stake is unstaked back to free balance first. +- Prior allocator agents are deregistered through the torus0 deregistration path after their inbound stake has moved. diff --git a/pallets/governance/src/lib.rs b/pallets/governance/src/lib.rs index e9948e5..e4bc7d1 100644 --- a/pallets/governance/src/lib.rs +++ b/pallets/governance/src/lib.rs @@ -41,7 +41,7 @@ use crate::{ pub mod pallet { #![allow(clippy::too_many_arguments)] - const STORAGE_VERSION: StorageVersion = StorageVersion::new(5); + const STORAGE_VERSION: StorageVersion = StorageVersion::new(6); use pallet_permission0_api::{CuratorPermissions, Permission0Api, Permission0CuratorApi}; use polkadot_sdk::sp_core::ConstBool; diff --git a/runtime/src/lib.rs b/runtime/src/lib.rs index fb0e215..a0e5ab8 100644 --- a/runtime/src/lib.rs +++ b/runtime/src/lib.rs @@ -22,6 +22,7 @@ use sp_version::RuntimeVersion; pub mod apis; pub mod benchmarks; pub mod configs; +pub mod migrations; pub mod precompiles; impl_opaque_keys! { @@ -37,7 +38,7 @@ pub const VERSION: RuntimeVersion = RuntimeVersion { spec_name: create_runtime_str!("torus-runtime"), impl_name: create_runtime_str!("torus-runtime"), authoring_version: 1, - spec_version: 28, + spec_version: 29, impl_version: 1, apis: apis::RUNTIME_API_VERSIONS, transaction_version: 1, @@ -82,7 +83,7 @@ pub type SignedPayload = sp_runtime::generic::SignedPayload::AccountId; +type Agent = pallet_torus0::agent::Agent; + +#[storage_alias(pallet_name)] +type Key = StorageValue, AccountId, OptionQuery>; + +/// Moves the allocator role to the current sudo key. +pub struct MigrateAllocator; + +impl OnRuntimeUpgrade for MigrateAllocator { + fn on_runtime_upgrade() -> Weight { + if StorageVersion::get::>() == StorageVersion::new(5) { + if let Some(new_allocator) = sudo_key() { + migrate_allocator(new_allocator); + } + + StorageVersion::new(6).put::>(); + } + + Weight::zero() + } +} + +fn sudo_key() -> Option { + Key::get() +} + +fn migrate_allocator(new_allocator: AccountId) { + let old_allocators = pallet_governance::Allocators::::iter_keys() + .filter(|allocator| allocator != &new_allocator) + .collect::>(); + + let old_agent = old_allocators + .iter() + .find_map(pallet_torus0::Agents::::get); + + pallet_governance::Whitelist::::insert(&new_allocator, ()); + replace_allocators(&new_allocator); + register_allocator(&new_allocator, old_agent.as_ref()); + transfer_allocator_stake(&old_allocators, &new_allocator); + + for old_allocator in old_allocators { + let _ = pallet_torus0::agent::deregister::(old_allocator); + } +} + +fn register_allocator(new_allocator: &AccountId, old_agent: Option<&Agent>) { + if pallet_torus0::Agents::::contains_key(new_allocator) { + return; + } + + if let Some(old_agent) = old_agent { + fund_registration_burn(new_allocator); + let _ = pallet_torus0::agent::register::( + new_allocator.clone(), + b"new_allocator".to_vec(), + old_agent.url.to_vec(), + old_agent.metadata.to_vec(), + ); + } +} + +fn replace_allocators(new_allocator: &AccountId) { + let _ = pallet_governance::Allocators::::clear(u32::MAX, None); + pallet_governance::Allocators::::insert(new_allocator, ()); +} + +fn fund_registration_burn(new_allocator: &AccountId) { + let _ = ::Currency::transfer( + &pallet_governance::DaoTreasuryAddress::::get(), + new_allocator, + pallet_torus0::Burn::::get(), + ExistenceRequirement::AllowDeath, + ); +} + +fn transfer_allocator_stake(old_allocators: &[AccountId], new_allocator: &AccountId) { + if !pallet_torus0::Agents::::contains_key(new_allocator) { + return; + } + + let allocator_stakes = pallet_torus0::StakingTo::::iter() + .filter(|(staker, _, _)| old_allocators.contains(staker)) + .collect::>(); + + for (old_allocator, staked, amount) in allocator_stakes { + let _ = pallet_torus0::stake::remove_stake::(old_allocator, staked, amount); + } + + let stakes = pallet_torus0::StakingTo::::iter() + .filter(|(staker, staked, _)| { + old_allocators.contains(staked) && !old_allocators.contains(staker) + }) + .collect::>(); + + for (staker, old_allocator, amount) in stakes { + let _ = pallet_torus0::stake::transfer_stake::( + staker, + old_allocator, + new_allocator.clone(), + amount, + ); + } +}