messages.rs 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  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 std::collections::{HashMap, HashSet};
  19. use darkfi_serial::{serialize, SerialDecodable, SerialEncodable};
  20. use rand::Rng;
  21. use crate::net;
  22. /// This struct represents a DHT key request
  23. #[derive(Debug, Clone, SerialDecodable, SerialEncodable)]
  24. pub struct KeyRequest {
  25. /// Request id
  26. pub id: blake3::Hash,
  27. /// Daemon id requesting the key
  28. pub from: blake3::Hash,
  29. /// Daemon id holding the key
  30. pub to: blake3::Hash,
  31. /// Key entry
  32. pub key: blake3::Hash,
  33. }
  34. impl KeyRequest {
  35. pub fn new(from: blake3::Hash, to: blake3::Hash, key: blake3::Hash) -> Self {
  36. // Generate a random id
  37. let mut rng = rand::thread_rng();
  38. let n: u16 = rng.gen();
  39. let id = blake3::hash(&serialize(&n));
  40. Self { id, from, to, key }
  41. }
  42. }
  43. impl net::Message for KeyRequest {
  44. fn name() -> &'static str {
  45. "keyrequest"
  46. }
  47. }
  48. /// This struct represents a DHT key request response
  49. #[derive(Debug, Clone, SerialDecodable, SerialEncodable)]
  50. pub struct KeyResponse {
  51. /// Response id
  52. pub id: blake3::Hash,
  53. /// Daemon id holding the key
  54. pub from: blake3::Hash,
  55. /// Daemon id holding the key
  56. pub to: blake3::Hash,
  57. /// Key entry
  58. pub key: blake3::Hash,
  59. /// Key value
  60. pub value: Vec<u8>,
  61. }
  62. impl KeyResponse {
  63. pub fn new(from: blake3::Hash, to: blake3::Hash, key: blake3::Hash, value: Vec<u8>) -> Self {
  64. // Generate a random id
  65. let mut rng = rand::thread_rng();
  66. let n: u16 = rng.gen();
  67. let id = blake3::hash(&serialize(&n));
  68. Self { id, from, to, key, value }
  69. }
  70. }
  71. impl net::Message for KeyResponse {
  72. fn name() -> &'static str {
  73. "keyresponse"
  74. }
  75. }
  76. /// This struct represents a lookup map request
  77. #[derive(Debug, Clone, SerialDecodable, SerialEncodable)]
  78. pub struct LookupRequest {
  79. /// Request id
  80. pub id: blake3::Hash,
  81. /// Daemon id executing the request
  82. pub daemon: blake3::Hash,
  83. /// Key entry
  84. pub key: blake3::Hash,
  85. /// Request type
  86. pub req_type: u8, // 0 for insert, 1 for remove
  87. }
  88. impl LookupRequest {
  89. pub fn new(daemon: blake3::Hash, key: blake3::Hash, req_type: u8) -> Self {
  90. // Generate a random id
  91. let mut rng = rand::thread_rng();
  92. let n: u16 = rng.gen();
  93. let id = blake3::hash(&serialize(&n));
  94. Self { id, daemon, key, req_type }
  95. }
  96. }
  97. impl net::Message for LookupRequest {
  98. fn name() -> &'static str {
  99. "lookuprequest"
  100. }
  101. }
  102. /// Auxiliary structure used for lookup map syncing.
  103. #[derive(Debug, SerialEncodable, SerialDecodable)]
  104. pub struct LookupMapRequest {
  105. /// Request id
  106. pub id: blake3::Hash,
  107. /// Daemon id executing the request
  108. pub daemon: blake3::Hash,
  109. }
  110. impl LookupMapRequest {
  111. pub fn new(daemon: blake3::Hash) -> Self {
  112. // Generate a random id
  113. let mut rng = rand::thread_rng();
  114. let n: u16 = rng.gen();
  115. let id = blake3::hash(&serialize(&n));
  116. Self { id, daemon }
  117. }
  118. }
  119. impl net::Message for LookupMapRequest {
  120. fn name() -> &'static str {
  121. "lookupmaprequest"
  122. }
  123. }
  124. /// Auxiliary structure used for consensus syncing.
  125. #[derive(Debug, Clone, SerialEncodable, SerialDecodable)]
  126. pub struct LookupMapResponse {
  127. /// Request id
  128. pub id: blake3::Hash,
  129. /// Daemon lookup map, containing nodes that holds each key
  130. pub lookup: HashMap<blake3::Hash, HashSet<blake3::Hash>>,
  131. }
  132. impl LookupMapResponse {
  133. pub fn new(lookup: HashMap<blake3::Hash, HashSet<blake3::Hash>>) -> Self {
  134. // Generate a random id
  135. let mut rng = rand::thread_rng();
  136. let n: u16 = rng.gen();
  137. let id = blake3::hash(&serialize(&n));
  138. Self { id, lookup }
  139. }
  140. }
  141. impl net::Message for LookupMapResponse {
  142. fn name() -> &'static str {
  143. "lookupmapresponse"
  144. }
  145. }