Skip to content

Commit

Permalink
fix(gtest): clean waitlist on exit_dispatch
Browse files Browse the repository at this point in the history
  • Loading branch information
playX18 committed Aug 29, 2024
1 parent cdd5b12 commit a47ae8a
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 7 deletions.
8 changes: 6 additions & 2 deletions gtest/src/manager/journal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@

//! Implementation of the `JournalHandler` trait for the `ExtManager`.
use std::collections::BTreeMap;

use super::{ExtManager, Gas, GenuineProgram, Program, TestActor};
use crate::{
manager::hold_bound::HoldBoundBuilder,
Expand All @@ -46,6 +44,7 @@ use gear_core::{
};
use gear_core_errors::SignalCode;
use gear_wasm_instrument::gas_metering::Schedule;
use std::collections::BTreeMap;

impl JournalHandler for ExtManager {
fn message_dispatched(
Expand Down Expand Up @@ -100,6 +99,11 @@ impl JournalHandler for ExtManager {
}

fn exit_dispatch(&mut self, id_exited: ProgramId, value_destination: ProgramId) {
self.waitlist.drain_key(id_exited).for_each(|entry| {
let message = self.wake_dispatch_requirements(entry);

self.dispatches.push_back(message);
});
Actors::modify(id_exited, |actor| {
let actor =
actor.unwrap_or_else(|| panic!("Can't find existing program {id_exited:?}"));
Expand Down
16 changes: 12 additions & 4 deletions gtest/src/manager/wait_wake.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,16 +131,24 @@ impl ExtManager {
program_id: ProgramId,
message_id: MessageId,
) -> Result<StoredDispatch, WaitlistErrorImpl> {
let (waitlisted, hold_interval) = self.waitlist.remove(program_id, message_id)?;
let expected_bn = hold_interval.finish;
self.waitlist
.remove(program_id, message_id)
.map(|waitlisted_message| self.wake_dispatch_requirements(waitlisted_message))
}

pub(crate) fn wake_dispatch_requirements(
&mut self,
(waitlisted, hold_interval): (StoredDispatch, Interval<BlockNumber>),
) -> StoredDispatch {
let expected = hold_interval.finish;

self.charge_for_hold(waitlisted.id(), hold_interval, StorageType::Waitlist);

let _ = self.task_pool.delete(
expected_bn,
expected,
ScheduledTask::RemoveFromWaitlist(waitlisted.destination(), waitlisted.id()),
);

Ok(waitlisted)
waitlisted
}
}
15 changes: 14 additions & 1 deletion gtest/src/state/waitlist.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,10 @@ use gear_common::{
auxiliary::{waitlist::*, BlockNumber},
storage::{Interval, IterableByKeyMap, Waitlist, WaitlistCallbacks},
};
use gear_core::ids::{MessageId, ProgramId};
use gear_core::{
ids::{MessageId, ProgramId},
message::StoredDispatch,
};

/// Waitlist manager which operates under the hood over
/// [`gear_common::auxiliary::waitlist::AuxiliaryWaitlist`].
Expand Down Expand Up @@ -65,6 +68,16 @@ impl WaitlistManager {
pub(crate) fn reset(&self) {
<AuxiliaryWaitlist<WaitlistCallbacksImpl> as Waitlist>::clear();
}

pub(crate) fn drain_key(
&self,
program_id: ProgramId,
) -> impl Iterator<Item = (StoredDispatch, Interval<BlockNumber>)> {
<AuxiliaryWaitlist<WaitlistCallbacksImpl> as IterableByKeyMap<(
StoredDispatch,
Interval<BlockNumber>,
)>>::drain_key(program_id)
}
}

/// Waitlist callbacks implementor.
Expand Down

0 comments on commit a47ae8a

Please sign in to comment.