Skip to content

Commit

Permalink
feat(gtest): Delayed dispatches and charging for holding (#4157)
Browse files Browse the repository at this point in the history
  • Loading branch information
playX18 authored Aug 27, 2024
1 parent 33bb931 commit 09977f4
Show file tree
Hide file tree
Showing 11 changed files with 987 additions and 150 deletions.
39 changes: 28 additions & 11 deletions gtest/src/gas_tree.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@
use crate::GAS_MULTIPLIER;
use gear_common::{
auxiliary::gas_provider::{AuxiliaryGasProvider, GasTreeError, PlainNodeId},
gas_provider::{ConsumeResultOf, GasNodeId, Provider, ReservableTree, Tree},
Gas, GasMultiplier, Origin,
gas_provider::{ConsumeResultOf, GasNodeId, LockableTree, Provider, ReservableTree, Tree},
Gas, GasMultiplier, LockId, Origin,
};
use gear_core::ids::{MessageId, ProgramId, ReservationId};

Expand Down Expand Up @@ -50,13 +50,18 @@ impl GasTreeManager {
origin: ProgramId,
mid: MessageId,
amount: Gas,
is_reply: bool,
) -> Result<PositiveImbalance, GasTreeError> {
GasTree::create(
origin.cast(),
GAS_MULTIPLIER,
GasNodeId::from(mid.cast::<PlainNodeId>()),
amount,
)
if !is_reply || !self.exists_and_deposit(mid) {
GasTree::create(
origin.cast(),
GAS_MULTIPLIER,
GasNodeId::from(mid.cast::<PlainNodeId>()),
amount,
)
} else {
Ok(PositiveImbalance::new(0))
}
}

/// Adapted by argument types version of the gas tree `create_deposit`
Expand All @@ -79,7 +84,7 @@ impl GasTreeManager {
pub(crate) fn split_with_value(
&self,
is_reply: bool,
original_mid: MessageId,
original_mid: impl Origin,
new_mid: MessageId,
amount: Gas,
) -> Result<(), GasTreeError> {
Expand Down Expand Up @@ -116,7 +121,7 @@ impl GasTreeManager {
/// Adapted by argument types version of the gas tree `cut` method.
pub(crate) fn cut(
&self,
original_node: MessageId,
original_node: impl Origin,
new_mid: MessageId,
amount: Gas,
) -> Result<(), GasTreeError> {
Expand All @@ -128,7 +133,7 @@ impl GasTreeManager {
}

/// Adapted by argument types version of the gas tree `get_limit` method.
pub(crate) fn get_limit(&self, mid: MessageId) -> Result<Gas, GasTreeError> {
pub(crate) fn get_limit(&self, mid: impl Origin) -> Result<Gas, GasTreeError> {
GasTree::get_limit(GasNodeId::from(mid.cast::<PlainNodeId>()))
}

Expand Down Expand Up @@ -164,6 +169,10 @@ impl GasTreeManager {
GasTree::exists(GasNodeId::from(node_id.cast::<PlainNodeId>()))
}

pub(crate) fn exists_and_deposit(&self, node_id: impl Origin) -> bool {
GasTree::exists_and_deposit(GasNodeId::from(node_id.cast::<PlainNodeId>()))
}

/// Adapted by argument types version of the gas tree `reset` method.
///
/// *Note* Call with caution as it completely resets the storage.
Expand All @@ -185,6 +194,14 @@ impl GasTreeManager {
GasTree::system_reserve(GasNodeId::from(key.cast::<PlainNodeId>()), amount)
}

pub fn lock(&self, key: MessageId, id: LockId, amount: Gas) -> Result<(), GasTreeError> {
GasTree::lock(GasNodeId::from(key.cast::<PlainNodeId>()), id, amount)
}

pub(crate) fn unlock_all(&self, key: impl Origin, id: LockId) -> Result<Gas, GasTreeError> {
GasTree::unlock_all(GasNodeId::from(key.cast::<PlainNodeId>()), id)
}

/// The id of node, external origin and funds multiplier for a key.
///
/// Error occurs if the tree is invalidated (has "orphan" nodes), and the
Expand Down
3 changes: 3 additions & 0 deletions gtest/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -581,6 +581,9 @@ pub mod constants {
/// Cost of storing delayed message per block.
pub const DISPATCH_HOLD_COST: Gas = 100;

/// Cost of storing message in mailbox
pub const MAILBOX_COST: Gas = 100;

/* Execution-related constants */
// TODO: use proper weights of instantiation and instrumentation (#3509).

Expand Down
13 changes: 10 additions & 3 deletions gtest/src/mailbox/actor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl<'a> ActorMailbox<'a> {
.ok_or(MailboxErrorImpl::ElementNotFound)?;
self.manager
.borrow_mut()
.claim_value_from_mailbox(self.user_id, mailboxed_msg.id())
.read_mailbox_message(self.user_id, mailboxed_msg.id())
.unwrap_or_else(|e| unreachable!("Unexpected mailbox error: {e:?}"));

Ok(())
Expand All @@ -127,7 +127,10 @@ impl<'a> ActorMailbox<'a> {

#[cfg(test)]
mod tests {
use crate::{Log, Program, System, DEFAULT_USER_ALICE, EXISTENTIAL_DEPOSIT};
use crate::{
Log, Program, System, DEFAULT_USER_ALICE, EXISTENTIAL_DEPOSIT, GAS_MULTIPLIER,
MAILBOX_THRESHOLD,
};
use codec::Encode;
use demo_constructor::{Call, Calls, Scheme, WASM_BINARY};
use gear_core::ids::ProgramId;
Expand Down Expand Up @@ -159,9 +162,13 @@ mod tests {
let res = system.run_next_block();
assert!(res.succeed.contains(&msg_id));
assert!(res.contains(&log));

assert_eq!(
system.balance_of(sender),
original_balance - value_send - res.spent_value()
original_balance
- value_send
- res.spent_value()
- GAS_MULTIPLIER.gas_to_value(MAILBOX_THRESHOLD)
);

let mailbox = system.get_mailbox(sender);
Expand Down
8 changes: 6 additions & 2 deletions gtest/src/mailbox/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,12 @@ pub(crate) struct MailboxManager;

impl MailboxManager {
/// Insert user message into mailbox.
pub(crate) fn insert(&self, message: MailboxedMessage) -> Result<(), MailboxErrorImpl> {
<AuxiliaryMailbox<MailboxCallbacksImpl> as Mailbox>::insert(message, u32::MAX)
pub(crate) fn insert(
&self,
message: MailboxedMessage,
expected: BlockNumber,
) -> Result<(), MailboxErrorImpl> {
<AuxiliaryMailbox<MailboxCallbacksImpl> as Mailbox>::insert(message, expected)
}

/// Remove user message from mailbox.
Expand Down
Loading

0 comments on commit 09977f4

Please sign in to comment.