linera_execution/evm/
inputs.rs1use alloy_primitives::Bytes;
8use linera_base::{
9 crypto::CryptoHash,
10 data_types::StreamUpdate,
11 ensure,
12 identifiers::{ApplicationId, ChainId, GenericApplicationId, StreamId, StreamName},
13};
14use revm_primitives::{address, Address, B256, U256};
15
16use crate::EvmExecutionError;
17
18alloy_sol_types::sol! {
19 struct InternalApplicationId {
20 bytes32 application_description_hash;
21 }
22
23 struct InternalGenericApplicationId {
24 uint8 choice;
25 InternalApplicationId user;
26 }
27
28 struct InternalStreamName {
29 bytes stream_name;
30 }
31
32 struct InternalStreamId {
33 InternalGenericApplicationId application_id;
34 InternalStreamName stream_name;
35 }
36
37 struct InternalChainId {
38 bytes32 value;
39 }
40
41 struct InternalStreamUpdate {
42 InternalChainId chain_id;
43 InternalStreamId stream_id;
44 uint32 previous_index;
45 uint32 next_index;
46 }
47
48 function process_streams(InternalStreamUpdate[] internal_streams);
49}
50
51fn crypto_hash_to_internal_crypto_hash(hash: CryptoHash) -> B256 {
52 let hash = <[u64; 4]>::from(hash);
53 let hash = linera_base::crypto::u64_array_to_be_bytes(hash);
54 hash.into()
55}
56
57impl From<ApplicationId> for InternalApplicationId {
58 fn from(application_id: ApplicationId) -> InternalApplicationId {
59 let application_description_hash =
60 crypto_hash_to_internal_crypto_hash(application_id.application_description_hash);
61 InternalApplicationId {
62 application_description_hash,
63 }
64 }
65}
66
67impl From<GenericApplicationId> for InternalGenericApplicationId {
68 fn from(generic_application_id: GenericApplicationId) -> InternalGenericApplicationId {
69 match generic_application_id {
70 GenericApplicationId::System => {
71 let application_description_hash = B256::ZERO;
72 InternalGenericApplicationId {
73 choice: 0,
74 user: InternalApplicationId {
75 application_description_hash,
76 },
77 }
78 }
79 GenericApplicationId::User(application_id) => InternalGenericApplicationId {
80 choice: 1,
81 user: application_id.into(),
82 },
83 }
84 }
85}
86
87impl From<ChainId> for InternalChainId {
88 fn from(chain_id: ChainId) -> InternalChainId {
89 let value = crypto_hash_to_internal_crypto_hash(chain_id.0);
90 InternalChainId { value }
91 }
92}
93
94impl From<StreamName> for InternalStreamName {
95 fn from(stream_name: StreamName) -> InternalStreamName {
96 let stream_name = Bytes::from(stream_name.0);
97 InternalStreamName { stream_name }
98 }
99}
100
101impl From<StreamId> for InternalStreamId {
102 fn from(stream_id: StreamId) -> InternalStreamId {
103 let application_id = stream_id.application_id.into();
104 let stream_name = stream_id.stream_name.into();
105 InternalStreamId {
106 application_id,
107 stream_name,
108 }
109 }
110}
111
112impl From<StreamUpdate> for InternalStreamUpdate {
113 fn from(stream_update: StreamUpdate) -> InternalStreamUpdate {
114 let chain_id = stream_update.chain_id.into();
115 let stream_id = stream_update.stream_id.into();
116 InternalStreamUpdate {
117 chain_id,
118 stream_id,
119 previous_index: stream_update.previous_index,
120 next_index: stream_update.next_index,
121 }
122 }
123}
124
125pub(crate) const PRECOMPILE_ADDRESS: Address = address!("000000000000000000000000000000000000000b");
128
129pub(crate) const ZERO_ADDRESS: Address = address!("0000000000000000000000000000000000000000");
133
134pub(crate) const SERVICE_ADDRESS: Address = address!("0000000000000000000000000000000000002000");
136
137pub(crate) const FAUCET_ADDRESS: Address = address!("0000000000000000000000000000000000004000");
139pub(crate) const FAUCET_BALANCE: U256 = U256::from_limbs([
140 0xffffffffffffffff,
141 0xffffffffffffffff,
142 0xffffffffffffffff,
143 0x7fffffffffffffff,
144]);
145
146pub(crate) const EXECUTE_MESSAGE_SELECTOR: &[u8] = &[173, 125, 234, 205];
149
150pub(crate) const PROCESS_STREAMS_SELECTOR: &[u8] = &[254, 72, 102, 28];
153
154pub(crate) const INSTANTIATE_SELECTOR: &[u8] = &[156, 163, 60, 158];
157
158pub(crate) fn forbid_execute_operation_origin(vec: &[u8]) -> Result<(), EvmExecutionError> {
159 ensure!(
160 vec != EXECUTE_MESSAGE_SELECTOR,
161 EvmExecutionError::IllegalOperationCall("function execute_message".to_string(),)
162 );
163 ensure!(
164 vec != PROCESS_STREAMS_SELECTOR,
165 EvmExecutionError::IllegalOperationCall("function process_streams".to_string(),)
166 );
167 ensure!(
168 vec != INSTANTIATE_SELECTOR,
169 EvmExecutionError::IllegalOperationCall("function instantiate".to_string(),)
170 );
171 Ok(())
172}
173
174pub(crate) fn ensure_message_length(
175 actual_length: usize,
176 min_length: usize,
177) -> Result<(), EvmExecutionError> {
178 ensure!(
179 actual_length >= min_length,
180 EvmExecutionError::OperationIsTooShort
181 );
182 Ok(())
183}
184
185pub(crate) fn ensure_selector_presence(
186 module: &[u8],
187 selector: &[u8],
188 fct_name: &str,
189) -> Result<(), EvmExecutionError> {
190 ensure!(
191 has_selector(module, selector),
192 EvmExecutionError::MissingFunction(fct_name.to_string())
193 );
194 Ok(())
195}
196
197pub(crate) fn has_selector(module: &[u8], selector: &[u8]) -> bool {
198 let push4 = 0x63; let mut vec = vec![push4];
200 vec.extend(selector);
201 module.windows(5).any(|window| window == vec)
202}
203
204pub(crate) fn get_revm_instantiation_bytes(value: Vec<u8>) -> Vec<u8> {
205 use alloy_primitives::Bytes;
206 use alloy_sol_types::{sol, SolCall};
207 sol! {
208 function instantiate(bytes value);
209 }
210 let bytes = Bytes::from(value);
211 let argument = instantiateCall { value: bytes };
212 argument.abi_encode()
213}
214
215pub(crate) fn get_revm_execute_message_bytes(value: Vec<u8>) -> Vec<u8> {
216 use alloy_primitives::Bytes;
217 use alloy_sol_types::{sol, SolCall};
218 sol! {
219 function execute_message(bytes value);
220 }
221 let value = Bytes::from(value);
222 let argument = execute_messageCall { value };
223 argument.abi_encode()
224}
225
226pub(crate) fn get_revm_process_streams_bytes(streams: Vec<StreamUpdate>) -> Vec<u8> {
227 use alloy_sol_types::SolCall;
228
229 let internal_streams = streams.into_iter().map(StreamUpdate::into).collect();
230
231 let fct_call = process_streamsCall { internal_streams };
232 fct_call.abi_encode()
233}
234
235#[cfg(test)]
236mod tests {
237 use revm_primitives::keccak256;
238
239 use crate::evm::inputs::{
240 process_streamsCall, EXECUTE_MESSAGE_SELECTOR, INSTANTIATE_SELECTOR,
241 PROCESS_STREAMS_SELECTOR,
242 };
243
244 #[test]
247 fn check_execute_message_selector() {
248 let selector = &keccak256("execute_message(bytes)".as_bytes())[..4];
249 assert_eq!(selector, EXECUTE_MESSAGE_SELECTOR);
250 }
251
252 #[test]
253 fn check_process_streams_selector() {
254 use alloy_sol_types::SolCall;
255 assert_eq!(
256 process_streamsCall::SIGNATURE,
257 "process_streams(((bytes32),((uint8,(bytes32)),(bytes)),uint32,uint32)[])"
258 );
259 assert_eq!(process_streamsCall::SELECTOR, PROCESS_STREAMS_SELECTOR);
260 }
261
262 #[test]
263 fn check_instantiate_selector() {
264 let selector = &keccak256("instantiate(bytes)".as_bytes())[..4];
265 assert_eq!(selector, INSTANTIATE_SELECTOR);
266 }
267}