utils.rs 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  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, sync::Arc};
  19. use log::info;
  20. use smol::Executor;
  21. use darkfi::{
  22. net::{P2p, P2pPtr, Settings, SESSION_ALL},
  23. rpc::jsonrpc::JsonSubscriber,
  24. validator::ValidatorPtr,
  25. };
  26. use crate::proto::{ProtocolBlock, ProtocolProposal, ProtocolSync, ProtocolTx};
  27. /// Auxiliary function to generate the sync P2P network and register all its protocols.
  28. pub async fn spawn_sync_p2p(
  29. settings: &Settings,
  30. validator: &ValidatorPtr,
  31. subscribers: &HashMap<&'static str, JsonSubscriber>,
  32. executor: Arc<Executor<'static>>,
  33. ) -> P2pPtr {
  34. info!(target: "darkfid", "Registering sync network P2P protocols...");
  35. let p2p = P2p::new(settings.clone(), executor.clone()).await;
  36. let registry = p2p.protocol_registry();
  37. let _validator = validator.clone();
  38. let _subscriber = subscribers.get("blocks").unwrap().clone();
  39. registry
  40. .register(SESSION_ALL, move |channel, p2p| {
  41. let validator = _validator.clone();
  42. let subscriber = _subscriber.clone();
  43. async move { ProtocolBlock::init(channel, validator, p2p, subscriber).await.unwrap() }
  44. })
  45. .await;
  46. let _validator = validator.clone();
  47. registry
  48. .register(SESSION_ALL, move |channel, _p2p| {
  49. let validator = _validator.clone();
  50. async move { ProtocolSync::init(channel, validator).await.unwrap() }
  51. })
  52. .await;
  53. let _validator = validator.clone();
  54. let _subscriber = subscribers.get("txs").unwrap().clone();
  55. registry
  56. .register(SESSION_ALL, move |channel, p2p| {
  57. let validator = _validator.clone();
  58. let subscriber = _subscriber.clone();
  59. async move { ProtocolTx::init(channel, validator, p2p, subscriber).await.unwrap() }
  60. })
  61. .await;
  62. p2p
  63. }
  64. /// Auxiliary function to generate the consensus P2P network and register all its protocols.
  65. pub async fn spawn_consensus_p2p(
  66. settings: &Settings,
  67. validator: &ValidatorPtr,
  68. subscribers: &HashMap<&'static str, JsonSubscriber>,
  69. executor: Arc<Executor<'static>>,
  70. ) -> P2pPtr {
  71. info!(target: "darkfid", "Registering consensus network P2P protocols...");
  72. let p2p = P2p::new(settings.clone(), executor.clone()).await;
  73. let registry = p2p.protocol_registry();
  74. let _validator = validator.clone();
  75. let _subscriber = subscribers.get("proposals").unwrap().clone();
  76. registry
  77. .register(SESSION_ALL, move |channel, p2p| {
  78. let validator = _validator.clone();
  79. let subscriber = _subscriber.clone();
  80. async move { ProtocolProposal::init(channel, validator, p2p, subscriber).await.unwrap() }
  81. })
  82. .await;
  83. p2p
  84. }