-
Notifications
You must be signed in to change notification settings - Fork 105
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(gtest): Introduce auxiliary waitlist impl to
gtest
(#4193)
- Loading branch information
Showing
8 changed files
with
186 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,84 @@ | ||
// This file is part of Gear. | ||
|
||
// Copyright (C) 2021-2024 Gear Technologies Inc. | ||
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 | ||
|
||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
//! Auxiliary implementation of the waitlist. | ||
|
||
use super::{AuxiliaryDoubleStorageWrap, BlockNumber, DoubleBTreeMap}; | ||
use crate::storage::{Interval, WaitlistError, WaitlistImpl, WaitlistKeyGen}; | ||
use core::cell::RefCell; | ||
use gear_core::{ | ||
ids::{MessageId, ProgramId}, | ||
message::StoredDispatch, | ||
}; | ||
|
||
/// Waitlist implementation that can be used in a native, non-wasm runtimes. | ||
pub type AuxiliaryWaitlist<WaitListCallbacks> = WaitlistImpl< | ||
WaitlistStorageWrap, | ||
WaitlistedMessage, | ||
BlockNumber, | ||
WaitlistErrorImpl, | ||
WaitlistErrorImpl, | ||
WaitListCallbacks, | ||
WaitlistKeyGen, | ||
>; | ||
/// Type represents message stored in the waitlist. | ||
pub type WaitlistedMessage = StoredDispatch; | ||
|
||
std::thread_local! { | ||
// Definition of the waitlist (`StorageDoubleMap`) global storage, accessed by the `Waitlist` trait implementor. | ||
pub(crate) static WAITLIST_STORAGE: RefCell<DoubleBTreeMap<ProgramId, MessageId, (WaitlistedMessage, Interval<BlockNumber>)>> = const { RefCell::new(DoubleBTreeMap::new()) }; | ||
} | ||
|
||
/// `Waitlist` double storage map manager. | ||
pub struct WaitlistStorageWrap; | ||
|
||
impl AuxiliaryDoubleStorageWrap for WaitlistStorageWrap { | ||
type Key1 = ProgramId; | ||
type Key2 = MessageId; | ||
type Value = (WaitlistedMessage, Interval<BlockNumber>); | ||
|
||
fn with_storage<F, R>(f: F) -> R | ||
where | ||
F: FnOnce(&DoubleBTreeMap<Self::Key1, Self::Key2, Self::Value>) -> R, | ||
{ | ||
WAITLIST_STORAGE.with_borrow(f) | ||
} | ||
|
||
fn with_storage_mut<F, R>(f: F) -> R | ||
where | ||
F: FnOnce(&mut DoubleBTreeMap<Self::Key1, Self::Key2, Self::Value>) -> R, | ||
{ | ||
WAITLIST_STORAGE.with_borrow_mut(f) | ||
} | ||
} | ||
|
||
/// An implementor of the error returned from calling `Waitlist` trait functions | ||
pub enum WaitlistErrorImpl { | ||
DuplicateKey, | ||
ElementNotFound, | ||
} | ||
|
||
impl WaitlistError for WaitlistErrorImpl { | ||
fn duplicate_key() -> Self { | ||
Self::DuplicateKey | ||
} | ||
|
||
fn element_not_found() -> Self { | ||
Self::ElementNotFound | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// This file is part of Gear. | ||
|
||
// Copyright (C) 2024 Gear Technologies Inc. | ||
// SPDX-License-Identifier: GPL-3.0-or-later WITH Classpath-exception-2.0 | ||
|
||
// This program is free software: you can redistribute it and/or modify | ||
// it under the terms of the GNU General Public License as published by | ||
// the Free Software Foundation, either version 3 of the License, or | ||
// (at your option) any later version. | ||
|
||
// This program is distributed in the hope that it will be useful, | ||
// but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
// GNU General Public License for more details. | ||
|
||
// You should have received a copy of the GNU General Public License | ||
// along with this program. If not, see <https://www.gnu.org/licenses/>. | ||
|
||
//! Waitlist manager. | ||
|
||
#![allow(unused)] | ||
|
||
use crate::blocks::GetBlockNumberImpl; | ||
use gear_common::{ | ||
auxiliary::{waitlist::*, BlockNumber}, | ||
storage::{Interval, IterableByKeyMap, Waitlist, WaitlistCallbacks}, | ||
}; | ||
use gear_core::ids::{MessageId, ProgramId}; | ||
|
||
/// Waitlist manager which operates under the hood over | ||
/// [`gear_common::auxiliary::waitlist::AuxiliaryWaitlist`]. | ||
#[derive(Debug, Default)] | ||
pub(crate) struct WaitlistManager; | ||
|
||
impl WaitlistManager { | ||
/// Check if message with `message_id` to a program with `program_id` is in | ||
/// the waitlist. | ||
pub(crate) fn contains(program_id: ProgramId, message_id: MessageId) -> bool { | ||
<AuxiliaryWaitlist<WaitlistCallbacksImpl> as Waitlist>::contains(&program_id, &message_id) | ||
} | ||
|
||
/// Insert message into waitlist. | ||
pub(crate) fn insert( | ||
&self, | ||
message: WaitlistedMessage, | ||
expected: BlockNumber, | ||
) -> Result<(), WaitlistErrorImpl> { | ||
<AuxiliaryWaitlist<WaitlistCallbacksImpl> as Waitlist>::insert(message, expected) | ||
} | ||
|
||
/// Remove message from the waitlist. | ||
pub(crate) fn remove( | ||
&self, | ||
program_id: ProgramId, | ||
message_id: MessageId, | ||
) -> Result<(WaitlistedMessage, Interval<BlockNumber>), WaitlistErrorImpl> { | ||
<AuxiliaryWaitlist<WaitlistCallbacksImpl> as Waitlist>::remove(program_id, message_id) | ||
} | ||
|
||
/// Fully reset waitlist. | ||
/// | ||
/// # Note: | ||
/// Must be called by `WaitlistManager` owner to reset waitlist | ||
/// when the owner is dropped. | ||
pub(crate) fn reset(&self) { | ||
<AuxiliaryWaitlist<WaitlistCallbacksImpl> as Waitlist>::clear(); | ||
} | ||
} | ||
|
||
/// Waitlist callbacks implementor. | ||
pub(crate) struct WaitlistCallbacksImpl; | ||
|
||
impl WaitlistCallbacks for WaitlistCallbacksImpl { | ||
type Value = WaitlistedMessage; | ||
type BlockNumber = BlockNumber; | ||
|
||
type GetBlockNumber = GetBlockNumberImpl; | ||
|
||
type OnInsert = (); | ||
type OnRemove = (); | ||
} |