Skip to content

Commit

Permalink
Allow specifying the caller in set_code function (#291)
Browse files Browse the repository at this point in the history
* add caller parameter to set_code function

* fmt

* add SetCodeOrigin enum
  • Loading branch information
Agusrodri authored Jun 25, 2024
1 parent fda8a40 commit 7e7163b
Show file tree
Hide file tree
Showing 5 changed files with 35 additions and 9 deletions.
14 changes: 13 additions & 1 deletion interpreter/src/runtime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,13 @@ pub struct Log {
pub data: Vec<u8>,
}

// Identify if the origin of set_code() comes from a transact or subcall.
#[derive(Clone, Debug)]
pub enum SetCodeOrigin {
Transaction,
Subcall(H160),
}

#[auto_impl::auto_impl(&, Box)]
pub trait RuntimeEnvironment {
/// Get environmental block hash.
Expand Down Expand Up @@ -156,7 +163,12 @@ pub trait RuntimeBackend: RuntimeBaseBackend {
/// Fully delete storages of an account.
fn reset_storage(&mut self, address: H160);
/// Set code of an account.
fn set_code(&mut self, address: H160, code: Vec<u8>) -> Result<(), ExitError>;
fn set_code(
&mut self,
address: H160,
code: Vec<u8>,
origin: SetCodeOrigin,
) -> Result<(), ExitError>;
/// Reset balance of an account.
fn reset_balance(&mut self, address: H160);
fn deposit(&mut self, target: H160, value: U256);
Expand Down
9 changes: 7 additions & 2 deletions interpreter/tests/usability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use evm_interpreter::{
opcode::Opcode,
runtime::{
Context, Log, RuntimeBackend, RuntimeBaseBackend, RuntimeEnvironment, RuntimeState,
TransactionContext,
SetCodeOrigin, TransactionContext,
},
EtableInterpreter, RunInterpreter,
};
Expand Down Expand Up @@ -165,7 +165,12 @@ impl RuntimeBackend for UnimplementedHandler {
unimplemented!()
}

fn set_code(&mut self, _address: H160, _code: Vec<u8>) -> Result<(), ExitError> {
fn set_code(
&mut self,
_address: H160,
_code: Vec<u8>,
_origin: SetCodeOrigin,
) -> Result<(), ExitError> {
unimplemented!()
}

Expand Down
9 changes: 7 additions & 2 deletions src/backend/overlayed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ use core::mem;

use evm_interpreter::{
error::{ExitError, ExitException},
runtime::{Log, RuntimeBackend, RuntimeBaseBackend, RuntimeEnvironment},
runtime::{Log, RuntimeBackend, RuntimeBaseBackend, RuntimeEnvironment, SetCodeOrigin},
};
use primitive_types::{H160, H256, U256};

Expand Down Expand Up @@ -192,7 +192,12 @@ impl<B: RuntimeBaseBackend> RuntimeBackend for OverlayedBackend<B> {
self.substate.storage_resets.insert(address);
}

fn set_code(&mut self, address: H160, code: Vec<u8>) -> Result<(), ExitError> {
fn set_code(
&mut self,
address: H160,
code: Vec<u8>,
_origin: SetCodeOrigin,
) -> Result<(), ExitError> {
self.substate.codes.insert(address, code);
Ok(())
}
Expand Down
7 changes: 5 additions & 2 deletions src/standard/invoker/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@ use evm_interpreter::{
},
opcode::Opcode,
runtime::{
Context, GasState, RuntimeBackend, RuntimeEnvironment, RuntimeState, TransactionContext,
Transfer,
Context, GasState, RuntimeBackend, RuntimeEnvironment, RuntimeState, SetCodeOrigin,
TransactionContext, Transfer,
},
Interpreter,
};
Expand Down Expand Up @@ -375,6 +375,7 @@ where
retbuf,
&mut substate,
handler,
SetCodeOrigin::Transaction,
)?;

Ok(TransactValue::Create {
Expand Down Expand Up @@ -564,6 +565,7 @@ where
match trap_data {
SubstackInvoke::Create { address, trap } => {
let retbuf = retval;
let caller = trap.scheme.caller();

let result = result.and_then(|_| {
routines::deploy_create_code(
Expand All @@ -572,6 +574,7 @@ where
retbuf.clone(),
&mut substate,
handler,
SetCodeOrigin::Subcall(caller),
)?;

Ok(address)
Expand Down
5 changes: 3 additions & 2 deletions src/standard/invoker/routines.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use alloc::vec::Vec;
use evm_interpreter::{
error::{CallTrapData, CreateTrapData, ExitError, ExitException, ExitResult},
opcode::Opcode,
runtime::{RuntimeBackend, RuntimeEnvironment, RuntimeState, Transfer},
runtime::{RuntimeBackend, RuntimeEnvironment, RuntimeState, SetCodeOrigin, Transfer},
};
use primitive_types::{H160, U256};

Expand Down Expand Up @@ -197,6 +197,7 @@ pub fn deploy_create_code<'config, S, H>(
retbuf: Vec<u8>,
state: &mut S,
handler: &mut H,
origin: SetCodeOrigin,
) -> Result<(), ExitError>
where
S: InvokerState<'config>,
Expand All @@ -212,7 +213,7 @@ where

state.record_codedeposit(retbuf.len())?;

handler.set_code(address, retbuf)?;
handler.set_code(address, retbuf, origin)?;

Ok(())
}

0 comments on commit 7e7163b

Please sign in to comment.