Skip to content

Commit

Permalink
Misc stuff for accumulator migrations
Browse files Browse the repository at this point in the history
  • Loading branch information
olegnn committed Jan 22, 2024
1 parent 50a39d1 commit dd139b9
Showing 1 changed file with 179 additions and 1 deletion.
180 changes: 179 additions & 1 deletion pallets/core/src/modules/accumulator/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -272,6 +272,10 @@ pub mod pallet {

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

#[storage_alias]
Expand All @@ -288,6 +292,30 @@ mod migration {
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>,
Expand Down Expand Up @@ -327,7 +355,19 @@ mod migration {

let keys: Vec<_> = {
AccumulatorKeys::<T>::drain()
.map(|(did, id, key): (Did, _, _)| (AccumulatorOwner(did.into()), id, key))
.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()
};

Expand All @@ -337,6 +377,144 @@ mod migration {
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)
}
}
Expand Down

0 comments on commit dd139b9

Please sign in to comment.