Skip to content

Commit

Permalink
use address string encoding for WASM FFI and add native_token to VpEnv
Browse files Browse the repository at this point in the history
  • Loading branch information
tzemanovic committed Oct 26, 2022
1 parent f1a6799 commit 922f3b9
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 8 deletions.
10 changes: 9 additions & 1 deletion shared/src/ledger/native_vp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ where
}

fn get_native_token(&self) -> Result<Address, storage_api::Error> {
Ok(self.ctx.storage.native_token.clone())
self.ctx.get_native_token()
}
}

Expand Down Expand Up @@ -387,6 +387,14 @@ where
.into_storage_result()
}

fn get_native_token(&'view self) -> Result<Address, storage_api::Error> {
vp_env::get_native_token(
&mut *self.gas_meter.borrow_mut(),
self.storage,
)
.into_storage_result()
}

fn iter_prefix(
&'view self,
prefix: &Key,
Expand Down
3 changes: 3 additions & 0 deletions shared/src/ledger/vp_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,9 @@ pub trait VpEnv<'view> {
/// current transaction is being applied.
fn get_block_epoch(&'view self) -> Result<Epoch, storage_api::Error>;

/// Get the address of the native token.
fn get_native_token(&'view self) -> Result<Address, storage_api::Error>;

/// Storage prefix iterator, ordered by storage keys. It will try to get an
/// iterator from the storage.
fn iter_prefix(
Expand Down
8 changes: 4 additions & 4 deletions shared/src/vm/host_env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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)
}
Expand Down
8 changes: 8 additions & 0 deletions tests/src/vm_host_env/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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]
Expand Down
5 changes: 3 additions & 2 deletions tx_prelude/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
9 changes: 8 additions & 1 deletion vp_prelude/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,6 +241,11 @@ impl<'view> VpEnv<'view> for Ctx {
get_block_epoch()
}

fn get_native_token(&'view self) -> Result<Address, Error> {
// Both `CtxPreStorageRead` and `CtxPostStorageRead` have the same impl
get_native_token()
}

fn iter_prefix(
&self,
prefix: &storage::Key,
Expand Down Expand Up @@ -490,5 +495,7 @@ fn get_native_token() -> Result<Address, Error> {
}
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"))
}

0 comments on commit 922f3b9

Please sign in to comment.