uplc_turbo/machine/
error.rs1use std::array::TryFromSliceError;
2
3use crate::{
4 binder::Eval,
5 bls::BlsError,
6 constant::{Constant, Integer},
7 data::PlutusData,
8 term::Term,
9 typ::Type,
10};
11
12use super::{value::Value, ExBudget};
13
14#[derive(thiserror::Error, Debug)]
15pub enum MachineError<'a, V>
16where
17 V: Eval<'a>,
18{
19 #[error("Explicit error term")]
20 ExplicitErrorTerm,
21 #[error("Non-function application")]
22 NonFunctionApplication(&'a Value<'a, V>, &'a Value<'a, V>),
23 #[error("Non-constant value")]
24 NotAConstant(&'a Value<'a, V>),
25 #[error("Open term evaluated")]
26 OpenTermEvaluated(&'a Term<'a, V>),
27 #[error("Out of budget")]
28 OutOfExError(ExBudget),
29 #[error("Unexpected builtin term argument")]
30 UnexpectedBuiltinTermArgument(&'a Term<'a, V>),
31 #[error("Non-polymorphic instantiation")]
32 NonPolymorphicInstantiation(&'a Value<'a, V>),
33 #[error("Builtin term argument expected")]
34 BuiltinTermArgumentExpected(&'a Term<'a, V>),
35 #[error("Non-constructor scrutinized")]
36 NonConstrScrutinized(&'a Value<'a, V>),
37 #[error("Non-integer index")]
38 MissingCaseBranch(&'a [&'a Term<'a, V>], &'a Value<'a, V>),
39 #[error(transparent)]
40 Runtime(RuntimeError<'a>),
41}
42
43#[derive(thiserror::Error, Debug)]
44pub enum RuntimeError<'a> {
45 #[error("Byte string out of bounds")]
46 ByteStringOutOfBounds(&'a [u8], &'a Integer),
47 #[error("Type mismatch")]
48 TypeMismatch(Type<'a>, &'a Constant<'a>),
49 #[error("Expected pair")]
50 ExpectedPair(&'a Constant<'a>),
51 #[error("Expected list")]
52 ExpectedList(&'a Constant<'a>),
53 #[error("Not data")]
54 NotData(&'a Constant<'a>),
55 #[error("Malformed data")]
56 MalFormedData(&'a PlutusData<'a>),
57 #[error("Empty list")]
58 EmptyList(&'a [&'a Constant<'a>]),
59 #[error("Unexpected Ed25519 public key length")]
60 UnexpectedEd25519PublicKeyLength(TryFromSliceError),
61 #[error("Unexpected Ed25519 signature length")]
62 UnexpectedEd25519SignatureLength(TryFromSliceError),
63 #[error("Division by zero")]
64 DivisionByZero(&'a Integer, &'a Integer),
65 #[error("MkCons type mismatch")]
66 MkConsTypeMismatch(&'a Constant<'a>),
67 #[error("Byte string cons not a byte")]
68 ByteStringConsNotAByte(&'a Integer),
69 #[error(transparent)]
70 Secp256k1(#[from] secp256k1::Error),
71 #[error(transparent)]
72 DecodeUtf8(#[from] std::str::Utf8Error),
73 #[error(transparent)]
74 Bls(#[from] BlsError),
75 #[error("Bls Error: Hash to curve dst too big")]
76 HashToCurveDstTooBig,
77 #[error(
78 "bytes size beyond limit when converting from integer\n{:>13} {0}\n{:>13} {1}",
79 "Size",
80 "Maximum"
81 )]
82 IntegerToByteStringSizeTooBig(&'a Integer, i64),
83 #[error(
84 "bytes size below limit when converting from integer\n{:>13} {0}\n{:>13} {1}",
85 "Size",
86 "Minimum"
87 )]
88 IntegerToByteStringSizeTooSmall(&'a Integer, usize),
89 #[error("integerToByteString encountered negative input\n{:>13} {0}", "Input")]
90 IntegerToByteStringNegativeInput(&'a Integer),
91 #[error("integerToByteString encountered negative size\n{:>13} {0}", "Size")]
92 IntegerToByteStringNegativeSize(&'a Integer),
93}
94
95impl<'a, V> MachineError<'a, V>
96where
97 V: Eval<'a>,
98{
99 pub fn runtime(runtime_error: RuntimeError<'a>) -> Self {
100 MachineError::Runtime(runtime_error)
101 }
102
103 pub fn type_mismatch(expected: Type<'a>, constant: &'a Constant<'a>) -> Self {
104 MachineError::runtime(RuntimeError::TypeMismatch(expected, constant))
105 }
106
107 pub fn mk_cons_type_mismatch(constant: &'a Constant<'a>) -> Self {
108 MachineError::runtime(RuntimeError::MkConsTypeMismatch(constant))
109 }
110
111 pub fn expected_pair(constant: &'a Constant<'a>) -> Self {
112 MachineError::runtime(RuntimeError::ExpectedPair(constant))
113 }
114
115 pub fn expected_list(constant: &'a Constant<'a>) -> Self {
116 MachineError::runtime(RuntimeError::ExpectedList(constant))
117 }
118
119 pub fn empty_list(constant: &'a [&'a Constant<'a>]) -> Self {
120 MachineError::runtime(RuntimeError::EmptyList(constant))
121 }
122
123 pub fn byte_string_out_of_bounds(byte_string: &'a [u8], index: &'a Integer) -> Self {
124 MachineError::runtime(RuntimeError::ByteStringOutOfBounds(byte_string, index))
125 }
126
127 pub fn not_data(constant: &'a Constant<'a>) -> Self {
128 MachineError::runtime(RuntimeError::NotData(constant))
129 }
130
131 pub fn malformed_data(plutus_data: &'a PlutusData<'a>) -> Self {
132 MachineError::runtime(RuntimeError::MalFormedData(plutus_data))
133 }
134
135 pub fn unexpected_ed25519_public_key_length(length: TryFromSliceError) -> Self {
136 MachineError::runtime(RuntimeError::UnexpectedEd25519PublicKeyLength(length))
137 }
138
139 pub fn unexpected_ed25519_signature_length(length: TryFromSliceError) -> Self {
140 MachineError::runtime(RuntimeError::UnexpectedEd25519SignatureLength(length))
141 }
142
143 pub fn division_by_zero(numerator: &'a Integer, denominator: &'a Integer) -> Self {
144 MachineError::runtime(RuntimeError::DivisionByZero(numerator, denominator))
145 }
146
147 pub fn byte_string_cons_not_a_byte(byte: &'a Integer) -> Self {
148 MachineError::runtime(RuntimeError::ByteStringConsNotAByte(byte))
149 }
150
151 pub fn secp256k1(error: secp256k1::Error) -> Self {
152 MachineError::runtime(RuntimeError::Secp256k1(error))
153 }
154
155 pub fn decode_utf8(error: std::str::Utf8Error) -> Self {
156 MachineError::runtime(RuntimeError::DecodeUtf8(error))
157 }
158
159 pub fn bls(error: BlsError) -> Self {
160 MachineError::runtime(RuntimeError::Bls(error))
161 }
162
163 pub fn hash_to_curve_dst_too_big() -> Self {
164 MachineError::runtime(RuntimeError::HashToCurveDstTooBig)
165 }
166
167 pub fn integer_to_byte_string_size_too_big(integer: &'a Integer, maximum: i64) -> Self {
168 MachineError::runtime(RuntimeError::IntegerToByteStringSizeTooBig(
169 integer, maximum,
170 ))
171 }
172
173 pub fn integer_to_byte_string_size_too_small(integer: &'a Integer, minimum: usize) -> Self {
174 MachineError::runtime(RuntimeError::IntegerToByteStringSizeTooSmall(
175 integer, minimum,
176 ))
177 }
178
179 pub fn integer_to_byte_string_negative_input(integer: &'a Integer) -> Self {
180 MachineError::runtime(RuntimeError::IntegerToByteStringNegativeInput(integer))
181 }
182
183 pub fn integer_to_byte_string_negative_size(integer: &'a Integer) -> Self {
184 MachineError::runtime(RuntimeError::IntegerToByteStringNegativeSize(integer))
185 }
186}