avm_interface/
call_request_parameters.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::JValue;
18use crate::CallSeDeErrors;
19
20use air_interpreter_interface::SerializedCallRequests;
21use polyplets::SecurityTetraplet;
22use serde::Deserialize;
23use serde::Serialize;
24
25use std::collections::HashMap;
26
27pub type CallRequests = HashMap<u32, CallRequestParams>;
28
29/// Contains arguments of a call instruction and all other necessary information
30/// required for calling a service.
31#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
32pub struct CallRequestParams {
33    /// Id of a service that should be called.
34    pub service_id: String,
35
36    /// Name of a function from service identified by service_id that should be called.
37    pub function_name: String,
38
39    /// Arguments that should be passed to the function.
40    pub arguments: Vec<JValue>,
41
42    /// Tetraplets that should be passed to the service.
43    pub tetraplets: Vec<Vec<SecurityTetraplet>>,
44}
45
46impl CallRequestParams {
47    pub fn new(
48        service_id: impl Into<String>,
49        function_name: impl Into<String>,
50        arguments: Vec<JValue>,
51        tetraplets: Vec<Vec<SecurityTetraplet>>,
52    ) -> Self {
53        Self {
54            service_id: service_id.into(),
55            function_name: function_name.into(),
56            arguments,
57            tetraplets,
58        }
59    }
60
61    pub(crate) fn from_raw(
62        call_params: air_interpreter_interface::CallRequestParams,
63    ) -> Result<Self, CallSeDeErrors> {
64        use air_interpreter_interface::CallArgumentsRepr;
65        use air_interpreter_interface::TetrapletsRepr;
66        use air_interpreter_sede::FromSerialized;
67
68        // TODO that's different JValue!
69        let arguments: Vec<JValue> = CallArgumentsRepr
70            .deserialize(&call_params.arguments)
71            .map_err(|de_error| CallSeDeErrors::CallParamsArgsDeFailed {
72                call_params: call_params.clone(),
73                de_error,
74            })?;
75
76        let tetraplets: Vec<Vec<SecurityTetraplet>> = TetrapletsRepr
77            .deserialize(&call_params.tetraplets)
78            .map_err(|de_error| CallSeDeErrors::CallParamsTetrapletsDeFailed {
79                call_params: call_params.clone(),
80                de_error,
81            })?;
82
83        let call_params = Self {
84            service_id: call_params.service_id,
85            function_name: call_params.function_name,
86            arguments,
87            tetraplets,
88        };
89
90        Ok(call_params)
91    }
92}
93
94pub(crate) fn from_raw_call_requests(
95    raw_call_params: SerializedCallRequests,
96) -> Result<CallRequests, CallSeDeErrors> {
97    use air_interpreter_interface::CallRequestsRepr;
98    use air_interpreter_sede::FromSerialized;
99
100    let call_requests: air_interpreter_interface::CallRequests =
101        match CallRequestsRepr.deserialize(&raw_call_params) {
102            Ok(requests) => requests,
103            Err(error) => {
104                return Err(CallSeDeErrors::CallRequestsDeError {
105                    raw_call_request: raw_call_params,
106                    error,
107                })
108                .map_err(Into::into)
109            }
110        };
111
112    call_requests
113        .into_iter()
114        .map(|(call_id, call_params)| -> Result<_, _> {
115            let call_params = CallRequestParams::from_raw(call_params)?;
116            Ok((call_id, call_params))
117        })
118        .collect::<Result<_, _>>()
119}