util.rs 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2023 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 darkfi::Result;
  19. use crate::model::{Session, SlotInfo};
  20. pub fn make_node_id(node_name: &String) -> Result<String> {
  21. let mut id = hex::encode(node_name);
  22. id.insert_str(0, "NODE");
  23. Ok(id)
  24. }
  25. pub fn make_network_id(node_name: &String) -> Result<String> {
  26. let mut id = hex::encode(node_name);
  27. id.insert_str(0, "NETWORK");
  28. Ok(id)
  29. }
  30. pub fn make_null_id(node_name: &String) -> Result<String> {
  31. let mut id = hex::encode(node_name);
  32. id.insert_str(0, "NULL");
  33. Ok(id)
  34. }
  35. pub fn make_session_id(node_id: &str, session: &Session) -> Result<String> {
  36. let mut num = 0_u64;
  37. let session_chars = match session {
  38. Session::Inbound => vec!['i', 'n'],
  39. Session::Outbound => vec!['o', 'u', 't'],
  40. //Session::Manual => vec!['m', 'a', 'n'],
  41. Session::Offline => vec!['o', 'f', 'f'],
  42. Session::Null => vec!['n', 'u', 'l', 'l'],
  43. };
  44. for i in session_chars {
  45. num += i as u64
  46. }
  47. for i in node_id.chars() {
  48. num += i as u64
  49. }
  50. let mut id = hex::encode(num.to_ne_bytes());
  51. id.insert_str(0, "SESSION");
  52. Ok(id)
  53. }
  54. pub fn make_info_id(id: &u64) -> Result<String> {
  55. let mut id = hex::encode(id.to_ne_bytes());
  56. id.insert_str(0, "INFO");
  57. Ok(id)
  58. }
  59. pub fn make_empty_id(node_id: &str, session: &Session, count: u64) -> Result<String> {
  60. let count = count * 2;
  61. let mut num = 0_u64;
  62. let id = match session {
  63. Session::Inbound => {
  64. let session_chars = vec!['i', 'n'];
  65. for i in session_chars {
  66. num += i as u64
  67. }
  68. for i in node_id.chars() {
  69. num += i as u64
  70. }
  71. num += count;
  72. let mut id = hex::encode(num.to_ne_bytes());
  73. id.insert_str(0, "EMPTYIN");
  74. id
  75. }
  76. Session::Outbound => {
  77. let session_chars = vec!['o', 'u', 't'];
  78. for i in session_chars {
  79. num += i as u64
  80. }
  81. for i in node_id.chars() {
  82. num += i as u64
  83. }
  84. num += count;
  85. let mut id = hex::encode(num.to_ne_bytes());
  86. id.insert_str(0, "EMPTYOUT");
  87. id
  88. }
  89. //Session::Manual => {
  90. // let session_chars = vec!['m', 'a', 'n'];
  91. // for i in session_chars {
  92. // num += i as u64
  93. // }
  94. // for i in node_id.chars() {
  95. // num += i as u64
  96. // }
  97. // num += count;
  98. // let mut id = hex::encode(num.to_ne_bytes());
  99. // id.insert_str(0, "EMPTYMAN");
  100. // id
  101. //}
  102. Session::Offline => {
  103. let session_chars = vec!['o', 'f', 'f'];
  104. for i in session_chars {
  105. num += i as u64
  106. }
  107. for i in node_id.chars() {
  108. num += i as u64
  109. }
  110. num += count;
  111. let mut id = hex::encode(num.to_ne_bytes());
  112. id.insert_str(0, "EMPTYOFF");
  113. id
  114. }
  115. Session::Null => {
  116. let session_chars = vec!['n', 'u', 'l', 'l'];
  117. for i in session_chars {
  118. num += i as u64
  119. }
  120. for i in node_id.chars() {
  121. num += i as u64
  122. }
  123. num += count;
  124. let mut id = hex::encode(num.to_ne_bytes());
  125. id.insert_str(0, "NULL");
  126. id
  127. }
  128. };
  129. Ok(id)
  130. }
  131. // TODO: Rename to is empty slot.
  132. pub fn is_empty_session(connects: &[SlotInfo]) -> bool {
  133. return connects.iter().all(|conn| conn.is_empty)
  134. }