From dbf0e2b181536e2cfd7889323a82881884605d57 Mon Sep 17 00:00:00 2001 From: Mihai Calin Luca Date: Mon, 22 Apr 2024 16:47:04 +0200 Subject: [PATCH] cleanup and extra impls for AddressValue --- .../tests/price_aggregator_blackbox_test.rs | 36 ++++---- .../src/scenario/model/value/address_value.rs | 84 +++++++++++++++++++ 2 files changed, 102 insertions(+), 18 deletions(-) diff --git a/contracts/core/price-aggregator/tests/price_aggregator_blackbox_test.rs b/contracts/core/price-aggregator/tests/price_aggregator_blackbox_test.rs index c57e36711e..bd73790c0a 100644 --- a/contracts/core/price-aggregator/tests/price_aggregator_blackbox_test.rs +++ b/contracts/core/price-aggregator/tests/price_aggregator_blackbox_test.rs @@ -44,26 +44,24 @@ impl PriceAggregatorTestState { fn new() -> Self { let mut world = world(); - let mut set_state_step = SetStateStep::new() - .put_account(OWNER, Account::new().nonce(1)) - .new_address(OWNER_ADDRESS_EXPR, 1, PRICE_AGGREGATOR_ADDRESS_EXPR) - .block_timestamp(100); + world.account(OWNER).nonce(1); + world.current_block().block_timestamp(100); + + world.set_state_step(SetStateStep::new()).new_address( + OWNER_ADDRESS_EXPR, + 1, + PRICE_AGGREGATOR_ADDRESS_EXPR, + ); let mut oracles = Vec::new(); for i in 1..=NR_ORACLES { let address_expr = format!("address:oracle{}", i); let address_value = AddressValue::from(address_expr.as_str()); - set_state_step = set_state_step.put_account( - address_expr.as_str(), - Account::new().nonce(1).balance(STAKE_AMOUNT), - ); - + world.account(address_expr).nonce(1).balance(STAKE_AMOUNT); oracles.push(address_value); } - world.set_state_step(set_state_step); - // let price_aggregator_contract = PriceAggregatorContract::new(PRICE_AGGREGATOR_ADDRESS_EXPR); let price_aggregator_whitebox = WhiteboxContract::new( PRICE_AGGREGATOR_ADDRESS_EXPR, multiversx_price_aggregator_sc::contract_obj, @@ -104,7 +102,7 @@ impl PriceAggregatorTestState { for address in self.oracles.iter() { self.world .tx() - .from(&address.to_address()) + .from(address) .to(PRICE_AGGREGATOR) .typed(price_aggregator_proxy::PriceAggregatorProxy) .stake() @@ -138,7 +136,7 @@ impl PriceAggregatorTestState { fn submit(&mut self, from: &AddressValue, submission_timestamp: u64, price: u64) { self.world .tx() - .from(&from.to_address()) + .from(from) .to(PRICE_AGGREGATOR) .typed(price_aggregator_proxy::PriceAggregatorProxy) .submit( @@ -160,7 +158,7 @@ impl PriceAggregatorTestState { ) { self.world .tx() - .from(&from.to_address()) + .from(from) .to(PRICE_AGGREGATOR) .typed(price_aggregator_proxy::PriceAggregatorProxy) .submit( @@ -178,7 +176,7 @@ impl PriceAggregatorTestState { fn vote_slash_member(&mut self, from: &AddressValue, member_to_slash: Address) { self.world .tx() - .from(&from.to_address()) + .from(from) .to(PRICE_AGGREGATOR) .typed(price_aggregator_proxy::PriceAggregatorProxy) .vote_slash_member(member_to_slash) @@ -283,7 +281,8 @@ fn test_price_aggregator_submit_round_ok() { let current_timestamp = 110; state .world - .set_state_step(SetStateStep::new().block_timestamp(current_timestamp)); + .current_block() + .block_timestamp(current_timestamp); // submit second state.submit(&state.oracles[1].clone(), 101, 11_000); @@ -340,7 +339,8 @@ fn test_price_aggregator_discarded_round() { let current_timestamp = 100 + MAX_ROUND_DURATION_SECONDS + 1; state .world - .set_state_step(SetStateStep::new().block_timestamp(current_timestamp)); + .current_block() + .block_timestamp(current_timestamp); // submit second - this will discard the previous submission state.submit(&state.oracles[1].clone(), current_timestamp - 1, 11_000); @@ -378,7 +378,7 @@ fn test_price_aggregator_slashing() { state .world .tx() - .from(&state.oracles[0].to_address()) + .from(&state.oracles[0]) .to(PRICE_AGGREGATOR) .typed(price_aggregator_proxy::PriceAggregatorProxy) .slash_member(state.oracles[1].to_address()) diff --git a/framework/scenario/src/scenario/model/value/address_value.rs b/framework/scenario/src/scenario/model/value/address_value.rs index 1315a4fbd5..8564bb6e47 100644 --- a/framework/scenario/src/scenario/model/value/address_value.rs +++ b/framework/scenario/src/scenario/model/value/address_value.rs @@ -1,3 +1,7 @@ +use multiversx_sc::{ + api::ManagedTypeApi, + types::{AnnotatedValue, ManagedAddress, ManagedBuffer, TxEnv, TxFrom, TxFromSpecified}, +}; use std::fmt; use crate::multiversx_sc::types::{Address, AddressExpr, ScExpr}; @@ -151,3 +155,83 @@ impl From> for AddressValue { } } } + +impl From<&AddressValue> for ManagedAddress +where + M: ManagedTypeApi, +{ + #[inline] + fn from(address_value: &AddressValue) -> Self { + ManagedAddress::from_address(&address_value.value) + } +} + +impl TxFrom for AddressValue +where + Env: TxEnv, +{ + fn resolve_address(&self, _env: &Env) -> ManagedAddress { + self.into() + } +} + +impl AnnotatedValue> for AddressValue +where + Env: TxEnv, +{ + fn annotation(&self, _env: &Env) -> multiversx_sc::types::ManagedBuffer { + ManagedBuffer::from(self.original.to_string()) + } + + fn to_value(&self, _env: &Env) -> ManagedAddress { + ManagedAddress::from_address(&self.value) + } + + fn into_value(self, _env: &Env) -> ManagedAddress { + ManagedAddress::from_address(&self.value) + } + + fn with_value_ref(&self, _env: &Env, f: F) -> R + where + F: FnOnce(&ManagedAddress) -> R, + { + f(&ManagedAddress::from_address(&self.value)) + } +} + +impl AnnotatedValue> for &AddressValue +where + Env: TxEnv, +{ + fn annotation(&self, _env: &Env) -> multiversx_sc::types::ManagedBuffer { + ManagedBuffer::from(self.original.to_string()) + } + + fn to_value(&self, _env: &Env) -> ManagedAddress { + ManagedAddress::from_address(&self.value) + } + + fn into_value(self, _env: &Env) -> ManagedAddress { + ManagedAddress::from_address(&self.value) + } + + fn with_value_ref(&self, _env: &Env, f: F) -> R + where + F: FnOnce(&ManagedAddress) -> R, + { + f(&ManagedAddress::from_address(&self.value)) + } +} + +impl TxFromSpecified for AddressValue where Env: TxEnv {} + +impl TxFrom for &AddressValue +where + Env: TxEnv, +{ + fn resolve_address(&self, _env: &Env) -> ManagedAddress { + ManagedAddress::from_address(&self.value) + } +} + +impl TxFromSpecified for &AddressValue where Env: TxEnv {}