1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
/*
 * AquaVM Workflow Engine
 *
 * Copyright (C) 2024 Fluence DAO
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

use crate::key_utils::derive_dummy_keypair;
#[cfg(feature = "test_with_native_code")]
pub use crate::native_test_runner::NativeAirRunner as DefaultAirRunner;
#[cfg(not(feature = "test_with_native_code"))]
pub use crate::wasm_test_runner::WasmAirRunner as DefaultAirRunner;

pub use crate::native_test_runner::NativeAirRunner;
pub use crate::wasm_test_runner::ReleaseWasmAirRunner;
pub use crate::wasm_test_runner::WasmAirRunner;

use super::CallServiceClosure;

use avm_server::avm_runner::*;
use avm_server::AVMRuntimeLimits;
use avm_server::AquaVMRuntimeLimits;
use fluence_keypair::KeyPair;
use futures::future::LocalBoxFuture;
use futures::StreamExt;

use std::collections::HashMap;
use std::collections::HashSet;

pub trait AirRunner {
    fn new(
        current_call_id: impl Into<String>,
        test_init_parameters: TestInitParameters,
    ) -> LocalBoxFuture<'static, Self>;

    #[allow(clippy::too_many_arguments)]
    fn call<'this>(
        &'this mut self,
        air: impl Into<String>,
        prev_data: impl Into<Vec<u8>>,
        data: impl Into<Vec<u8>>,
        init_peer_id: impl Into<String>,
        timestamp: u64,
        ttl: u32,
        override_current_peer_id: Option<String>,
        call_results: avm_server::CallResults,
        key_pair: &KeyPair,
        particle_id: String,
    ) -> LocalBoxFuture<'this, Result<RawAVMOutcome, Box<dyn std::error::Error + 'this>>>;

    fn get_current_peer_id(&self) -> &str;
}

pub struct TestRunner<R = DefaultAirRunner> {
    pub runner: R,
    call_service: CallServiceClosure<'static>,
    pub keypair: KeyPair,
}

#[derive(Debug, Default, Clone)]
pub struct TestRunParameters {
    pub init_peer_id: String,
    pub timestamp: u64,
    pub ttl: u32,
    pub override_current_peer_id: Option<String>,
    pub particle_id: String,
}

/// This struct is used to set limits for the test runner creating AVMRunner.
#[derive(Debug, Default, Clone, Copy)]
pub struct TestInitParameters {
    pub air_size_limit: Option<u64>,
    pub particle_size_limit: Option<u64>,
    pub call_result_size_limit: Option<u64>,
    pub hard_limit_enabled: bool,
}

impl<R: AirRunner> TestRunner<R> {
    pub async fn call(
        &mut self,
        air: impl Into<String>,
        prev_data: impl Into<Vec<u8>>,
        data: impl Into<Vec<u8>>,
        test_run_params: TestRunParameters,
    ) -> Result<RawAVMOutcome, String> {
        let air = air.into();
        let mut prev_data = prev_data.into();
        let mut data = data.into();

        let TestRunParameters {
            init_peer_id,
            timestamp,
            ttl,
            override_current_peer_id,
            particle_id,
        } = test_run_params;

        let mut call_results = HashMap::new();
        let mut next_peer_pks = HashSet::new();

        loop {
            let mut outcome: RawAVMOutcome = self
                .runner
                .call(
                    air.clone(),
                    prev_data,
                    data,
                    init_peer_id.clone(),
                    timestamp,
                    ttl,
                    override_current_peer_id.clone(),
                    call_results,
                    &self.keypair,
                    particle_id.clone(),
                )
                .await
                .map_err(|e| e.to_string())?;

            next_peer_pks.extend(outcome.next_peer_pks);

            if outcome.call_requests.is_empty() {
                outcome.next_peer_pks = next_peer_pks.into_iter().collect::<Vec<_>>();
                return Ok(outcome);
            }

            call_results = futures::stream::iter(outcome.call_requests.into_iter())
                .then(|(id, call_parameters)| {
                    let service_result = (self.call_service)(call_parameters);
                    async move { (id, service_result.await) }
                })
                .collect::<HashMap<_, _>>()
                .await;

            prev_data = outcome.data;
            data = vec![];
        }
    }

    #[allow(clippy::too_many_arguments)]
    pub async fn call_single<'this>(
        &'this mut self,
        air: impl Into<String>,
        prev_data: impl Into<Vec<u8>>,
        data: impl Into<Vec<u8>>,
        init_peer_id: impl Into<String>,
        timestamp: u64,
        ttl: u32,
        override_current_peer_id: Option<String>,
        call_results: avm_server::CallResults,
        particle_id: impl Into<String>,
    ) -> Result<RawAVMOutcome, Box<dyn std::error::Error + 'this>> {
        self.runner
            .call(
                air,
                prev_data,
                data,
                init_peer_id,
                timestamp,
                ttl,
                override_current_peer_id,
                call_results,
                &self.keypair,
                particle_id.into(),
            )
            .await
    }
}

pub async fn create_avm(
    call_service: CallServiceClosure<'static>,
    current_peer_id: impl Into<String>,
) -> TestRunner {
    create_custom_avm(call_service, current_peer_id).await
}

pub async fn create_custom_avm<R: AirRunner>(
    call_service: CallServiceClosure<'static>,
    current_peer_id: impl Into<String>,
) -> TestRunner<R> {
    let current_peer_id = current_peer_id.into();
    let (keypair, _) = derive_dummy_keypair(&current_peer_id);
    let runner = R::new(current_peer_id, <_>::default()).await;

    TestRunner {
        runner,
        call_service,
        keypair: keypair.into_inner(),
    }
}

pub async fn create_avm_with_key<R: AirRunner>(
    keypair: impl Into<KeyPair>,
    call_service: CallServiceClosure<'static>,
    test_init_parameters: TestInitParameters,
) -> TestRunner<R> {
    let keypair = keypair.into();
    let current_peer_id = keypair.public().to_peer_id().to_string();
    let runner = R::new(current_peer_id, test_init_parameters).await;

    TestRunner {
        runner,
        call_service,
        keypair,
    }
}

impl TestRunParameters {
    pub fn new(
        init_peer_id: impl Into<String>,
        timestamp: u64,
        ttl: u32,
        particle_id: impl Into<String>,
    ) -> Self {
        Self {
            init_peer_id: init_peer_id.into(),
            timestamp,
            ttl,
            override_current_peer_id: None,
            particle_id: particle_id.into(),
        }
    }

    pub fn from_init_peer_id(init_peer_id: impl Into<String>) -> Self {
        Self {
            init_peer_id: init_peer_id.into(),
            ..<_>::default()
        }
    }

    pub fn from_timestamp(timestamp: u64) -> Self {
        Self {
            timestamp,
            ..<_>::default()
        }
    }

    pub fn from_ttl(ttl: u32) -> Self {
        Self {
            ttl,
            ..<_>::default()
        }
    }

    pub fn with_particle_id(mut self, particle_id: impl Into<String>) -> Self {
        self.particle_id = particle_id.into();
        self
    }
}

impl TestInitParameters {
    pub fn new(
        air_size_limit: u64,
        particle_size_limit: u64,
        call_result_size_limit: u64,
        hard_limit_enabled: bool,
    ) -> Self {
        Self {
            air_size_limit: Some(air_size_limit),
            particle_size_limit: Some(particle_size_limit),
            call_result_size_limit: Some(call_result_size_limit),
            hard_limit_enabled,
        }
    }

    pub fn no_limits() -> Self {
        Self {
            air_size_limit: Some(u64::MAX),
            particle_size_limit: Some(u64::MAX),
            call_result_size_limit: Some(u64::MAX),
            hard_limit_enabled: false,
        }
    }
}

impl From<TestInitParameters> for AVMRuntimeLimits {
    fn from(value: TestInitParameters) -> Self {
        AVMRuntimeLimits::new(
            value.air_size_limit,
            value.particle_size_limit,
            value.call_result_size_limit,
            value.hard_limit_enabled,
        )
    }
}

impl From<TestInitParameters> for AquaVMRuntimeLimits {
    fn from(value: TestInitParameters) -> Self {
        use air_interpreter_interface::MAX_AIR_SIZE;
        use air_interpreter_interface::MAX_CALL_RESULT_SIZE;
        use air_interpreter_interface::MAX_PARTICLE_SIZE;
        let air_size_limit = value.air_size_limit.unwrap_or(MAX_AIR_SIZE);
        let particle_size_limit: u64 = value.particle_size_limit.unwrap_or(MAX_PARTICLE_SIZE);
        let call_result_size_limit = value.call_result_size_limit.unwrap_or(MAX_CALL_RESULT_SIZE);

        AquaVMRuntimeLimits::new(
            air_size_limit,
            particle_size_limit,
            call_result_size_limit,
            value.hard_limit_enabled,
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::call_services::{set_variables_call_service, VariableOptionSource};

    use avm_interface::CallRequestParams;
    use serde_json::json;

    #[tokio::test]
    async fn test_override_current_peer_id() {
        let spell_id = "spell_id";
        let host_peer_id = "host_peer_id";
        let script = format!(r#"(call "{spell_id}" ("service" "func") [])"#);

        let variables = maplit::hashmap! {
            "func".to_owned() => json!("success"),
        };

        let key_format = fluence_keypair::KeyFormat::Ed25519;
        let keypair = KeyPair::generate(key_format);
        let keypair2 = KeyPair::generate(key_format);

        let mut client = create_custom_avm::<NativeAirRunner>(
            set_variables_call_service(variables, VariableOptionSource::FunctionName),
            host_peer_id,
        )
        .await;

        let current_result_1 = client
            .runner
            .call(
                &script,
                "",
                "",
                spell_id,
                0,
                0,
                None,
                HashMap::new(),
                &keypair,
                "".to_owned(),
            )
            .await
            .expect("call should be success");

        assert_eq!(
            current_result_1.ret_code, 0,
            "{:?}",
            current_result_1.error_message
        );

        let expected_current_call_requests = HashMap::new();
        let expected_current_next_peers_pks = vec![spell_id.to_owned()];

        assert_eq!(
            current_result_1.call_requests,
            expected_current_call_requests
        );
        assert_eq!(
            current_result_1.next_peer_pks,
            expected_current_next_peers_pks
        );

        let spell_result_1 = client
            .runner
            .call(
                script,
                "",
                "",
                spell_id,
                0,
                0,
                Some(spell_id.to_owned()),
                HashMap::new(),
                &keypair2,
                "".to_owned(),
            )
            .await
            .expect("call should be success");

        let expected_spell_call_requests = maplit::hashmap! {
            1 => CallRequestParams::new("service", "func", vec![], vec![]),
        };
        let expected_spell_next_peers_pks = Vec::<String>::new();

        assert_eq!(spell_result_1.call_requests, expected_spell_call_requests);
        assert_eq!(spell_result_1.next_peer_pks, expected_spell_next_peers_pks);
    }
}