test.rs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2020-2024 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::{AsyncDecodable, AsyncEncodable};
  25. use log::{error, info};
  26. use sled_overlay::sled;
  27. use smol::fs;
  28. use url::Url;
  29. use evgrd::{FetchEventsMessage, LocalEventGraph, VersionMessage, MSG_EVENT, MSG_FETCHEVENTS};
  30. async fn amain() -> Result<()> {
  31. info!("Instantiating event DAG");
  32. let ex = std::sync::Arc::new(smol::Executor::new());
  33. let datastore = expand_path("~/.local/darkfi/evgrd")?;
  34. fs::create_dir_all(&datastore).await?;
  35. let sled_db = sled::open(datastore)?;
  36. let evgr = LocalEventGraph::new(sled_db.clone(), "evgrd_testdag", 1, ex.clone()).await?;
  37. let endpoint = "tcp://127.0.0.1:5588";
  38. let endpoint = Url::parse(endpoint)?;
  39. let dialer = Dialer::new(endpoint, None).await?;
  40. let timeout = std::time::Duration::from_secs(60);
  41. println!("Connecting...");
  42. let mut stream = dialer.dial(Some(timeout)).await?;
  43. println!("Connected!");
  44. let version = VersionMessage::new();
  45. version.encode_async(&mut stream).await?;
  46. let server_version = VersionMessage::decode_async(&mut stream).await?;
  47. println!("Server version: {}", server_version.protocol_version);
  48. let unref_tips = evgr.unreferenced_tips.read().await.clone();
  49. let fetchevs = FetchEventsMessage::new(unref_tips);
  50. MSG_FETCHEVENTS.encode_async(&mut stream).await?;
  51. fetchevs.encode_async(&mut stream).await?;
  52. loop {
  53. let msg_type = u8::decode_async(&mut stream).await?;
  54. println!("Received: {msg_type:?}");
  55. if msg_type != MSG_EVENT {
  56. error!("Received invalid msg_type: {msg_type}");
  57. return Err(Error::MalformedPacket)
  58. }
  59. let ev = event_graph::Event::decode_async(&mut stream).await?;
  60. let genesis_timestamp = evgr.current_genesis.read().await.clone().timestamp;
  61. let ev_id = ev.id();
  62. if !evgr.dag.contains_key(ev_id.as_bytes()).unwrap() &&
  63. ev.validate(&evgr.dag, genesis_timestamp, evgr.days_rotation, None).await?
  64. {
  65. println!("got {ev:?}");
  66. evgr.dag_insert(&[ev]).await.unwrap();
  67. } else {
  68. println!("Event is invalid!")
  69. }
  70. }
  71. }
  72. fn main() {
  73. let _ = smol::block_on(amain());
  74. }