Skip to content

Commit 47c86db

Browse files
committed
fix: ensure rollback context in a case of error
1 parent dcf3fae commit 47c86db

File tree

1 file changed

+22
-5
lines changed

1 file changed

+22
-5
lines changed

clarity/src/vm/clarity_wasm.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -428,11 +428,28 @@ pub fn initialize_contract(
428428
results.push(placeholder_for_type(result_ty));
429429
}
430430

431-
top_level
432-
.call(&mut store, &[], results.as_mut_slice())
433-
.map_err(|e| {
434-
error_mapping::resolve_error(e, instance, &mut store, &epoch, &clarity_version)
435-
})?;
431+
let top_level_result = top_level.call(&mut store, &[], results.as_mut_slice());
432+
match top_level_result {
433+
Ok(_) => {}
434+
Err(e) => {
435+
// Before propagating the error, attempt to roll back the function context.
436+
// If the rollback fails, immediately return a rollback-specific error.
437+
if store.data_mut().global_context.roll_back().is_err() {
438+
return Err(Error::Wasm(WasmError::Expect(
439+
"Expected entry to rollback".into(),
440+
)));
441+
}
442+
443+
// Rollback succeeded, so resolve and return the original runtime error.
444+
return Err(error_mapping::resolve_error(
445+
e,
446+
instance,
447+
&mut store,
448+
&epoch,
449+
&clarity_version,
450+
));
451+
}
452+
}
436453

437454
// Save the compiled Wasm module into the contract context
438455
store.data_mut().contract_context_mut()?.set_wasm_module(

0 commit comments

Comments
 (0)