Skip to content

Commit 6440d9b

Browse files
committed
x
1 parent 3b79c9d commit 6440d9b

4 files changed

Lines changed: 34 additions & 15 deletions

File tree

Cargo.lock

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

fendermint/app/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ bytes = { workspace = true }
1414
cid = { workspace = true }
1515
hex = { workspace = true }
1616
fs-err = { workspace = true }
17+
futures-util = { workspace = true }
1718
k256 = { workspace = true }
1819
lazy_static = { workspace = true }
1920
libipld = { workspace = true }

fendermint/app/src/app.rs

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
11
// Copyright 2022-2024 Protocol Labs
22
// SPDX-License-Identifier: Apache-2.0, MIT
33
use std::future::Future;
4+
use std::panic::catch_unwind;
45
use std::sync::Arc;
6+
use futures_util::FutureExt;
57

68
use crate::observe::{
79
BlockCommitted, BlockProposalEvaluated, BlockProposalReceived, BlockProposalSent, Message,
@@ -12,7 +14,7 @@ use crate::AppExitCode;
1214
use crate::BlockHeight;
1315
use crate::{tmconv::*, VERSION};
1416
use actors_custom_api::gas_market::Reading;
15-
use anyhow::{anyhow, Context, Result};
17+
use anyhow::{anyhow, bail, Context, Result};
1618
use async_stm::{atomically, atomically_or_err};
1719
use async_trait::async_trait;
1820
use cid::Cid;
@@ -324,16 +326,33 @@ where
324326
guard.take().expect("exec state empty")
325327
}
326328

327-
/// Take the execution state, update it, put it back, return the output.
329+
/// Update the execution state using the provided closure.
330+
///
331+
/// Note: Deals with panics in the user provided closure as well.
328332
async fn modify_exec_state<T, F, R>(&self, f: F) -> Result<T>
329333
where
330-
F: FnOnce(FvmExecState<BS>) -> R,
334+
F: FnOnce(&mut FvmExecState<BS>) -> R,
331335
R: Future<Output = Result<(FvmExecState<BS>, T)>>,
332336
{
333337
let mut guard = self.exec_state.lock().await;
334338
let state = guard.take().expect("exec state empty");
335339

336-
let (state, ret) = f(state).await?;
340+
let fut = catch_unwind(move || { f(state) });
341+
let fut: R = match fut {
342+
Ok(fut) => fut,
343+
Err(err) => {
344+
*guard = Some(backup);
345+
bail!("Encounterd panic in `modify_exec_state` user provided future generator invocation: {err:?}")
346+
}
347+
};
348+
let res = fut.catch_unwind().await?;
349+
let (state, ret): (FvmExecState<BS>, T) = match res {
350+
Ok(tup) => tup,
351+
Err(err) => {
352+
*guard = Some(state);
353+
bail!("Encounterd panic in `modify_exec_state` user provided future generator future polling: {err:?}");
354+
}
355+
};
337356

338357
*guard = Some(state);
339358

fendermint/vm/interpreter/src/fvm/interpreter.rs

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ struct Actor {
4343
state: ActorState,
4444
}
4545

46+
/// Interprets messages as received from the ABCI layer
4647
#[derive(Clone)]
4748
pub struct FvmMessagesInterpreter<DB, C>
4849
where
@@ -107,19 +108,16 @@ where
107108
state: &FvmExecState<ReadOnlyBlockstore<DB>>,
108109
msg: &FvmMessage,
109110
) -> Result<CheckResponse> {
110-
let Actor {
111+
let Some(Actor {
111112
id: _,
112113
state: actor,
113-
} = match self.lookup_actor(state, &msg.from)? {
114-
Some(actor) => actor,
115-
None => {
116-
return Ok(CheckResponse::new(
117-
msg,
118-
ExitCode::SYS_SENDER_INVALID,
119-
None,
120-
None,
121-
))
122-
}
114+
}) = self.lookup_actor(state, &msg.from)? else {
115+
return Ok(CheckResponse::new(
116+
msg,
117+
ExitCode::SYS_SENDER_INVALID,
118+
None,
119+
None,
120+
))
123121
};
124122

125123
let balance_needed = msg.gas_fee_cap.clone() * msg.gas_limit;

0 commit comments

Comments
 (0)