Skip to content

Commit e0d70f5

Browse files
committed
fix: propagate script errors from native-simulator instead of ignoring them
The run_simulator function was ignoring the return value from __ckb_std_main, always returning 0 (success) regardless of whether the contract actually failed. This made it impossible to test error conditions when using the native-simulator feature. Changed run_simulator to return Result<u64, ScriptError> and properly propagate validation failures using ScriptError::validation_failure(), which is consistent with how errors are handled in non-simulator mode.
1 parent 29c24a7 commit e0d70f5

File tree

1 file changed

+11
-4
lines changed

1 file changed

+11
-4
lines changed

src/context.rs

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ use crate::tx_verifier::OutputsDataVerifier;
22
use ckb_chain_spec::consensus::{ConsensusBuilder, TYPE_ID_CODE_HASH};
33
use ckb_error::Error as CKBError;
44
use ckb_mock_tx_types::{MockCellDep, MockInfo, MockInput, MockTransaction, ReprMockTransaction};
5+
#[cfg(feature = "native-simulator")]
6+
use ckb_script::ScriptError;
57
use ckb_script::{TransactionScriptsVerifier, TxVerifyEnv};
68
use ckb_traits::{CellDataProvider, ExtensionProvider, HeaderProvider};
79
use ckb_types::{
@@ -526,7 +528,9 @@ impl Context {
526528
};
527529

528530
let use_cycles = match self.simulator_binaries.get(&code_hash) {
529-
Some(sim_path) => self.run_simulator(sim_path, tx, group),
531+
Some(sim_path) => self
532+
.run_simulator(sim_path, tx, group)
533+
.map_err(|e| e.source(group))?,
530534
None => {
531535
group.script.code_hash();
532536
verifier
@@ -548,7 +552,7 @@ impl Context {
548552
sim_path: &PathBuf,
549553
tx: &TransactionView,
550554
group: &ckb_script::ScriptGroup,
551-
) -> u64 {
555+
) -> Result<u64, ScriptError> {
552556
println!(
553557
"run native-simulator: {}",
554558
sim_path.file_name().unwrap().to_str().unwrap()
@@ -632,9 +636,12 @@ impl Context {
632636
.get(b"__ckb_std_main")
633637
.expect("load function : __ckb_std_main");
634638
let argv = vec![];
635-
func(0, argv.as_ptr());
639+
let result = func(0, argv.as_ptr());
640+
if result != 0 {
641+
return Err(ScriptError::validation_failure(&group.script, result));
642+
}
636643
}
637-
0
644+
Ok(0)
638645
}
639646

640647
#[cfg(feature = "native-simulator")]

0 commit comments

Comments
 (0)