11// Copyright 2022-2024 Protocol Labs
22// SPDX-License-Identifier: Apache-2.0, MIT
33use std:: future:: Future ;
4- use std:: panic:: catch_unwind;
54use std:: sync:: Arc ;
6- use futures_util:: FutureExt ;
75
86use crate :: observe:: {
97 BlockCommitted , BlockProposalEvaluated , BlockProposalReceived , BlockProposalSent , Message ,
@@ -14,7 +12,7 @@ use crate::AppExitCode;
1412use crate :: BlockHeight ;
1513use crate :: { tmconv:: * , VERSION } ;
1614use actors_custom_api:: gas_market:: Reading ;
17- use anyhow:: { anyhow, bail , Context , Result } ;
15+ use anyhow:: { anyhow, Context , Result } ;
1816use async_stm:: { atomically, atomically_or_err} ;
1917use async_trait:: async_trait;
2018use 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