Skip to content

Commit

Permalink
Merge pull request #1128 from multiversx/vh-api-8-vm-remove
Browse files Browse the repository at this point in the history
DebugApi completely removed from VM crate
  • Loading branch information
andrei-marinica committed Jun 27, 2023
2 parents 527f642 + dd26252 commit 4173796
Show file tree
Hide file tree
Showing 58 changed files with 102 additions and 2,789 deletions.
17 changes: 8 additions & 9 deletions contracts/examples/factorial/tests/factorial_test.rs
Original file line number Diff line number Diff line change
@@ -1,34 +1,33 @@
use factorial::*;
use multiversx_sc::types::BigUint;
use multiversx_sc_scenario::DebugApi;
use multiversx_sc_scenario::api::StaticApi;

#[test]
fn test_factorial() {
let _ = DebugApi::dummy();
let factorial = factorial::contract_obj::<DebugApi>();
let factorial = factorial::contract_obj::<StaticApi>();

assert_eq!(
BigUint::<DebugApi>::from(1u32),
BigUint::<StaticApi>::from(1u32),
factorial.factorial(0u32.into())
);
assert_eq!(
BigUint::<DebugApi>::from(1u32),
BigUint::<StaticApi>::from(1u32),
factorial.factorial(1u32.into())
);
assert_eq!(
BigUint::<DebugApi>::from(2u32),
BigUint::<StaticApi>::from(2u32),
factorial.factorial(2u32.into())
);
assert_eq!(
BigUint::<DebugApi>::from(6u32),
BigUint::<StaticApi>::from(6u32),
factorial.factorial(3u32.into())
);
assert_eq!(
BigUint::<DebugApi>::from(24u32),
BigUint::<StaticApi>::from(24u32),
factorial.factorial(4u32.into())
);
assert_eq!(
BigUint::<DebugApi>::from(120u32),
BigUint::<StaticApi>::from(120u32),
factorial.factorial(5u32.into())
);
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ fn test_raffle_and_claim() {
let owner = wrapper.create_user_account(&rust_biguint!(0));
let alice = wrapper.create_user_account(&full_reward_amount);

let nft_token_id = managed_token_id!(b"NFT-123456");
let nft_token_id = b"NFT-123456";

let nft_balance = rust_biguint!(1);
let nft_attributes: [u8; 0] = [];
Expand All @@ -51,7 +51,7 @@ fn test_raffle_and_claim() {
// setup the mock contract
wrapper
.execute_tx(&alice, &seed_nft_minter_mock_sc, &rust_biguint!(0), |sc| {
sc.init(nft_token_id.clone());
sc.init(managed_token_id!(nft_token_id));

sc.set_nft_count(nft_count);
})
Expand Down Expand Up @@ -149,11 +149,10 @@ fn test_raffle_and_claim() {

// claim the rewards

let token_identifier = nft_token_id.clone().to_boxed_bytes().into_vec();
let nft_payments: Vec<TxTokenTransfer> = nft_nonces
.iter()
.map(|nonce| TxTokenTransfer {
token_identifier: token_identifier.clone(),
token_identifier: nft_token_id.to_vec(),
nonce: *nonce,
value: rust_biguint!(1),
})
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use multiversx_sc_snippets::{
},
multiversx_sc_scenario::{
bech32,
multiversx_chain_vm::tx_mock::TxContextRef,
scenario_format::interpret_trait::InterpreterContext,
scenario_model::{IntoBlockchainCall, TxExpect, TypedScDeploy},
DebugApi,
},
StepBuffer,
};
Expand Down Expand Up @@ -75,7 +75,7 @@ impl ComposabilityInteract {
pub async fn typed_sc_deploy_vault(
&mut self,
call_state: &CallState,
) -> Vec<TypedScDeploy<OptionalValue<ManagedBuffer<TxContextRef>>>> {
) -> Vec<TypedScDeploy<OptionalValue<ManagedBuffer<DebugApi>>>> {
let mut typed_vault_deploys = Vec::new();
for _ in call_state.vaults.iter() {
let typed_sc_deploy = self
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ fn world() -> ScenarioWorld {
let mut blockchain = ScenarioWorld::new();
blockchain.set_current_dir_from_workspace("contracts/feature-tests/panic-message-features");

blockchain.register_partial_contract_new_api::<panic_message_features::AbiProvider, _>(
blockchain.register_partial_contract::<panic_message_features::AbiProvider, _>(
"file:output/panic-message-features.wasm",
panic_message_features::ContractBuilder,
"panic-message-features",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,11 @@ use num_traits::ToPrimitive;
use basic_features::BasicFeatures;
use multiversx_sc::{
codec::Empty,
types::{Address, BigUint, EsdtLocalRole, EsdtTokenPayment, ManagedVec, TokenIdentifier},
types::{Address, BigUint, EsdtLocalRole, EsdtTokenPayment, ManagedVec, TokenIdentifier}, err_msg,
};
use multiversx_sc_scenario::{
api::DebuggerApi as DebugApi, assert_values_eq, managed_address, managed_biguint,
managed_buffer, managed_token_id, rust_biguint, testing_framework::*,
api::DebugApi, assert_values_eq, managed_address, managed_biguint, managed_buffer,
managed_token_id, rust_biguint, testing_framework::*,
};
use rust_testing_framework_tester::{dummy_module::DummyModule, *};

Expand Down Expand Up @@ -1187,7 +1187,7 @@ fn managed_environment_test() {
}

#[test]
#[should_panic] // VMHooksApi misuse: operation called with handles from 2 different contexts
// #[should_panic] // VMHooksApi misuse: operation called with handles from 2 different contexts
fn managed_environment_consistency_test() {
let mut wrapper = BlockchainStateWrapper::new();
let adder_wrapper = wrapper.create_sc_account(
Expand All @@ -1205,11 +1205,13 @@ fn managed_environment_consistency_test() {
let sum = first_var + second_var;
assert_eq!(sum, third_var);
})
.assert_ok();
.assert_error(
err_msg::DEBUG_API_ERR_STATUS,
err_msg::DEBUG_API_ERR_BAD_HANDLE_CONTEXT,
);
}

#[test]
#[should_panic] // VMHooksApi misuse: operation called with handles from 2 different contexts
fn test_managed_values_standalone_consistency() {
let _ = DebugApi::dummy();

Expand All @@ -1233,7 +1235,10 @@ fn test_managed_values_standalone_consistency() {
TokenIdentifier::<DebugApi>::from_esdt_bytes(b"FOO-a1a1a1")
);
})
.assert_ok();
.assert_error(
err_msg::DEBUG_API_ERR_STATUS,
err_msg::DEBUG_API_ERR_BAD_HANDLE_CONTEXT,
);
}

#[test]
Expand Down Expand Up @@ -1267,9 +1272,10 @@ fn test_managed_values_argument_and_return_value_consistency() {
assert_eq!(result.to_u64().unwrap(), 49);
},
)
.assert_ok();
// 'result' was created in the context of 'execute_tx'
assert_eq!(result.to_u64().unwrap(), 49);
.assert_error(
err_msg::DEBUG_API_ERR_STATUS,
err_msg::DEBUG_API_ERR_BAD_HANDLE_CONTEXT,
);
}

#[test]
Expand Down
5 changes: 5 additions & 0 deletions framework/base/src/err_msg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,8 @@ pub const STORAGE_NOT_32_BYTES: &[u8] = b"32 bytes of data expected in storage a

/// Mirrors the error message from the VM.
pub const ERROR_SIGNALLED_BY_SMARTCONTRACT: &str = "error signalled by smartcontract";

/// An additional non-VM status, meant just to signal an error in the debugger infrastructure of in the tests.
pub const DEBUG_API_ERR_STATUS: u64 = 100;
pub const DEBUG_API_ERR_BAD_HANDLE_CONTEXT: &str =
"VMHooksApi misuse: operation called with handles from 2 different contexts";
2 changes: 1 addition & 1 deletion framework/scenario/src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@ mod managed_type_api_vh;
mod vm_api_vh;

pub(crate) use impl_vh::i32_to_bool;
pub use impl_vh::{DebugHandle, DebuggerApi, StaticApi, VMHooksApi, VMHooksApiBackend};
pub use impl_vh::{DebugApi, DebugHandle, StaticApi, VMHooksApi, VMHooksApiBackend};
2 changes: 1 addition & 1 deletion framework/scenario/src/api/impl_vh.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ mod static_api;
mod vm_hooks_api;
mod vm_hooks_backend;

pub use debug_api::DebuggerApi;
pub use debug_api::DebugApi;
pub use debug_handle_vh::DebugHandle;
pub use static_api::StaticApi;
pub(crate) use vm_hooks_api::i32_to_bool;
Expand Down
33 changes: 17 additions & 16 deletions framework/scenario/src/api/impl_vh/debug_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,10 @@ use std::rc::Rc;

use multiversx_chain_vm::{
executor::VMHooks,
tx_mock::{StaticVarData, StaticVarStack, TxContext, TxContextStack},
tx_mock::{StaticVarData, StaticVarStack, TxContext, TxContextStack, TxPanic},
vm_hooks::{TxContextWrapper, VMHooksDispatcher},
};
use multiversx_sc::err_msg;

use super::{DebugHandle, VMHooksApi, VMHooksApiBackend};

Expand Down Expand Up @@ -37,10 +38,7 @@ impl VMHooksApiBackend for DebugApiBackend {
where
F: FnOnce(&dyn VMHooks) -> R,
{
assert!(
Rc::ptr_eq(&handle1.context, &handle2.context),
"VMHooksApi misuse: operation called with handles from 2 different contexts"
);
assert_handles_on_same_context(&handle1, &handle2);
Self::with_vm_hooks_ctx_1(handle1, f)
}

Expand All @@ -53,14 +51,8 @@ impl VMHooksApiBackend for DebugApiBackend {
where
F: FnOnce(&dyn VMHooks) -> R,
{
assert!(
Rc::ptr_eq(&handle1.context, &handle2.context),
"VMHooksApi misuse: operation called with handles from 2 different contexts"
);
assert!(
Rc::ptr_eq(&handle1.context, &handle3.context),
"VMHooksApi misuse: operation called with handles from 2 different contexts"
);
assert_handles_on_same_context(&handle1, &handle2);
assert_handles_on_same_context(&handle1, &handle3);
Self::with_vm_hooks_ctx_1(handle1, f)
}

Expand All @@ -78,9 +70,9 @@ impl VMHooksApiBackend for DebugApiBackend {
}

/// TODO: rename to DebugApi
pub type DebuggerApi = VMHooksApi<DebugApiBackend>;
pub type DebugApi = VMHooksApi<DebugApiBackend>;

impl DebuggerApi {
impl DebugApi {
pub fn dummy() {
let tx_context = TxContext::dummy();
let tx_context_rc = Rc::new(tx_context);
Expand All @@ -90,8 +82,17 @@ impl DebuggerApi {
}
}

impl std::fmt::Debug for DebuggerApi {
impl std::fmt::Debug for DebugApi {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str("DebugApi")
}
}

fn assert_handles_on_same_context(handle1: &DebugHandle, handle2: &DebugHandle) {
if !Rc::ptr_eq(&handle1.context, &handle2.context) {
std::panic::panic_any(TxPanic {
status: err_msg::DEBUG_API_ERR_STATUS,
message: err_msg::DEBUG_API_ERR_BAD_HANDLE_CONTEXT.to_string(),
})
}
}
44 changes: 2 additions & 42 deletions framework/scenario/src/facade/scenario_world.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::{
api::DebuggerApi,
api::DebugApi,
multiversx_chain_vm::world_mock::ContractContainer,
multiversx_sc::{
api,
Expand All @@ -9,7 +9,6 @@ use crate::{
scenario_format::{interpret_trait::InterpreterContext, value_interpreter::interpret_string},
vm_go_tool::run_vm_go_tool,
};
use multiversx_chain_vm::DebugApi;
use std::path::{Path, PathBuf};

use super::debugger_backend::DebuggerBackend;
Expand Down Expand Up @@ -127,11 +126,7 @@ impl ScenarioWorld {
) {
self.register_contract_container(
expression,
ContractContainer::new(
contract_builder.new_contract_obj::<DebuggerApi>(),
None,
false,
),
ContractContainer::new(contract_builder.new_contract_obj::<DebugApi>(), None, false),
)
}

Expand Down Expand Up @@ -182,41 +177,6 @@ impl ScenarioWorld {
);
}

/// Temporary.
///
/// TODO: use DebuggerApi in `register_partial_contract` when all tests pass.
pub fn register_partial_contract_new_api<Abi, B>(
&mut self,
expression: &str,
contract_builder: B,
sub_contract_name: &str,
) where
Abi: ContractAbiProvider,
B: CallableContractBuilder,
{
let multi_contract_config = multiversx_sc_meta::multi_contract_config::<Abi>(
self.current_dir
.join("multicontract.toml")
.to_str()
.unwrap(),
);
let sub_contract = multi_contract_config.find_contract(sub_contract_name);
let contract_obj = if sub_contract.settings.external_view {
contract_builder.new_contract_obj::<api::ExternalViewApi<DebuggerApi>>()
} else {
contract_builder.new_contract_obj::<DebuggerApi>()
};

self.register_contract_container(
expression,
ContractContainer::new(
contract_obj,
Some(sub_contract.all_exported_function_names()),
sub_contract.settings.panic_message,
),
);
}

/// Exports current scenario to a JSON file, as created.
pub fn write_scenario_trace<P: AsRef<Path>>(&mut self, file_path: P) {
if let Some(trace) = &mut self.get_mut_contract_debugger_backend().trace {
Expand Down
2 changes: 1 addition & 1 deletion framework/scenario/src/facade/scenario_world_steps.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
use crate::{
api::DebugApi,
facade::ScenarioWorld,
multiversx_sc::{
codec::{CodecFrom, PanicErrorHandler, TopEncodeMulti},
types::{Address, ContractCall},
},
scenario::{model::*, ScenarioRunner},
};
use multiversx_chain_vm::DebugApi;

impl ScenarioWorld {
/// Imports and processes steps from an external scenario file.
Expand Down
3 changes: 2 additions & 1 deletion framework/scenario/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ pub mod whitebox;
/// Unfortunately, the `deprecated` annotation doesn't function for reexports.
pub use whitebox as testing_framework;

pub use multiversx_chain_vm::{self, bech32, num_bigint, DebugApi};
pub use api::DebugApi;
pub use multiversx_chain_vm::{self, bech32, num_bigint};

pub use multiversx_sc;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
use crate::multiversx_sc::{
codec::TopEncodeMulti,
types::{
ContractCall, ContractCallNoPayment, ContractCallWithEgld,
ContractCallWithEgldOrSingleEsdt, ContractCallWithMultiEsdt, ContractDeploy,
use super::{ScCallStep, ScDeployStep, ScQueryStep, TypedScCall, TypedScDeploy, TypedScQuery};
use crate::{
api::DebugApi,
multiversx_sc::{
codec::TopEncodeMulti,
types::{
ContractCall, ContractCallNoPayment, ContractCallWithEgld,
ContractCallWithEgldOrSingleEsdt, ContractCallWithMultiEsdt, ContractDeploy,
},
},
};

use multiversx_chain_vm::DebugApi;

use super::{ScCallStep, ScDeployStep, ScQueryStep, TypedScCall, TypedScDeploy, TypedScQuery};

/// Converts a `ContractCall` or `ContractDeploy` into a scenario object that additonally
/// contains gas costs and transaction-related data.
pub trait IntoBlockchainCall {
Expand Down
2 changes: 1 addition & 1 deletion framework/scenario/src/scenario/model/step/step_sc_call.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
api::DebugApi,
scenario::model::{AddressValue, BigUintValue, BytesValue, TxCall, TxESDT, TxExpect, U64Value},
scenario_model::TxResponse,
DebugApi,
};

use crate::multiversx_sc::{
Expand Down
Loading

0 comments on commit 4173796

Please sign in to comment.