Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Test migration with oldidgraph #2887

Draft
wants to merge 2 commits into
base: p-710-include-optional-network-parameter-to-vc-request
Choose a base branch
from
Draft
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
10 changes: 8 additions & 2 deletions tee-worker/app-libs/sgx-runtime/src/migration.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,17 @@ use super::*;
use crate::Runtime;
use frame_support::{traits::OnRuntimeUpgrade, weights::Weight};

// This is a place we triggre migrations
// This is a place we trigger migrations
// For more details, see: https://www.notion.so/web3builders/How-to-upgrade-enclave-one-worker-edfaf5871b4441579f9471074032ed1e
pub struct Upgrade;
impl OnRuntimeUpgrade for Upgrade {
fn on_runtime_upgrade() -> Weight {
pallet_imt::migrations::migrate_to_v1::<Runtime, IdentityManagement>()
[
pallet_imt::migrations::migrate_to_v1::<Runtime, IdentityManagement>(),
// pallet_imt::migrations::migrate_to_v2::<Runtime, IdentityManagement>(),
pallet_imt::migrations::migrate_to_v3::<Runtime, IdentityManagement>(),
]
.iter()
.fold(Weight::zero(), |lhs, rhs| lhs.saturating_add(*rhs))
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,13 @@ pub enum IdentityStatus {
pub struct IdentityContext<T: Config> {
// the sidechain block number at which the identity is linked
pub link_block: BlockNumberOf<T>,
// a list of web3 networks on which the identity should be used
pub web3networks: Vec<Web3Network>,
// the identity status
pub status: IdentityStatus,
}

impl<T: Config> IdentityContext<T> {
pub fn new(link_block: BlockNumberOf<T>) -> Self {
Self { link_block, status: IdentityStatus::Active, web3networks: Vec::new() }
Self { link_block, status: IdentityStatus::Active }
}

pub fn deactivate(&mut self) {
Expand Down
120 changes: 89 additions & 31 deletions tee-worker/litentry/pallets/identity-management/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
// You should have received a copy of the GNU General Public License
// along with Litentry. If not, see <https://www.gnu.org/licenses/>.

use crate::{IDGraphs, IdentityContext};
use frame_support::{
traits::{GetStorageVersion, PalletInfoAccess, StorageVersion},
weights::Weight,
Expand All @@ -22,42 +23,99 @@ use frame_support::{
// This is just an example of how to write a custom migration
pub fn migrate_to_v1<T: crate::Config, P: GetStorageVersion + PalletInfoAccess>() -> Weight {
let on_chain_storage_version = <P as GetStorageVersion>::on_chain_storage_version();
if on_chain_storage_version < 1 {
log::info!("Doing migrations now for IMT version {:?}", on_chain_storage_version);
StorageVersion::new(1).put::<P>();
if on_chain_storage_version != 0 {
return Weight::zero()
}
Weight::zero()
log::info!("Doing migrations now for IMT version {:?}", on_chain_storage_version);
StorageVersion::new(1).put::<P>();
Weight::from_all(1)
}

pub fn migrate_to_v2<T: crate::Config, P: GetStorageVersion + PalletInfoAccess>() -> Weight {
let on_chain_storage_version = <P as GetStorageVersion>::on_chain_storage_version();
if on_chain_storage_version < 2 {
log::info!("Doing migrations now for IMT version {:?}", on_chain_storage_version);
StorageVersion::new(2).put::<P>();

// '//Charlie' -> 0x90b5ab205c6974c9ea841be688864633dc9ca8a357843eeacf2314649965fe22
let mut charlie = [0; 32];
hex::decode_to_slice(
"90b5ab205c6974c9ea841be688864633dc9ca8a357843eeacf2314649965fe22",
&mut charlie,
)
.unwrap();

// Update after P-174:
// the following code snippet only serves as an example based on the old codebase, it doesn't
// work now as we don't have the storage item anymore, thus commented out.
//
// set Charlie's shielding key to [2u8; 32]
// verifiable via:
// ```
// ./bin/litentry-cli trusted --mrenclave $mrenclave get-storage IdentityManagement UserShieldingKeys 90b5ab205c6974c9ea841be688864633dc9ca8a357843eeacf2314649965fe22
// ```
// migration::put_storage_value::<RequestAesKey>(
// "IdentityManagement".as_bytes(),
// "UserShieldingKeys".as_bytes(),
// &charlie.blake2_128_concat(),
// [2u8; 32],
// );
if on_chain_storage_version != 1 {
return Weight::zero()
}
log::info!("Doing migrations now for IMT version {:?}", on_chain_storage_version);
StorageVersion::new(2).put::<P>();

// '//Charlie' -> 0x90b5ab205c6974c9ea841be688864633dc9ca8a357843eeacf2314649965fe22
let mut charlie = [0; 32];
hex::decode_to_slice(
"90b5ab205c6974c9ea841be688864633dc9ca8a357843eeacf2314649965fe22",
&mut charlie,
)
.unwrap();

// Update after P-174:
// the following code snippet only serves as an example based on the old codebase, it doesn't
// work now as we don't have the storage item anymore, thus commented out.
//
// set Charlie's shielding key to [2u8; 32]
// verifiable via:
// ```
// ./bin/litentry-cli trusted --mrenclave $mrenclave get-storage IdentityManagement UserShieldingKeys 90b5ab205c6974c9ea841be688864633dc9ca8a357843eeacf2314649965fe22
// ```
// migration::put_storage_value::<RequestAesKey>(
// "IdentityManagement".as_bytes(),
// "UserShieldingKeys".as_bytes(),
// &charlie.blake2_128_concat(),
// [2u8; 32],
// );
Weight::zero()
}

mod v2 {
use crate::{BlockNumberOf, Config, IdentityStatus, Pallet, Web3Network};
use codec::{Decode, Encode};
use frame_support::{pallet_prelude::OptionQuery, storage_alias, Blake2_128Concat};
use litentry_primitives::Identity;
use scale_info::TypeInfo;
use sp_std::vec::Vec;

// The context associated with the (litentry-account, did) pair
#[derive(Clone, Eq, PartialEq, Debug, Encode, Decode, TypeInfo)]
#[scale_info(skip_type_params(T))]
#[codec(mel_bound())]
pub struct IdentityContext<T: Config> {
// the sidechain block number at which the identity is linked
pub link_block: BlockNumberOf<T>,
// a list of web3 networks on which the identity should be used
pub web3networks: Vec<Web3Network>,
// the identity status
pub status: IdentityStatus,
}

#[storage_alias]
pub type IDGraphs<T: Config> = StorageDoubleMap<
Pallet<T>,
Blake2_128Concat,
Identity,
Blake2_128Concat,
Identity,
IdentityContext<T>,
OptionQuery,
>;
}

pub fn migrate_to_v3<T: crate::Config, P: GetStorageVersion + PalletInfoAccess>() -> Weight {
let on_chain_storage_version = <P as GetStorageVersion>::on_chain_storage_version();
if on_chain_storage_version != 2 {
return Weight::zero()
}

log::info!("Doing migrations now for IMT version {:?}", on_chain_storage_version);
StorageVersion::new(2).put::<P>();

let mut count = 0;
for (who, identity, context) in v2::IDGraphs::<T>::drain() {
IDGraphs::<T>::insert(
&who,
&identity,
IdentityContext { link_block: context.link_block, status: context.status },
);
count += 1;
}

Weight::from_all(count + 1)
}
26 changes: 9 additions & 17 deletions tee-worker/litentry/pallets/identity-management/src/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ fn link_twitter_identity_works() {
));
assert_eq!(
IMT::id_graphs(who.clone(), alice_twitter_identity(1)).unwrap(),
IdentityContext { link_block: 1, status: IdentityStatus::Active, web3networks: vec![] }
IdentityContext { link_block: 1, status: IdentityStatus::Active }
);
assert_eq!(crate::IDGraphLens::<Test>::get(&who), 2);
});
Expand All @@ -84,7 +84,7 @@ fn link_substrate_identity_works() {
));
assert_eq!(
IMT::id_graphs(who.clone(), alice_substrate_identity()).unwrap(),
IdentityContext { link_block: 1, status: IdentityStatus::Active, web3networks: vec![] }
IdentityContext { link_block: 1, status: IdentityStatus::Active }
);
assert_eq!(crate::IDGraphLens::<Test>::get(&who), 2);
});
Expand All @@ -101,7 +101,7 @@ fn link_evm_identity_works() {
));
assert_eq!(
IMT::id_graphs(who.clone(), alice_evm_identity()).unwrap(),
IdentityContext { link_block: 1, status: IdentityStatus::Active, web3networks: vec![] }
IdentityContext { link_block: 1, status: IdentityStatus::Active }
);
assert_eq!(crate::IDGraphLens::<Test>::get(&who), 2);
});
Expand All @@ -117,7 +117,7 @@ fn link_identity_fails_for_linked_identity() {
assert_ok!(IMT::link_identity(RuntimeOrigin::signed(ALICE), bob.clone(), alice.clone(),));
assert_eq!(
IMT::id_graphs(bob.clone(), alice.clone()).unwrap(),
IdentityContext { link_block: 1, status: IdentityStatus::Active, web3networks: vec![] }
IdentityContext { link_block: 1, status: IdentityStatus::Active }
);
assert_eq!(crate::IDGraphLens::<Test>::get(&bob), 2);

Expand Down Expand Up @@ -150,7 +150,7 @@ fn cannot_link_identity_again() {
));
assert_eq!(
IMT::id_graphs(who_bob.clone(), alice_substrate_identity()).unwrap(),
IdentityContext { link_block: 1, status: IdentityStatus::Active, web3networks: vec![] }
IdentityContext { link_block: 1, status: IdentityStatus::Active }
);
assert_eq!(crate::IDGraphLens::<Test>::get(&who_bob), 2);

Expand Down Expand Up @@ -213,7 +213,7 @@ fn deactivate_identity_works() {
));
assert_eq!(
IMT::id_graphs(who.clone(), alice_substrate_identity()).unwrap(),
IdentityContext { link_block: 1, status: IdentityStatus::Active, web3networks: vec![] }
IdentityContext { link_block: 1, status: IdentityStatus::Active }
);

let id_graph = IMT::id_graph(&who.clone());
Expand All @@ -227,11 +227,7 @@ fn deactivate_identity_works() {
));
assert_eq!(
IMT::id_graphs(who.clone(), alice_substrate_identity()).unwrap(),
IdentityContext {
link_block: 1,
status: IdentityStatus::Inactive,
web3networks: vec![]
}
IdentityContext { link_block: 1, status: IdentityStatus::Inactive }
);

let id_graph = IMT::id_graph(&who.clone())
Expand Down Expand Up @@ -264,7 +260,7 @@ fn activate_identity_works() {
));
assert_eq!(
IMT::id_graphs(who.clone(), alice_substrate_identity()).unwrap(),
IdentityContext { link_block: 1, status: IdentityStatus::Active, web3networks: vec![] }
IdentityContext { link_block: 1, status: IdentityStatus::Active }
);
let id_graph = IMT::id_graph(&who.clone());
assert_eq!(id_graph.len(), 2);
Expand All @@ -277,11 +273,7 @@ fn activate_identity_works() {
));
assert_eq!(
IMT::id_graphs(who.clone(), alice_substrate_identity()).unwrap(),
IdentityContext {
link_block: 1,
status: IdentityStatus::Inactive,
web3networks: vec![]
}
IdentityContext { link_block: 1, status: IdentityStatus::Inactive }
);
let id_graph = IMT::id_graph(&who.clone())
.into_iter()
Expand Down