mod.rs 5.1 KB

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