Skip to content

Commit

Permalink
Added migrations to runtime and improved code
Browse files Browse the repository at this point in the history
  • Loading branch information
mqxf committed Dec 18, 2023
1 parent 2c38d38 commit ecb080e
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 43 deletions.
2 changes: 2 additions & 0 deletions core-processor/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
}
}
Expand Down
4 changes: 2 additions & 2 deletions core/src/message/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)>,
Expand Down Expand Up @@ -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>>,
Expand Down Expand Up @@ -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 {
Expand Down
91 changes: 51 additions & 40 deletions pallets/gear-messenger/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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();
Expand All @@ -63,40 +48,22 @@ 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))
},
);

Dispatches::<T>::translate(|_, store: LinkedNode<MessageId, v2::StoredDispatch>| {
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))
},
);

Expand Down Expand Up @@ -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,
)]
Expand All @@ -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> {
Expand Down Expand Up @@ -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};
Expand Down Expand Up @@ -310,7 +300,7 @@ mod test {
pid,
mid,
(
dispatch,
dispatch.clone(),
Interval {
start: 0,
finish: 1,
Expand All @@ -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,
Expand All @@ -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()
);
});
}
}
5 changes: 4 additions & 1 deletion runtime/vara/src/migrations.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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>,
);

0 comments on commit ecb080e

Please sign in to comment.