subcoin_script/
error.rs

1use crate::constants::{MAX_OPS_PER_SCRIPT, MAX_SCRIPT_ELEMENT_SIZE, MAX_STACK_SIZE};
2use crate::interpreter::{CheckMultiSigError, CheckSigError};
3use crate::num::NumError;
4use crate::signature_checker::SignatureError;
5use crate::stack::StackError;
6
7/// Script error type.
8#[derive(Debug, Eq, PartialEq, thiserror::Error)]
9pub enum Error {
10    ///////////////////////////
11    // Script error.
12    ///////////////////////////
13    /// ErrEvalFalse is returned when the script evaluated without error but
14    /// terminated with a false top stack element.
15    #[error("script terminated with a false stack element")]
16    EvalFalse,
17    #[error("Return early if OP_RETURN is executed in the script.")]
18    OpReturn,
19
20    // Max sizes.
21    #[error("Exceeds max script size")]
22    ScriptSize,
23    #[error(
24        "Size of the element pushed to the stack exceeds MAX_SCRIPT_ELEMENT_SIZE ({MAX_SCRIPT_ELEMENT_SIZE})"
25    )]
26    PushSize,
27    #[error("Exceeds max operations ({MAX_OPS_PER_SCRIPT}) per script")]
28    OpCount,
29    // Stack and altstack combined depth is over the limit.
30    #[error("Exceeds stack limit ({MAX_STACK_SIZE})")]
31    StackSize,
32    #[error("sig count")]
33    SigCount,
34    #[error("pubkey count")]
35    PubkeyCount,
36
37    // Failed verify operations.
38    #[error("OP_VERIFY failed at opcode: {0:?}")]
39    Verify(bitcoin::opcodes::Opcode),
40
41    // Logical/Format/Canonical errors.
42    #[error("bad opcode")]
43    BadOpcode,
44    // A disabled opcode is encountered in a script.
45    #[error("attempt to execute disabled opcode {0}")]
46    DisabledOpcode(bitcoin::opcodes::Opcode),
47    #[error(transparent)]
48    Stack(#[from] StackError),
49    // ErrUnbalancedConditional is returned when an OP_ELSE or OP_ENDIF is
50    // encountered in a script without first having an OP_IF or OP_NOTIF or
51    // the end of script is reached without encountering an OP_ENDIF when
52    // an OP_IF or OP_NOTIF was previously encountered.
53    #[error("unbalanced conditional")]
54    UnbalancedConditional,
55
56    // CHECKLOCKTIMEVERIFY and CHECKSEQUENCEVERIFY
57    // Script contains an opcode that interprets a negative lock time.
58    #[error("Locktime is negative")]
59    NegativeLocktime,
60    // Script contains an opcode that involves a lock time and the required
61    // lock time has not been reached.
62    #[error("Required lock time has not been reached.")]
63    UnsatisfiedLocktime,
64
65    // Malleability
66    #[error("sig hash type")]
67    SigHashType,
68    #[error("Native witness program cannot also have a signature script")]
69    WitnessMalleated,
70    #[error("witness unexpected")]
71    WitnessUnexpected,
72    #[error("witness malleated p2sh")]
73    WitnessMalleatedP2SH,
74    // ErrCleanStack is returned when the ScriptVerifyCleanStack flag
75    // is set, and after evaluation, the stack does not contain only a
76    // single element.
77    #[error("clean stack")]
78    CleanStack,
79    // ErrMinimalIf is returned if ScriptVerifyWitness is set and the
80    // operand of an OP_IF/OP_NOF_IF are not either an empty vector or
81    // [0x01].
82    #[error("minimal if")]
83    Minimalif,
84    #[error("signature push only")]
85    SigPushOnly,
86    #[error("witness program witness empty")]
87    WitnessProgramWitnessEmpty,
88    #[error("witness program mismatch")]
89    WitnessProgramMismatch,
90    #[error("witness program wrong length")]
91    WitnessProgramWrongLength,
92
93    // Softfork safeness.
94    #[error("NOP opcode encountered when DISCOURAGE_UPGRADABLE_NOPS flag is set")]
95    DiscourageUpgradableNops,
96    #[error("discourage upgradable witness program")]
97    DiscourageUpgradableWitnessProgram,
98    #[error("discourage upgradable taproot program")]
99    DiscourageUpgradableTaprootProgram,
100    #[error("discourage upgradable taproot version")]
101    DiscourageUpgradableTaprootVersion,
102    // ErrDiscourageOpSuccess is returned if
103    // ScriptVerifyDiscourageOpSuccess is active, and a OP_SUCCESS op code
104    // is encountered during tapscript validation.
105    #[error("OP_SUCCESS encountered when ScriptVerifyDiscourageOpSuccess is active.")]
106    DiscourageOpSuccess,
107    // ErrDiscourageUpgradeableTaprootVersion is returned if
108    // ScriptVerifyDiscourageUpgradeableTaprootVersion is active and a leaf
109    // version encountered isn't the base leaf version.
110    #[error("discourage upgrable pubkey type")]
111    DiscourageUpgradablePubkeyType,
112
113    // Taproot
114    #[error("schnorr sig size")]
115    SchnorrSigSize,
116    #[error("schnorr sig hash type")]
117    SchnorrSigHashType,
118    #[error("schnorr sig")]
119    SchnorrSig,
120    #[error("Taproot control block has a bad length")]
121    TaprootWrongControlSize,
122    #[error("taproot validation weight")]
123    TaprootValidationWeight,
124    #[error("taproot checkmultisig")]
125    TaprootCheckmultisig,
126    // ErrMinimalIf is returned if ScriptVerifyWitness is set and the
127    // operand of an OP_IF/OP_NOF_IF are not either an empty vector or
128    // [0x01].
129    #[error("taproot minimalif")]
130    TaprootMinimalif,
131
132    // Constant scriptCode
133    // OP_CODESEPARATOR is used in a non-segwit script.
134    #[error("OP_CODESEPARATOR is used in a non-segwit script.")]
135    OpCodeSeparator,
136    #[error("sig findanddelete")]
137    SigFindAndDelete,
138
139    #[error("error count")]
140    ErrorCount,
141
142    // Extended errors.
143    #[error("No script execution")]
144    NoScriptExecution,
145    #[error("Invalid alt stack operation")]
146    InvalidAltStackOperation,
147    #[error("{0} is unknown")]
148    UnknownOpcode(bitcoin::opcodes::Opcode),
149    #[error("Failed to read instruction: {0:?}")]
150    ReadInstruction(bitcoin::script::Error),
151
152    #[error(transparent)]
153    CheckSig(#[from] CheckSigError),
154    #[error(transparent)]
155    CheckMultiSig(#[from] CheckMultiSigError),
156    #[error(transparent)]
157    Num(#[from] NumError),
158    #[error(transparent)]
159    Signature(#[from] SignatureError),
160
161    #[error("schnorr signature error: {0:?}")]
162    SchnorrSignature(bitcoin::taproot::SigFromSliceError),
163    #[error("taproot error: {0:?}")]
164    Taproot(bitcoin::taproot::TaprootError),
165    #[error("secp256k1 error: {0:?}")]
166    Secp256k1(bitcoin::secp256k1::Error),
167}