Skip to content

Commit

Permalink
Fix get_invoker_type (#486)
Browse files Browse the repository at this point in the history
* Fix get_invoker_type

* fix
  • Loading branch information
leighmcculloch authored Sep 26, 2022
1 parent ed3056b commit e110dff
Show file tree
Hide file tree
Showing 14 changed files with 24 additions and 23 deletions.
2 changes: 1 addition & 1 deletion soroban-env-common/src/env.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ macro_rules! call_macro_with_all_host_functions {
{"a", fn log_fmt_values(fmt:Object, args:Object) -> RawVal }
/// Get whether the contract invocation is from an account or
/// another contract. Returns 0 for account, 1 for contract.
{"b", fn get_invoker_type() -> u32 }
{"b", fn get_invoker_type() -> u64 }
/// Get the AccountID object type of the account which invoked
/// the running contract. Traps if the running contract was not
/// invoked by an account.
Expand Down
14 changes: 7 additions & 7 deletions soroban-env-common/src/invoker.rs
Original file line number Diff line number Diff line change
@@ -1,24 +1,24 @@
use crate::ConversionError;

#[derive(Copy, Clone, Debug, Eq, PartialEq, PartialOrd, Ord)]
#[repr(u32)]
#[repr(u64)]
pub enum InvokerType {
Account = 0,
Contract = 1,
}

impl From<InvokerType> for u32 {
impl From<InvokerType> for u64 {
fn from(v: InvokerType) -> Self {
v as u32
v as u64
}
}

impl TryFrom<u32> for InvokerType {
impl TryFrom<u64> for InvokerType {
type Error = ConversionError;

fn try_from(v: u32) -> Result<Self, Self::Error> {
const ACCOUNT: u32 = InvokerType::Account as u32;
const CONTRACT: u32 = InvokerType::Contract as u32;
fn try_from(v: u64) -> Result<Self, Self::Error> {
const ACCOUNT: u64 = InvokerType::Account as u64;
const CONTRACT: u64 = InvokerType::Contract as u64;
match v {
ACCOUNT => Ok(Self::Account),
CONTRACT => Ok(Self::Contract),
Expand Down
2 changes: 1 addition & 1 deletion soroban-env-common/src/meta.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,5 +56,5 @@
// implementations over a long period of time.

soroban_env_macros::generate_env_meta_consts!(
interface_version: 18,
interface_version: 19,
);
13 changes: 7 additions & 6 deletions soroban-env-host/src/host.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1159,21 +1159,22 @@ impl VmCallerCheckedEnv for Host {
}

// TODO: Assess metering.
fn get_invoker_type(&self, _vmcaller: &mut VmCaller<Host>) -> Result<u32, HostError> {
fn get_invoker_type(&self, _vmcaller: &mut VmCaller<Host>) -> Result<u64, HostError> {
let frames = self.0.context.borrow();
// If the previous frame exists and is a contract, return its ID, otherwise return
// the account invoking.
let st = match frames.as_slice() {
// There are always two frames when WASM is executed in the VM.
[.., f2, _] => match f2 {
#[cfg(feature = "vm")]
Frame::ContractVM(_, _) => Ok(InvokerType::Contract),
Frame::HostFunction(_) => Err(self.err_general(
"host function found in second to last frame which is unexpected",
)),
Frame::HostFunction(_) => Ok(InvokerType::Account),
Frame::Token(id, _) => Ok(InvokerType::Contract),
#[cfg(feature = "testutils")]
Frame::TestContract(_, _) => Ok(InvokerType::Contract),
},
// In tests contracts are executed with a single frame.
// TODO: Investigate this discrepancy: https://github.com/stellar/rs-soroban-env/issues/485.
[f1] => match f1 {
#[cfg(feature = "vm")]
Frame::ContractVM(_, _) => {
Expand All @@ -1188,12 +1189,12 @@ impl VmCallerCheckedEnv for Host {
},
_ => Err(self.err_general("no frames to derive the invoker from")),
}?;
Ok(st as u32)
Ok(st as u64)
}

// Notes on metering: covered by the components
fn get_invoking_account(&self, vmcaller: &mut VmCaller<Host>) -> Result<Object, HostError> {
if self.get_invoker_type(vmcaller)? != InvokerType::Account as u32 {
if self.get_invoker_type(vmcaller)? != InvokerType::Account as u64 {
return Err(self.err_general("invoker is not an account"));
}
Ok(self
Expand Down
2 changes: 1 addition & 1 deletion soroban-env-host/src/host/data_helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl Host {

// notes on metering: covers the key and salt. Rest are free.
pub fn id_preimage_from_source_account(&self, salt: Uint256) -> Result<Vec<u8>, HostError> {
if self.get_invoker_type()? != InvokerType::Account as u32 {
if self.get_invoker_type()? != InvokerType::Account as u64 {
return Err(self.err_general("invoker is not an account"));
}

Expand Down
8 changes: 4 additions & 4 deletions soroban-test-wasms/wasm-workspace/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions soroban-test-wasms/wasm-workspace/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -94,9 +94,9 @@ wasmi = { package = "soroban-wasmi", git = "https://github.com/stellar/wasmi", r
# when you change the Env trait itself, and whatever change you make to Env may
# well cause recompilation to fail. Again, you will know if it doesn't compile!

soroban-sdk = { git = "https://github.com/stellar/rs-soroban-sdk", rev = "1d1c866d" }
soroban-sdk-macros = { git = "https://github.com/stellar/rs-soroban-sdk", rev = "1d1c866d" }
soroban-spec = { git = "https://github.com/stellar/rs-soroban-sdk", rev = "ad95e20c" }
soroban-sdk = { git = "https://github.com/stellar/rs-soroban-sdk", rev = "fcbcc1bf" }
soroban-sdk-macros = { git = "https://github.com/stellar/rs-soroban-sdk", rev = "fcbcc1bf" }
soroban-spec = { git = "https://github.com/stellar/rs-soroban-sdk", rev = "fcbcc1bf" }

# soroban-sdk = { path = "../../../rs-soroban-sdk/soroban-sdk" }
# soroban-sdk-macros = { path = "../../../rs-soroban-sdk/soroban-sdk-macros"}
Expand Down
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_add_i32.wasm
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_contract_data.wasm
Binary file not shown.
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_fib.wasm
Binary file not shown.
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_linear_memory.wasm
Binary file not shown.
Binary file modified soroban-test-wasms/wasm-workspace/opt/example_vec.wasm
Binary file not shown.

0 comments on commit e110dff

Please sign in to comment.