Skip to content

Commit

Permalink
Remove executed migrations (#175)
Browse files Browse the repository at this point in the history
* Remove executed migrations

* Tweaks
  • Loading branch information
olegnn authored May 16, 2024
1 parent 1dab350 commit a1ee3dc
Show file tree
Hide file tree
Showing 12 changed files with 21 additions and 617 deletions.
30 changes: 1 addition & 29 deletions pallets/core/src/common/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::util::btree_set;

use crate::{
common::{AuthorizeTarget, ForSigType, Signature},
did::{self, Did, DidKey, DidMethodKey, DidOrDidMethodKey, DidOrDidMethodKeySignature},
did::{self, DidKey, DidMethodKey, DidOrDidMethodKey, DidOrDidMethodKeySignature},
util::{
Action, ActionExecutionError, ActionWithNonce, NonceError, StorageRef, Types, WithNonce,
},
Expand Down Expand Up @@ -338,31 +338,3 @@ pub trait HasPolicy<T: Limits>: Sized {
rec_update(action, this_opt, f, &mut proof.into_iter())
}
}

/// Authorization logic containing rules to modify some data entity.
#[derive(
Encode, Decode, CloneNoBound, PartialEqNoBound, EqNoBound, DebugNoBound, MaxEncodedLen,
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(
feature = "serde",
serde(bound(serialize = "T: Sized", deserialize = "T: Sized"))
)]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
pub enum OldPolicy<T: Limits> {
/// Set of `DID`s allowed to modify the entity.
OneOf(
#[cfg_attr(feature = "serde", serde(with = "btree_set"))]
BoundedBTreeSet<Did, T::MaxPolicyControllers>,
),
}

impl<T: Limits> From<OldPolicy<T>> for Policy<T> {
fn from(old_policy: OldPolicy<T>) -> Self {
match old_policy {
OldPolicy::OneOf(set) => {
Self::OneOf(set.into_iter().map(Into::into).try_collect().unwrap())
}
}
}
}
261 changes: 2 additions & 259 deletions pallets/core/src/modules/accumulator/mod.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,14 @@
use crate::{
common::{self, signatures::ForSigType, CurveType},
did,
did::{Did, DidOrDidMethodKeySignature},
did::DidOrDidMethodKeySignature,
util::{ActionWithNonce, ActionWrapper, Bytes, IncId},
};
pub use actions::*;
use codec::{Decode, Encode, MaxEncodedLen};
use frame_support::{
dispatch::{DispatchResult, Weight},
ensure, storage_alias,
traits::Get,
ensure,
};
use sp_std::{fmt::Debug, prelude::*};
use utils::CheckedDivCeil;
Expand Down Expand Up @@ -243,262 +242,6 @@ pub mod pallet {
.execute_removable(Self::remove_accumulator_)
}
}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_runtime_upgrade() -> Weight {
migration::migrate::<T>()
}
}
}

mod migration {
use super::*;
use crate::{
common::{Limits, TypesAndLimits},
util::*,
};
use frame_support::pallet_prelude::*;

#[storage_alias]
pub type AccumulatorOwnerCounters<T: Config> =
StorageMap<Pallet<T>, Blake2_128Concat, Did, StoredAccumulatorOwnerCounters, ValueQuery>;

#[storage_alias]
pub type AccumulatorParams<T: Config> = StorageDoubleMap<
Pallet<T>,
Blake2_128Concat,
Did,
Identity,
IncId,
AccumulatorParameters<T>,
>;

#[derive(
scale_info_derive::TypeInfo,
Encode,
Decode,
CloneNoBound,
PartialEqNoBound,
EqNoBound,
DebugNoBound,
MaxEncodedLen,
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[cfg_attr(
feature = "serde",
serde(bound(serialize = "T: Sized", deserialize = "T: Sized"))
)]
#[scale_info(skip_type_params(T))]
pub struct AccumulatorPublicKey<T: Limits> {
pub curve_type: CurveType,
pub bytes: BoundedBytes<T::MaxAccumulatorPublicKeySize>,
/// The params used to generate the public key (`P_tilde` comes from params)
pub params_ref: Option<(Did, IncId)>,
}

#[storage_alias]
pub type AccumulatorKeys<T: Config> = StorageDoubleMap<
Pallet<T>,
Blake2_128Concat,
Did,
Identity,
IncId,
AccumulatorPublicKey<T>,
>;

pub fn migrate<T: Config>() -> Weight {
let mut reads_writes = 0;

let counters: Vec<_> = {
AccumulatorOwnerCounters::<T>::drain()
.map(|(did, counters): (Did, _)| (AccumulatorOwner(did.into()), counters))
.collect()
};

reads_writes += counters.len() as u64;
frame_support::log::info!("Migrated {} accumulator counters", counters.len());
for (did, counters) in counters {
super::pallet::AccumulatorOwnerCounters::<T>::insert(did, counters);
}

let params: Vec<_> = {
AccumulatorParams::<T>::drain()
.map(|(did, id, params): (Did, _, _)| (AccumulatorOwner(did.into()), id, params))
.collect()
};

reads_writes += params.len() as u64;
frame_support::log::info!("Migrated {} accumulator params", params.len());
for (did, id, params) in params {
super::pallet::AccumulatorParams::<T>::insert(did, id, params);
}

let keys: Vec<_> = {
AccumulatorKeys::<T>::drain()
.map(|(did, id, key): (Did, _, _)| {
(
AccumulatorOwner(did.into()),
id,
super::AccumulatorPublicKey {
curve_type: key.curve_type,
bytes: key.bytes,
params_ref: key
.params_ref
.map(|(did, key_id)| (AccumulatorOwner(did.into()), key_id.into())),
},
)
})
.collect()
};

reads_writes += keys.len() as u64;
frame_support::log::info!("Migrated {} accumulator keys", keys.len());
for (did, id, params) in keys {
super::pallet::AccumulatorKeys::<T>::insert(did, id, params);
}

#[derive(
Encode,
Decode,
scale_info_derive::TypeInfo,
CloneNoBound,
PartialEqNoBound,
EqNoBound,
DebugNoBound,
MaxEncodedLen,
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[cfg_attr(
feature = "serde",
serde(bound(serialize = "T: Sized", deserialize = "T: Sized"))
)]
#[scale_info(skip_type_params(T))]
pub struct AccumulatorCommon<T: Limits> {
pub accumulated: BoundedBytes<T::MaxAccumulatorAccumulatedSize>,
pub key_ref: (Did, IncId),
}

impl<T: Limits> From<AccumulatorCommon<T>> for super::AccumulatorCommon<T> {
fn from(
AccumulatorCommon {
accumulated,
key_ref: (did, key_id),
}: AccumulatorCommon<T>,
) -> Self {
super::AccumulatorCommon {
accumulated,
key_ref: (AccumulatorOwner(did.into()), key_id),
}
}
}

impl<T: Limits> From<UniversalAccumulator<T>> for super::UniversalAccumulator<T> {
fn from(UniversalAccumulator { common, max_size }: UniversalAccumulator<T>) -> Self {
super::UniversalAccumulator {
common: common.into(),
max_size,
}
}
}

#[derive(
Encode,
Decode,
scale_info_derive::TypeInfo,
CloneNoBound,
PartialEqNoBound,
EqNoBound,
DebugNoBound,
MaxEncodedLen,
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[cfg_attr(
feature = "serde",
serde(bound(serialize = "T: Sized", deserialize = "T: Sized"))
)]
#[scale_info(skip_type_params(T))]
pub struct UniversalAccumulator<T: Limits> {
pub common: AccumulatorCommon<T>,
/// This is not enforced on chain and serves as metadata only
pub max_size: u64,
}

#[derive(
Encode,
Decode,
scale_info_derive::TypeInfo,
Clone,
PartialEq,
Eq,
DebugNoBound,
MaxEncodedLen,
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[cfg_attr(
feature = "serde",
serde(bound(serialize = "T: Sized", deserialize = "T: Sized"))
)]
#[scale_info(skip_type_params(T))]
pub enum Accumulator<T: Limits> {
Positive(AccumulatorCommon<T>),
Universal(UniversalAccumulator<T>),
}

impl<T: Limits> From<Accumulator<T>> for super::Accumulator<T> {
fn from(acc: Accumulator<T>) -> Self {
match acc {
Accumulator::Positive(acc) => super::Accumulator::Positive(acc.into()),
Accumulator::Universal(acc) => super::Accumulator::Universal(acc.into()),
}
}
}

#[derive(
scale_info_derive::TypeInfo, Encode, Decode, Clone, PartialEq, Eq, Debug, MaxEncodedLen,
)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[cfg_attr(feature = "serde", serde(rename_all = "camelCase"))]
#[cfg_attr(
feature = "serde",
serde(bound(serialize = "T: Sized", deserialize = "T: Sized"))
)]
#[scale_info(skip_type_params(T))]
pub struct AccumulatorWithUpdateInfo<T>
where
T: TypesAndLimits,
{
pub created_at: T::BlockNumber,
pub last_updated_at: T::BlockNumber,
pub accumulator: Accumulator<T>,
}

let mut accs = 0;
Accumulators::<T>::translate_values(
|AccumulatorWithUpdateInfo {
created_at,
last_updated_at,
accumulator,
}| {
accs += 1;

Some(super::AccumulatorWithUpdateInfo {
created_at,
last_updated_at,
accumulator: accumulator.into(),
})
},
);

frame_support::log::info!("Migrated {} accumulators", accs);
reads_writes += accs;

T::DbWeight::get().reads_writes(reads_writes, reads_writes)
}
}

impl<T: Config> SubstrateWeight<T> {
Expand Down
27 changes: 0 additions & 27 deletions pallets/core/src/modules/attest/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,33 +187,6 @@ pub mod pallet {
Ok(())
}
}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_runtime_upgrade() -> Weight {
use did::Did;
use frame_support::storage_alias;

let mut reads_writes = 0;

let attestations: Vec<_> = {
#[storage_alias]
pub type Attestations<T: Config> =
StorageMap<Pallet<T>, Blake2_128Concat, Did, Attestation<T>, ValueQuery>;

Attestations::<T>::drain()
.map(|(did, attest): (Did, _)| (Attester(did.into()), attest))
.collect()
};

reads_writes += attestations.len() as u64;
for (did, attest) in attestations {
Attestations::<T>::insert(did, attest);
}

T::DbWeight::get().reads_writes(reads_writes, reads_writes)
}
}
}

impl<T: Config> SubstrateWeight<T> {
Expand Down
19 changes: 1 addition & 18 deletions pallets/core/src/modules/blob/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use crate::{
common::{signatures::ForSigType, AuthorizeTarget, Types},
did::{self, Did, DidKey, DidMethodKey, DidOrDidMethodKey, DidOrDidMethodKeySignature},
did::{self, DidKey, DidMethodKey, DidOrDidMethodKey, DidOrDidMethodKeySignature},
util::{ActionWithNonce, BoundedBytes, Bytes},
};
use codec::{Decode, Encode, MaxEncodedLen};
Expand Down Expand Up @@ -136,23 +136,6 @@ pub mod pallet {
Ok(())
}
}

#[pallet::hooks]
impl<T: Config> Hooks<BlockNumberFor<T>> for Pallet<T> {
fn on_runtime_upgrade() -> Weight {
let mut reads_writes = 0;

Blobs::<T>::translate_values(|(did, blob): (Did, BoundedBytes<T::MaxBlobSize>)| {
reads_writes += 1;

Some((BlobOwner(did.into()), blob))
});

frame_support::log::info!("Translated {} blobs", reads_writes);

T::DbWeight::get().reads_writes(reads_writes, reads_writes)
}
}
}

impl<T: Config> SubstrateWeight<T> {
Expand Down
Loading

0 comments on commit a1ee3dc

Please sign in to comment.