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<OutgoingMessageInfoNoDelay<ReplyMessage>>, // u32 is delay awakening: Vec<(MessageId, u32)>, - awoken: BTreeSet<MessageId>, // u64 is gas limit // TODO: add Option<ReservationId> 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<u32, Option<Payload>>, @@ -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<T: Config>(PhantomData<T>); -impl<T: Config> MigrateToV3<T> { - fn migrate_context_store(ctx: v2::ContextStore) -> ContextStore { - ContextStore::new( - ctx.outgoing, - ctx.reply, - ctx.initialized, - ctx.reservation_nonce, - ctx.system_reservation, - ) - } -} - impl<T: Config> OnRuntimeUpgrade for MigrateToV3<T> { fn on_runtime_upgrade() -> Weight { let current = Pallet::<T>::current_storage_version(); @@ -63,14 +48,7 @@ impl<T: Config> OnRuntimeUpgrade for MigrateToV3<T> { Waitlist::<T>::translate( |_, _, store: (v2::StoredDispatch, Interval<T::BlockNumber>)| { 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<T: Config> OnRuntimeUpgrade for MigrateToV3<T> { 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::<T>::translate( |_, store: (v2::StoredDispatch, Interval<T::BlockNumber>)| { 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<ContextStore>, } + impl Into<gear_core::message::StoredDispatch> 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<u64>, } + impl Into<gear_core::message::ContextStore> 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<T: Config>(PhantomData<T>); impl<T: Config> StorageInstance for DispatchesPrefix<T> { @@ -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::<Test>::insert( mid, ( - dispatch3, + dispatch3.clone(), Interval { start: 0, finish: 1, @@ -342,6 +332,27 @@ mod test { MigrateToV3::<Test>::post_upgrade(state).unwrap(); assert_eq!(StorageVersion::get::<GearMessenger>(), 3); + + assert_eq!( + Waitlist::<Test>::get(pid, mid) + .expect("Waitlist failed to migrate.") + .0, + dispatch.into() + ); + + assert_eq!( + Dispatches::<Test>::get(mid) + .expect("Waitlist failed to migrate.") + .value, + dispatch2.into() + ); + + assert_eq!( + DispatchStash::<Test>::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<Runtime>,); +pub type Migrations = ( + pallet_gear_program::migrations::MigrateToV3<Runtime>, + pallet_gear_messenger::migrations::MigrateToV3<Runtime>, +);