Skip to content

Commit b767340

Browse files
committed
alternative
1 parent 6440d9b commit b767340

3 files changed

Lines changed: 15 additions & 32 deletions

File tree

Cargo.lock

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

fendermint/app/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@ bytes = { workspace = true }
1414
cid = { workspace = true }
1515
hex = { workspace = true }
1616
fs-err = { workspace = true }
17-
futures-util = { workspace = true }
1817
k256 = { workspace = true }
1918
lazy_static = { workspace = true }
2019
libipld = { workspace = true }

fendermint/app/src/app.rs

Lines changed: 15 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
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;
54
use std::sync::Arc;
6-
use futures_util::FutureExt;
75

86
use crate::observe::{
97
BlockCommitted, BlockProposalEvaluated, BlockProposalReceived, BlockProposalSent, Message,
@@ -14,7 +12,7 @@ use crate::AppExitCode;
1412
use crate::BlockHeight;
1513
use crate::{tmconv::*, VERSION};
1614
use actors_custom_api::gas_market::Reading;
17-
use anyhow::{anyhow, bail, Context, Result};
15+
use anyhow::{anyhow, Context, Result};
1816
use async_stm::{atomically, atomically_or_err};
1917
use async_trait::async_trait;
2018
use cid::Cid;
@@ -329,32 +327,16 @@ where
329327
/// Update the execution state using the provided closure.
330328
///
331329
/// Note: Deals with panics in the user provided closure as well.
332-
async fn modify_exec_state<T, F, R>(&self, f: F) -> Result<T>
330+
async fn modify_exec_state<T, G, F>(&self, generator: G) -> Result<T>
333331
where
334-
F: FnOnce(&mut FvmExecState<BS>) -> R,
335-
R: Future<Output = Result<(FvmExecState<BS>, T)>>,
332+
G: for<'s> FnOnce(&'s mut FvmExecState<BS>) -> F,
333+
F: Future<Output = Result<T>>,
334+
T: 'static,
336335
{
337336
let mut guard = self.exec_state.lock().await;
338-
let state = guard.take().expect("exec state empty");
339-
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-
};
356-
357-
*guard = Some(state);
337+
let maybe_state = guard.as_mut();
338+
let state = maybe_state.expect("exec state empty");
339+
let ret = generator(state).await?;
358340

359341
Ok(ret)
360342
}
@@ -437,10 +419,13 @@ where
437419
async fn refresh_validators_cache(&self) -> Result<()> {
438420
// TODO: This should be read only state, but we can't use the read-only view here
439421
// because it hasn't been committed to state store yet.
440-
self.modify_exec_state(|mut s| async {
441-
let mut cache = self.validators_cache.lock().await;
442-
*cache = Some(ValidatorCache::new_from_state(&mut s)?);
443-
Ok((s, ()))
422+
let mut cache = self.validators_cache.lock().await;
423+
self.modify_exec_state(|s| {
424+
let x = ValidatorCache::new_from_state(s);
425+
async move {
426+
*cache = Some(x?);
427+
Ok(())
428+
}
444429
})
445430
.await?;
446431

0 commit comments

Comments
 (0)