Skip to content

Commit 7e7163b

Browse files
authored
Allow specifying the caller in set_code function (#291)
* add caller parameter to set_code function * fmt * add SetCodeOrigin enum
1 parent fda8a40 commit 7e7163b

File tree

5 files changed

+35
-9
lines changed

5 files changed

+35
-9
lines changed

interpreter/src/runtime.rs

+13-1
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,13 @@ pub struct Log {
7777
pub data: Vec<u8>,
7878
}
7979

80+
// Identify if the origin of set_code() comes from a transact or subcall.
81+
#[derive(Clone, Debug)]
82+
pub enum SetCodeOrigin {
83+
Transaction,
84+
Subcall(H160),
85+
}
86+
8087
#[auto_impl::auto_impl(&, Box)]
8188
pub trait RuntimeEnvironment {
8289
/// Get environmental block hash.
@@ -156,7 +163,12 @@ pub trait RuntimeBackend: RuntimeBaseBackend {
156163
/// Fully delete storages of an account.
157164
fn reset_storage(&mut self, address: H160);
158165
/// Set code of an account.
159-
fn set_code(&mut self, address: H160, code: Vec<u8>) -> Result<(), ExitError>;
166+
fn set_code(
167+
&mut self,
168+
address: H160,
169+
code: Vec<u8>,
170+
origin: SetCodeOrigin,
171+
) -> Result<(), ExitError>;
160172
/// Reset balance of an account.
161173
fn reset_balance(&mut self, address: H160);
162174
fn deposit(&mut self, target: H160, value: U256);

interpreter/tests/usability.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use evm_interpreter::{
77
opcode::Opcode,
88
runtime::{
99
Context, Log, RuntimeBackend, RuntimeBaseBackend, RuntimeEnvironment, RuntimeState,
10-
TransactionContext,
10+
SetCodeOrigin, TransactionContext,
1111
},
1212
EtableInterpreter, RunInterpreter,
1313
};
@@ -165,7 +165,12 @@ impl RuntimeBackend for UnimplementedHandler {
165165
unimplemented!()
166166
}
167167

168-
fn set_code(&mut self, _address: H160, _code: Vec<u8>) -> Result<(), ExitError> {
168+
fn set_code(
169+
&mut self,
170+
_address: H160,
171+
_code: Vec<u8>,
172+
_origin: SetCodeOrigin,
173+
) -> Result<(), ExitError> {
169174
unimplemented!()
170175
}
171176

src/backend/overlayed.rs

+7-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use core::mem;
77

88
use evm_interpreter::{
99
error::{ExitError, ExitException},
10-
runtime::{Log, RuntimeBackend, RuntimeBaseBackend, RuntimeEnvironment},
10+
runtime::{Log, RuntimeBackend, RuntimeBaseBackend, RuntimeEnvironment, SetCodeOrigin},
1111
};
1212
use primitive_types::{H160, H256, U256};
1313

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

195-
fn set_code(&mut self, address: H160, code: Vec<u8>) -> Result<(), ExitError> {
195+
fn set_code(
196+
&mut self,
197+
address: H160,
198+
code: Vec<u8>,
199+
_origin: SetCodeOrigin,
200+
) -> Result<(), ExitError> {
196201
self.substate.codes.insert(address, code);
197202
Ok(())
198203
}

src/standard/invoker/mod.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ use evm_interpreter::{
1212
},
1313
opcode::Opcode,
1414
runtime::{
15-
Context, GasState, RuntimeBackend, RuntimeEnvironment, RuntimeState, TransactionContext,
16-
Transfer,
15+
Context, GasState, RuntimeBackend, RuntimeEnvironment, RuntimeState, SetCodeOrigin,
16+
TransactionContext, Transfer,
1717
},
1818
Interpreter,
1919
};
@@ -375,6 +375,7 @@ where
375375
retbuf,
376376
&mut substate,
377377
handler,
378+
SetCodeOrigin::Transaction,
378379
)?;
379380

380381
Ok(TransactValue::Create {
@@ -564,6 +565,7 @@ where
564565
match trap_data {
565566
SubstackInvoke::Create { address, trap } => {
566567
let retbuf = retval;
568+
let caller = trap.scheme.caller();
567569

568570
let result = result.and_then(|_| {
569571
routines::deploy_create_code(
@@ -572,6 +574,7 @@ where
572574
retbuf.clone(),
573575
&mut substate,
574576
handler,
577+
SetCodeOrigin::Subcall(caller),
575578
)?;
576579

577580
Ok(address)

src/standard/invoker/routines.rs

+3-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ use alloc::vec::Vec;
33
use evm_interpreter::{
44
error::{CallTrapData, CreateTrapData, ExitError, ExitException, ExitResult},
55
opcode::Opcode,
6-
runtime::{RuntimeBackend, RuntimeEnvironment, RuntimeState, Transfer},
6+
runtime::{RuntimeBackend, RuntimeEnvironment, RuntimeState, SetCodeOrigin, Transfer},
77
};
88
use primitive_types::{H160, U256};
99

@@ -197,6 +197,7 @@ pub fn deploy_create_code<'config, S, H>(
197197
retbuf: Vec<u8>,
198198
state: &mut S,
199199
handler: &mut H,
200+
origin: SetCodeOrigin,
200201
) -> Result<(), ExitError>
201202
where
202203
S: InvokerState<'config>,
@@ -212,7 +213,7 @@ where
212213

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

215-
handler.set_code(address, retbuf)?;
216+
handler.set_code(address, retbuf, origin)?;
216217

217218
Ok(())
218219
}

0 commit comments

Comments
 (0)