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

feat(gtest): process time-limited wait messages #3605

Merged
merged 2 commits into from
Dec 22, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
53 changes: 43 additions & 10 deletions gtest/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ use gear_core::{
ids::{CodeId, MessageId, ProgramId, ReservationId},
memory::PageBuf,
message::{
Dispatch, DispatchKind, MessageWaitedType, ReplyMessage, ReplyPacket, StoredDispatch,
StoredMessage,
Dispatch, DispatchKind, Message, MessageWaitedType, ReplyMessage, ReplyPacket,
StoredDispatch, StoredMessage,
},
pages::{GearPage, PageU32Size, WasmPage},
program::Program as CoreProgram,
Expand Down Expand Up @@ -236,6 +236,7 @@ pub(crate) struct ExtManager {
pub(crate) dispatches: VecDeque<StoredDispatch>,
pub(crate) mailbox: HashMap<ProgramId, Vec<StoredMessage>>,
pub(crate) wait_list: BTreeMap<(ProgramId, MessageId), StoredDispatch>,
pub(crate) wait_list_schedules: BTreeMap<u32, Vec<(ProgramId, MessageId)>>,
pub(crate) wait_init_list: BTreeMap<ProgramId, Vec<MessageId>>,
pub(crate) gas_limits: BTreeMap<MessageId, u64>,
pub(crate) delayed_dispatches: HashMap<u32, Vec<Dispatch>>,
Expand Down Expand Up @@ -332,6 +333,36 @@ impl ExtManager {
.unwrap_or_default()
}

/// Process scheduled wait list.
pub(crate) fn process_scheduled_wait_list(&mut self, bn: u32) -> Vec<RunResult> {
self.wait_list_schedules
.remove(&bn)
.map(|ids| {
ids.into_iter()
.filter_map(|key| {
self.wait_list.remove(&key).map(|dispatch| {
let (kind, message, ..) = dispatch.into_parts();
let message = Message::new(
message.id(),
message.source(),
message.destination(),
message
.payload_bytes()
.to_vec()
.try_into()
.unwrap_or_default(),
self.gas_limits.get(&message.id()).copied(),
message.value(),
message.details(),
);
self.run_dispatch(Dispatch::new(kind, message))
})
})
.collect()
})
.unwrap_or_default()
}

/// Check if the current block number should trigger new epoch and reset
/// the provided random data.
pub(crate) fn check_epoch(&mut self) {
Expand Down Expand Up @@ -630,11 +661,6 @@ impl ExtManager {
self.others_failed = false;
self.main_gas_burned = Gas::zero();
self.others_gas_burned = Gas::zero();

// TODO: Remove this check after #349.
if !self.dispatches.is_empty() {
panic!("Message queue isn't empty");
}
}

fn mark_failed(&mut self, msg_id: MessageId) {
Expand Down Expand Up @@ -993,14 +1019,21 @@ impl JournalHandler for ExtManager {
fn wait_dispatch(
&mut self,
dispatch: StoredDispatch,
_duration: Option<u32>,
duration: Option<u32>,
_: MessageWaitedType,
) {
log::debug!("[{}] wait", dispatch.id());

self.message_consumed(dispatch.id());
self.wait_list
.insert((dispatch.destination(), dispatch.id()), dispatch);
let dest = dispatch.destination();
let id = dispatch.id();
self.wait_list.insert((dest, id), dispatch);
if let Some(duration) = duration {
self.wait_list_schedules
.entry(self.block_info.height + duration)
.or_default()
.push((dest, id));
}
}

fn wake_message(
Expand Down
4 changes: 3 additions & 1 deletion gtest/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,9 @@ impl System {
.block_info
.timestamp
.saturating_add(BLOCK_DURATION_IN_MSECS);
manager.process_delayed_dispatches(next_block_number)
let mut results = manager.process_delayed_dispatches(next_block_number);
results.extend(manager.process_scheduled_wait_list(next_block_number));
results
})
.collect::<Vec<Vec<_>>>()
.concat()
Expand Down