Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -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.
Expand Down
25 changes: 25 additions & 0 deletions docs/changes/spec-29.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion pallets/governance/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
5 changes: 3 additions & 2 deletions runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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! {
Expand All @@ -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,
Expand Down Expand Up @@ -82,7 +83,7 @@ pub type SignedPayload = sp_runtime::generic::SignedPayload<RuntimeCall, SignedE
/// All migrations of the runtime, aside from the ones declared in the pallets.
///
/// This can be a tuple of types, each implementing `OnRuntimeUpgrade`.
type Migrations = ();
type Migrations = (migrations::MigrateAllocator,);

/// Executive: handles dispatch to the various modules.
pub type RuntimeExecutive = frame_executive::Executive<
Expand Down
118 changes: 118 additions & 0 deletions runtime/src/migrations.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
use alloc::vec::Vec;

use polkadot_sdk::{
frame_support::{
pallet_prelude::*,
storage_alias,
traits::{Currency, ExistenceRequirement, OnRuntimeUpgrade, StorageVersion},
},
frame_system,
sp_weights::Weight,
};

use crate::Runtime;

type AccountId = <Runtime as frame_system::Config>::AccountId;
type Agent = pallet_torus0::agent::Agent<Runtime>;

#[storage_alias(pallet_name)]
type Key = StorageValue<polkadot_sdk::pallet_sudo::Pallet<Runtime>, 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::<pallet_governance::Pallet<Runtime>>() == StorageVersion::new(5) {
if let Some(new_allocator) = sudo_key() {
migrate_allocator(new_allocator);
}

StorageVersion::new(6).put::<pallet_governance::Pallet<Runtime>>();
}

Weight::zero()
}
}

fn sudo_key() -> Option<AccountId> {
Key::get()
}

fn migrate_allocator(new_allocator: AccountId) {
let old_allocators = pallet_governance::Allocators::<Runtime>::iter_keys()
.filter(|allocator| allocator != &new_allocator)
.collect::<Vec<_>>();

let old_agent = old_allocators
.iter()
.find_map(pallet_torus0::Agents::<Runtime>::get);

pallet_governance::Whitelist::<Runtime>::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::<Runtime>(old_allocator);
}
}

fn register_allocator(new_allocator: &AccountId, old_agent: Option<&Agent>) {
if pallet_torus0::Agents::<Runtime>::contains_key(new_allocator) {
return;
}

if let Some(old_agent) = old_agent {
fund_registration_burn(new_allocator);
let _ = pallet_torus0::agent::register::<Runtime>(
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::<Runtime>::clear(u32::MAX, None);
pallet_governance::Allocators::<Runtime>::insert(new_allocator, ());
}

fn fund_registration_burn(new_allocator: &AccountId) {
let _ = <Runtime as pallet_torus0::Config>::Currency::transfer(
&pallet_governance::DaoTreasuryAddress::<Runtime>::get(),
new_allocator,
pallet_torus0::Burn::<Runtime>::get(),
ExistenceRequirement::AllowDeath,
);
}

fn transfer_allocator_stake(old_allocators: &[AccountId], new_allocator: &AccountId) {
if !pallet_torus0::Agents::<Runtime>::contains_key(new_allocator) {
return;
}

let allocator_stakes = pallet_torus0::StakingTo::<Runtime>::iter()
.filter(|(staker, _, _)| old_allocators.contains(staker))
.collect::<Vec<_>>();

for (old_allocator, staked, amount) in allocator_stakes {
let _ = pallet_torus0::stake::remove_stake::<Runtime>(old_allocator, staked, amount);
}

let stakes = pallet_torus0::StakingTo::<Runtime>::iter()
.filter(|(staker, staked, _)| {
old_allocators.contains(staked) && !old_allocators.contains(staker)
})
.collect::<Vec<_>>();

for (staker, old_allocator, amount) in stakes {
let _ = pallet_torus0::stake::transfer_stake::<Runtime>(
staker,
old_allocator,
new_allocator.clone(),
amount,
);
}
}
Loading