mod.rs 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  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 std::{collections::HashMap, sync::Arc};
  19. use darkfi::{
  20. net::{P2p, P2pPtr, Settings},
  21. rpc::jsonrpc::JsonSubscriber,
  22. system::ExecutorPtr,
  23. validator::ValidatorPtr,
  24. Result,
  25. };
  26. use log::info;
  27. // TODO: Protocal functions need to be protected so peers can't spam us.
  28. /// Block proposal broadcast protocol
  29. mod protocol_proposal;
  30. pub use protocol_proposal::{ProposalMessage, ProtocolProposalHandler, ProtocolProposalHandlerPtr};
  31. /// Validator blockchain sync protocol
  32. mod protocol_sync;
  33. pub use protocol_sync::{
  34. ForkSyncRequest, ForkSyncResponse, HeaderSyncRequest, HeaderSyncResponse, ProtocolSyncHandler,
  35. ProtocolSyncHandlerPtr, SyncRequest, SyncResponse, TipRequest, TipResponse, BATCH,
  36. };
  37. /// Transaction broadcast protocol
  38. mod protocol_tx;
  39. pub use protocol_tx::{ProtocolTxHandler, ProtocolTxHandlerPtr};
  40. /// Atomic pointer to the Darkfid P2P protocols handler.
  41. pub type DarkfidP2pHandlerPtr = Arc<DarkfidP2pHandler>;
  42. /// Darkfid P2P protocols handler.
  43. pub struct DarkfidP2pHandler {
  44. /// P2P network pointer
  45. pub p2p: P2pPtr,
  46. /// `ProtocolProposal` messages handler
  47. proposals: ProtocolProposalHandlerPtr,
  48. /// `ProtocolSync` messages handler
  49. sync: ProtocolSyncHandlerPtr,
  50. /// `ProtocolTx` messages handler
  51. txs: ProtocolTxHandlerPtr,
  52. }
  53. impl DarkfidP2pHandler {
  54. /// Initialize a Darkfid P2P protocols handler.
  55. ///
  56. /// A new P2P instance is generated using provided settings and all
  57. /// corresponding protocols are registered.
  58. pub async fn init(settings: &Settings, executor: &ExecutorPtr) -> Result<DarkfidP2pHandlerPtr> {
  59. info!(
  60. target: "darkfid::proto::mod::DarkfidP2pHandler::init",
  61. "Initializing a new Darkfid P2P handler..."
  62. );
  63. // Generate a new P2P instance
  64. let p2p = P2p::new(settings.clone(), executor.clone()).await?;
  65. // Generate a new `ProtocolProposal` messages handler
  66. let proposals = ProtocolProposalHandler::init(&p2p).await;
  67. // Generate a new `ProtocolSync` messages handler
  68. let sync = ProtocolSyncHandler::init(&p2p).await;
  69. // Generate a new `ProtocolTx` messages handler
  70. let txs = ProtocolTxHandler::init(&p2p).await;
  71. info!(
  72. target: "darkfid::proto::mod::DarkfidP2pHandler::init",
  73. "Darkfid P2P handler generated successfully!"
  74. );
  75. Ok(Arc::new(Self { p2p, proposals, sync, txs }))
  76. }
  77. /// Start the Darkfid P2P protocols handler for provided validator.
  78. pub async fn start(
  79. &self,
  80. executor: &ExecutorPtr,
  81. validator: &ValidatorPtr,
  82. subscribers: &HashMap<&'static str, JsonSubscriber>,
  83. ) -> Result<()> {
  84. info!(
  85. target: "darkfid::proto::mod::DarkfidP2pHandler::start",
  86. "Starting the Darkfid P2P handler..."
  87. );
  88. // Start the `ProtocolProposal` messages handler
  89. let subscriber = subscribers.get("proposals").unwrap().clone();
  90. self.proposals.start(executor, validator, &self.p2p, subscriber).await?;
  91. // Start the `ProtocolSync` messages handler
  92. self.sync.start(executor, validator).await?;
  93. // Start the `ProtocolTx` messages handler
  94. let subscriber = subscribers.get("txs").unwrap().clone();
  95. self.txs.start(executor, validator, subscriber).await?;
  96. // Start the P2P instance
  97. self.p2p.clone().start().await?;
  98. info!(
  99. target: "darkfid::proto::mod::DarkfidP2pHandler::start",
  100. "Darkfid P2P handler started successfully!"
  101. );
  102. Ok(())
  103. }
  104. /// Stop the Darkfid P2P protocols handler.
  105. pub async fn stop(&self) {
  106. info!(target: "darkfid::proto::mod::DarkfidP2pHandler::stop", "Terminating Darkfid P2P handler...");
  107. // Stop the P2P instance
  108. self.p2p.stop().await;
  109. // Start the `ProtocolTx` messages handler
  110. self.txs.stop().await;
  111. // Start the `ProtocolSync` messages handler
  112. self.sync.stop().await;
  113. // Start the `ProtocolProposal` messages handler
  114. self.proposals.stop().await;
  115. info!(target: "darkfid::proto::mod::DarkfidP2pHandler::stop", "Darkfid P2P handler terminated successfully!");
  116. }
  117. }