Skip to content

Commit

Permalink
fix(gtest): allow zero balance for exec::exit() (#3848)
Browse files Browse the repository at this point in the history
  • Loading branch information
StackOverflowExcept1on authored Apr 1, 2024
1 parent 26a077e commit 30bfd2a
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 11 deletions.
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion gtest/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,4 @@ demo-custom.workspace = true
demo-piggy-bank.workspace = true
demo-ping.workspace = true
demo-futures-unordered.workspace = true
demo-meta-io.workspace = true
demo-constructor = { workspace = true, features = ["std"] }
19 changes: 14 additions & 5 deletions gtest/src/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,6 +217,15 @@ impl Actors {
}
}

/// Simple boolean for whether an account needs to be kept in existence.
#[derive(PartialEq)]
pub(crate) enum MintMode {
/// Operation must not result in the account going out of existence.
KeepAlive,
/// Operation may result in account going out of existence.
AllowDeath,
}

#[derive(Default, Debug)]
pub(crate) struct ExtManager {
// State metadata
Expand Down Expand Up @@ -598,8 +607,8 @@ impl ExtManager {
)
}

pub(crate) fn mint_to(&mut self, id: &ProgramId, value: Balance) {
if value < crate::EXISTENTIAL_DEPOSIT {
pub(crate) fn mint_to(&mut self, id: &ProgramId, value: Balance, mint_mode: MintMode) {
if mint_mode == MintMode::KeepAlive && value < crate::EXISTENTIAL_DEPOSIT {
panic!(
"An attempt to mint value ({}) less than existential deposit ({})",
value,
Expand Down Expand Up @@ -983,7 +992,7 @@ impl JournalHandler for ExtManager {

fn exit_dispatch(&mut self, id_exited: ProgramId, value_destination: ProgramId) {
if let Some((_, balance)) = self.actors.remove(&id_exited) {
self.mint_to(&value_destination, balance);
self.mint_to(&value_destination, balance, MintMode::AllowDeath);
}
}

Expand Down Expand Up @@ -1126,9 +1135,9 @@ impl JournalHandler for ExtManager {
}
}

self.mint_to(to, value);
self.mint_to(to, value, MintMode::KeepAlive);
} else {
self.mint_to(&from, value);
self.mint_to(&from, value, MintMode::KeepAlive);
}
}

Expand Down
23 changes: 21 additions & 2 deletions gtest/src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

use crate::{
log::RunResult,
manager::{Balance, ExtManager, Program as InnerProgram, TestActor},
manager::{Balance, ExtManager, MintMode, Program as InnerProgram, TestActor},
system::System,
Result,
};
Expand Down Expand Up @@ -750,7 +750,9 @@ impl<'a> Program<'a> {

/// Mint balance to the account.
pub fn mint(&mut self, value: Balance) {
self.manager.borrow_mut().mint_to(&self.id(), value)
self.manager
.borrow_mut()
.mint_to(&self.id(), value, MintMode::KeepAlive)
}

/// Returns the balance of the account.
Expand Down Expand Up @@ -1124,4 +1126,21 @@ mod tests {
assert!(!result.main_failed());
}
}

#[test]
fn test_handle_exit_with_zero_balance() {
use demo_constructor::{demo_exit_handle, WASM_BINARY};

let sys = System::new();
sys.init_logger();

let user_id = [42; 32];
let prog = Program::from_opt_and_meta_code_with_id(&sys, 137, WASM_BINARY.to_vec(), None);

let run_result = prog.send(user_id, demo_exit_handle::scheme());
assert!(!run_result.main_failed());

let run_result = prog.send_bytes(user_id, []);
assert!(!run_result.main_failed());
}
}
6 changes: 4 additions & 2 deletions gtest/src/system.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
use crate::{
log::RunResult,
mailbox::Mailbox,
manager::{Actors, Balance, ExtManager},
manager::{Actors, Balance, ExtManager, MintMode},
program::{Program, ProgramIdWrapper},
BLOCK_DURATION_IN_MSECS,
};
Expand Down Expand Up @@ -270,7 +270,9 @@ impl System {
/// Mint balance to user with given `id` and `value`.
pub fn mint_to<ID: Into<ProgramIdWrapper>>(&self, id: ID, value: Balance) {
let actor_id = id.into().0;
self.0.borrow_mut().mint_to(&actor_id, value);
self.0
.borrow_mut()
.mint_to(&actor_id, value, MintMode::KeepAlive);
}

/// Returns balance of user with given `id`.
Expand Down

0 comments on commit 30bfd2a

Please sign in to comment.