midnight_onchain_vm/
error.rs1use crate::vm::{MAX_LOG_SIZE, MAX_STACK_HEIGHT};
15use base_crypto::fab::{AlignedValue, InvalidBuiltinDecode};
16use derive_where::derive_where;
17use runtime_state::state::{CELL_BOUND, StateValue};
18use std::convert::Infallible;
19use std::error::Error;
20use std::fmt::{self, Display, Formatter};
21use storage::db::DB;
22use transient_crypto::merkle_tree;
23
24#[derive_where(Debug, Clone, PartialEq, Eq)]
25pub enum OnchainProgramError<D: DB> {
26 RanOffStack,
27 RanPastProgramEnd,
28 ExpectedCell(StateValue<D>),
29 Decode(InvalidBuiltinDecode),
30 ArithmeticOverflow,
31 TooLongForEqual,
32 TypeError(String),
34 OutOfGas,
35 BoundsExceeded,
36 LogBoundExceeded,
37 InvalidArgs(String),
38 MissingKey,
39 CacheMiss,
40 AttemptedArrayDelete,
41 ReadMismatch {
42 expected: AlignedValue,
43 actual: AlignedValue,
44 },
45 CellBoundExceeded,
46 StackOverflow,
47 MerkleTreeError(merkle_tree::InvalidUpdate),
48}
49
50impl<D: DB> From<Infallible> for OnchainProgramError<D> {
51 fn from(err: Infallible) -> OnchainProgramError<D> {
52 match err {}
53 }
54}
55
56impl<D: DB> From<InvalidBuiltinDecode> for OnchainProgramError<D> {
57 fn from(err: InvalidBuiltinDecode) -> OnchainProgramError<D> {
58 OnchainProgramError::Decode(err)
59 }
60}
61
62impl<D: DB> Display for OnchainProgramError<D> {
63 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
64 use OnchainProgramError::*;
65 match self {
66 RanOffStack => write!(f, "ran off stack"),
67 RanPastProgramEnd => write!(f, "ran past end of program"),
68 ExpectedCell(state_value) => {
69 let descriptor = match state_value {
70 StateValue::Null => Some("null"),
71 StateValue::Cell(_) => Some("cell"),
72 StateValue::BoundedMerkleTree(_) => Some("bounded Merkle tree"),
73 StateValue::Map(_) => Some("map"),
74 StateValue::Array(_) => Some("array"),
75 _ => None,
76 };
77 match descriptor {
78 Some(d) => write!(f, "expected a cell, received {d}"),
79 None => write!(f, "bug found: unexpected state value"),
80 }
81 }
82 ArithmeticOverflow => write!(f, "arithmetic overflow"),
83 TooLongForEqual => write!(f, "data is too long for equality check"),
84 TypeError(msg) => write!(f, "invalid operation for type: {}", msg),
85 OutOfGas => write!(f, "ran out of gas budget"),
86 BoundsExceeded => write!(f, "exceeded structure bounds"),
87 InvalidArgs(msg) => write!(f, "invalid argument to primitive operation: {msg}"),
88 MissingKey => write!(f, "key not found"),
89 CacheMiss => write!(f, "value declared to be in cache wasn't"),
90 AttemptedArrayDelete => write!(f, "attempted to remove from an array type"),
91 ReadMismatch { expected, actual } => write!(
92 f,
93 "mismatch between expected ({expected:?}) and actual ({actual:?}) read"
94 ),
95 Decode(err) => err.fmt(f),
96 CellBoundExceeded => write!(f, "exceeded the maximum bound for cells: {CELL_BOUND}"),
97 LogBoundExceeded => write!(
98 f,
99 "exceeded the maximum bound for log instruction: {MAX_LOG_SIZE}"
100 ),
101 StackOverflow => write!(
102 f,
103 "exceeded the maximum bound for Impact stack: {MAX_STACK_HEIGHT}"
104 ),
105 MerkleTreeError(msg) => write!(f, "merkle tree error: {msg}"),
106 }
107 }
108}
109
110impl<D: DB> Error for OnchainProgramError<D> {}