revm_interpreter/interpreter/
contract.rs
1use revm_primitives::TxKind;
2
3use super::analysis::to_analysed;
4use crate::{
5 primitives::{Address, Bytecode, Bytes, Env, B256, U256},
6 CallInputs,
7};
8
9#[derive(Clone, Debug, Default)]
11#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
12pub struct Contract {
13 pub input: Bytes,
15 pub bytecode: Bytecode,
18 pub hash: Option<B256>,
20 pub target_address: Address,
22 pub bytecode_address: Option<Address>,
25 pub caller: Address,
27 pub call_value: U256,
29}
30
31impl Contract {
32 #[inline]
34 pub fn new(
35 input: Bytes,
36 bytecode: Bytecode,
37 hash: Option<B256>,
38 target_address: Address,
39 bytecode_address: Option<Address>,
40 caller: Address,
41 call_value: U256,
42 ) -> Self {
43 let bytecode = to_analysed(bytecode);
44
45 Self {
46 input,
47 bytecode,
48 hash,
49 target_address,
50 bytecode_address,
51 caller,
52 call_value,
53 }
54 }
55
56 #[inline]
58 pub fn new_env(env: &Env, bytecode: Bytecode, hash: Option<B256>) -> Self {
59 let contract_address = match env.tx.transact_to {
60 TxKind::Call(caller) => caller,
61 TxKind::Create => Address::ZERO,
62 };
63 let bytecode_address = match env.tx.transact_to {
64 TxKind::Call(caller) => Some(caller),
65 TxKind::Create => None,
66 };
67 Self::new(
68 env.tx.data.clone(),
69 bytecode,
70 hash,
71 contract_address,
72 bytecode_address,
73 env.tx.caller,
74 env.tx.value,
75 )
76 }
77
78 #[inline]
80 pub fn new_with_context(
81 input: Bytes,
82 bytecode: Bytecode,
83 hash: Option<B256>,
84 call_context: &CallInputs,
85 ) -> Self {
86 Self::new(
87 input,
88 bytecode,
89 hash,
90 call_context.target_address,
91 Some(call_context.bytecode_address),
92 call_context.caller,
93 call_context.call_value(),
94 )
95 }
96
97 #[inline]
99 pub fn is_valid_jump(&self, pos: usize) -> bool {
100 self.bytecode
101 .legacy_jump_table()
102 .map(|i| i.is_valid(pos))
103 .unwrap_or(false)
104 }
105}