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    /// A non-segwit script code contained the signature `FindAndDelete` removes.
59    SigFindAndDelete,
60}
61
62#[derive(Error, Debug, PartialEq, Clone)]
63pub enum ConsensusError {
64    #[error("Transaction validation failed: {0}")]
65    TransactionValidation(Cow<'static, str>),
66
67    #[error("Block validation failed: {0}")]
68    BlockValidation(Cow<'static, str>),
69
70    #[error("Script execution failed: {0}")]
71    ScriptExecution(Cow<'static, str>),
72
73    /// Script failed with a detailed error code.
74    ///
75    /// This variant is preferred for new code that needs precise, libbitcoin-compatible
76    /// error reporting. Older callers can continue to use the `ScriptExecution` variant.
77    #[error("Script execution failed with code {code:?}: {message}")]
78    ScriptErrorWithCode {
79        code: ScriptErrorCode,
80        message: Cow<'static, str>,
81    },
82
83    #[error("UTXO not found: {0}")]
84    UtxoNotFound(Cow<'static, str>),
85
86    #[error("Invalid signature: {0}")]
87    InvalidSignature(Cow<'static, str>),
88
89    #[error("Invalid proof of work: {0}")]
90    InvalidProofOfWork(Cow<'static, str>),
91
92    #[error("Economic validation failed: {0}")]
93    EconomicValidation(Cow<'static, str>),
94
95    #[error("Serialization error: {0}")]
96    Serialization(Cow<'static, str>),
97
98    #[error("Consensus rule violation: {0}")]
99    ConsensusRuleViolation(Cow<'static, str>),
100
101    #[error("Invalid sighash type: {0}")]
102    InvalidSighashType(u8),
103
104    #[error("Invalid input index: {0}")]
105    InvalidInputIndex(usize),
106
107    #[error("Invalid prevouts count: expected {0}, got {1}")]
108    InvalidPrevoutsCount(usize, usize),
109}
110
111pub type Result<T> = std::result::Result<T, ConsensusError>;