Skip to content

Commit

Permalink
Address conversions removed
Browse files Browse the repository at this point in the history
  • Loading branch information
andrei-marinica committed Sep 30, 2024
1 parent 647a571 commit e31136e
Show file tree
Hide file tree
Showing 13 changed files with 44 additions and 67 deletions.
2 changes: 1 addition & 1 deletion framework/scenario/src/api/impl_vh/static_api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ impl StaticApi {
///
/// This placeholder then needs to be converted to something useful.
pub fn is_current_address_placeholder(address: &Address) -> bool {
address.as_array() == StaticApiVMHooksHandler::CURRENT_ADDRESS_PLACEHOLDER.as_array()
address == &StaticApiVMHooksHandler::CURRENT_ADDRESS_PLACEHOLDER
}

pub fn reset() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ impl<'w> TxEnvMockDeployAddress for ScenarioEnvExec<'w> {
.world
.get_state()
.accounts
.get(&from_value.to_vm_address())
.get(&from_value.to_address())
.expect("sender does not exist")
.nonce;
let new_address_value = address_annotated(self, &new_address);
Expand Down
22 changes: 11 additions & 11 deletions framework/scenario/src/facade/world_tx/scenario_set_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ impl ScenarioWorld {
let balance_value = big_uint_annotated(&env, &balance);
let accounts = &mut self.get_mut_state().accounts;
for (vm_address_key, account) in accounts.iter_mut() {
if vm_address_key == &address_value.to_vm_address() {
if vm_address_key == &address_value.to_address() {
account.egld_balance.clone_from(&balance_value.value);
}
}
Expand All @@ -88,8 +88,8 @@ impl ScenarioWorld {
let address_value = address_annotated(&env, &address);
let balance_value = big_uint_annotated(&env, &balance);
let accounts = &mut self.get_mut_state().accounts;
for (vm_address, account) in accounts.iter_mut() {
if vm_address == &address_value.to_vm_address() {
for (address, account) in accounts.iter_mut() {
if address == &address_value.to_address() {
account.esdt.set_esdt_balance(
token_id.to_vec(),
0,
Expand Down Expand Up @@ -129,16 +129,16 @@ impl ScenarioWorld {
let mut esdt_attributes = Vec::new();
let _ = attributes.top_encode(&mut esdt_attributes);
let accounts = &mut self.get_mut_state().accounts;
for (vm_address, account) in accounts.iter_mut() {
if vm_address == &address_value.to_vm_address() {
for (account_address, account) in accounts.iter_mut() {
if account_address == &address_value.to_address() {
account.esdt.set_esdt_balance(
token_id.to_vec(),
nonce_value.value,
&balance_value.value,
EsdtInstanceMetadata {
creator: creator
.as_ref()
.map(|c| address_annotated(&env, c).to_vm_address()),
.map(|c| address_annotated(&env, c).to_address()),
attributes: esdt_attributes.clone(),
royalties: royalties_value.value,
name: name.unwrap_or_default().to_vec(),
Expand All @@ -156,8 +156,8 @@ impl ScenarioWorld {
BigUintValue: From<V>,
{
let accounts = &mut self.get_mut_state().accounts;
for (vm_address, account) in accounts.iter_mut() {
if vm_address == &AddressKey::from(address).to_vm_address() {
for (account_address, account) in accounts.iter_mut() {
if account_address == &AddressKey::from(address).to_address() {
account
.developer_rewards
.clone_from(&BigUintValue::from(developer_rewards).value);
Expand All @@ -172,8 +172,8 @@ impl ScenarioWorld {
let env = self.new_env_data();
let address_value = address_annotated(&env, &address);
let accounts = &mut self.get_mut_state().accounts;
for (vm_address, account) in accounts.iter_mut() {
if vm_address == &address_value.to_vm_address() {
for (account_address, account) in accounts.iter_mut() {
if account_address == &address_value.to_address() {
account.esdt.set_roles(
token_id.to_vec(),
roles
Expand Down Expand Up @@ -231,7 +231,7 @@ impl<'w> SetStateBuilderBase<'w> {
.vm_runner
.blockchain_mock
.state
.account_exists(&address.to_vm_address()),
.account_exists(&address.to_address()),
"updating existing accounts currently not supported"
);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ impl TxResponse {
.result_logs
.iter()
.map(|tx_log| Log {
address: Address::from_slice(tx_log.address.as_bytes()),
address: tx_log.address.clone(),
endpoint: tx_log.endpoint.to_string(),
topics: tx_log.topics.clone(),
data: tx_log.data.clone(),
Expand Down
4 changes: 0 additions & 4 deletions framework/scenario/src/scenario/model/value/address_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,6 @@ impl AddressKey {
pub fn to_address(&self) -> Address {
self.value.clone()
}

pub fn to_vm_address(&self) -> multiversx_chain_vm::types::VMAddress {
self.value.as_array().into()
}
}

impl Ord for AddressKey {
Expand Down
4 changes: 0 additions & 4 deletions framework/scenario/src/scenario/model/value/address_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,6 @@ impl AddressValue {
pub fn to_address(&self) -> Address {
self.value.clone()
}

pub fn to_vm_address(&self) -> multiversx_chain_vm::types::VMAddress {
self.value.as_array().into()
}
}

impl fmt::Display for AddressValue {
Expand Down
2 changes: 1 addition & 1 deletion framework/scenario/src/scenario/run_vm/check_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ impl ScenarioVMRunner {

fn execute(state: &BlockchainState, accounts: &CheckAccounts) {
for (expected_address, expected_account) in accounts.accounts.iter() {
if let Some(account) = state.accounts.get(&expected_address.to_vm_address()) {
if let Some(account) = state.accounts.get(&expected_address.to_address()) {
assert!(
expected_account.nonce.check(account.nonce),
"bad account nonce. Address: {}. Want: {}. Have: {}",
Expand Down
4 changes: 2 additions & 2 deletions framework/scenario/src/scenario/run_vm/sc_call.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,8 +86,8 @@ impl ScenarioVMRunner {
fn tx_input_from_call(sc_call_step: &ScCallStep) -> TxInput {
let tx = &sc_call_step.tx;
TxInput {
from: tx.from.to_vm_address(),
to: tx.to.to_vm_address(),
from: tx.from.to_address(),
to: tx.to.to_address(),
egld_value: tx.egld_value.value.clone(),
esdt_values: tx_esdt_transfers_from_scenario(tx.esdt_value.as_slice()),
func_name: tx.function.clone().into(),
Expand Down
4 changes: 2 additions & 2 deletions framework/scenario/src/scenario/run_vm/sc_deploy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ impl ScenarioVMRunner {
tx_result.pending_calls.no_calls(),
"Async calls from constructors are currently not supported"
);
(new_address.as_array().into(), tx_result)
(new_address, tx_result)
}

pub fn perform_sc_deploy_lambda_and_check<F>(
Expand All @@ -65,7 +65,7 @@ impl ScenarioVMRunner {
fn tx_input_from_deploy(sc_deploy_step: &ScDeployStep) -> TxInput {
let tx = &sc_deploy_step.tx;
TxInput {
from: tx.from.to_vm_address(),
from: tx.from.to_address(),
to: multiversx_chain_vm::types::VMAddress::zero(),
egld_value: tx.egld_value.value.clone(),
esdt_values: Vec::new(),
Expand Down
4 changes: 2 additions & 2 deletions framework/scenario/src/scenario/run_vm/sc_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@ impl ScenarioVMRunner {

fn tx_input_from_query(sc_query_step: &ScQueryStep) -> TxInput {
TxInput {
from: sc_query_step.tx.to.to_vm_address(),
to: sc_query_step.tx.to.to_vm_address(),
from: sc_query_step.tx.to.to_address(),
to: sc_query_step.tx.to.to_address(),
egld_value: BigUint::from(0u32),
esdt_values: Vec::new(),
func_name: sc_query_step.tx.function.clone().into(),
Expand Down
10 changes: 5 additions & 5 deletions framework/scenario/src/scenario/run_vm/set_state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ fn execute(state: &mut BlockchainState, set_state_step: &SetStateStep) {
);

state.validate_and_add_account(AccountData {
address: address.to_vm_address(),
address: address.to_address(),
nonce: account
.nonce
.as_ref()
Expand Down Expand Up @@ -65,7 +65,7 @@ fn execute(state: &mut BlockchainState, set_state_step: &SetStateStep) {
contract_owner: account
.owner
.as_ref()
.map(|address_value| address_value.to_vm_address()),
.map(|address_value| address_value.to_address()),
developer_rewards: account
.developer_rewards
.as_ref()
Expand All @@ -79,9 +79,9 @@ fn execute(state: &mut BlockchainState, set_state_step: &SetStateStep) {
"field should have SC format"
);
state.put_new_address(
new_address.creator_address.to_vm_address(),
new_address.creator_address.to_address(),
new_address.creator_nonce.value,
new_address.new_address.to_vm_address(),
new_address.new_address.to_address(),
)
}
for new_token_identifier in set_state_step.new_token_identifiers.iter().cloned() {
Expand Down Expand Up @@ -155,7 +155,7 @@ fn convert_scenario_esdt_instance_to_world_mock(
creator: scenario_esdt
.creator
.as_ref()
.map(|creator| creator.to_vm_address()),
.map(|creator| creator.to_address()),
royalties: scenario_esdt
.royalties
.as_ref()
Expand Down
6 changes: 3 additions & 3 deletions framework/scenario/src/scenario/run_vm/transfer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,16 +18,16 @@ impl ScenarioVMRunner {

pub fn perform_validator_reward(&mut self, validator_rewards_step: &ValidatorRewardStep) {
self.blockchain_mock.state.increase_validator_reward(
&validator_rewards_step.tx.to.to_vm_address(),
&validator_rewards_step.tx.to.to_address(),
&validator_rewards_step.tx.egld_value.value,
);
}
}

fn tx_input_from_transfer(tx_transfer: &TxTransfer) -> TxInput {
TxInput {
from: tx_transfer.from.to_vm_address(),
to: tx_transfer.to.to_vm_address(),
from: tx_transfer.from.to_address(),
to: tx_transfer.to.to_address(),
egld_value: tx_transfer.egld_value.value.clone(),
esdt_values: tx_esdt_transfers_from_scenario(tx_transfer.esdt_value.as_slice()),
func_name: TxFunctionName::EMPTY,
Expand Down
45 changes: 15 additions & 30 deletions framework/scenario/src/whitebox_legacy/contract_obj_wrapper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,7 @@ use crate::{
ScenarioWorld,
};
use multiversx_chain_scenario_format::interpret_trait::InterpretableFrom;
use multiversx_chain_vm::{
tx_mock::{TxContext, TxContextStack, TxFunctionName, TxResult},
types::VMAddress,
};
use multiversx_chain_vm::tx_mock::{TxContext, TxContextStack, TxFunctionName, TxResult};
use multiversx_sc::types::{BigUint, H256};
use num_traits::Zero;

Expand Down Expand Up @@ -89,7 +86,7 @@ impl BlockchainStateWrapper {
}

pub fn check_egld_balance(&self, address: &Address, expected_balance: &num_bigint::BigUint) {
let actual_balance = match &self.world.get_state().accounts.get(&to_vm_address(address)) {
let actual_balance = match &self.world.get_state().accounts.get(address) {
Some(acc) => acc.egld_balance.clone(),
None => num_bigint::BigUint::zero(),
};
Expand All @@ -109,7 +106,7 @@ impl BlockchainStateWrapper {
token_id: &[u8],
expected_balance: &num_bigint::BigUint,
) {
let actual_balance = match &self.world.get_state().accounts.get(&to_vm_address(address)) {
let actual_balance = match &self.world.get_state().accounts.get(address) {
Some(acc) => acc.esdt.get_esdt_balance(token_id, 0),
None => num_bigint::BigUint::zero(),
};
Expand All @@ -135,7 +132,7 @@ impl BlockchainStateWrapper {
T: TopEncode + TopDecode + PartialEq + core::fmt::Debug,
{
let (actual_balance, actual_attributes_serialized) =
match &self.world.get_state().accounts.get(&to_vm_address(address)) {
match &self.world.get_state().accounts.get(address) {
Some(acc) => {
let esdt_data = acc.esdt.get_by_identifier_or_default(token_id);
let opt_instance = esdt_data.instances.get_by_nonce(nonce);
Expand Down Expand Up @@ -304,20 +301,19 @@ impl BlockchainStateWrapper {
CB: ContractBase<Api = DebugApi> + CallableContract + 'static,
ContractObjBuilder: 'static + Copy + Fn() -> CB,
{
let deployer_vm_address = to_vm_address(deployer);
let deployer_acc = self
.world
.get_state()
.accounts
.get(&deployer_vm_address)
.get(deployer)
.unwrap()
.clone();

let new_sc_address = self.address_factory.new_sc_address();
self.world.get_mut_state().put_new_address(
deployer_vm_address,
deployer.clone(),
deployer_acc.nonce,
to_vm_address(&new_sc_address),
new_sc_address.clone(),
);

ContractObjWrapper::new(new_sc_address, obj_builder)
Expand Down Expand Up @@ -493,8 +489,7 @@ impl BlockchainStateWrapper {
}

pub fn add_mandos_set_account(&mut self, address: &Address) {
let vm_address = to_vm_address(address);
if let Some(acc) = self.world.get_state().accounts.get(&vm_address).cloned() {
if let Some(acc) = self.world.get_state().accounts.get(address).cloned() {
let opt_contract_path = self.address_to_code_path.get(address);
if let Some(trace) = &mut self.world.get_mut_debugger_backend().trace {
MandosGenerator::new(&mut trace.scenario_trace, &mut self.current_tx_id)
Expand All @@ -504,8 +499,7 @@ impl BlockchainStateWrapper {
}

pub fn add_mandos_check_account(&mut self, address: &Address) {
let vm_address = to_vm_address(address);
if let Some(acc) = self.world.get_state().accounts.get(&vm_address).cloned() {
if let Some(acc) = self.world.get_state().accounts.get(address).cloned() {
if let Some(trace) = &mut self.world.get_mut_debugger_backend().trace {
MandosGenerator::new(&mut trace.scenario_trace, &mut self.current_tx_id)
.check_account(&acc);
Expand Down Expand Up @@ -662,7 +656,7 @@ impl BlockchainStateWrapper {

impl BlockchainStateWrapper {
pub fn get_egld_balance(&self, address: &Address) -> num_bigint::BigUint {
match self.world.get_state().accounts.get(&to_vm_address(address)) {
match self.world.get_state().accounts.get(address) {
Some(acc) => acc.egld_balance.clone(),
None => panic!(
"get_egld_balance: Account {:?} does not exist",
Expand All @@ -677,7 +671,7 @@ impl BlockchainStateWrapper {
token_id: &[u8],
token_nonce: u64,
) -> num_bigint::BigUint {
match self.world.get_state().accounts.get(&to_vm_address(address)) {
match self.world.get_state().accounts.get(address) {
Some(acc) => acc.esdt.get_esdt_balance(token_id, token_nonce),
None => panic!(
"get_esdt_balance: Account {:?} does not exist",
Expand All @@ -692,7 +686,7 @@ impl BlockchainStateWrapper {
token_id: &[u8],
token_nonce: u64,
) -> Option<T> {
match self.world.get_state().accounts.get(&to_vm_address(address)) {
match self.world.get_state().accounts.get(address) {
Some(acc) => match acc.esdt.get_by_identifier(token_id) {
Some(esdt_data) => esdt_data
.instances
Expand All @@ -708,8 +702,8 @@ impl BlockchainStateWrapper {
}

pub fn dump_state(&self) {
for addr in self.world.get_state().accounts.keys() {
self.dump_state_for_account_hex_attributes(&to_framework_address(addr));
for address in self.world.get_state().accounts.keys() {
self.dump_state_for_account_hex_attributes(address);
println!();
}
}
Expand All @@ -725,8 +719,7 @@ impl BlockchainStateWrapper {
&self,
address: &Address,
) {
let vm_address = to_vm_address(address);
let account = match self.world.get_state().accounts.get(&vm_address) {
let account = match self.world.get_state().accounts.get(address) {
Some(acc) => acc,
None => panic!(
"dump_state_for_account: Account {:?} does not exist",
Expand Down Expand Up @@ -817,11 +810,3 @@ where
let c_base = func();
Box::new(c_base)
}

fn to_vm_address(address: &Address) -> VMAddress {
address.as_array().into()
}

fn to_framework_address(vm_address: &VMAddress) -> Address {
vm_address.as_array().into()
}

0 comments on commit e31136e

Please sign in to comment.