Skip to content

Commit cc54ebd

Browse files
committed
refactor: use struct for AbortedByCallback
1 parent 829a96e commit cc54ebd

File tree

5 files changed

+88
-37
lines changed

5 files changed

+88
-37
lines changed

clarity/src/vm/clarity.rs

Lines changed: 35 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,30 @@ pub enum Error {
2020
Interpreter(InterpreterError),
2121
BadTransaction(String),
2222
CostError(ExecutionCost, ExecutionCost),
23-
AbortedByCallback(Option<Value>, AssetMap, Vec<StacksTransactionEvent>, String),
23+
AbortedByCallback {
24+
/// What the output value of the transaction would have been.
25+
/// This will be a Some for contract-calls, and None for contract initialization txs.
26+
output: Option<Value>,
27+
/// The asset map which was evaluated by the abort callback
28+
assets_modified: AssetMap,
29+
/// The events from the transaction processing
30+
tx_events: Vec<StacksTransactionEvent>,
31+
/// A human-readable explanation for aborting the transaction
32+
reason: String,
33+
},
2434
}
2535

2636
impl fmt::Display for Error {
2737
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
28-
match *self {
38+
match self {
2939
Error::CostError(ref a, ref b) => {
30-
write!(f, "Cost Error: {} cost exceeded budget of {} cost", a, b)
40+
write!(f, "Cost Error: {a} cost exceeded budget of {b} cost")
3141
}
3242
Error::Analysis(ref e) => fmt::Display::fmt(e, f),
3343
Error::Parse(ref e) => fmt::Display::fmt(e, f),
34-
Error::AbortedByCallback(..) => write!(f, "Post condition aborted transaction"),
44+
Error::AbortedByCallback { reason, .. } => {
45+
write!(f, "Post condition aborted transaction: {reason}")
46+
}
3547
Error::Interpreter(ref e) => fmt::Display::fmt(e, f),
3648
Error::BadTransaction(ref s) => fmt::Display::fmt(s, f),
3749
}
@@ -42,7 +54,7 @@ impl std::error::Error for Error {
4254
fn cause(&self) -> Option<&dyn std::error::Error> {
4355
match *self {
4456
Error::CostError(ref _a, ref _b) => None,
45-
Error::AbortedByCallback(..) => None,
57+
Error::AbortedByCallback { .. } => None,
4658
Error::Analysis(ref e) => Some(e),
4759
Error::Parse(ref e) => Some(e),
4860
Error::Interpreter(ref e) => Some(e),
@@ -331,16 +343,16 @@ pub trait TransactionConnection: ClarityConnection {
331343
},
332344
abort_call_back,
333345
)
334-
.and_then(|(value, assets, events, aborted)| {
335-
if let Some(aborted) = aborted {
336-
Err(Error::AbortedByCallback(
337-
Some(value),
338-
assets,
339-
events,
340-
aborted,
341-
))
346+
.and_then(|(value, assets_modified, tx_events, reason)| {
347+
if let Some(reason) = reason {
348+
Err(Error::AbortedByCallback {
349+
output: Some(value),
350+
assets_modified,
351+
tx_events,
352+
reason,
353+
})
342354
} else {
343-
Ok((value, assets, events))
355+
Ok((value, assets_modified, tx_events))
344356
}
345357
})
346358
}
@@ -364,7 +376,7 @@ pub trait TransactionConnection: ClarityConnection {
364376
where
365377
F: FnOnce(&AssetMap, &mut ClarityDatabase) -> Option<String>,
366378
{
367-
let (_, asset_map, events, aborted) = self.with_abort_callback(
379+
let (_, assets_modified, tx_events, reason) = self.with_abort_callback(
368380
|vm_env| {
369381
if let Some(max_execution_time_duration) = max_execution_time {
370382
vm_env
@@ -383,10 +395,15 @@ pub trait TransactionConnection: ClarityConnection {
383395
},
384396
abort_call_back,
385397
)?;
386-
if let Some(aborted) = aborted {
387-
Err(Error::AbortedByCallback(None, asset_map, events, aborted))
398+
if let Some(reason) = reason {
399+
Err(Error::AbortedByCallback {
400+
output: None,
401+
assets_modified,
402+
tx_events,
403+
reason,
404+
})
388405
} else {
389-
Ok((asset_map, events))
406+
Ok((assets_modified, tx_events))
390407
}
391408
}
392409
}

stackslib/src/chainstate/stacks/db/transactions.rs

Lines changed: 39 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,17 @@ pub enum ClarityRuntimeTxError {
370370
error: clarity_error,
371371
err_type: &'static str,
372372
},
373-
AbortedByCallback(Option<Value>, AssetMap, Vec<StacksTransactionEvent>, String),
373+
AbortedByCallback {
374+
/// What the output value of the transaction would have been.
375+
/// This will be a Some for contract-calls, and None for contract initialization txs.
376+
output: Option<Value>,
377+
/// The asset map which was evaluated by the abort callback
378+
assets_modified: AssetMap,
379+
/// The events from the transaction processing
380+
tx_events: Vec<StacksTransactionEvent>,
381+
/// A human-readable explanation for aborting the transaction
382+
reason: String,
383+
},
374384
CostError(ExecutionCost, ExecutionCost),
375385
AnalysisError(CheckErrors),
376386
Rejectable(clarity_error),
@@ -400,9 +410,17 @@ pub fn handle_clarity_runtime_error(error: clarity_error) -> ClarityRuntimeTxErr
400410
ClarityRuntimeTxError::AnalysisError(check_error)
401411
}
402412
}
403-
clarity_error::AbortedByCallback(val, assets, events, reason) => {
404-
ClarityRuntimeTxError::AbortedByCallback(val, assets, events, reason)
405-
}
413+
clarity_error::AbortedByCallback {
414+
output,
415+
assets_modified,
416+
tx_events,
417+
reason,
418+
} => ClarityRuntimeTxError::AbortedByCallback {
419+
output,
420+
assets_modified,
421+
tx_events,
422+
reason,
423+
},
406424
clarity_error::CostError(cost, budget) => ClarityRuntimeTxError::CostError(cost, budget),
407425
unhandled_error => ClarityRuntimeTxError::Rejectable(unhandled_error),
408426
}
@@ -1105,7 +1123,12 @@ impl StacksChainState {
11051123
Some(error.to_string()),
11061124
)
11071125
}
1108-
ClarityRuntimeTxError::AbortedByCallback(value, assets, events, reason) => {
1126+
ClarityRuntimeTxError::AbortedByCallback {
1127+
output,
1128+
assets_modified,
1129+
tx_events,
1130+
reason,
1131+
} => {
11091132
info!("Contract-call aborted by post-condition";
11101133
"txid" => %tx.txid(),
11111134
"origin" => %origin_account.principal,
@@ -1115,9 +1138,9 @@ impl StacksChainState {
11151138
"function_args" => %VecDisplay(&contract_call.function_args));
11161139
let receipt = StacksTransactionReceipt::from_condition_aborted_contract_call(
11171140
tx.clone(),
1118-
events,
1119-
value.expect("BUG: Post condition contract call must provide would-have-been-returned value"),
1120-
assets.get_stx_burned_total()?,
1141+
tx_events,
1142+
output.expect("BUG: Post condition contract call must provide would-have-been-returned value"),
1143+
assets_modified.get_stx_burned_total()?,
11211144
total_cost,
11221145
reason,
11231146
);
@@ -1352,12 +1375,17 @@ impl StacksChainState {
13521375
};
13531376
return Ok(receipt);
13541377
}
1355-
ClarityRuntimeTxError::AbortedByCallback(_, assets, events, reason) => {
1378+
ClarityRuntimeTxError::AbortedByCallback {
1379+
assets_modified,
1380+
tx_events,
1381+
reason,
1382+
..
1383+
} => {
13561384
let receipt =
13571385
StacksTransactionReceipt::from_condition_aborted_smart_contract(
13581386
tx.clone(),
1359-
events,
1360-
assets.get_stx_burned_total()?,
1387+
tx_events,
1388+
assets_modified.get_stx_burned_total()?,
13611389
contract_analysis,
13621390
total_cost,
13631391
reason,

stackslib/src/chainstate/stacks/miner.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -650,11 +650,17 @@ impl TransactionResult {
650650
clarity_err
651651
}
652652
}
653-
ClarityRuntimeTxError::AbortedByCallback(val, assets, events, reason) => {
654-
Error::ClarityError(clarity_error::AbortedByCallback(
655-
val, assets, events, reason,
656-
))
657-
}
653+
ClarityRuntimeTxError::AbortedByCallback {
654+
output,
655+
assets_modified,
656+
tx_events,
657+
reason,
658+
} => Error::ClarityError(clarity_error::AbortedByCallback {
659+
output,
660+
assets_modified,
661+
tx_events,
662+
reason,
663+
}),
658664
},
659665
Error::InvalidFee => {
660666
// The transaction didn't have enough STX left over after it was run.

stackslib/src/clarity_vm/clarity.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2567,8 +2567,8 @@ mod tests {
25672567
)
25682568
})
25692569
.unwrap_err();
2570-
let result_value = if let Error::AbortedByCallback(v, ..) = e {
2571-
v.unwrap()
2570+
let result_value = if let Error::AbortedByCallback { output, .. } = e {
2571+
output.unwrap()
25722572
} else {
25732573
panic!("Expects a AbortedByCallback error")
25742574
};

stackslib/src/clarity_vm/tests/large_contract.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1018,7 +1018,7 @@ pub fn fcall_memory_test(#[case] clarity_version: ClarityVersion, #[case] epoch_
10181018
)
10191019
.unwrap_err()
10201020
{
1021-
ClarityError::AbortedByCallback(..) => true,
1021+
ClarityError::AbortedByCallback { .. } => true,
10221022
_ => false,
10231023
});
10241024
});

0 commit comments

Comments
 (0)