Skip to content

Commit

Permalink
Integration origin from runtime context (#562)
Browse files Browse the repository at this point in the history
  • Loading branch information
vyzo authored Aug 19, 2022
1 parent 36ec85a commit 0dc90b0
Show file tree
Hide file tree
Showing 8 changed files with 60 additions and 29 deletions.
45 changes: 28 additions & 17 deletions Cargo.lock

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

4 changes: 2 additions & 2 deletions actors/evm/src/interpreter/execution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,9 +79,9 @@ pub fn execute<'r, BS: Blockstore, RT: Runtime<BS>>(
OpCode::SHR => bitwise::shr(&mut runtime.stack),
OpCode::SAR => bitwise::sar(&mut runtime.stack),
OpCode::KECCAK256 => hash::keccak256(runtime)?,
OpCode::ADDRESS => context::address(runtime, system)?,
OpCode::ADDRESS => context::address(runtime, system),
OpCode::BALANCE => storage::balance(runtime, system)?,
OpCode::CALLER => context::caller(runtime, system)?,
OpCode::CALLER => context::caller(runtime, system),
OpCode::CALLVALUE => context::call_value(runtime, system),
OpCode::CALLDATALOAD => call::calldataload(runtime),
OpCode::CALLDATASIZE => call::calldatasize(runtime),
Expand Down
18 changes: 9 additions & 9 deletions actors/evm/src/interpreter/instructions/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,10 @@ pub fn blockhash<'r, BS: Blockstore, RT: Runtime<BS>>(

#[inline]
pub fn caller<'r, BS: Blockstore, RT: Runtime<BS>>(
_state: &mut ExecutionState,
_platform: &'r System<'r, BS, RT>,
) -> Result<(), StatusCode> {
todo!()
state: &mut ExecutionState,
platform: &'r System<'r, BS, RT>,
) {
state.stack.push(U256::from(platform.rt.message().caller().id().unwrap()))
}

#[inline]
Expand All @@ -30,18 +30,18 @@ pub fn call_value<'r, BS: Blockstore, RT: Runtime<BS>>(

#[inline]
pub fn address<'r, BS: Blockstore, RT: Runtime<BS>>(
_state: &mut ExecutionState,
_platform: &'r System<'r, BS, RT>,
) -> Result<(), StatusCode> {
todo!()
state: &mut ExecutionState,
platform: &'r System<'r, BS, RT>,
) {
state.stack.push(U256::from(platform.rt.message().receiver().id().unwrap()))
}

#[inline]
pub fn origin<'r, BS: Blockstore, RT: Runtime<BS>>(
state: &mut ExecutionState,
platform: &'r System<'r, BS, RT>,
) {
state.stack.push(U256::from(platform.rt.message().caller().id().unwrap()))
state.stack.push(U256::from(platform.rt.message().origin().id().unwrap()))
}

#[inline]
Expand Down
3 changes: 2 additions & 1 deletion actors/evm/tests/basic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ fn basic_contract_construction_and_invocation() {
// invoke constructor
rt.actor_code_cids.insert(contract, *EVM_ACTOR_CODE_ID);
rt.expect_validate_caller_any();
rt.set_origin(contract);

let result = rt
.call::<evm::EvmContractActor>(
Expand All @@ -33,7 +34,7 @@ fn basic_contract_construction_and_invocation() {
solidity_params.append(&mut hex::decode("f8b2cb4f").unwrap()); // function selector
// caller id address in U256 form
let mut arg0 = vec![0u8; 32];
//arg0[31] = 100;
arg0[31] = 100; // the contract address
solidity_params.append(&mut arg0);

let params = evm::InvokeParams { input_data: RawBytes::from(solidity_params) };
Expand Down
4 changes: 4 additions & 0 deletions runtime/src/runtime/fvm.rs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,10 @@ impl MessageInfo for FvmMessage {
Address::new_id(fvm::message::caller())
}

fn origin(&self) -> Address {
Address::new_id(fvm::message::origin())
}

fn receiver(&self) -> Address {
Address::new_id(fvm::message::receiver())
}
Expand Down
3 changes: 3 additions & 0 deletions runtime/src/runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,9 @@ pub trait MessageInfo {
/// The address of the immediate calling actor. Always an ID-address.
fn caller(&self) -> Address;

/// The address of the origin of the current invocation. Always an ID-address
fn origin(&self) -> Address;

/// The address of the actor receiving the message. Always an ID-address.
fn receiver(&self) -> Address;

Expand Down
9 changes: 9 additions & 0 deletions runtime/src/test_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ pub struct MockRuntime {
pub receiver: Address,
pub caller: Address,
pub caller_type: Cid,
pub origin: Address,
pub value_received: TokenAmount,
pub hash_func: Box<dyn Fn(&[u8]) -> [u8; 32]>,
pub network_version: NetworkVersion,
Expand Down Expand Up @@ -258,6 +259,7 @@ impl Default for MockRuntime {
receiver: Address::new_id(0),
caller: Address::new_id(0),
caller_type: Default::default(),
origin: Address::new_id(0),
value_received: Default::default(),
hash_func: Box::new(blake2b_256),
network_version: NetworkVersion::V0,
Expand Down Expand Up @@ -423,6 +425,10 @@ impl MockRuntime {
self.actor_code_cids.insert(address, code_id);
}

pub fn set_origin(&mut self, address: Address) {
self.origin = address;
}

pub fn set_address_actor_type(&mut self, address: Address, actor_type: Cid) {
self.actor_code_cids.insert(address, actor_type);
}
Expand Down Expand Up @@ -658,6 +664,9 @@ impl MessageInfo for MockRuntime {
fn caller(&self) -> Address {
self.caller
}
fn origin(&self) -> Address {
self.origin
}
fn receiver(&self) -> Address {
self.receiver
}
Expand Down
3 changes: 3 additions & 0 deletions test_vm/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -520,6 +520,9 @@ impl MessageInfo for InvocationCtx<'_, '_> {
fn caller(&self) -> Address {
self.msg.from
}
fn origin(&self) -> Address {
self.resolve_address(&self.top.originator_stable_addr).unwrap()
}
fn receiver(&self) -> Address {
self.to()
}
Expand Down

0 comments on commit 0dc90b0

Please sign in to comment.