Skip to main content

blvm_primitives/
error.rs

1//! Error types for consensus validation
2
3use std::borrow::Cow;
4use thiserror::Error;
5
6/// Detailed script error codes, modeled after libbitcoin-consensus `verify_result`.
7///
8/// NOTE: This enum intentionally mirrors libbitcoin's naming where possible so that
9/// callers can perform precise compatibility checks. Not all variants are currently
10/// used in production code yet – they provide a complete mapping target that script
11/// validation can adopt incrementally.
12#[derive(Debug, Clone, Copy, PartialEq, Eq)]
13pub enum ScriptErrorCode {
14    EvalTrue,
15    EvalFalse,
16    ScriptSize,
17    PushSize,
18    OpCount,
19    StackSize,
20    SigCount,
21    PubkeyCount,
22    Verify,
23    EqualVerify,
24    CheckMultisigVerify,
25    ChecksigVerify,
26    NumEqualVerify,
27    BadOpcode,
28    DisabledOpcode,
29    InvalidStackOperation,
30    InvalidAltstackOperation,
31    UnbalancedConditional,
32    SigHashType,
33    SigDer,
34    MinimalData,
35    SigPushOnly,
36    SigHighS,
37    SigNullDummy,
38    PubkeyType,
39    CleanStack,
40    MinimalIf,
41    SigNullFail,
42    DiscourageUpgradableNops,
43    DiscourageUpgradableWitnessProgram,
44    WitnessProgramWrongLength,
45    WitnessProgramEmptyWitness,
46    WitnessProgramMismatch,
47    WitnessMalleated,
48    WitnessMalleatedP2SH,
49    WitnessUnexpected,
50    WitnessPubkeyType,
51    TxInvalid,
52    TxSizeInvalid,
53    TxInputInvalid,
54    NegativeLocktime,
55    UnsatisfiedLocktime,
56    ValueOverflow,
57    UnknownError,
58}
59
60#[derive(Error, Debug, PartialEq, Clone)]
61pub enum ConsensusError {
62    #[error("Transaction validation failed: {0}")]
63    TransactionValidation(Cow<'static, str>),
64
65    #[error("Block validation failed: {0}")]
66    BlockValidation(Cow<'static, str>),
67
68    #[error("Script execution failed: {0}")]
69    ScriptExecution(Cow<'static, str>),
70
71    /// Script failed with a detailed error code.
72    ///
73    /// This variant is preferred for new code that needs precise, libbitcoin-compatible
74    /// error reporting. Older callers can continue to use the `ScriptExecution` variant.
75    #[error("Script execution failed with code {code:?}: {message}")]
76    ScriptErrorWithCode {
77        code: ScriptErrorCode,
78        message: Cow<'static, str>,
79    },
80
81    #[error("UTXO not found: {0}")]
82    UtxoNotFound(Cow<'static, str>),
83
84    #[error("Invalid signature: {0}")]
85    InvalidSignature(Cow<'static, str>),
86
87    #[error("Invalid proof of work: {0}")]
88    InvalidProofOfWork(Cow<'static, str>),
89
90    #[error("Economic validation failed: {0}")]
91    EconomicValidation(Cow<'static, str>),
92
93    #[error("Serialization error: {0}")]
94    Serialization(Cow<'static, str>),
95
96    #[error("Consensus rule violation: {0}")]
97    ConsensusRuleViolation(Cow<'static, str>),
98
99    #[error("Invalid sighash type: {0}")]
100    InvalidSighashType(u8),
101
102    #[error("Invalid input index: {0}")]
103    InvalidInputIndex(usize),
104
105    #[error("Invalid prevouts count: expected {0}, got {1}")]
106    InvalidPrevoutsCount(usize, usize),
107}
108
109pub type Result<T> = std::result::Result<T, ConsensusError>;