Skip to main content

tari_script/
error.rs

1// Copyright 2020. The Tari Project
2// Redistribution and use in source and binary forms, with or without modification, are permitted provided that the
3// following conditions are met:
4// 1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following
5// disclaimer.
6// 2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the
7// following disclaimer in the documentation and/or other materials provided with the distribution.
8// 3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote
9// products derived from this software without specific prior written permission.
10// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
11// INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
12// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
13// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
14// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
15// WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
16// USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
17
18use std::num::TryFromIntError;
19
20use serde::{Deserialize, Serialize};
21use tari_max_size::MaxSizeVecError;
22use tari_utilities::ByteArrayError;
23use thiserror::Error;
24
25#[derive(Debug, Clone, Error, PartialEq, Eq, Serialize, Deserialize)]
26pub enum ScriptError {
27    #[error("The script failed with an explicit Return")]
28    Return,
29    #[error("The stack cannot exceed MAX_STACK_SIZE items")]
30    StackOverflow,
31    #[error("The script completed execution with a stack size other than one")]
32    NonUnitLengthStack,
33    #[error("Tried to pop an element off an empty stack")]
34    StackUnderflow,
35    #[error("An operand was applied to incompatible types")]
36    IncompatibleTypes,
37    #[error("A script opcode resulted in a value that exceeded the maximum or minimum value")]
38    ValueExceedsBounds,
39    #[error("The script encountered an invalid opcode")]
40    InvalidOpcode,
41    #[error("The script is missing closing opcodes (Else or EndIf)")]
42    MissingOpcode,
43    #[error("The script contained an invalid signature")]
44    InvalidSignature,
45    #[error("The serialised stack contained invalid input")]
46    InvalidInput,
47    #[error("The script contained invalid data")]
48    InvalidData,
49    #[error("A verification opcode failed, aborting the script immediately")]
50    VerifyFailed,
51    #[error("as_hash requires a Digest function that returns at least 32 bytes")]
52    InvalidDigest,
53    #[error("A compare opcode failed, aborting the script immediately with reason: `{0}`")]
54    CompareFailed(String),
55    #[error("Max sized vector error: {0}")]
56    MaxSizeVecError(#[from] MaxSizeVecError),
57}
58
59impl ScriptError {
60    pub fn to_std_io_error(self) -> std::io::Error {
61        std::io::Error::other(self.to_string())
62    }
63}
64
65impl From<TryFromIntError> for ScriptError {
66    fn from(_err: TryFromIntError) -> ScriptError {
67        ScriptError::ValueExceedsBounds
68    }
69}
70
71impl From<ByteArrayError> for ScriptError {
72    fn from(_err: ByteArrayError) -> ScriptError {
73        ScriptError::InvalidData
74    }
75}