|
1 | 1 | use fvm_ipld_encoding::{BytesDe, BytesSer};
|
2 | 2 | use fvm_shared::{address::Address, METHOD_SEND};
|
| 3 | +use num_traits::Zero; |
3 | 4 |
|
4 | 5 | use crate::interpreter::precompiles::PrecompileContext;
|
5 | 6 |
|
@@ -122,112 +123,125 @@ pub fn call<RT: Runtime>(
|
122 | 123 | &[]
|
123 | 124 | };
|
124 | 125 |
|
125 |
| - if precompiles::Precompiles::<RT>::is_precompile(&dst) { |
126 |
| - let context = PrecompileContext { |
127 |
| - is_static: matches!(kind, CallKind::StaticCall) || system.readonly, |
128 |
| - gas, |
129 |
| - value, |
130 |
| - }; |
131 |
| - |
132 |
| - match precompiles::Precompiles::call_precompile(system.rt, dst, input_data, context) |
133 |
| - .map_err(StatusCode::from) |
134 |
| - { |
135 |
| - Ok(return_data) => (1, return_data), |
136 |
| - Err(status) => { |
137 |
| - let msg = format!("{}", status); |
138 |
| - (0, msg.as_bytes().to_vec()) |
| 126 | + 'early_ret: { |
| 127 | + if precompiles::Precompiles::<RT>::is_precompile(&dst) { |
| 128 | + let context = PrecompileContext { |
| 129 | + is_static: matches!(kind, CallKind::StaticCall) || system.readonly, |
| 130 | + gas, |
| 131 | + value, |
| 132 | + }; |
| 133 | + |
| 134 | + match precompiles::Precompiles::call_precompile(system.rt, dst, input_data, context) |
| 135 | + .map_err(StatusCode::from) |
| 136 | + { |
| 137 | + Ok(return_data) => (1, return_data), |
| 138 | + Err(status) => { |
| 139 | + let msg = format!("{}", status); |
| 140 | + (0, msg.as_bytes().to_vec()) |
| 141 | + } |
139 | 142 | }
|
140 |
| - } |
141 |
| - } else { |
142 |
| - let call_result = match kind { |
143 |
| - CallKind::Call | CallKind::StaticCall => { |
144 |
| - let dst_addr: EthAddress = dst.into(); |
145 |
| - let dst_addr: Address = dst_addr.try_into().expect("address is a precompile"); |
146 |
| - |
147 |
| - // Special casing for account/embryo/non-existent actors: we just do a SEND (method 0) |
148 |
| - // which allows us to transfer funds (and create embryos) |
149 |
| - let target_actor_code = system |
150 |
| - .rt |
151 |
| - .resolve_address(&dst_addr) |
152 |
| - .and_then(|actor_id| system.rt.get_actor_code_cid(&actor_id)); |
153 |
| - let target_actor_type = target_actor_code |
154 |
| - .as_ref() |
155 |
| - .and_then(|cid| system.rt.resolve_builtin_actor_type(cid)); |
156 |
| - let actor_exists = target_actor_code.is_some(); |
157 |
| - |
158 |
| - if !actor_exists && value.is_zero() { |
159 |
| - // If the actor doesn't exist and we're not sending value, return with |
160 |
| - // "success". The EVM only auto-creates actors when sending value. |
161 |
| - // |
162 |
| - // NOTE: this will also apply if we're in read-only mode, because we can't |
163 |
| - // send value in read-only mode anyways. |
164 |
| - Ok(RawBytes::default()) |
165 |
| - } else { |
166 |
| - let method = if !actor_exists |
167 |
| - || matches!(target_actor_type, Some(Type::Embryo | Type::Account)) |
168 |
| - { |
169 |
| - // If the target actor doesn't exist or is an account or an embryo, |
170 |
| - // switch to a basic "send" so the call will still work even if the |
171 |
| - // target actor would reject a normal ethereum call. |
172 |
| - METHOD_SEND |
173 |
| - } else if system.readonly || kind == CallKind::StaticCall { |
174 |
| - // Invoke, preserving read-only mode. |
175 |
| - Method::InvokeContractReadOnly as u64 |
| 143 | + } else { |
| 144 | + let dst_addr: EthAddress = dst.into(); |
| 145 | + let dst_addr: Address = dst_addr.try_into().expect("address is a precompile"); |
| 146 | + |
| 147 | + let call_result = match kind { |
| 148 | + CallKind::Call | CallKind::StaticCall => { |
| 149 | + // Special casing for account/embryo/non-existent actors: we just do a SEND (method 0) |
| 150 | + // which allows us to transfer funds (and create embryos) |
| 151 | + let target_actor_code = system |
| 152 | + .rt |
| 153 | + .resolve_address(&dst_addr) |
| 154 | + .and_then(|actor_id| system.rt.get_actor_code_cid(&actor_id)); |
| 155 | + let target_actor_type = target_actor_code |
| 156 | + .as_ref() |
| 157 | + .and_then(|cid| system.rt.resolve_builtin_actor_type(cid)); |
| 158 | + let actor_exists = target_actor_code.is_some(); |
| 159 | + |
| 160 | + if !actor_exists && value.is_zero() { |
| 161 | + // If the actor doesn't exist and we're not sending value, return with |
| 162 | + // "success". The EVM only auto-creates actors when sending value. |
| 163 | + // |
| 164 | + // NOTE: this will also apply if we're in read-only mode, because we can't |
| 165 | + // send value in read-only mode anyways. |
| 166 | + Ok(RawBytes::default()) |
176 | 167 | } else {
|
177 |
| - // Otherwise, invoke normally. |
178 |
| - Method::InvokeContract as u64 |
| 168 | + let method = if !actor_exists |
| 169 | + || matches!(target_actor_type, Some(Type::Embryo | Type::Account)) |
| 170 | + { |
| 171 | + // If the target actor doesn't exist or is an account or an embryo, |
| 172 | + // switch to a basic "send" so the call will still work even if the |
| 173 | + // target actor would reject a normal ethereum call. |
| 174 | + METHOD_SEND |
| 175 | + } else if system.readonly || kind == CallKind::StaticCall { |
| 176 | + // Invoke, preserving read-only mode. |
| 177 | + Method::InvokeContractReadOnly as u64 |
| 178 | + } else { |
| 179 | + // Otherwise, invoke normally. |
| 180 | + Method::InvokeContract as u64 |
| 181 | + }; |
| 182 | + system.send( |
| 183 | + &dst_addr, |
| 184 | + method, |
| 185 | + // TODO: support IPLD codecs #758 |
| 186 | + RawBytes::serialize(BytesSer(input_data))?, |
| 187 | + TokenAmount::from(&value), |
| 188 | + ) |
| 189 | + } |
| 190 | + } |
| 191 | + CallKind::DelegateCall => { |
| 192 | + // first invoke GetBytecode to get the code CID from the target |
| 193 | + let cid = match system |
| 194 | + .rt |
| 195 | + .resolve_address(&dst_addr) |
| 196 | + .and_then(|id| system.rt.get_actor_code_cid(&id)) |
| 197 | + { |
| 198 | + Some(cid) => cid, |
| 199 | + // failure to find CID is flattened to an empty return |
| 200 | + None => break 'early_ret (1, vec![]), |
| 201 | + }; |
| 202 | + |
| 203 | + let code = match system.rt.resolve_builtin_actor_type(&cid) { |
| 204 | + Some(Type::EVM) => system.rt |
| 205 | + .send(&dst_addr, crate::Method::GetBytecode as u64, Default::default(), TokenAmount::zero())? |
| 206 | + .deserialize()? |
| 207 | + , |
| 208 | + // other builtin actors & native actors |
| 209 | + _ => todo!("revert when calling delegate call for native actors") |
| 210 | + }; |
| 211 | + |
| 212 | + // and then invoke self with delegate; readonly context is sticky |
| 213 | + let params = DelegateCallParams { |
| 214 | + code, |
| 215 | + input: input_data.to_vec(), |
| 216 | + readonly: system.readonly, |
179 | 217 | };
|
180 | 218 | system.send(
|
181 |
| - &dst_addr, |
182 |
| - method, |
183 |
| - // TODO: support IPLD codecs #758 |
184 |
| - RawBytes::serialize(BytesSer(input_data))?, |
| 219 | + &system.rt.message().receiver(), |
| 220 | + Method::InvokeContractDelegate as u64, |
| 221 | + RawBytes::serialize(¶ms)?, |
185 | 222 | TokenAmount::from(&value),
|
186 | 223 | )
|
187 | 224 | }
|
188 |
| - } |
189 |
| - CallKind::DelegateCall => { |
190 |
| - // first invoke GetBytecode to get the code CID from the target |
191 |
| - let code = crate::interpreter::instructions::ext::get_evm_bytecode_cid( |
192 |
| - system.rt, dst, |
193 |
| - )? |
194 |
| - .unwrap_evm(StatusCode::InvalidArgument(format!( |
195 |
| - "DELEGATECALL cannot call native actor {}", |
196 |
| - dst |
197 |
| - )))?; |
198 |
| - |
199 |
| - // and then invoke self with delegate; readonly context is sticky |
200 |
| - let params = DelegateCallParams { |
201 |
| - code, |
202 |
| - input: input_data.to_vec(), |
203 |
| - readonly: system.readonly, |
204 |
| - }; |
205 |
| - system.send( |
206 |
| - &system.rt.message().receiver(), |
207 |
| - Method::InvokeContractDelegate as u64, |
208 |
| - RawBytes::serialize(¶ms)?, |
209 |
| - TokenAmount::from(&value), |
210 |
| - ) |
211 |
| - } |
212 | 225 |
|
213 |
| - CallKind::CallCode => Err(ActorError::unchecked( |
214 |
| - EVM_CONTRACT_EXECUTION_ERROR, |
215 |
| - "unsupported opcode".to_string(), |
216 |
| - )), |
217 |
| - }; |
218 |
| - match call_result { |
219 |
| - Ok(result) => { |
220 |
| - // Support the "empty" result. We often use this to mean "returned nothing" and |
221 |
| - // it's important to support, e.g., sending to accounts. |
222 |
| - if result.is_empty() { |
223 |
| - (1, Vec::new()) |
224 |
| - } else { |
225 |
| - // TODO: support IPLD codecs #758 |
226 |
| - let BytesDe(result) = result.deserialize()?; |
227 |
| - (1, result) |
| 226 | + CallKind::CallCode => Err(ActorError::unchecked( |
| 227 | + EVM_CONTRACT_EXECUTION_ERROR, |
| 228 | + "unsupported opcode".to_string(), |
| 229 | + )), |
| 230 | + }; |
| 231 | + match call_result { |
| 232 | + Ok(result) => { |
| 233 | + // Support the "empty" result. We often use this to mean "returned nothing" and |
| 234 | + // it's important to support, e.g., sending to accounts. |
| 235 | + if result.is_empty() { |
| 236 | + (1, Vec::new()) |
| 237 | + } else { |
| 238 | + // TODO: support IPLD codecs #758 |
| 239 | + let BytesDe(result) = result.deserialize()?; |
| 240 | + (1, result) |
| 241 | + } |
228 | 242 | }
|
| 243 | + Err(ae) => (0, ae.data().to_vec()), |
229 | 244 | }
|
230 |
| - Err(ae) => (0, ae.data().to_vec()), |
231 | 245 | }
|
232 | 246 | }
|
233 | 247 | };
|
|
0 commit comments