p2p_method.rs 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2025 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. use async_trait::async_trait;
  19. use super::{
  20. jsonrpc::{ErrorCode, JsonError, JsonResponse, JsonResult},
  21. util::*,
  22. };
  23. use crate::net;
  24. #[async_trait]
  25. pub trait HandlerP2p: Sync + Send {
  26. async fn p2p_get_info(&self, id: u16, _params: JsonValue) -> JsonResult {
  27. let mut channels = Vec::new();
  28. for channel in self.p2p().hosts().channels() {
  29. let session = match channel.session_type_id() {
  30. net::session::SESSION_INBOUND => "inbound",
  31. net::session::SESSION_OUTBOUND => "outbound",
  32. net::session::SESSION_MANUAL => "manual",
  33. net::session::SESSION_REFINE => "refine",
  34. net::session::SESSION_SEED => "seed",
  35. net::session::SESSION_DIRECT => "direct",
  36. _ => panic!("invalid result from channel.session_type_id()"),
  37. };
  38. // For transport mixed connections send the mixed url to aid in debugging
  39. channels.push(json_map([
  40. ("url", JsonStr(channel.display_address().to_string())),
  41. ("session", json_str(session)),
  42. ("id", JsonNum(channel.info.id.into())),
  43. ]));
  44. }
  45. let mut slots = Vec::new();
  46. for channel_id in self.p2p().session_outbound().slot_info().await {
  47. slots.push(JsonNum(channel_id.into()));
  48. }
  49. let result =
  50. json_map([("channels", JsonArray(channels)), ("outbound_slots", JsonArray(slots))]);
  51. JsonResponse::new(result, id).into()
  52. }
  53. // RPCAPI:
  54. // Set the number of outbound connections for the P2P stack.
  55. // Takes a positive integer representing the desired number of outbound connection slots.
  56. // Returns `true` on success. If the number is greater than current, new slots are added.
  57. // If the number is less than current, slots are removed (prioritizing empty slots).
  58. //
  59. // --> {"jsonrpc": "2.0", "method": "p2p.set_outbound_connections", "params": [5], "id": 42}
  60. // <-- {"jsonrpc": "2.0", "result": true, "id": 42}
  61. async fn p2p_set_outbound_connections(&self, id: u16, params: JsonValue) -> JsonResult {
  62. let Some(params) = params.get::<Vec<JsonValue>>() else {
  63. return JsonError::new(ErrorCode::InvalidParams, None, id).into()
  64. };
  65. if params.len() != 1 || !params[0].is_number() {
  66. return JsonError::new(ErrorCode::InvalidParams, None, id).into()
  67. }
  68. let n_f64 = params[0].get::<f64>().unwrap();
  69. let n = *n_f64 as u32;
  70. if *n_f64 != n as f64 || n == 0 {
  71. return JsonError::new(ErrorCode::InvalidParams, None, id).into()
  72. }
  73. if let Err(e) = self.p2p().session_outbound().set_outbound_connections(n as usize).await {
  74. return JsonError::new(ErrorCode::InternalError, Some(e.to_string()), id).into()
  75. }
  76. JsonResponse::new(JsonValue::Boolean(true), id).into()
  77. }
  78. fn p2p(&self) -> net::P2pPtr;
  79. }