Skip to content

Commit 7efaa7b

Browse files
authored
Merge pull request #368 from filecoin-project/fix/remove-unwraps
fix: fvm: remove all unwraps
2 parents dff0d05 + 5f9d88d commit 7efaa7b

File tree

2 files changed

+41
-13
lines changed

2 files changed

+41
-13
lines changed

rust/src/fvm/engine.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,10 @@ impl MultiEngineContainer {
6868
}
6969

7070
pub fn get(&self, nv: u32) -> anyhow::Result<Arc<dyn AbstractMultiEngine + 'static>> {
71-
let mut locked = self.engines.lock().unwrap();
71+
let mut locked = self
72+
.engines
73+
.lock()
74+
.map_err(|e| anyhow!("engine lock poisoned: {e}"))?;
7275
Ok(match locked.entry(nv) {
7376
Entry::Occupied(v) => v.get().clone(),
7477
Entry::Vacant(v) => v
@@ -254,8 +257,10 @@ mod v2 {
254257
self,
255258
Message2 {
256259
version: msg.version.try_into().context("invalid message version")?,
257-
from: Address2::from_bytes(&msg.from.to_bytes()).unwrap(),
258-
to: Address2::from_bytes(&msg.to.to_bytes()).unwrap(),
260+
from: Address2::from_bytes(&msg.from.to_bytes())
261+
.context("unsupported from address")?,
262+
to: Address2::from_bytes(&msg.to.to_bytes())
263+
.context("unsupported to address")?,
259264
sequence: msg.sequence,
260265
value: TokenAmount2::from_atto(msg.value.atto().clone()),
261266
method_num: msg.method_num,
@@ -365,7 +370,10 @@ mod v2 {
365370
value,
366371
} => Some(ExecutionEvent::Call {
367372
from,
368-
to: Address::from_bytes(&to.to_bytes()).unwrap(),
373+
to: Address::from_bytes(&to.to_bytes())
374+
// There's nothing we can do here, so we use the "chaos" actor
375+
// ID.
376+
.unwrap_or_else(|_| Address::new_id(98)),
369377
method,
370378
params: bytes_to_block(params),
371379
value: TokenAmount::from_atto(value.atto().clone()),

rust/src/fvm/machine.rs

Lines changed: 29 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,9 @@ fn fvm_machine_execute_message(
198198
let mut executor = executor
199199
.machine
200200
.as_ref()
201-
.expect("missing executor")
201+
.context("missing executor")?
202202
.lock()
203-
.unwrap();
203+
.map_err(|e| anyhow!("executor lock poisoned: {e}"))?;
204204
let apply_ret = executor.execute_message(message, apply_kind, chain_len as usize)?;
205205

206206
let exec_trace = if !apply_ret.exec_trace.is_empty() {
@@ -243,11 +243,31 @@ fn fvm_machine_execute_message(
243243
.map(|info| info.to_string().into_boxed_str().into());
244244

245245
// TODO: use the non-bigint token amount everywhere in the FVM
246-
let penalty: u128 = apply_ret.penalty.atto().try_into().unwrap();
247-
let miner_tip: u128 = apply_ret.miner_tip.atto().try_into().unwrap();
248-
let base_fee_burn: u128 = apply_ret.base_fee_burn.atto().try_into().unwrap();
249-
let over_estimation_burn: u128 = apply_ret.over_estimation_burn.atto().try_into().unwrap();
250-
let refund: u128 = apply_ret.refund.atto().try_into().unwrap();
246+
let penalty: u128 = apply_ret
247+
.penalty
248+
.atto()
249+
.try_into()
250+
.context("penalty exceeds u128 attoFIL")?;
251+
let miner_tip: u128 = apply_ret
252+
.miner_tip
253+
.atto()
254+
.try_into()
255+
.context("miner tip exceeds u128 attoFIL")?;
256+
let base_fee_burn: u128 = apply_ret
257+
.base_fee_burn
258+
.atto()
259+
.try_into()
260+
.context("base fee burn exceeds u128 attoFIL")?;
261+
let over_estimation_burn: u128 = apply_ret
262+
.over_estimation_burn
263+
.atto()
264+
.try_into()
265+
.context("overestimation burn exceeds u128 attoFIL")?;
266+
let refund: u128 = apply_ret
267+
.refund
268+
.atto()
269+
.try_into()
270+
.context("refund exceeds u128 attoFIL")?;
251271
let gas_refund = apply_ret.gas_refund;
252272
let gas_burned = apply_ret.gas_burned;
253273

@@ -313,9 +333,9 @@ fn fvm_machine_flush(executor: &'_ InnerFvmMachine) -> repr_c::Box<Result<c_slic
313333
let mut executor = executor
314334
.machine
315335
.as_ref()
316-
.expect("missing executor")
336+
.context("missing executor")?
317337
.lock()
318-
.unwrap();
338+
.map_err(|e| anyhow!("executor lock poisoned: {e}"))?;
319339
let cid = executor.flush()?;
320340

321341
Ok(cid.to_bytes().into_boxed_slice().into())

0 commit comments

Comments
 (0)