recv.rs 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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 darkfi::{
  19. event_graph::{self},
  20. net::transport::Dialer,
  21. util::path::expand_path,
  22. Error, Result,
  23. };
  24. use darkfi_serial::{
  25. async_trait, deserialize_async_partial, AsyncDecodable, AsyncEncodable, SerialDecodable,
  26. SerialEncodable,
  27. };
  28. use log::{error, info};
  29. use sled_overlay::sled;
  30. use smol::fs;
  31. use url::Url;
  32. use evgrd::{FetchEventsMessage, LocalEventGraph, VersionMessage, MSG_EVENT, MSG_FETCHEVENTS};
  33. #[derive(Clone, Debug, SerialEncodable, SerialDecodable)]
  34. pub struct Privmsg {
  35. pub channel: String,
  36. pub nick: String,
  37. pub msg: String,
  38. }
  39. async fn amain() -> Result<()> {
  40. info!("Instantiating event DAG");
  41. let ex = std::sync::Arc::new(smol::Executor::new());
  42. let datastore = expand_path("~/.local/share/darkfi/evgrd-test-client")?;
  43. fs::create_dir_all(&datastore).await?;
  44. let sled_db = sled::open(datastore)?;
  45. let evgr = LocalEventGraph::new(sled_db.clone(), "evgrd_testdag", 1, ex.clone()).await?;
  46. let endpoint = "tcp://127.0.0.1:5588";
  47. let endpoint = Url::parse(endpoint)?;
  48. let dialer = Dialer::new(endpoint, None).await?;
  49. let timeout = std::time::Duration::from_secs(60);
  50. println!("Connecting...");
  51. let mut stream = dialer.dial(Some(timeout)).await?;
  52. println!("Connected!");
  53. let version = VersionMessage::new();
  54. version.encode_async(&mut stream).await?;
  55. let server_version = VersionMessage::decode_async(&mut stream).await?;
  56. println!("Server version: {}", server_version.protocol_version);
  57. let unref_tips = evgr.unreferenced_tips.read().await.clone();
  58. let fetchevs = FetchEventsMessage::new(unref_tips);
  59. MSG_FETCHEVENTS.encode_async(&mut stream).await?;
  60. fetchevs.encode_async(&mut stream).await?;
  61. loop {
  62. let msg_type = u8::decode_async(&mut stream).await?;
  63. println!("Received: {msg_type:?}");
  64. if msg_type != MSG_EVENT {
  65. error!("Received invalid msg_type: {msg_type}");
  66. return Err(Error::MalformedPacket)
  67. }
  68. let ev = event_graph::Event::decode_async(&mut stream).await?;
  69. let genesis_timestamp = evgr.current_genesis.read().await.clone().timestamp;
  70. let ev_id = ev.id();
  71. if !evgr.dag.contains_key(ev_id.as_bytes()).unwrap() &&
  72. ev.validate(&evgr.dag, genesis_timestamp, evgr.days_rotation, None).await?
  73. {
  74. println!("got {ev:?}");
  75. evgr.dag_insert(&[ev.clone()]).await.unwrap();
  76. let privmsg: Privmsg = match deserialize_async_partial(ev.content()).await {
  77. Ok((v, _)) => v,
  78. Err(e) => {
  79. println!("Failed deserializing incoming Privmsg event: {}", e);
  80. continue
  81. }
  82. };
  83. println!("privmsg: {privmsg:?}");
  84. } else {
  85. println!("Event is invalid!")
  86. }
  87. }
  88. }
  89. fn main() {
  90. let _ = smol::block_on(amain());
  91. }