Skip to content

Commit 4952b9b

Browse files
committed
use closure for early return
1 parent 107e2a7 commit 4952b9b

File tree

2 files changed

+62
-64
lines changed

2 files changed

+62
-64
lines changed

actors/evm/src/interpreter/instructions/call.rs

+40-40
Original file line numberDiff line numberDiff line change
@@ -197,28 +197,28 @@ pub fn call_generic<RT: Runtime>(
197197
&[]
198198
};
199199

200-
'early_ret: {
201-
if precompiles::Precompiles::<RT>::is_precompile(&dst) {
202-
let context = PrecompileContext {
203-
is_static: matches!(kind, CallKind::StaticCall) || system.readonly,
204-
gas,
205-
value,
206-
};
200+
if precompiles::Precompiles::<RT>::is_precompile(&dst) {
201+
let context = PrecompileContext {
202+
is_static: matches!(kind, CallKind::StaticCall) || system.readonly,
203+
gas,
204+
value,
205+
};
207206

208-
match precompiles::Precompiles::call_precompile(system.rt, dst, input_data, context)
209-
.map_err(StatusCode::from)
210-
{
211-
Ok(return_data) => (1, return_data),
212-
Err(status) => {
213-
let msg = format!("{}", status);
214-
(0, msg.as_bytes().to_vec())
215-
}
207+
match precompiles::Precompiles::call_precompile(system.rt, dst, input_data, context)
208+
.map_err(StatusCode::from)
209+
{
210+
Ok(return_data) => (1, return_data),
211+
Err(status) => {
212+
let msg = format!("{}", status);
213+
(0, msg.as_bytes().to_vec())
216214
}
217-
} else {
218-
let dst_addr: EthAddress = dst.into();
219-
let dst_addr: Address = dst_addr.try_into().expect("address is a precompile");
215+
}
216+
} else {
217+
let dst_addr: EthAddress = dst.into();
218+
let dst_addr: Address = dst_addr.try_into().expect("address is a precompile");
220219

221-
let call_result = match kind {
220+
let call_result = (|| {
221+
match kind {
222222
CallKind::Call | CallKind::StaticCall => {
223223
// Special casing for account/embryo/non-existent actors: we just do a SEND (method 0)
224224
// which allows us to transfer funds (and create embryos)
@@ -271,17 +271,17 @@ pub fn call_generic<RT: Runtime>(
271271
{
272272
Some(cid) => cid,
273273
// failure to find CID is flattened to an empty return
274-
None => break 'early_ret (1, vec![]),
274+
None => return Ok(RawBytes::default()),
275275
};
276276

277277
let code = match system.rt.resolve_builtin_actor_type(&cid) {
278-
Some(Type::EVM) => system.rt
279-
.send(&dst_addr, crate::Method::GetBytecode as u64, Default::default(), TokenAmount::zero())?
280-
.deserialize()?
281-
,
282-
// other builtin actors & native actors
283-
_ => todo!("revert when calling delegate call for native actors")
284-
};
278+
Some(Type::EVM) => system.rt
279+
.send(&dst_addr, crate::Method::GetBytecode as u64, Default::default(), TokenAmount::zero())?
280+
.deserialize()?
281+
,
282+
// other builtin actors & native actors
283+
_ => todo!("revert when calling delegate call for native actors")
284+
};
285285

286286
// and then invoke self with delegate; readonly context is sticky
287287
let params = DelegateCallParams {
@@ -301,21 +301,21 @@ pub fn call_generic<RT: Runtime>(
301301
EVM_CONTRACT_EXECUTION_ERROR,
302302
"unsupported opcode".to_string(),
303303
)),
304-
};
305-
match call_result {
306-
Ok(result) => {
307-
// Support the "empty" result. We often use this to mean "returned nothing" and
308-
// it's important to support, e.g., sending to accounts.
309-
if result.is_empty() {
310-
(1, Vec::new())
311-
} else {
312-
// TODO: support IPLD codecs #758
313-
let BytesDe(result) = result.deserialize()?;
314-
(1, result)
315-
}
304+
}
305+
})();
306+
match call_result {
307+
Ok(result) => {
308+
// Support the "empty" result. We often use this to mean "returned nothing" and
309+
// it's important to support, e.g., sending to accounts.
310+
if result.is_empty() {
311+
(1, Vec::new())
312+
} else {
313+
// TODO: support IPLD codecs #758
314+
let BytesDe(result) = result.deserialize()?;
315+
(1, result)
316316
}
317-
Err(ae) => (0, ae.data().to_vec()),
318317
}
318+
Err(ae) => (0, ae.data().to_vec()),
319319
}
320320
}
321321
};

actors/evm/src/interpreter/instructions/ext.rs

+22-24
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,12 @@ pub fn extcodecopy(
6060
) -> Result<(), StatusCode> {
6161
let bytecode = match get_cid_type(system.rt, addr) {
6262
CodeCid::EVM(addr) => get_evm_bytecode(system.rt, &addr)?,
63-
CodeCid::NotFound |
64-
CodeCid::Account => Vec::new(),
63+
CodeCid::NotFound | CodeCid::Account => Vec::new(),
6564
// calling EXTCODECOPY on native actors results with a single byte 0xFE which solidtiy uses for its `assert`/`throw` methods
6665
// and in general invalid EVM bytecode
67-
_ => vec![0xFE]
66+
_ => vec![0xFE],
6867
};
69-
68+
7069
copy_to_memory(&mut state.memory, dest_offset, size, data_offset, bytecode.as_slice(), true)
7170
}
7271

@@ -95,30 +94,29 @@ pub fn get_cid_type(rt: &impl Runtime, addr: U256) -> CodeCid {
9594

9695
if let Ok(addr) = addr.try_into() {
9796
rt.resolve_address(&addr)
98-
.and_then(|id| {
99-
rt.get_actor_code_cid(&id).map(|cid| {
100-
let code_cid = rt
101-
.resolve_builtin_actor_type(&cid)
102-
.map(|t| {
103-
match t {
104-
Type::Account => CodeCid::Account,
105-
// TODO part of current account abstraction hack where emryos are accounts
106-
Type::Embryo => CodeCid::Account,
107-
Type::EVM => CodeCid::EVM(addr),
108-
// remaining builtin actors are native
109-
_ => CodeCid::Native(cid),
110-
}
111-
// not a builtin actor, so it is probably a native actor
112-
})
113-
.unwrap_or(CodeCid::Native(cid));
114-
code_cid
97+
.and_then(|id| {
98+
rt.get_actor_code_cid(&id).map(|cid| {
99+
let code_cid = rt
100+
.resolve_builtin_actor_type(&cid)
101+
.map(|t| {
102+
match t {
103+
Type::Account => CodeCid::Account,
104+
// TODO part of current account abstraction hack where emryos are accounts
105+
Type::Embryo => CodeCid::Account,
106+
Type::EVM => CodeCid::EVM(addr),
107+
// remaining builtin actors are native
108+
_ => CodeCid::Native(cid),
109+
}
110+
// not a builtin actor, so it is probably a native actor
111+
})
112+
.unwrap_or(CodeCid::Native(cid));
113+
code_cid
114+
})
115115
})
116-
})
117-
.unwrap_or(CodeCid::NotFound)
116+
.unwrap_or(CodeCid::NotFound)
118117
} else {
119118
CodeCid::NotFound
120119
}
121-
122120
}
123121

124122
pub fn get_evm_bytecode(rt: &impl Runtime, addr: &Address) -> Result<Vec<u8>, StatusCode> {

0 commit comments

Comments
 (0)