Skip to main content

ave_core/evaluation/
response.rs

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