marine_call_parameters/
lib.rs

1/*
2 * Copyright 2020 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
17#[cfg(all(feature = "marine-abi", target_arch = "wasm32"))]
18use marine_macro::marine;
19
20use serde::Serialize;
21use serde::Deserialize;
22
23/// Describes an origin that set corresponding value.
24#[cfg_attr(all(target_arch = "wasm32", feature = "marine-abi"), marine)]
25#[derive(Clone, Debug, Default, Eq, PartialEq, Hash, Serialize, Deserialize)]
26#[cfg_attr(
27    feature = "rkyv",
28    derive(::rkyv::Archive, ::rkyv::Serialize, ::rkyv::Deserialize)
29)]
30#[cfg_attr(feature = "rkyv", archive(check_bytes))]
31pub struct SecurityTetraplet {
32    /// Id of a peer where corresponding value was set.
33    pub peer_pk: String,
34
35    /// Id of a service that set corresponding value.
36    pub service_id: String,
37
38    /// Name of a function that returned corresponding value.
39    pub function_name: String,
40
41    /// Value was produced by applying this `lens` to the output from `call_service`.
42    pub lens: String,
43}
44
45impl SecurityTetraplet {
46    pub fn new(
47        peer_pk: impl Into<String>,
48        service_id: impl Into<String>,
49        function_name: impl Into<String>,
50        lens: impl Into<String>,
51    ) -> Self {
52        Self {
53            peer_pk: peer_pk.into(),
54            service_id: service_id.into(),
55            function_name: function_name.into(),
56            lens: lens.into(),
57        }
58    }
59
60    /// Create a tetraplet for string literals defined in the script
61    /// such as variable here `(call ("" "") "" ["variable_1"])`.
62    pub fn literal_tetraplet(init_peer_id: impl Into<String>) -> Self {
63        Self {
64            // these variables represent the initiator peer
65            peer_pk: init_peer_id.into(),
66            service_id: String::new(),
67            function_name: String::new(),
68            // lens can't be applied to the string literals
69            lens: String::new(),
70        }
71    }
72
73    pub fn add_lens(&mut self, lens: &str) {
74        self.lens.push_str(lens)
75    }
76}
77
78/// This struct contains parameters that would be accessible by Wasm modules.
79#[cfg_attr(all(target_arch = "wasm32", feature = "marine-abi"), marine)]
80#[derive(Clone, PartialEq, Default, Eq, Debug, Serialize, Deserialize)]
81#[cfg_attr(
82    feature = "rkyv",
83    derive(::rkyv::Archive, ::rkyv::Serialize, ::rkyv::Deserialize)
84)]
85#[cfg_attr(feature = "rkyv", archive(check_bytes))]
86pub struct CallParameters {
87    /// Parameters of the particle that caused this call.
88    pub particle: ParticleParameters,
89
90    /// Id of the current service.
91    pub service_id: String,
92
93    /// Id of the service creator.
94    pub service_creator_peer_id: String,
95
96    /// PeerId of the peer who hosts worker with this service.
97    pub host_id: String,
98
99    /// PeerId of the worker who hosts this service.
100    pub worker_id: String,
101
102    /// Security tetraplets which described origin of the arguments.
103    pub tetraplets: Vec<Vec<SecurityTetraplet>>,
104}
105
106#[cfg_attr(all(target_arch = "wasm32", feature = "marine-abi"), marine)]
107#[derive(Clone, PartialEq, Default, Eq, Debug, Serialize, Deserialize)]
108#[cfg_attr(
109    feature = "rkyv",
110    derive(::rkyv::Archive, ::rkyv::Serialize, ::rkyv::Deserialize)
111)]
112#[cfg_attr(feature = "rkyv", archive(check_bytes))]
113pub struct ParticleParameters {
114    /// Id of the particle which execution resulted a call this service.
115    pub id: String,
116
117    /// Peer id of the AIR script initiator.
118    pub init_peer_id: String,
119
120    /// Unix timestamp of the particle start time.
121    pub timestamp: u64,
122
123    /// Time to live for this particle in milliseconds.
124    pub ttl: u32,
125
126    /// AIR script in this particle.
127    pub script: String,
128
129    /// Signature made by particle initiator -- init_peer_id.
130    pub signature: Vec<u8>,
131
132    /// particle.signature signed by host_id -- used for FS access.
133    pub token: String,
134}
135
136use std::fmt;
137impl fmt::Display for SecurityTetraplet {
138    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
139        write!(
140            f,
141            "peer_pk: {}, service_id: {}, function_name: {}, lens: {}",
142            self.peer_pk, self.service_id, self.function_name, self.lens
143        )
144    }
145}
146
147/// This functions takes from host current call parameters.
148/// Beware that this implies import function call which takes some time.
149#[cfg(all(feature = "marine-abi", target_arch = "wasm32"))]
150pub fn get_call_parameters() -> CallParameters {
151    // it's safe until it is executed on standard Fluence node with appropriate import function
152    unsafe {
153        get_call_raw_parameters();
154        let raw_call_parameters = crate::internal::get_result_ptr();
155        CallParameters::__m_generated_deserialize(raw_call_parameters as _)
156    }
157}
158
159#[cfg(not(all(target_arch = "wasm32", feature = "marine-abi")))]
160pub fn get_call_parameters() -> CallParameters {
161    unimplemented!()
162}
163
164#[cfg(all(feature = "marine-abi", target_arch = "wasm32"))]
165#[link(wasm_import_module = "__marine_host_api_v3")]
166#[allow(improper_ctypes)]
167extern "C" {
168    // returns serialized current call parameters
169    #[link_name = "get_call_parameters"]
170    fn get_call_raw_parameters();
171}
172
173#[cfg(all(feature = "marine-abi", target_arch = "wasm32"))]
174#[allow(unused_extern_crates)]
175extern crate self as marine_rs_sdk;
176
177#[cfg(all(feature = "marine-abi", target_arch = "wasm32"))]
178#[allow(unused_imports)]
179mod internal {
180    pub(crate) use marine_rs_sdk_main::add_object_to_release;
181    pub(crate) use marine_rs_sdk_main::get_result_ptr;
182    pub(crate) use marine_rs_sdk_main::get_result_size;
183    pub(crate) use marine_rs_sdk_main::set_result_ptr;
184    pub(crate) use marine_rs_sdk_main::set_result_size;
185}