test.rs 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. async_daemonize, cli_desc,
  20. event_graph::{self, proto::ProtocolEventGraph, EventGraph, EventGraphPtr},
  21. net::{
  22. session::SESSION_DEFAULT,
  23. settings::SettingsOpt as NetSettingsOpt,
  24. transport::{Dialer, Listener, PtListener, PtStream},
  25. P2p, P2pPtr,
  26. },
  27. rpc::{
  28. jsonrpc::JsonSubscriber,
  29. server::{listen_and_serve, RequestHandler},
  30. },
  31. system::{sleep, StoppableTask, StoppableTaskPtr},
  32. util::path::{expand_path, get_config_path},
  33. Error, Result,
  34. };
  35. use darkfi_serial::{
  36. async_trait, deserialize_async, serialize_async, AsyncDecodable, AsyncEncodable, Encodable,
  37. SerialDecodable, SerialEncodable,
  38. };
  39. use log::{debug, error, info, warn};
  40. use url::Url;
  41. use evgrd::{FetchEventsMessage, LocalEventGraph, VersionMessage, MSG_EVENT, MSG_FETCHEVENTS};
  42. async fn amain() -> Result<()> {
  43. let evgr = LocalEventGraph::new();
  44. let endpoint = "tcp://127.0.0.1:5588";
  45. let endpoint = Url::parse(endpoint)?;
  46. let dialer = Dialer::new(endpoint, None).await?;
  47. let timeout = std::time::Duration::from_secs(60);
  48. info!("Connecting...");
  49. let mut stream = dialer.dial(Some(timeout)).await?;
  50. info!("Connected!");
  51. let version = VersionMessage::new();
  52. version.encode_async(&mut stream).await?;
  53. let server_version = VersionMessage::decode_async(&mut stream).await?;
  54. info!("Server version: {}", server_version.protocol_version);
  55. let fetchevs = FetchEventsMessage::new(evgr.unref_tips.clone());
  56. MSG_FETCHEVENTS.encode_async(&mut stream).await?;
  57. fetchevs.encode_async(&mut stream).await?;
  58. loop {
  59. let msg_type = u8::decode_async(&mut stream).await?;
  60. if msg_type != MSG_EVENT {
  61. error!("Received invalid msg_type: {msg_type}");
  62. return Err(Error::MalformedPacket)
  63. }
  64. let ev = event_graph::Event::decode_async(&mut stream).await?;
  65. }
  66. Ok(())
  67. }
  68. fn main() {
  69. smol::block_on(amain());
  70. }