diff --git a/shared/src/ledger/native_vp.rs b/shared/src/ledger/native_vp.rs index 1f83f42c42..e4ef2ce651 100644 --- a/shared/src/ledger/native_vp.rs +++ b/shared/src/ledger/native_vp.rs @@ -230,7 +230,7 @@ where } fn get_native_token(&self) -> Result { - Ok(self.ctx.storage.native_token.clone()) + self.ctx.get_native_token() } } @@ -387,6 +387,14 @@ where .into_storage_result() } + fn get_native_token(&'view self) -> Result { + vp_env::get_native_token( + &mut *self.gas_meter.borrow_mut(), + self.storage, + ) + .into_storage_result() + } + fn iter_prefix( &'view self, prefix: &Key, diff --git a/shared/src/ledger/vp_env.rs b/shared/src/ledger/vp_env.rs index 0172f986c5..fab888b9a3 100644 --- a/shared/src/ledger/vp_env.rs +++ b/shared/src/ledger/vp_env.rs @@ -66,6 +66,9 @@ pub trait VpEnv<'view> { /// current transaction is being applied. fn get_block_epoch(&'view self) -> Result; + /// Get the address of the native token. + fn get_native_token(&'view self) -> Result; + /// Storage prefix iterator, ordered by storage keys. It will try to get an /// iterator from the storage. fn iter_prefix( diff --git a/shared/src/vm/host_env.rs b/shared/src/vm/host_env.rs index cf522b786c..47eba121e8 100644 --- a/shared/src/vm/host_env.rs +++ b/shared/src/vm/host_env.rs @@ -1569,10 +1569,10 @@ where let storage = unsafe { env.ctx.storage.get() }; tx_add_gas(env, MIN_STORAGE_GAS)?; let native_token = storage.native_token.clone(); - let native_token_bytes = native_token.try_to_vec().unwrap(); + let native_token_string = native_token.encode(); let gas = env .memory - .write_bytes(result_ptr, native_token_bytes) + .write_string(result_ptr, native_token_string) .map_err(|e| TxRuntimeError::MemoryError(Box::new(e)))?; tx_add_gas(env, gas) } @@ -1841,10 +1841,10 @@ where let gas_meter = unsafe { env.ctx.gas_meter.get() }; let storage = unsafe { env.ctx.storage.get() }; let native_token = vp_env::get_native_token(gas_meter, storage)?; - let native_token_bytes = native_token.try_to_vec().unwrap(); + let native_token_string = native_token.encode(); let gas = env .memory - .write_bytes(result_ptr, native_token_bytes) + .write_string(result_ptr, native_token_string) .map_err(|e| vp_env::RuntimeError::MemoryError(Box::new(e)))?; vp_env::add_gas(gas_meter, gas) } diff --git a/tests/src/vm_host_env/mod.rs b/tests/src/vm_host_env/mod.rs index 3325c6eb6a..75c27f649f 100644 --- a/tests/src/vm_host_env/mod.rs +++ b/tests/src/vm_host_env/mod.rs @@ -260,6 +260,10 @@ mod tests { tx::ctx().get_block_epoch().unwrap(), tx_host_env::with(|env| env.storage.get_current_epoch().0) ); + assert_eq!( + tx::ctx().get_native_token().unwrap(), + tx_host_env::with(|env| env.storage.native_token.clone()) + ); } /// An example how to write a VP host environment integration test @@ -510,6 +514,10 @@ mod tests { vp::CTX.get_block_epoch().unwrap(), vp_host_env::with(|env| env.storage.get_current_epoch().0) ); + assert_eq!( + vp::CTX.get_native_token().unwrap(), + vp_host_env::with(|env| env.storage.native_token.clone()) + ); } #[test] diff --git a/tx_prelude/src/lib.rs b/tx_prelude/src/lib.rs index 3909793f6f..209c8e132f 100644 --- a/tx_prelude/src/lib.rs +++ b/tx_prelude/src/lib.rs @@ -177,8 +177,9 @@ impl StorageRead<'_> for Ctx { let slice = unsafe { slice::from_raw_parts(result.as_ptr(), address::ADDRESS_LEN) }; - Ok(Address::try_from_slice(slice) - .expect("Cannot decode native address")) + let address_str = + std::str::from_utf8(slice).expect("Cannot decode native address"); + Ok(Address::decode(address_str).expect("Cannot decode native address")) } fn iter_prefix( diff --git a/vp_prelude/src/lib.rs b/vp_prelude/src/lib.rs index a302bb5ac3..14fef3133c 100644 --- a/vp_prelude/src/lib.rs +++ b/vp_prelude/src/lib.rs @@ -241,6 +241,11 @@ impl<'view> VpEnv<'view> for Ctx { get_block_epoch() } + fn get_native_token(&'view self) -> Result { + // Both `CtxPreStorageRead` and `CtxPostStorageRead` have the same impl + get_native_token() + } + fn iter_prefix( &self, prefix: &storage::Key, @@ -490,5 +495,7 @@ fn get_native_token() -> Result { } let slice = unsafe { slice::from_raw_parts(result.as_ptr(), address::ADDRESS_LEN) }; - Ok(Address::try_from_slice(slice).expect("Cannot decode native address")) + let address_str = + std::str::from_utf8(slice).expect("Cannot decode native address"); + Ok(Address::decode(address_str).expect("Cannot decode native address")) }