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
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
// Copyright 2022 IOTA Stiftung
// SPDX-License-Identifier: Apache-2.0

//! Node core API routes.

use std::str::FromStr;

use bee_api_types::{
    dtos::{PeerDto, ReceiptDto},
    responses::{
        BlockMetadataResponse, BlockResponse, InfoResponse, MilestoneResponse, OutputMetadataResponse, OutputResponse,
        PeersResponse, ReceiptsResponse, RoutesResponse, SubmitBlockResponse, TipsResponse, TreasuryResponse,
        UtxoChangesResponse,
    },
};
use bee_block::{
    output::OutputId,
    payload::{
        milestone::{MilestoneId, MilestonePayload},
        transaction::TransactionId,
    },
    Block, BlockDto, BlockId,
};
use packable::PackableExt;
use url::Url;

use crate::{
    constants::DEFAULT_API_TIMEOUT,
    node_manager::node::{Node, NodeAuth},
    Client, Error, Result,
};

/// NodeInfo wrapper which contains the node info and the url from the node (useful when multiple nodes are used)
#[derive(Debug, Serialize, Deserialize)]
pub struct NodeInfoWrapper {
    /// The returned node info
    #[serde(rename = "nodeInfo")]
    pub node_info: InfoResponse,
    /// The url from the node which returned the node info
    pub url: String,
}

impl Client {
    // Node routes.

    /// Returns the health of the node.
    /// GET /health
    pub async fn get_health(&self, url: &str) -> Result<bool> {
        let path = "health";

        let mut url = Url::parse(url)?;
        url.set_path(path);
        let status = crate::node_manager::http_client::HttpClient::new()
            .get(
                Node {
                    url,
                    auth: None,
                    disabled: false,
                },
                DEFAULT_API_TIMEOUT,
            )
            .await?
            .status();

        match status {
            200 => Ok(true),
            _ => Ok(false),
        }
    }

    /// Returns the available API route groups of the node.
    /// GET /api/routes
    pub async fn get_routes(&self) -> Result<RoutesResponse> {
        let path = "api/routes";

        self.node_manager
            .get_request(path, None, self.get_timeout(), false, false)
            .await
    }

    /// Returns general information about the node.
    /// GET /api/core/v2/info
    pub async fn get_info(&self) -> Result<NodeInfoWrapper> {
        let path = "api/core/v2/info";

        self.node_manager
            .get_request(path, None, self.get_timeout(), false, false)
            .await
    }

    /// GET /api/core/v2/info endpoint
    pub async fn get_node_info(url: &str, auth: Option<NodeAuth>) -> Result<InfoResponse> {
        let mut url = crate::node_manager::builder::validate_url(Url::parse(url)?)?;
        if let Some(auth) = &auth {
            if let Some((name, password)) = &auth.basic_auth_name_pwd {
                url.set_username(name)
                    .map_err(|_| crate::Error::UrlAuthError("username"))?;
                url.set_password(Some(password))
                    .map_err(|_| crate::Error::UrlAuthError("password"))?;
            }
        }
        let path = "api/core/v2/info";
        url.set_path(path);

        let resp: InfoResponse = crate::node_manager::http_client::HttpClient::new()
            .get(
                Node {
                    url,
                    auth,
                    disabled: false,
                },
                DEFAULT_API_TIMEOUT,
            )
            .await?
            .into_json()
            .await?;

        Ok(resp)
    }

    // Tangle routes.

    /// Returns tips that are ideal for attaching a block.
    /// GET /api/core/v2/tips
    pub async fn get_tips(&self) -> Result<Vec<BlockId>> {
        let path = "api/core/v2/tips";

        let resp = self
            .node_manager
            .get_request::<TipsResponse>(path, None, self.get_timeout(), false, false)
            .await?;

        resp.tips
            .iter()
            .map(|tip| BlockId::from_str(tip).map_err(Error::BlockError))
            .collect::<Result<Vec<_>>>()
    }

    // Blocks routes.

    /// Returns the BlockId of the submitted block.
    /// POST JSON to /api/core/v2/blocks
    pub async fn post_block(&self, block: &Block) -> Result<BlockId> {
        let path = "api/core/v2/blocks";
        let local_pow = self.get_local_pow();
        let timeout = if local_pow {
            self.get_timeout()
        } else {
            self.get_remote_pow_timeout()
        };
        let block_dto = BlockDto::from(block);

        // fallback to local PoW if remote PoW fails
        let resp = match self
            .node_manager
            .post_request_json::<SubmitBlockResponse>(path, timeout, serde_json::to_value(block_dto)?, local_pow)
            .await
        {
            Ok(res) => res,
            Err(e) => {
                if let Error::NodeError(e) = e {
                    let fallback_to_local_pow = self.get_fallback_to_local_pow();
                    // hornet and bee return different error blocks
                    if (e == *"No available nodes with remote Pow"
                        || e.contains("proof of work is not enabled")
                        || e.contains("`Pow` not enabled"))
                        && fallback_to_local_pow
                    {
                        // Without this we get:within `impl Future<Output = [async output]>`, the trait `Send` is not
                        // implemented for `std::sync::RwLockWriteGuard<'_, NetworkInfo>`
                        {
                            let mut client_network_info =
                                self.network_info.write().map_err(|_| crate::Error::PoisonError)?;
                            // switch to local PoW
                            client_network_info.local_pow = true;
                        }
                        let block_res = crate::api::finish_pow(self, block.payload().cloned()).await;
                        let block_with_local_pow = match block_res {
                            Ok(block) => {
                                // reset local PoW state
                                let mut client_network_info =
                                    self.network_info.write().map_err(|_| crate::Error::PoisonError)?;
                                client_network_info.local_pow = false;
                                block
                            }
                            Err(e) => {
                                // reset local PoW state
                                let mut client_network_info =
                                    self.network_info.write().map_err(|_| crate::Error::PoisonError)?;
                                client_network_info.local_pow = false;
                                return Err(e);
                            }
                        };
                        let block_dto = BlockDto::from(&block_with_local_pow);

                        self.node_manager
                            .post_request_json(path, timeout, serde_json::to_value(block_dto)?, true)
                            .await?
                    } else {
                        return Err(Error::NodeError(e));
                    }
                } else {
                    return Err(e);
                }
            }
        };

        Ok(BlockId::from_str(&resp.block_id)?)
    }

    /// Returns the BlockId of the submitted block.
    /// POST /api/core/v2/blocks
    pub async fn post_block_raw(&self, block: &Block) -> Result<BlockId> {
        let path = "api/core/v2/blocks";
        let local_pow = self.get_local_pow();
        let timeout = if local_pow {
            self.get_timeout()
        } else {
            self.get_remote_pow_timeout()
        };

        // fallback to local Pow if remote Pow fails
        let resp = match self
            .node_manager
            .post_request_bytes::<SubmitBlockResponse>(path, timeout, &block.pack_to_vec(), local_pow)
            .await
        {
            Ok(res) => res,
            Err(e) => {
                if let Error::NodeError(e) = e {
                    let fallback_to_local_pow = self.get_fallback_to_local_pow();
                    // hornet and bee return different error blocks
                    if (e == *"No available nodes with remote Pow"
                        || e.contains("proof of work is not enabled")
                        || e.contains("`Pow` not enabled"))
                        && fallback_to_local_pow
                    {
                        // Without this we get:within `impl Future<Output = [async output]>`, the trait `Send` is not
                        // implemented for `std::sync::RwLockWriteGuard<'_, NetworkInfo>`
                        {
                            let mut client_network_info =
                                self.network_info.write().map_err(|_| crate::Error::PoisonError)?;
                            // switch to local PoW
                            client_network_info.local_pow = true;
                        }
                        let block_res = crate::api::finish_pow(self, block.payload().cloned()).await;
                        let block_with_local_pow = match block_res {
                            Ok(block) => {
                                // reset local PoW state
                                let mut client_network_info =
                                    self.network_info.write().map_err(|_| crate::Error::PoisonError)?;
                                client_network_info.local_pow = false;
                                block
                            }
                            Err(e) => {
                                // reset local PoW state
                                let mut client_network_info =
                                    self.network_info.write().map_err(|_| crate::Error::PoisonError)?;
                                client_network_info.local_pow = false;
                                return Err(e);
                            }
                        };
                        self.node_manager
                            .post_request_bytes(path, timeout, &block_with_local_pow.pack_to_vec(), true)
                            .await?
                    } else {
                        return Err(Error::NodeError(e));
                    }
                } else {
                    return Err(e);
                }
            }
        };

        Ok(BlockId::from_str(&resp.block_id)?)
    }

    /// Finds a block by its BlockId. This method returns the given block object.
    /// GET /api/core/v2/blocks/{BlockId}
    pub async fn get_block(&self, block_id: &BlockId) -> Result<Block> {
        let path = &format!("api/core/v2/blocks/{}", block_id);

        let resp = self
            .node_manager
            .get_request::<BlockResponse>(path, None, self.get_timeout(), false, true)
            .await?;

        match resp {
            BlockResponse::Json(dto) => Ok(Block::try_from_dto(&dto, &self.get_protocol_parameters()?)?),
            BlockResponse::Raw(_) => Err(crate::Error::UnexpectedApiResponse),
        }
    }

    /// Finds a block by its BlockId. This method returns the given block raw data.
    /// GET /api/core/v2/blocks/{BlockId}
    pub async fn get_block_raw(&self, block_id: &BlockId) -> Result<Vec<u8>> {
        let path = &format!("api/core/v2/blocks/{}", block_id);

        self.node_manager
            .get_request_bytes(path, None, self.get_timeout())
            .await
    }

    /// Returns the metadata of a block.
    /// GET /api/core/v2/blocks/{BlockId}/metadata
    pub async fn get_block_metadata(&self, block_id: &BlockId) -> Result<BlockMetadataResponse> {
        let path = &format!("api/core/v2/blocks/{}/metadata", block_id);

        self.node_manager
            .get_request(path, None, self.get_timeout(), true, true)
            .await
    }

    // UTXO routes.

    /// Finds an output, as JSON, by its OutputId (TransactionId + output_index).
    /// GET /api/core/v2/outputs/{outputId}
    pub async fn get_output(&self, output_id: &OutputId) -> Result<OutputResponse> {
        let path = &format!("api/core/v2/outputs/{}", output_id);

        self.node_manager
            .get_request(path, None, self.get_timeout(), false, true)
            .await
    }

    /// Finds an output, as raw bytes, by its OutputId (TransactionId + output_index).
    /// GET /api/core/v2/outputs/{outputId}
    pub async fn get_output_raw(&self, output_id: &OutputId) -> Result<Vec<u8>> {
        let path = &format!("api/core/v2/outputs/{}", output_id);

        self.node_manager
            .get_request_bytes(path, None, self.get_timeout())
            .await
    }

    /// Get the metadata for a given `OutputId` (TransactionId + output_index).
    /// GET /api/core/v2/outputs/{outputId}/metadata
    pub async fn get_output_metadata(&self, output_id: &OutputId) -> Result<OutputMetadataResponse> {
        let path = &format!("api/core/v2/outputs/{}/metadata", output_id);

        self.node_manager
            .get_request::<OutputMetadataResponse>(path, None, self.get_timeout(), false, true)
            .await
    }

    /// Gets all stored receipts.
    /// GET /api/core/v2/receipts
    pub async fn get_receipts(&self) -> Result<Vec<ReceiptDto>> {
        let path = &"api/core/v2/receipts";

        let resp = self
            .node_manager
            .get_request::<ReceiptsResponse>(path, None, DEFAULT_API_TIMEOUT, false, false)
            .await?;

        Ok(resp.receipts)
    }

    /// Gets the receipts by the given milestone index.
    /// GET /api/core/v2/receipts/{migratedAt}
    pub async fn get_receipts_migrated_at(&self, milestone_index: u32) -> Result<Vec<ReceiptDto>> {
        let path = &format!("api/core/v2/receipts/{}", milestone_index);

        let resp = self
            .node_manager
            .get_request::<ReceiptsResponse>(path, None, DEFAULT_API_TIMEOUT, false, false)
            .await?;

        Ok(resp.receipts)
    }

    /// Gets the current treasury output.
    /// The treasury output contains all tokens from the legacy network that have not yet been migrated.
    /// GET /api/core/v2/treasury
    pub async fn get_treasury(&self) -> Result<TreasuryResponse> {
        let path = "api/core/v2/treasury";

        self.node_manager
            .get_request(path, None, DEFAULT_API_TIMEOUT, false, false)
            .await
    }

    /// Returns the block, as object, that was included in the ledger for a given TransactionId.
    /// GET /api/core/v2/transactions/{transactionId}/included-block
    pub async fn get_included_block(&self, transaction_id: &TransactionId) -> Result<Block> {
        let path = &format!("api/core/v2/transactions/{}/included-block", transaction_id);

        let resp = self
            .node_manager
            .get_request::<BlockResponse>(path, None, self.get_timeout(), true, true)
            .await?;

        match resp {
            BlockResponse::Json(dto) => Ok(Block::try_from_dto(&dto, &self.get_protocol_parameters()?)?),
            BlockResponse::Raw(_) => Err(crate::Error::UnexpectedApiResponse),
        }
    }

    /// Returns the block, as raw bytes, that was included in the ledger for a given TransactionId.
    /// GET /api/core/v2/transactions/{transactionId}/included-block
    pub async fn get_included_block_raw(&self, transaction_id: &TransactionId) -> Result<Vec<u8>> {
        let path = &format!("api/core/v2/transactions/{}/included-block", transaction_id);

        self.node_manager
            .get_request_bytes(path, None, self.get_timeout())
            .await
    }

    // Milestones routes.

    /// Gets the milestone by the given milestone id.
    /// GET /api/core/v2/milestones/{milestoneId}
    pub async fn get_milestone_by_id(&self, milestone_id: &MilestoneId) -> Result<MilestonePayload> {
        let path = &format!("api/core/v2/milestones/{}", milestone_id);

        let resp = self
            .node_manager
            .get_request::<MilestoneResponse>(path, None, self.get_timeout(), false, true)
            .await?;

        match resp {
            MilestoneResponse::Json(dto) => Ok(MilestonePayload::try_from_dto(&dto, &self.get_protocol_parameters()?)?),
            MilestoneResponse::Raw(_) => Err(crate::Error::UnexpectedApiResponse),
        }
    }

    /// Gets the milestone by the given milestone id.
    /// GET /api/core/v2/milestones/{milestoneId}
    pub async fn get_milestone_by_id_raw(&self, milestone_id: &MilestoneId) -> Result<Vec<u8>> {
        let path = &format!("api/core/v2/milestones/{}", milestone_id);

        self.node_manager
            .get_request_bytes(path, None, self.get_timeout())
            .await
    }

    /// Gets all UTXO changes of a milestone by its milestone id.
    /// GET /api/core/v2/milestones/{milestoneId}/utxo-changes
    pub async fn get_utxo_changes_by_id(&self, milestone_id: &MilestoneId) -> Result<UtxoChangesResponse> {
        let path = &format!("api/core/v2/milestones/{}/utxo-changes", milestone_id);

        self.node_manager
            .get_request(path, None, self.get_timeout(), false, false)
            .await
    }

    /// Gets the milestone by the given milestone index.
    /// GET /api/core/v2/milestones/{index}
    pub async fn get_milestone_by_index(&self, index: u32) -> Result<MilestonePayload> {
        let path = &format!("api/core/v2/milestones/by-index/{}", index);

        let resp = self
            .node_manager
            .get_request::<MilestoneResponse>(path, None, self.get_timeout(), false, true)
            .await?;

        match resp {
            MilestoneResponse::Json(dto) => Ok(MilestonePayload::try_from_dto(&dto, &self.get_protocol_parameters()?)?),
            MilestoneResponse::Raw(_) => Err(crate::Error::UnexpectedApiResponse),
        }
    }

    /// Gets the milestone by the given milestone index.
    /// GET /api/core/v2/milestones/{index}
    pub async fn get_milestone_by_index_raw(&self, index: u32) -> Result<Vec<u8>> {
        let path = &format!("api/core/v2/milestones/by-index/{}", index);

        self.node_manager
            .get_request_bytes(path, None, self.get_timeout())
            .await
    }

    /// Gets all UTXO changes of a milestone by its milestone index.
    /// GET /api/core/v2/milestones/by-index/{index}/utxo-changes
    pub async fn get_utxo_changes_by_index(&self, index: u32) -> Result<UtxoChangesResponse> {
        let path = &format!("api/core/v2/milestones/by-index/{}/utxo-changes", index);

        self.node_manager
            .get_request(path, None, self.get_timeout(), false, false)
            .await
    }

    // Peers routes.

    /// GET /api/core/v2/peers
    pub async fn get_peers(&self) -> Result<Vec<PeerDto>> {
        let path = "api/core/v2/peers";

        let resp = self
            .node_manager
            .get_request::<PeersResponse>(path, None, self.get_timeout(), false, false)
            .await?;

        Ok(resp.0)
    }

    // // RoutePeer is the route for getting peers by their peerID.
    // // GET returns the peer
    // // DELETE deletes the peer.
    // RoutePeer = "/peers/:" + restapipkg.ParameterPeerID

    // // RoutePeers is the route for getting all peers of the node.
    // // GET returns a list of all peers.
    // // POST adds a new peer.
    // RoutePeers = "/peers"

    // Control routes.

    // // RouteControlDatabasePrune is the control route to manually prune the database.
    // // POST prunes the database.
    // RouteControlDatabasePrune = "/control/database/prune"

    // // RouteControlSnapshotsCreate is the control route to manually create a snapshot files.
    // // POST creates a snapshot (full, delta or both).
    // RouteControlSnapshotsCreate = "/control/snapshots/create"
}