Skip to main content

ave_core/evaluation/
response.rs

1use ave_common::{ValueWrapper, identity::DigestIdentifier};
2
3use borsh::{BorshDeserialize, BorshSerialize};
4use serde::{Deserialize, Serialize};
5
6use crate::evaluation::compiler::error::CompilerError;
7use crate::evaluation::runner::error::RunnerError;
8
9/// A struct representing an evaluation response.
10#[derive(
11    Debug,
12    Clone,
13    Serialize,
14    Deserialize,
15    Eq,
16    PartialEq,
17    BorshSerialize,
18    BorshDeserialize,
19)]
20pub enum EvaluationRes {
21    Error {
22        error: EvaluatorError,
23        eval_req_hash: DigestIdentifier,
24        req_subject_data_hash: DigestIdentifier,
25    },
26    Abort(String),
27    TimeOut,
28    Response {
29        response: EvaluatorResponse,
30        eval_req_hash: DigestIdentifier,
31        req_subject_data_hash: DigestIdentifier,
32    },
33    Reboot,
34}
35
36#[derive(
37    Debug,
38    Clone,
39    Serialize,
40    Deserialize,
41    Eq,
42    PartialEq,
43    BorshSerialize,
44    BorshDeserialize,
45    Hash,
46)]
47pub enum EvaluatorError {
48    InvalidEventSignature,
49    InvalidEventRequest(String),
50    Runner(EvalRunnerError),
51    InternalError(String),
52}
53
54#[derive(
55    Debug,
56    Clone,
57    Serialize,
58    Deserialize,
59    Eq,
60    PartialEq,
61    BorshSerialize,
62    BorshDeserialize,
63    Hash,
64)]
65pub enum EvalRunnerError {
66    InvalidEvent(String),
67    ContractFailed(String),
68    ContractNotFound(String),
69}
70
71impl std::fmt::Display for EvalRunnerError {
72    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
73        match self {
74            Self::InvalidEvent(msg) => write!(f, "invalid event: {}", msg),
75            Self::ContractFailed(msg) => write!(f, "contract failed: {}", msg),
76            Self::ContractNotFound(msg) => {
77                write!(f, "contract not found: {}", msg)
78            }
79        }
80    }
81}
82
83impl std::fmt::Display for EvaluatorError {
84    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
85        match self {
86            Self::InvalidEventSignature => write!(f, "invalid event signature"),
87            Self::InvalidEventRequest(e) => {
88                write!(f, "invalid event request {}", e)
89            }
90            Self::Runner(e) => write!(f, "runner error: {}", e),
91            Self::InternalError(msg) => write!(f, "internal error: {}", msg),
92        }
93    }
94}
95
96impl From<CompilerError> for EvaluatorError {
97    fn from(value: CompilerError) -> Self {
98        match value {
99            // Errores del usuario: el contrato enviado es inválido
100            CompilerError::Base64DecodeFailed { .. } => {
101                Self::InvalidEventRequest(value.to_string())
102            }
103            CompilerError::CompilationFailed
104            | CompilerError::InvalidModule { .. }
105            | CompilerError::EntryPointNotFound { .. }
106            | CompilerError::ContractCheckFailed { .. }
107            | CompilerError::ContractExecutionFailed { .. }
108            | CompilerError::InvalidContractOutput { .. } => {
109                Self::Runner(EvalRunnerError::ContractFailed(value.to_string()))
110            }
111            // Fallos del sistema: no deberían ocurrir en un entorno sano
112            CompilerError::CargoBuildFailed { .. }
113            | CompilerError::DirectoryCreationFailed { .. }
114            | CompilerError::FileWriteFailed { .. }
115            | CompilerError::FileReadFailed { .. }
116            | CompilerError::MissingHelper { .. }
117            | CompilerError::FuelLimitError { .. }
118            | CompilerError::WasmPrecompileFailed { .. }
119            | CompilerError::WasmDeserializationFailed { .. }
120            | CompilerError::InstantiationFailed { .. }
121            | CompilerError::MemoryAllocationFailed { .. }
122            | CompilerError::SerializationError { .. } => {
123                Self::InternalError(value.to_string())
124            }
125        }
126    }
127}
128
129impl From<RunnerError> for EvaluatorError {
130    fn from(value: RunnerError) -> Self {
131        match value {
132            RunnerError::InvalidEvent { .. } => {
133                Self::Runner(EvalRunnerError::InvalidEvent(value.to_string()))
134            }
135            RunnerError::ContractFailed { .. } => {
136                Self::Runner(EvalRunnerError::ContractFailed(value.to_string()))
137            }
138            RunnerError::ContractNotFound { .. } => Self::Runner(
139                EvalRunnerError::ContractNotFound(value.to_string()),
140            ),
141            RunnerError::MissingHelper { .. } => {
142                Self::InternalError(value.to_string())
143            }
144            RunnerError::WasmError { .. } => {
145                Self::InternalError(value.to_string())
146            }
147            RunnerError::SerializationError { .. } => {
148                Self::InternalError(value.to_string())
149            }
150            RunnerError::MemoryError { .. } => {
151                Self::InternalError(value.to_string())
152            }
153        }
154    }
155}
156
157#[derive(
158    Debug,
159    Clone,
160    Serialize,
161    Deserialize,
162    Eq,
163    PartialEq,
164    BorshSerialize,
165    BorshDeserialize,
166    Hash,
167)]
168pub struct EvaluatorResponse {
169    /// The patch to apply to the state.
170    pub patch: ValueWrapper,
171    /// The hash of the state after applying the patch.
172    pub properties_hash: DigestIdentifier,
173    /// Whether approval is required for the evaluation to be applied to the state.
174    pub appr_required: bool,
175}
176
177pub enum ResponseSummary {
178    Reboot,
179    Error,
180    Ok,
181}
182
183impl ResponseSummary {
184    pub const fn is_ok(&self) -> bool {
185        match self {
186            Self::Reboot => false,
187            Self::Error => false,
188            Self::Ok => true,
189        }
190    }
191}