From 30bfd2a00c45122595db43d5f4a4c6ba999c0f9c Mon Sep 17 00:00:00 2001 From: StackOverflowExcept1on <109800286+StackOverflowExcept1on@users.noreply.github.com> Date: Mon, 1 Apr 2024 14:00:26 +0300 Subject: [PATCH] fix(gtest): allow zero balance for `exec::exit()` (#3848) --- Cargo.lock | 2 +- gtest/Cargo.toml | 2 +- gtest/src/manager.rs | 19 ++++++++++++++----- gtest/src/program.rs | 23 +++++++++++++++++++++-- gtest/src/system.rs | 6 ++++-- 5 files changed, 41 insertions(+), 11 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 4cf4ec8b0ad..533588f0fcb 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -5233,9 +5233,9 @@ name = "gtest" version = "1.2.0" dependencies = [ "colored", + "demo-constructor", "demo-custom", "demo-futures-unordered", - "demo-meta-io", "demo-piggy-bank", "demo-ping", "derive_more", diff --git a/gtest/Cargo.toml b/gtest/Cargo.toml index 73f8817e787..a6e16d2104f 100644 --- a/gtest/Cargo.toml +++ b/gtest/Cargo.toml @@ -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"] } diff --git a/gtest/src/manager.rs b/gtest/src/manager.rs index 86e16d70a2a..c417a33fc02 100644 --- a/gtest/src/manager.rs +++ b/gtest/src/manager.rs @@ -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 @@ -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, @@ -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); } } @@ -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); } } diff --git a/gtest/src/program.rs b/gtest/src/program.rs index ae7a5ed9eac..8f7c67aa332 100644 --- a/gtest/src/program.rs +++ b/gtest/src/program.rs @@ -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, }; @@ -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. @@ -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()); + } } diff --git a/gtest/src/system.rs b/gtest/src/system.rs index 6e4131b361b..3f29ed37017 100644 --- a/gtest/src/system.rs +++ b/gtest/src/system.rs @@ -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, }; @@ -270,7 +270,9 @@ impl System { /// Mint balance to user with given `id` and `value`. pub fn mint_to>(&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`.