air_interpreter_interface/
run_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
17#[cfg(feature = "marine")]
18use fluence_it_types::ne_vec::NEVec;
19#[cfg(feature = "marine")]
20use fluence_it_types::IValue;
21#[cfg(feature = "marine")]
22use marine_rs_sdk::marine;
23use serde::Deserialize;
24use serde::Serialize;
25
26/// Parameters that a host side should pass to an interpreter and that necessary for execution.
27#[cfg_attr(feature = "marine", marine)]
28#[derive(Debug, Clone, Eq, PartialEq, Serialize, Deserialize)]
29pub struct RunParameters {
30    /// Peer id of a peer that start this particle.
31    pub init_peer_id: String,
32
33    /// Peer id of a current peer.
34    pub current_peer_id: String,
35
36    /// Unix timestamp from a particle in milliseconds.
37    /// It represents time when this particle was sent from the init peer id.
38    pub timestamp: u64,
39
40    /// TTL set by init peer id in milliseconds.
41    pub ttl: u32,
42
43    /// A key format.
44    ///
45    /// This value is the result of `fluence_keypair::KeyType::into`.
46    pub key_format: u8,
47
48    /// A secret key material.
49    ///
50    /// The value is the result `fluence_keypair::KeyPair::secret`, for compatibility
51    /// with JS client who can only serialize to secret key, not to keypair.
52    pub secret_key_bytes: Vec<u8>,
53
54    /// Unique particle ID.
55    pub particle_id: String,
56
57    /// The AIR script size limit.
58    pub air_size_limit: u64,
59
60    /// The particle data size limit.
61    pub particle_size_limit: u64,
62
63    /// This is the limit for the size of service call result.
64    pub call_result_size_limit: u64,
65
66    /// This knob controls hard RAM limits behavior for AVMRunner.
67    pub hard_limit_enabled: bool,
68}
69
70impl RunParameters {
71    #![allow(clippy::too_many_arguments)]
72    pub fn new(
73        init_peer_id: String,
74        current_peer_id: String,
75        timestamp: u64,
76        ttl: u32,
77        key_format: u8,
78        secret_key_bytes: Vec<u8>,
79        particle_id: String,
80        air_size_limit: u64,
81        particle_size_limit: u64,
82        call_result_size_limit: u64,
83        hard_limit_enabled: bool,
84    ) -> Self {
85        Self {
86            init_peer_id,
87            current_peer_id,
88            timestamp,
89            ttl,
90            key_format,
91            secret_key_bytes,
92            particle_id,
93            air_size_limit,
94            particle_size_limit,
95            call_result_size_limit,
96            hard_limit_enabled,
97        }
98    }
99
100    #[cfg(feature = "marine")]
101    pub fn into_ivalue(self) -> IValue {
102        let run_parameters = vec![
103            IValue::String(self.init_peer_id),
104            IValue::String(self.current_peer_id),
105            IValue::U64(self.timestamp),
106            IValue::U32(self.ttl),
107            IValue::U8(self.key_format),
108            IValue::ByteArray(self.secret_key_bytes),
109            IValue::String(self.particle_id),
110            IValue::U64(self.air_size_limit),
111            IValue::U64(self.particle_size_limit),
112            IValue::U64(self.call_result_size_limit),
113            IValue::Boolean(self.hard_limit_enabled),
114        ];
115        // unwrap is safe here because run_parameters is non-empty array
116        let run_parameters = NEVec::new(run_parameters).unwrap();
117        IValue::Record(run_parameters)
118    }
119}