From ecb080e5f792a2d0fd94f5de5bbbec7073eb6aa9 Mon Sep 17 00:00:00 2001 From: mqxf Date: Mon, 18 Dec 2023 13:53:30 +0100 Subject: [PATCH] Added migrations to runtime and improved code --- core-processor/src/common.rs | 2 + core/src/message/context.rs | 4 +- pallets/gear-messenger/src/migrations.rs | 91 +++++++++++++----------- runtime/vara/src/migrations.rs | 5 +- 4 files changed, 59 insertions(+), 43 deletions(-) diff --git a/core-processor/src/common.rs b/core-processor/src/common.rs index df96c83a92e..08a70010d6f 100644 --- a/core-processor/src/common.rs +++ b/core-processor/src/common.rs @@ -140,6 +140,8 @@ impl DispatchResult { system_reservation_context, page_update: Default::default(), allocations: Default::default(), + // This function is only used to generate a dispatch result if nothing is executed, + // therefore reply_sent will always be false reply_sent: false, } } diff --git a/core/src/message/context.rs b/core/src/message/context.rs index 159c38dfdb0..3832684a5a7 100644 --- a/core/src/message/context.rs +++ b/core/src/message/context.rs @@ -130,7 +130,6 @@ pub struct ContextOutcome { reply: Option>, // u32 is delay awakening: Vec<(MessageId, u32)>, - awoken: BTreeSet, // u64 is gas limit // TODO: add Option after #1828 reply_deposits: Vec<(MessageId, u64)>, @@ -192,6 +191,7 @@ pub struct ContextStore { } impl ContextStore { + //TODO: Remove, only used in migrations /// Create a new context store with the provided parameters. pub fn new( outgoing: BTreeMap>, @@ -498,7 +498,7 @@ impl MessageContext { /// Wake message by it's message id. pub fn wake(&mut self, waker_id: MessageId, delay: u32) -> Result<(), Error> { - if self.outcome.awoken.insert(waker_id) { + if self.outcome.awakening.iter().any(|v| v.0 == waker_id) { self.outcome.awakening.push((waker_id, delay)); Ok(()) } else { diff --git a/pallets/gear-messenger/src/migrations.rs b/pallets/gear-messenger/src/migrations.rs index 5229757ad05..8dfe07e1734 100644 --- a/pallets/gear-messenger/src/migrations.rs +++ b/pallets/gear-messenger/src/migrations.rs @@ -22,10 +22,7 @@ use frame_support::{ traits::{Get, GetStorageVersion, OnRuntimeUpgrade}, weights::Weight, }; -use gear_core::{ - ids::MessageId, - message::{ContextStore, StoredDispatch}, -}; +use gear_core::ids::MessageId; use sp_std::marker::PhantomData; #[cfg(feature = "try-runtime")] use { @@ -35,18 +32,6 @@ use { pub struct MigrateToV3(PhantomData); -impl MigrateToV3 { - fn migrate_context_store(ctx: v2::ContextStore) -> ContextStore { - ContextStore::new( - ctx.outgoing, - ctx.reply, - ctx.initialized, - ctx.reservation_nonce, - ctx.system_reservation, - ) - } -} - impl OnRuntimeUpgrade for MigrateToV3 { fn on_runtime_upgrade() -> Weight { let current = Pallet::::current_storage_version(); @@ -63,14 +48,7 @@ impl OnRuntimeUpgrade for MigrateToV3 { Waitlist::::translate( |_, _, store: (v2::StoredDispatch, Interval)| { weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 1)); - Some(( - StoredDispatch::new( - store.0.kind, - store.0.message, - store.0.context.map(Self::migrate_context_store), - ), - store.1, - )) + Some((store.0.into(), store.1)) }, ); @@ -78,25 +56,14 @@ impl OnRuntimeUpgrade for MigrateToV3 { weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 1)); Some(LinkedNode { next: store.next, - value: StoredDispatch::new( - store.value.kind, - store.value.message, - store.value.context.map(Self::migrate_context_store), - ), + value: store.value.into(), }) }); DispatchStash::::translate( |_, store: (v2::StoredDispatch, Interval)| { weight = weight.saturating_add(T::DbWeight::get().reads_writes(1, 1)); - Some(( - StoredDispatch::new( - store.0.kind, - store.0.message, - store.0.context.map(Self::migrate_context_store), - ), - store.1, - )) + Some((store.0.into(), store.1)) }, ); @@ -166,6 +133,16 @@ mod v2 { pub context: Option, } + impl Into for StoredDispatch { + fn into(self) -> gear_core::message::StoredDispatch { + gear_core::message::StoredDispatch::new( + self.kind, + self.message, + self.context.map(Into::into), + ) + } + } + #[derive( Clone, Default, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Decode, Encode, TypeInfo, )] @@ -179,6 +156,18 @@ mod v2 { pub system_reservation: Option, } + impl Into for ContextStore { + fn into(self) -> gear_core::message::ContextStore { + gear_core::message::ContextStore::new( + self.outgoing, + self.reply, + self.initialized, + self.reservation_nonce, + self.system_reservation, + ) + } + } + pub struct DispatchesPrefix(PhantomData); impl StorageInstance for DispatchesPrefix { @@ -248,6 +237,7 @@ mod test { use crate::{ migrations::{v2, MigrateToV3}, mock::*, + DispatchStash, Dispatches, Waitlist, }; use common::storage::{Interval, LinkedNode}; use frame_support::{pallet_prelude::StorageVersion, traits::OnRuntimeUpgrade}; @@ -310,7 +300,7 @@ mod test { pid, mid, ( - dispatch, + dispatch.clone(), Interval { start: 0, finish: 1, @@ -322,14 +312,14 @@ mod test { mid, LinkedNode { next: None, - value: dispatch2, + value: dispatch2.clone(), }, ); v2::DispatchStash::::insert( mid, ( - dispatch3, + dispatch3.clone(), Interval { start: 0, finish: 1, @@ -342,6 +332,27 @@ mod test { MigrateToV3::::post_upgrade(state).unwrap(); assert_eq!(StorageVersion::get::(), 3); + + assert_eq!( + Waitlist::::get(pid, mid) + .expect("Waitlist failed to migrate.") + .0, + dispatch.into() + ); + + assert_eq!( + Dispatches::::get(mid) + .expect("Waitlist failed to migrate.") + .value, + dispatch2.into() + ); + + assert_eq!( + DispatchStash::::get(mid) + .expect("Waitlist failed to migrate.") + .0, + dispatch3.into() + ); }); } } diff --git a/runtime/vara/src/migrations.rs b/runtime/vara/src/migrations.rs index 73efbb2158b..b9fea07df8c 100644 --- a/runtime/vara/src/migrations.rs +++ b/runtime/vara/src/migrations.rs @@ -19,4 +19,7 @@ #[allow(unused)] use crate::*; -pub type Migrations = (pallet_gear_program::migrations::MigrateToV3,); +pub type Migrations = ( + pallet_gear_program::migrations::MigrateToV3, + pallet_gear_messenger::migrations::MigrateToV3, +);