Skip to content

Commit

Permalink
fix(gtest): clean waitlist on exit_dispatch (#4205)
Browse files Browse the repository at this point in the history
  • Loading branch information
playX18 authored Sep 3, 2024
1 parent 1b67210 commit d5347d2
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 5 deletions.
23 changes: 23 additions & 0 deletions gtest/src/manager/journal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,32 @@ impl JournalHandler for ExtManager {
"Exit dispatch: id_exited = {id_exited}, value_destination = {value_destination}"
);

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:?}"));

if let TestActor::Initialized(Program::Genuine(program)) =
std::mem::replace(actor, TestActor::Dormant)
{
for (reservation_id, slot) in program.gas_reservation_map {
let slot = self.remove_gas_reservation_slot(reservation_id, slot);

let result = self.task_pool.delete(
slot.finish,
ScheduledTask::RemoveGasReservation(id_exited, reservation_id),
);
log::debug!(
"remove_gas_reservation_map; program_id = {id_exited:?}, result = {result:?}"
);
}
}

*actor = TestActor::Dormant
});

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 d5347d2

Please sign in to comment.