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

fix(core): Allow waking the same message between waits #3475

Merged
merged 61 commits into from
Feb 27, 2024
Merged
Show file tree
Hide file tree
Changes from 37 commits
Commits
Show all changes
61 commits
Select commit Hold shift + click to select a range
27c5e26
Remove field
mqxf Nov 7, 2023
7678468
Updated gsdk
mqxf Nov 7, 2023
99535d6
Replace Vec with Map
mqxf Nov 10, 2023
bea16af
Fmt
mqxf Nov 10, 2023
b53960f
Started making migrations
mqxf Nov 27, 2023
e4088a1
Added migrations
mqxf Nov 28, 2023
bb66f9f
Cargo fmt
mqxf Nov 28, 2023
0c621bc
Added pre and post checks for migrations
mqxf Nov 28, 2023
09d6ad3
Fixed import errors
mqxf Nov 28, 2023
aed712e
Cargo fmt
mqxf Nov 28, 2023
31bd346
Added DispatchStash migration
mqxf Nov 28, 2023
df85ef7
Removed reply_sent from ContextStore
mqxf Nov 28, 2023
f68e6f0
Updated version numbers
mqxf Nov 28, 2023
319c16f
Rename module
mqxf Nov 28, 2023
2c38d38
Requested changes
mqxf Dec 4, 2023
ecb080e
Added migrations to runtime and improved code
mqxf Dec 18, 2023
462a6d8
Trigger CI
mqxf Dec 18, 2023
13c801f
Replaced Into with From
mqxf Dec 18, 2023
d266683
Merge branch 'master' into ms/issue-2763
mqxf Dec 25, 2023
86d9aa8
I forgot to add a !
mqxf Dec 25, 2023
786e884
Merge branch 'master' into ms/issue-2763
mqxf Dec 25, 2023
2ed8f4a
Merge branch 'master' into ms/issue-2763
mqxf Dec 27, 2023
31b2e0e
Changed StoredDispatch in DispatchStash to StoredDelayedDispatch
mqxf Dec 27, 2023
c9d8128
Merge branch 'ms/issue-2763' of https://github.com/gear-tech/gear int…
mqxf Dec 27, 2023
a0b3c54
Fmt
mqxf Dec 27, 2023
3293ca4
Merge branch 'master' into ms/issue-2763
mqxf Dec 27, 2023
4a4e4c9
generate gsdk meta
mqxf Dec 27, 2023
2b8ad40
Merge branch 'ms/issue-2763' of https://github.com/gear-tech/gear int…
mqxf Dec 27, 2023
4c17364
Merge branch 'master' into ms/issue-2763
mqxf Jan 3, 2024
b958b6b
Merge branch 'master' into ms/issue-2763
mqxf Jan 9, 2024
9d1c184
Update T::BlockNumber -> BlockNumberFor<T>
mqxf Jan 9, 2024
8869613
.
mqxf Jan 9, 2024
c23c966
..
mqxf Jan 9, 2024
7d166c0
Add import
mqxf Jan 9, 2024
26b7d8f
Fmt
mqxf Jan 9, 2024
0bffdcf
Fix clippy errors
mqxf Jan 9, 2024
4503287
Fmt
mqxf Jan 9, 2024
cabd2d0
Merge branch 'master' into ms/issue-2763
mqxf Jan 17, 2024
a9af635
Fixed conflict issue
mqxf Jan 17, 2024
ed71c60
Merge branch 'master' into ms/issue-2763
mqxf Jan 18, 2024
6754fa4
Random tests
mqxf Jan 18, 2024
cd1b04b
Fix CI
mqxf Jan 18, 2024
1c61e11
Merge branch 'master' into ms/issue-2763
mqxf Jan 18, 2024
ae93137
Merge branch 'master' into ms/issue-2763
mqxf Jan 19, 2024
2d2ed03
Merge branch 'master' into ms/issue-2763
mqxf Jan 23, 2024
ed3290b
Merge branch 'master' into ms/issue-2763
mqxf Jan 26, 2024
bb52627
Update pallets/gear-messenger/src/migrations.rs
mqxf Feb 4, 2024
408797a
Update pallets/gear-messenger/src/migrations.rs
mqxf Feb 4, 2024
220a277
Update pallets/gear-messenger/src/migrations.rs
mqxf Feb 4, 2024
dc8edb8
Merge branch 'master' into ms/issue-2763
mqxf Feb 7, 2024
63713ec
Update core/src/message/context.rs
mqxf Feb 7, 2024
ebbe027
Update runtime/vara/src/migrations.rs
mqxf Feb 7, 2024
e99f5c0
Update core/src/message/stored.rs
mqxf Feb 7, 2024
a8d95f1
Fixed changes
mqxf Feb 7, 2024
3c74f5a
Added weights
mqxf Feb 8, 2024
f4e3f34
Merge branch 'master' into ms/issue-2763
mqxf Feb 8, 2024
a85dfe8
Merge branch 'master' into ms/issue-2763
mqxf Feb 16, 2024
2b3c166
Panic in pre upgrade
mqxf Feb 16, 2024
3e809b6
Add explanation to `StoredDelayedDispatch`
ark0f Feb 19, 2024
1cde774
Use deconstruction in for-loops
ark0f Feb 19, 2024
64b8506
Merge branch 'master' into ms/issue-2763
mqxf Feb 24, 2024
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
6 changes: 5 additions & 1 deletion common/src/storage/complex/messenger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,10 @@ pub trait Messenger {
///
/// Present to clarify compiler behavior over associated types.
type QueuedDispatch;
/// Stored values type for `Self::DispatchStash`.
///
/// Present to clarify compiler behavior over associated types.
type DelayedDispatch;
/// First key of the waitlist storage.
///
/// Present to clarify compiler behavior over associated types.
Expand Down Expand Up @@ -167,7 +171,7 @@ pub trait Messenger {

type DispatchStash: MapStorage<
Key = Self::DispatchStashKey,
Value = (Self::QueuedDispatch, Interval<Self::BlockNumber>),
Value = (Self::DelayedDispatch, Interval<Self::BlockNumber>),
>;

/// Resets all related to messenger storages.
Expand Down
5 changes: 5 additions & 0 deletions core-processor/src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,8 @@ pub struct DispatchResult {
pub page_update: BTreeMap<GearPage, PageBuf>,
/// New allocations set for program if it has been changed.
pub allocations: BTreeSet<WasmPage>,
/// Whether this execution sent out a reply.
pub reply_sent: bool,
}

impl DispatchResult {
Expand Down Expand Up @@ -138,6 +140,9 @@ 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,
mqxf marked this conversation as resolved.
Show resolved Hide resolved
}
}
}
Expand Down
1 change: 1 addition & 0 deletions core-processor/src/executor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,7 @@ where
system_reservation_context: info.system_reservation_context,
page_update,
allocations: info.allocations,
reply_sent: info.reply_sent,
})
}

Expand Down
3 changes: 3 additions & 0 deletions core-processor/src/ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,7 @@ pub struct ExtInfo {
pub program_candidates_data: BTreeMap<CodeId, Vec<(MessageId, ProgramId)>>,
pub program_rents: BTreeMap<ProgramId, u32>,
pub context_store: ContextStore,
pub reply_sent: bool,
}

/// Trait to which ext must have to work in processor wasm executor.
Expand Down Expand Up @@ -408,6 +409,7 @@ impl ProcessorExternalities for Ext {
outgoing_dispatches: generated_dispatches,
awakening,
reply_deposits,
reply_sent,
} = outcome.drain();

let system_reservation_context = SystemReservationContext {
Expand All @@ -434,6 +436,7 @@ impl ProcessorExternalities for Ext {
context_store,
program_candidates_data,
program_rents,
reply_sent,
};
Ok(info)
}
Expand Down
3 changes: 2 additions & 1 deletion core-processor/src/processing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -296,6 +296,7 @@ pub fn process_success(
context_store,
allocations,
reply_deposits,
reply_sent,
..
} = dispatch_result;

Expand Down Expand Up @@ -367,7 +368,7 @@ pub fn process_success(

// Sending auto-generated reply about success execution.
if matches!(kind, SuccessfulDispatchResultKind::Success)
&& !context_store.reply_sent()
&& !reply_sent
&& !dispatch.is_reply()
&& dispatch.kind() != DispatchKind::Signal
{
Expand Down
16 changes: 15 additions & 1 deletion core/src/message/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,10 @@

use crate::{
ids::{MessageId, ProgramId},
message::{DispatchKind, GasLimit, Payload, StoredDispatch, StoredMessage, Value},
message::{
DispatchKind, GasLimit, Payload, StoredDelayedDispatch, StoredDispatch, StoredMessage,
Value,
},
};
use alloc::string::ToString;
use core::{convert::TryInto, ops::Deref};
Expand Down Expand Up @@ -297,6 +300,12 @@ impl From<Dispatch> for StoredDispatch {
}
}

impl From<Dispatch> for StoredDelayedDispatch {
fn from(dispatch: Dispatch) -> StoredDelayedDispatch {
StoredDelayedDispatch::new(dispatch.kind, dispatch.message.into())
}
}

impl From<Dispatch> for (DispatchKind, Message) {
fn from(dispatch: Dispatch) -> (DispatchKind, Message) {
(dispatch.kind, dispatch.message)
Expand All @@ -314,6 +323,11 @@ impl Dispatch {
self.into()
}

/// Convert Dispatch into gasless StoredDelayedDispatch.
pub fn into_stored_delayed(self) -> StoredDelayedDispatch {
self.into()
}

/// Decompose Dispatch for it's components: DispatchKind and Message.
pub fn into_parts(self) -> (DispatchKind, Message) {
self.into()
Expand Down
35 changes: 24 additions & 11 deletions core/src/message/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,8 @@ pub struct ContextOutcomeDrain {
pub awakening: Vec<(MessageId, u32)>,
/// Reply deposits to be provided.
pub reply_deposits: Vec<(MessageId, u64)>,
/// Whether this execution sent out a reply.
pub reply_sent: bool,
}

/// Context outcome.
Expand Down Expand Up @@ -151,6 +153,7 @@ impl ContextOutcome {
/// Destructs outcome after execution and returns provided dispatches and awaken message ids.
pub fn drain(self) -> ContextOutcomeDrain {
let mut dispatches = Vec::new();
let reply_sent = self.reply.is_some();

for (msg, delay, reservation) in self.init.into_iter() {
dispatches.push((msg.into_dispatch(self.program_id), delay, reservation));
Expand All @@ -172,6 +175,7 @@ impl ContextOutcome {
outgoing_dispatches: dispatches,
awakening: self.awakening,
reply_deposits: self.reply_deposits,
reply_sent,
}
}
}
Expand All @@ -182,13 +186,29 @@ pub struct ContextStore {
outgoing: BTreeMap<u32, Option<Payload>>,
reply: Option<Payload>,
initialized: BTreeSet<ProgramId>,
awaken: BTreeSet<MessageId>,
mqxf marked this conversation as resolved.
Show resolved Hide resolved
reply_sent: bool,
reservation_nonce: ReservationNonce,
system_reservation: Option<u64>,
}

impl ContextStore {
//TODO: Remove, only used in migrations
mqxf marked this conversation as resolved.
Show resolved Hide resolved
/// Create a new context store with the provided parameters.
pub fn new(
mqxf marked this conversation as resolved.
Show resolved Hide resolved
outgoing: BTreeMap<u32, Option<Payload>>,
reply: Option<Payload>,
initialized: BTreeSet<ProgramId>,
reservation_nonce: ReservationNonce,
system_reservation: Option<u64>,
) -> Self {
Self {
outgoing,
reply,
initialized,
reservation_nonce,
system_reservation,
}
}

/// Returns stored within message context reservation nonce.
///
/// Will be non zero, if any reservations were created during
Expand Down Expand Up @@ -216,11 +236,6 @@ impl ContextStore {
pub fn system_reservation(&self) -> Option<u64> {
self.system_reservation
}

/// Get info about was reply sent.
pub fn reply_sent(&self) -> bool {
self.reply_sent
}
}

/// Context of currently processing incoming message.
Expand Down Expand Up @@ -266,7 +281,7 @@ impl MessageContext {

/// Return bool defining was reply sent within the execution.
pub fn reply_sent(&self) -> bool {
self.store.reply_sent
self.outcome.reply.is_some()
}

/// Send a new program initialization message.
Expand Down Expand Up @@ -434,7 +449,6 @@ impl MessageContext {
let message = ReplyMessage::from_packet(message_id, packet);

self.outcome.reply = Some((message, reservation));
self.store.reply_sent = true;

Ok(message_id)
} else {
Expand Down Expand Up @@ -484,9 +498,8 @@ impl MessageContext {

/// Wake message by it's message id.
pub fn wake(&mut self, waker_id: MessageId, delay: u32) -> Result<(), Error> {
if self.store.awaken.insert(waker_id) {
if !self.outcome.awakening.iter().any(|v| v.0 == waker_id) {
breathx marked this conversation as resolved.
Show resolved Hide resolved
self.outcome.awakening.push((waker_id, delay));

Ok(())
} else {
Err(Error::DuplicateWaking)
Expand Down
2 changes: 1 addition & 1 deletion core/src/message/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub use incoming::{IncomingDispatch, IncomingMessage};
pub use init::{InitMessage, InitPacket};
pub use reply::{ReplyMessage, ReplyPacket};
pub use signal::SignalMessage;
pub use stored::{StoredDispatch, StoredMessage};
pub use stored::{StoredDelayedDispatch, StoredDispatch, StoredMessage};
pub use user::{UserMessage, UserStoredMessage};

use super::buffer::LimitedVec;
Expand Down
56 changes: 56 additions & 0 deletions core/src/message/stored.rs
Original file line number Diff line number Diff line change
Expand Up @@ -216,3 +216,59 @@ impl Deref for StoredDispatch {
self.message()
}
}

impl From<StoredDelayedDispatch> for StoredDispatch {
fn from(dispatch: StoredDelayedDispatch) -> Self {
StoredDispatch::new(dispatch.kind, dispatch.message, None)
}
}

/// Stored message with entry point.
#[derive(Clone, Debug, Eq, Hash, Ord, PartialEq, PartialOrd, Decode, Encode, TypeInfo)]
pub struct StoredDelayedDispatch {
ark0f marked this conversation as resolved.
Show resolved Hide resolved
/// Entry point.
kind: DispatchKind,
/// Stored message.
message: StoredMessage,
}

impl From<StoredDelayedDispatch> for (DispatchKind, StoredMessage) {
fn from(dispatch: StoredDelayedDispatch) -> (DispatchKind, StoredMessage) {
(dispatch.kind, dispatch.message)
}
}

impl StoredDelayedDispatch {
/// Create new StoredDelayedDispatch.
pub fn new(kind: DispatchKind, message: StoredMessage) -> Self {
Self { kind, message }
}

/// Convert StoredDispatch into IncomingDispatch for program processing.
pub fn into_incoming(self, gas_limit: GasLimit) -> IncomingDispatch {
IncomingDispatch::new(self.kind, self.message.into_incoming(gas_limit), None)
}
mqxf marked this conversation as resolved.
Show resolved Hide resolved

/// Decompose StoredDelayedDispatch for it's components: DispatchKind, StoredMessage.
pub fn into_parts(self) -> (DispatchKind, StoredMessage) {
self.into()
}

/// Entry point for the message.
pub fn kind(&self) -> DispatchKind {
self.kind
}

/// Dispatch message reference.
pub fn message(&self) -> &StoredMessage {
&self.message
}
}

impl Deref for StoredDelayedDispatch {
type Target = StoredMessage;

fn deref(&self) -> &Self::Target {
self.message()
}
}
9 changes: 7 additions & 2 deletions gsdk/src/metadata/generated.rs
Original file line number Diff line number Diff line change
Expand Up @@ -790,8 +790,6 @@ pub mod runtime_types {
>,
>,
pub initialized: ::std::vec::Vec<runtime_types::gear_core::ids::ProgramId>,
pub awaken: ::std::vec::Vec<runtime_types::gear_core::ids::MessageId>,
pub reply_sent: ::core::primitive::bool,
pub reservation_nonce:
runtime_types::gear_core::reservation::ReservationNonce,
pub system_reservation: ::core::option::Option<::core::primitive::u64>,
Expand All @@ -802,6 +800,13 @@ pub mod runtime_types {
#[derive(
Debug, crate::gp::Decode, crate::gp::DecodeAsType, crate::gp::Encode,
)]
pub struct StoredDelayedDispatch {
pub kind: runtime_types::gear_core::message::DispatchKind,
pub message: runtime_types::gear_core::message::stored::StoredMessage,
}
#[derive(
Debug, crate::gp::Decode, crate::gp::DecodeAsType, crate::gp::Encode,
)]
pub struct StoredDispatch {
pub kind: runtime_types::gear_core::message::DispatchKind,
pub message: runtime_types::gear_core::message::stored::StoredMessage,
Expand Down
7 changes: 5 additions & 2 deletions pallets/gear-debug/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub mod pallet {
use gear_core::{
ids::ProgramId,
memory::PageBuf,
message::{StoredDispatch, StoredMessage},
message::{StoredDelayedDispatch, StoredDispatch, StoredMessage},
pages::{GearPage, PageU32Size, WasmPage},
};
use primitive_types::H256;
Expand All @@ -62,7 +62,10 @@ pub mod pallet {
/// Storage with codes for programs.
type CodeStorage: CodeStorage;

type Messenger: Messenger<QueuedDispatch = StoredDispatch>;
type Messenger: Messenger<
QueuedDispatch = StoredDispatch,
DelayedDispatch = StoredDelayedDispatch,
>;

type ProgramStorage: ProgramStorage
+ IterableMap<(ProgramId, common::Program<BlockNumberFor<Self>>)>;
Expand Down
11 changes: 7 additions & 4 deletions pallets/gear-messenger/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,8 @@
#![doc(html_logo_url = "https://docs.gear.rs/logo.svg")]
#![doc(html_favicon_url = "https://gear-tech.io/favicons/favicon.ico")]

pub mod migrations;

// Runtime mock for running tests.
#[cfg(test)]
mod mock;
Expand All @@ -161,12 +163,12 @@ pub mod pallet {
use frame_system::pallet_prelude::BlockNumberFor;
use gear_core::{
ids::{MessageId, ProgramId},
message::{StoredDispatch, UserStoredMessage},
message::{StoredDelayedDispatch, StoredDispatch, UserStoredMessage},
};
use sp_std::{convert::TryInto, marker::PhantomData};

/// The current storage version.
pub(crate) const MESSENGER_STORAGE_VERSION: StorageVersion = StorageVersion::new(2);
pub(crate) const MESSENGER_STORAGE_VERSION: StorageVersion = StorageVersion::new(3);

// Gear Messenger Pallet's `Config`.
#[pallet::config]
Expand Down Expand Up @@ -413,14 +415,14 @@ pub mod pallet {
// Private storage for dispatch stash elements.
#[pallet::storage]
pub type DispatchStash<T: Config> =
StorageMap<_, Identity, MessageId, (StoredDispatch, Interval<BlockNumberFor<T>>)>;
StorageMap<_, Identity, MessageId, (StoredDelayedDispatch, Interval<BlockNumberFor<T>>)>;

// Public wrap of the dispatch stash elements.
common::wrap_storage_map!(
storage: DispatchStash,
name: DispatchStashWrap,
key: MessageId,
value: (StoredDispatch, Interval<BlockNumberFor<T>>)
value: (StoredDelayedDispatch, Interval<BlockNumberFor<T>>)
);

// ----
Expand Down Expand Up @@ -591,6 +593,7 @@ pub mod pallet {
type MailboxSecondKey = MessageId;
type MailboxedMessage = UserStoredMessage;
type QueuedDispatch = StoredDispatch;
mqxf marked this conversation as resolved.
Show resolved Hide resolved
type DelayedDispatch = StoredDelayedDispatch;
type WaitlistFirstKey = ProgramId;
type WaitlistSecondKey = MessageId;
type WaitlistedMessage = StoredDispatch;
Expand Down
Loading