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::InvalidContractPath { .. }
114            | CompilerError::DirectoryCreationFailed { .. }
115            | CompilerError::FileWriteFailed { .. }
116            | CompilerError::FileReadFailed { .. }
117            | CompilerError::MissingHelper { .. }
118            | CompilerError::ContractRegisterFailed { .. }
119            | CompilerError::ToolchainFingerprintFailed { .. }
120            | CompilerError::FuelLimitError { .. }
121            | CompilerError::WasmPrecompileFailed { .. }
122            | CompilerError::WasmDeserializationFailed { .. }
123            | CompilerError::InstantiationFailed { .. }
124            | CompilerError::MemoryAllocationFailed { .. }
125            | CompilerError::SerializationError { .. } => {
126                Self::InternalError(value.to_string())
127            }
128        }
129    }
130}
131
132impl From<RunnerError> for EvaluatorError {
133    fn from(value: RunnerError) -> Self {
134        match value {
135            RunnerError::InvalidEvent { .. } => {
136                Self::Runner(EvalRunnerError::InvalidEvent(value.to_string()))
137            }
138            RunnerError::ContractFailed { .. } => {
139                Self::Runner(EvalRunnerError::ContractFailed(value.to_string()))
140            }
141            RunnerError::ContractNotFound { .. } => Self::Runner(
142                EvalRunnerError::ContractNotFound(value.to_string()),
143            ),
144            RunnerError::MissingHelper { .. } => {
145                Self::InternalError(value.to_string())
146            }
147            RunnerError::WasmError { .. } => {
148                Self::InternalError(value.to_string())
149            }
150            RunnerError::SerializationError { .. } => {
151                Self::InternalError(value.to_string())
152            }
153            RunnerError::MemoryError { .. } => {
154                Self::InternalError(value.to_string())
155            }
156        }
157    }
158}
159
160#[derive(
161    Debug,
162    Clone,
163    Serialize,
164    Deserialize,
165    Eq,
166    PartialEq,
167    BorshSerialize,
168    BorshDeserialize,
169    Hash,
170)]
171pub struct EvaluatorResponse {
172    /// The patch to apply to the state.
173    pub patch: ValueWrapper,
174    /// The hash of the state after applying the patch.
175    pub properties_hash: DigestIdentifier,
176    /// Whether approval is required for the evaluation to be applied to the state.
177    pub appr_required: bool,
178}
179
180pub enum ResponseSummary {
181    Reboot,
182    Error,
183    Ok,
184}
185
186impl ResponseSummary {
187    pub const fn is_ok(&self) -> bool {
188        match self {
189            Self::Reboot => false,
190            Self::Error => false,
191            Self::Ok => true,
192        }
193    }
194}