avm_interface/
outcome.rs

1/*
2 * Copyright 2021 Fluence Labs Limited
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use super::CallRequests;
18use crate::raw_outcome::RawAVMOutcome;
19
20use air_interpreter_interface::SoftLimitsTriggering;
21use serde::Deserialize;
22use serde::Serialize;
23
24use std::time::Duration;
25
26#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
27pub struct AVMOutcome {
28    /// Contains script data that should be preserved in an executor of this interpreter
29    /// regardless of ret_code value.
30    pub data: Vec<u8>,
31
32    /// Collected parameters of all met call instructions that could be executed on a current peer.
33    pub call_requests: CallRequests,
34
35    /// Public keys of peers that should receive data.
36    pub next_peer_pks: Vec<String>,
37
38    /// Memory in bytes AVM linear heap was extended during execution by.
39    pub memory_delta: usize,
40
41    /// Time of a particle execution
42    /// (it counts only execution time without operations with DataStore and so on)
43    pub execution_time: Duration,
44
45    /// To store and convey soft limits triggering flags.
46    pub soft_limits_triggering: SoftLimitsTriggering,
47}
48
49#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
50pub struct ErrorAVMOutcome {
51    pub error_code: i64,
52    pub error_message: String,
53    pub outcome: AVMOutcome,
54}
55
56impl AVMOutcome {
57    fn new(
58        data: Vec<u8>,
59        call_requests: CallRequests,
60        next_peer_pks: Vec<String>,
61        memory_delta: usize,
62        execution_time: Duration,
63        soft_limits_triggering: SoftLimitsTriggering,
64    ) -> Self {
65        Self {
66            data,
67            call_requests,
68            next_peer_pks,
69            memory_delta,
70            execution_time,
71            soft_limits_triggering,
72        }
73    }
74
75    #[allow(clippy::result_large_err)]
76    pub fn from_raw_outcome(
77        raw_outcome: RawAVMOutcome,
78        memory_delta: usize,
79        execution_time: Duration,
80    ) -> Result<Self, ErrorAVMOutcome> {
81        use air_interpreter_interface::INTERPRETER_SUCCESS;
82
83        let RawAVMOutcome {
84            ret_code,
85            error_message,
86            data,
87            call_requests,
88            next_peer_pks,
89            soft_limits_triggering,
90        } = raw_outcome;
91
92        let avm_outcome = AVMOutcome::new(
93            data,
94            call_requests,
95            next_peer_pks,
96            memory_delta,
97            execution_time,
98            soft_limits_triggering,
99        );
100
101        if ret_code == INTERPRETER_SUCCESS {
102            Ok(avm_outcome)
103        } else {
104            Err(ErrorAVMOutcome::new(ret_code, error_message, avm_outcome))
105        }
106    }
107}
108
109impl ErrorAVMOutcome {
110    fn new(error_code: i64, error_message: String, outcome: AVMOutcome) -> Self {
111        Self {
112            error_code,
113            error_message,
114            outcome,
115        }
116    }
117}