Skip to content

Commit 785be28

Browse files
authored
feat: expose target/bytecode addresses on PrecompileInput (#161)
* feat: expose target/bytecode addresses on PrecompileInput * is_direct_call
1 parent 1650da0 commit 785be28

File tree

1 file changed

+67
-0
lines changed

1 file changed

+67
-0
lines changed

crates/evm/src/precompiles.rs

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -406,6 +406,8 @@ where
406406
caller: inputs.caller_address,
407407
value: inputs.call_value,
408408
internals: EvmInternals::new(journal, &context.block),
409+
target_address: inputs.target_address,
410+
bytecode_address: inputs.bytecode_address.expect("always set for precompile calls"),
409411
});
410412

411413
match precompile_result {
@@ -524,10 +526,63 @@ pub struct PrecompileInput<'a> {
524526
pub caller: Address,
525527
/// Value sent with the call.
526528
pub value: U256,
529+
/// Target address of the call. Would be the same as `bytecode_address` unless it's a
530+
/// DELEGATECALL.
531+
pub target_address: Address,
532+
/// Bytecode address of the call.
533+
pub bytecode_address: Address,
527534
/// Various hooks for interacting with the EVM state.
528535
pub internals: EvmInternals<'a>,
529536
}
530537

538+
impl<'a> PrecompileInput<'a> {
539+
/// Returns the calldata of the call.
540+
pub fn data(&self) -> &[u8] {
541+
self.data
542+
}
543+
544+
/// Returns the caller address of the call.
545+
pub fn caller(&self) -> &Address {
546+
&self.caller
547+
}
548+
549+
/// Returns the gas limit of the call.
550+
pub fn gas(&self) -> u64 {
551+
self.gas
552+
}
553+
554+
/// Returns the value of the call.
555+
pub fn value(&self) -> &U256 {
556+
&self.value
557+
}
558+
559+
/// Returns the target address of the call.
560+
pub fn target_address(&self) -> &Address {
561+
&self.target_address
562+
}
563+
564+
/// Returns the bytecode address of the call.
565+
pub fn bytecode_address(&self) -> &Address {
566+
&self.bytecode_address
567+
}
568+
569+
/// Returns whether the call is a direct call, i.e when precompile was called directly and not
570+
/// via a DELEGATECALL/CALLCODE.
571+
pub fn is_direct_call(&self) -> bool {
572+
self.target_address == self.bytecode_address
573+
}
574+
575+
/// Returns the [`EvmInternals`].
576+
pub fn internals(&self) -> &EvmInternals<'_> {
577+
&self.internals
578+
}
579+
580+
/// Returns a mutable reference to the [`EvmInternals`].
581+
pub fn internals_mut(&mut self) -> &mut EvmInternals<'a> {
582+
&mut self.internals
583+
}
584+
}
585+
531586
/// Trait for implementing precompiled contracts.
532587
#[auto_impl::auto_impl(Arc)]
533588
pub trait Precompile {
@@ -697,6 +752,8 @@ mod tests {
697752
caller: Address::ZERO,
698753
value: U256::ZERO,
699754
internals: EvmInternals::new(&mut ctx.journaled_state, &ctx.block),
755+
target_address: identity_address,
756+
bytecode_address: identity_address,
700757
})
701758
.unwrap();
702759
assert_eq!(result.bytes, test_input, "Identity precompile should return the input data");
@@ -729,6 +786,8 @@ mod tests {
729786
caller: Address::ZERO,
730787
value: U256::ZERO,
731788
internals: EvmInternals::new(&mut ctx.journaled_state, &ctx.block),
789+
target_address: identity_address,
790+
bytecode_address: identity_address,
732791
})
733792
.unwrap();
734793
assert_eq!(
@@ -762,6 +821,8 @@ mod tests {
762821
caller: Address::ZERO,
763822
value: U256::ZERO,
764823
internals: EvmInternals::new(&mut ctx.journaled_state, &ctx.block),
824+
target_address: Address::ZERO,
825+
bytecode_address: Address::ZERO,
765826
})
766827
.unwrap();
767828
assert_eq!(result.gas_used, 15);
@@ -832,6 +893,8 @@ mod tests {
832893
caller: Address::ZERO,
833894
value: U256::ZERO,
834895
internals: EvmInternals::new(&mut ctx.journaled_state, &ctx.block),
896+
target_address: dynamic_address,
897+
bytecode_address: dynamic_address,
835898
})
836899
.unwrap();
837900
assert_eq!(result.gas_used, 100);
@@ -863,6 +926,8 @@ mod tests {
863926
gas: gas_limit,
864927
caller: Address::ZERO,
865928
value: U256::ZERO,
929+
target_address: identity_address,
930+
bytecode_address: identity_address,
866931
internals: EvmInternals::new(&mut ctx.journaled_state, &ctx.block),
867932
})
868933
.unwrap();
@@ -891,6 +956,8 @@ mod tests {
891956
caller: Address::ZERO,
892957
value: U256::ZERO,
893958
internals: EvmInternals::new(&mut ctx.journaled_state, &ctx.block),
959+
target_address: identity_address,
960+
bytecode_address: identity_address,
894961
})
895962
.unwrap();
896963
assert_eq!(

0 commit comments

Comments
 (0)