mod.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 rand::{distributions::Alphanumeric, thread_rng, Rng};
  19. pub mod events_queue;
  20. pub mod model;
  21. pub mod protocol_event;
  22. pub mod view;
  23. pub trait EventMsg {
  24. fn new() -> Self;
  25. }
  26. pub fn gen_id(len: usize) -> String {
  27. thread_rng().sample_iter(&Alphanumeric).take(len).map(char::from).collect()
  28. }
  29. #[cfg(test)]
  30. mod tests {
  31. use super::{
  32. events_queue::EventsQueue,
  33. model::{Event, EventId, Model},
  34. protocol_event::{Inv, InvItem, Seen, SeenPtr},
  35. view::View,
  36. EventMsg,
  37. };
  38. use crate::util::time::Timestamp;
  39. use darkfi_serial::{SerialDecodable, SerialEncodable};
  40. #[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
  41. struct TestEvent {
  42. pub nick: String,
  43. pub msg: String,
  44. }
  45. impl EventMsg for TestEvent {
  46. fn new() -> Self {
  47. Self { nick: "groot".to_string(), msg: "I am groot!!".to_string() }
  48. }
  49. }
  50. #[async_std::test]
  51. async fn event_graph_integration() {
  52. // Base structures
  53. let events_queue = EventsQueue::<TestEvent>::new();
  54. let mut model = Model::new(events_queue.clone());
  55. let view = View::new(events_queue);
  56. // Buffers
  57. let seen_event: SeenPtr<EventId> = Seen::new();
  58. let seen_inv: SeenPtr<EventId> = Seen::new();
  59. let seen_ids = Seen::new();
  60. // Keeps track of the events we received, but haven't read yet
  61. let mut unread_msgs = vec![];
  62. let test_event0 =
  63. TestEvent { nick: "brawndo".to_string(), msg: "Electrolytes".to_string() };
  64. let test_event1 =
  65. TestEvent { nick: "camacho".to_string(), msg: "Shieeeeeeeet".to_string() };
  66. // We create an event and broadcast it
  67. let head_hash = model.get_head_hash();
  68. let event0 = Event {
  69. previous_event_hash: head_hash,
  70. action: test_event0,
  71. timestamp: Timestamp::current_time(),
  72. };
  73. // Simulate receiving the event
  74. assert!(seen_ids.push(&event0.hash()).await);
  75. // Simulate receiving the event again
  76. assert!(!seen_ids.push(&event0.hash()).await);
  77. // Add the event into the model
  78. model.add(event0.clone()).await;
  79. // Send inventory
  80. let inv0 = Inv { invs: vec![InvItem { hash: event0.hash() }] };
  81. // Simulate recieving the inventory
  82. assert!(seen_inv.push(&inv0.invs[0].hash).await);
  83. // Simulate recieving the inventory again
  84. assert!(!seen_inv.push(&inv0.invs[0].hash).await);
  85. // TODO: getdata (self.send_getdata(vec![inv_item.hash]).await?)
  86. // Add the event to the unread msgs vec
  87. unread_msgs.push(event0);
  88. // TODO: Simulate network behaviour, etc.
  89. }
  90. }