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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
use self::{
    client::GraphQlClient,
    data::{
        address, all_blocks, all_stake_pools, all_vote_plans, block, block_by_id,
        blocks_by_chain_length, epoch, last_block, settings, stake_pool, transaction_by_id,
        transaction_by_id_certificates, transactions_by_address, vote_plan_by_id, Address,
        AllBlocks, AllStakePools, AllVotePlans, Block, BlockById, BlocksByChainLength, Epoch,
        LastBlock, Settings, StakePool, TransactionById, TransactionByIdCertificates,
        TransactionsByAddress, VotePlanById,
    },
};
use crate::testing::configuration::get_explorer_app;
use graphql_client::{GraphQLQuery, *};
use jormungandr_lib::{crypto::hash::Hash, interfaces::BlockDate};
use std::{
    fs::File,
    process::{Command, Stdio},
    str::FromStr,
    time::Duration,
};
mod client;
pub mod configuration;
pub mod data;
pub mod verifiers;
mod wrappers;

use crate::jormungandr::explorer::configuration::ExplorerConfiguration;
use data::PoolId;
use jortestkit::{file, process::Wait};
use serde::Serialize;
use std::{
    path::{Path, PathBuf},
    process::Output,
};
use thiserror::Error;
pub use wrappers::LastBlockResponse;

#[derive(Error, Debug)]
pub enum ExplorerError {
    #[error("graph client error")]
    ClientError(#[from] client::GraphQlClientError),
    #[error("json serializiation error")]
    SerializationError(#[from] serde_json::Error),
    #[error("request error")]
    ReqwestError(#[from] reqwest::Error),
    #[error("cannot bootstrap explorer")]
    Bootstrap,
}

#[derive(Clone)]
pub struct Explorer {
    client: GraphQlClient,
    print_log: bool,
}

pub struct ExplorerProcess {
    handler: Option<std::process::Child>,
    configuration: ExplorerConfiguration,
    client: Explorer,
}

impl ExplorerProcess {
    pub fn new(configuration: ExplorerConfiguration) -> Result<Self, ExplorerError> {
        let path = get_explorer_app();

        let mut explorer_cmd = Command::new(path);
        explorer_cmd.args([
            "--node",
            configuration.node_address.as_ref(),
            "--binding-address",
            &format!(
                "{}:{}",
                &configuration.explorer_listen_address, &configuration.explorer_port
            ),
            "--log-output",
            "stdout",
        ]);

        if let Some(storage) = &configuration.storage_dir {
            explorer_cmd.arg("--storage-dir");
            explorer_cmd.arg(storage);
        }

        if configuration.params.address_bech32_prefix.is_some() {
            explorer_cmd.args([
                "--address-bech32-prefix",
                configuration.params.address_bech32_prefix.as_ref().unwrap(),
            ]);
        }

        if configuration.params.query_depth_limit.is_some() {
            explorer_cmd.args([
                "--query-depth-limit",
                &configuration.params.query_depth_limit.unwrap().to_string(),
            ]);
        }

        if configuration.params.query_complexity_limit.is_some() {
            explorer_cmd.args([
                "--query-complexity-limit",
                &configuration
                    .params
                    .query_complexity_limit
                    .unwrap()
                    .to_string(),
            ]);
        }

        println!("starting explorer: {:?}", explorer_cmd);

        let (stdout_cfg, stderr_cfg) = if let Some(logs_dir) = configuration.logs_dir.as_ref() {
            (
                Stdio::from(
                    File::create(logs_dir.join("explorer.log")).expect("explorer stdout file"),
                ),
                Stdio::from(
                    File::create(logs_dir.join("explorer.err")).expect("explorer stderr file"),
                ),
            )
        } else {
            (Stdio::piped(), Stdio::piped())
        };

        let handler = Some(
            explorer_cmd
                .stdout(stdout_cfg)
                .stderr(stderr_cfg)
                .spawn()
                .expect("failed to execute explorer process"),
        );

        let mut wait_bootstrap = Wait::new(Duration::from_secs(1), 10);
        while !wait_bootstrap.timeout_reached() {
            if reqwest::blocking::Client::new()
                .head(configuration.explorer_listen_http_address())
                .send()
                .is_ok()
            {
                break;
            };
            wait_bootstrap.advance();
        }

        if wait_bootstrap.timeout_reached() {
            Err(ExplorerError::Bootstrap)
        } else {
            Ok(ExplorerProcess {
                client: Explorer::new(format!(
                    "{}:{}",
                    configuration.explorer_listen_address, configuration.explorer_port
                )),
                handler,
                configuration,
            })
        }
    }

    /// get an explorer client configured to use this instance.
    ///
    /// take into account that while the Explorer client itself is Clone, if the ExplorerProcess
    /// gets dropped then the client will become useless.
    pub fn client(&self) -> &Explorer {
        &self.client
    }

    pub fn client_mut(&mut self) -> &mut Explorer {
        &mut self.client
    }

    pub fn is_up(&self) -> bool {
        reqwest::blocking::Client::new()
            .head(self.configuration.explorer_listen_http_address())
            .send()
            .is_ok()
    }

    pub fn wait_to_be_up(&self, seconds_wait: u64, attempts: u64) -> bool {
        let mut wait = Wait::new(Duration::from_secs(seconds_wait), attempts);
        while !wait.timeout_reached() {
            if self.is_up() {
                break;
            };
            wait.advance();
            println!("Waiting for explorer to be up");
        }

        if wait.timeout_reached() {
            println!("Timeout reached");
            false
        } else {
            println!("Explorer is up");
            true
        }
    }

    pub fn configuration(&self) -> &ExplorerConfiguration {
        &self.configuration
    }

    pub fn shutdown(mut self) -> Option<Output> {
        if let Some(mut handler) = self.handler.take() {
            let _ = handler.kill();
            Some(handler.wait_with_output().unwrap())
        } else {
            None
        }
    }
}

impl Drop for ExplorerProcess {
    fn drop(&mut self) {
        if let Some(mut handler) = self.handler.take() {
            let _ = handler.kill();
            handler.wait().unwrap();
        }
    }
}

impl Explorer {
    pub fn new(explorer_listen_address: String) -> Explorer {
        Explorer {
            client: GraphQlClient::new(explorer_listen_address),
            print_log: true,
        }
    }

    pub fn uri(&self) -> String {
        self.client.base_url()
    }

    pub fn disable_logs(&mut self) {
        self.print_log = false;
        self.client.disable_print();
    }

    pub fn enable_logs(&mut self) {
        self.print_log = true;
        self.client.enable_print();
    }

    pub fn print_request<T: Serialize>(&self, query: &QueryBody<T>) {
        if !self.print_log {
            return;
        }

        println!("running query: {:?}, against: {}", query.query, self.uri());
    }

    pub fn address<S: Into<String>>(
        &self,
        bech32_address: S,
    ) -> Result<Response<address::ResponseData>, ExplorerError> {
        let query = Address::build_query(address::Variables {
            bech32: bech32_address.into(),
        });
        self.print_request(&query);
        let response = self.client.run(query).map_err(ExplorerError::ClientError)?;
        let response_body: Response<address::ResponseData> = response.json()?;
        self.print_log(&response_body);
        Ok(response_body)
    }

    pub fn stake_pools(
        &self,
        limit: i64,
    ) -> Result<Response<all_stake_pools::ResponseData>, ExplorerError> {
        let query = AllStakePools::build_query(all_stake_pools::Variables { first: limit });
        self.print_request(&query);
        let response = self.client.run(query).map_err(ExplorerError::ClientError)?;
        let response_body = response.json()?;
        self.print_log(&response_body);
        Ok(response_body)
    }

    pub fn block(&self, hash: Hash) -> Result<Response<block::ResponseData>, ExplorerError> {
        let query = Block::build_query(block::Variables {
            id: hash.to_string(),
        });
        self.print_request(&query);
        let response = self.client.run(query).map_err(ExplorerError::ClientError)?;
        let response_body: Response<block::ResponseData> = response.json()?;
        self.print_log(&response_body);
        Ok(response_body)
    }

    pub fn block_by_id(
        &self,
        id: String,
    ) -> Result<Response<block_by_id::ResponseData>, ExplorerError> {
        let query = BlockById::build_query(block_by_id::Variables { id });
        self.print_request(&query);
        let response = self.client.run(query).map_err(ExplorerError::ClientError)?;
        let response_body: Response<block_by_id::ResponseData> = response.json()?;
        self.print_log(&response_body);
        Ok(response_body)
    }

    pub fn blocks(&self, limit: i64) -> Result<Response<all_blocks::ResponseData>, ExplorerError> {
        let query = AllBlocks::build_query(all_blocks::Variables { last: limit });
        self.print_request(&query);
        let response = self.client.run(query).map_err(ExplorerError::ClientError)?;
        let response_body = response.json()?;
        self.print_log(&response_body);
        Ok(response_body)
    }

    pub fn last_block(&self) -> Result<LastBlockResponse, ExplorerError> {
        let query = LastBlock::build_query(last_block::Variables);
        self.print_request(&query);
        let response = self.client.run(query).map_err(ExplorerError::ClientError)?;
        let response_body = response.json()?;
        self.print_log(&response_body);
        Ok(LastBlockResponse::new(response_body))
    }

    pub fn blocks_at_chain_length(
        &self,
        length: u32,
    ) -> Result<Response<blocks_by_chain_length::ResponseData>, ExplorerError> {
        let query = BlocksByChainLength::build_query(blocks_by_chain_length::Variables {
            length: length.to_string(),
        });
        self.print_request(&query);
        let response = self.client.run(query).map_err(ExplorerError::ClientError)?;
        let response_body = response.json()?;
        self.print_log(&response_body);
        Ok(response_body)
    }

    pub fn epoch(
        &self,
        epoch_number: u32,
        limit: i64,
    ) -> Result<Response<epoch::ResponseData>, ExplorerError> {
        let query = Epoch::build_query(epoch::Variables {
            id: epoch_number.to_string(),
            blocks_limit: limit,
        });
        self.print_request(&query);
        let response = self.client.run(query).map_err(ExplorerError::ClientError)?;
        let response_body = response.json()?;
        self.print_log(&response_body);
        Ok(response_body)
    }

    pub fn stake_pool(
        &self,
        id: PoolId,
        limit: i64,
    ) -> Result<Response<stake_pool::ResponseData>, ExplorerError> {
        let query = StakePool::build_query(stake_pool::Variables { id, first: limit });
        self.print_request(&query);
        let response = self.client.run(query).map_err(ExplorerError::ClientError)?;
        let response_body = response.json()?;
        self.print_log(&response_body);
        Ok(response_body)
    }

    pub fn settings(&self) -> Result<Response<settings::ResponseData>, ExplorerError> {
        let query = Settings::build_query(settings::Variables);
        self.print_request(&query);
        let response = self.client.run(query).map_err(ExplorerError::ClientError)?;
        let response_body = response.json()?;
        self.print_log(&response_body);
        Ok(response_body)
    }

    pub fn vote_plans(
        &self,
        limit: i64,
    ) -> Result<Response<all_vote_plans::ResponseData>, ExplorerError> {
        let query = AllVotePlans::build_query(all_vote_plans::Variables { first: limit });
        self.print_request(&query);
        let response = self.client.run(query).map_err(ExplorerError::ClientError)?;
        let response_body = response.json()?;
        self.print_log(&response_body);
        Ok(response_body)
    }

    pub fn vote_plan(
        &self,
        id: String,
    ) -> Result<Response<vote_plan_by_id::ResponseData>, ExplorerError> {
        let query = VotePlanById::build_query(vote_plan_by_id::Variables { id });
        self.print_request(&query);
        let response = self.client.run(query).map_err(ExplorerError::ClientError)?;
        let response_body: Response<vote_plan_by_id::ResponseData> = response.json()?;
        self.print_log(&response_body);
        Ok(response_body)
    }

    pub fn transaction(
        &self,
        hash: Hash,
    ) -> Result<Response<transaction_by_id::ResponseData>, ExplorerError> {
        let query = TransactionById::build_query(transaction_by_id::Variables {
            id: hash.to_string(),
        });
        self.print_request(&query);
        let response = self.client.run(query).map_err(ExplorerError::ClientError)?;
        let response_body: Response<transaction_by_id::ResponseData> = response.json()?;
        self.print_log(&response_body);
        Ok(response_body)
    }

    pub fn transaction_certificates(
        &self,
        hash: Hash,
    ) -> Result<Response<transaction_by_id_certificates::ResponseData>, ExplorerError> {
        let query =
            TransactionByIdCertificates::build_query(transaction_by_id_certificates::Variables {
                id: hash.to_string(),
            });
        self.print_request(&query);
        let response = self.client.run(query).map_err(ExplorerError::ClientError)?;
        let response_body: Response<transaction_by_id_certificates::ResponseData> =
            response.json()?;
        self.print_log(&response_body);
        Ok(response_body)
    }

    pub fn transactions_address<S: Into<String>>(
        &self,
        bech32_address: S,
    ) -> Result<Response<transactions_by_address::ResponseData>, ExplorerError> {
        let query = TransactionsByAddress::build_query(transactions_by_address::Variables {
            bech32: bech32_address.into(),
        });
        self.print_request(&query);
        let response = self.client.run(query).map_err(ExplorerError::ClientError)?;
        let response_body: Response<transactions_by_address::ResponseData> = response.json()?;
        self.print_log(&response_body);
        Ok(response_body)
    }

    pub fn current_time(&self) -> BlockDate {
        self.last_block().unwrap().block_date()
    }

    pub fn run<T: Serialize>(
        &self,
        query: QueryBody<T>,
    ) -> Result<reqwest::blocking::Response, ExplorerError> {
        self.print_request(&query);
        let response = self.client.run(query).map_err(ExplorerError::ClientError)?;
        self.print_log(&response);
        Ok(response)
    }

    fn print_log<T: std::fmt::Debug>(&self, response: &T) {
        if self.print_log {
            println!("Response: {:?}", &response);
        }
    }
}

#[allow(dead_code)]
pub fn compare_schema<P: AsRef<Path>>(actual_schema_path: P) {
    let expected_schema_path =
        PathBuf::from_str("./jormungandr-automation/resources/explorer/graphql/schema.graphql")
            .unwrap();

    if !file::have_the_same_content(actual_schema_path.as_ref(), &expected_schema_path).unwrap() {
        file::copy_file(actual_schema_path.as_ref(), &expected_schema_path, true).unwrap();
        println!("discrepancies detected, already replaced file with new content. Please commit to update schema");
    }
}