Skip to content

Commit aa20011

Browse files
Merge pull request #6641 from Jiloc/chore/document-cost-errors
chore: add docstrings for `CostErrors`
2 parents ea6f561 + 421f4fc commit aa20011

File tree

2 files changed

+51
-2
lines changed

2 files changed

+51
-2
lines changed

clarity-types/src/errors/cost.rs

Lines changed: 29 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,43 @@ use std::fmt;
1616

1717
use crate::execution_cost::ExecutionCost;
1818

19+
/// Errors related to cost tracking and resource accounting in the Clarity VM.
20+
///
21+
/// Each error variant is annotated with "Invalidates Block" status, indicating
22+
/// whether the inclusion of the transaction that caused the error should cause
23+
/// an entire block to be rejected.
1924
#[derive(Debug, PartialEq, Eq)]
2025
pub enum CostErrors {
21-
CostComputationFailed(String),
26+
/// Arithmetic overflow in cost computation during type-checking, exceeding the maximum threshold.
27+
/// Invalidates Block: true.
2228
CostOverflow,
29+
/// Cumulative type-checking cost exceeds the allocated budget, indicating budget depletion.
30+
/// The first `ExecutionCost` represents the total consumed cost, and the second represents the budget limit.
31+
/// Invalidates Block: true.
2332
CostBalanceExceeded(ExecutionCost, ExecutionCost),
33+
/// Memory usage during type-checking exceeds the allocated budget.
34+
/// The first `u64` represents the total consumed memory, and the second represents the memory limit.
35+
/// Invalidates Block:
36+
/// - true if happens during contract analysis
37+
/// - false if happens during contract intitialization or contract call.
2438
MemoryBalanceExceeded(u64, u64),
39+
/// Failure to access or load cost-related contracts or their state during runtime operations.
40+
/// Invalidates Block: false.
2541
CostContractLoadFailure,
42+
/// Failure in cost-tracking due to an unexpected condition or invalid state.
43+
/// The `String` wraps the specific reason for the failure.
44+
/// Invalidates Block: false.
45+
CostComputationFailed(String),
46+
// Time checker errors
47+
/// Type-checking time exceeds the allowed budget, halting analysis to ensure responsiveness.
48+
/// Invalidates Block: true.
49+
ExecutionTimeExpired,
50+
/// Unexpected condition or failure, indicating a bug or invalid state.
51+
/// Invalidates Block: true.
2652
InterpreterFailure,
53+
/// Unexpected condition or failure, indicating a bug or invalid state.
54+
/// Invalidates Block: true.
2755
Expect(String),
28-
ExecutionTimeExpired,
2956
}
3057

3158
impl fmt::Display for CostErrors {

clarity/src/vm/clarity.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,28 @@ impl From<CheckError> for Error {
8181
}
8282
}
8383

84+
/// Converts [`InterpreterError`] to [`Error`] for transaction execution contexts.
85+
///
86+
/// This conversion is used in:
87+
/// - [`TransactionConnection::initialize_smart_contract`]
88+
/// - [`TransactionConnection::run_contract_call`]
89+
/// - [`TransactionConnection::run_stx_transfer`]
90+
///
91+
/// # Notes
92+
///
93+
/// - [`CheckErrors::MemoryBalanceExceeded`] and [`CheckErrors::CostComputationFailed`]
94+
/// are intentionally not converted to [`Error::CostError`].
95+
/// Instead, they remain wrapped in `Error::Interpreter(Unchecked(CheckErrors::MemoryBalanceExceeded))`,
96+
/// which causes the transaction to fail, but still be included in the block.
97+
///
98+
/// - This behavior differs from direct conversions of [`CheckError`] and [`ParseError`] to [`Error`],
99+
/// where [`CheckErrors::MemoryBalanceExceeded`] is converted to [`Error::CostError`],
100+
/// during contract analysis.
101+
///
102+
/// As a result:
103+
/// - A `MemoryBalanceExceeded` during contract analysis causes the block to be rejected.
104+
/// - A `MemoryBalanceExceeded` during execution (initialization or contract call)
105+
/// causes the transaction to fail, but the block remains valid.
84106
impl From<InterpreterError> for Error {
85107
fn from(e: InterpreterError) -> Self {
86108
match &e {

0 commit comments

Comments
 (0)