darkfid/proto/
mod.rs

1/* This file is part of DarkFi (https://dark.fi)
2 *
3 * Copyright (C) 2020-2026 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
19use std::sync::Arc;
20
21use darkfi::{
22    net::{P2p, P2pPtr, Settings},
23    system::ExecutorPtr,
24    Result,
25};
26use tracing::info;
27
28use crate::DarkfiNodePtr;
29
30/// Block proposal broadcast protocol
31mod protocol_proposal;
32pub use protocol_proposal::{ProposalMessage, ProtocolProposalHandler, ProtocolProposalHandlerPtr};
33
34/// Validator blockchain sync protocol
35mod protocol_sync;
36pub use protocol_sync::{
37    ForkHeaderHashRequest, ForkHeaderHashResponse, ForkHeadersRequest, ForkHeadersResponse,
38    ForkProposalsRequest, ForkProposalsResponse, ForkSyncRequest, ForkSyncResponse,
39    HeaderSyncRequest, HeaderSyncResponse, ProtocolSyncHandler, ProtocolSyncHandlerPtr,
40    SyncRequest, SyncResponse, TipRequest, TipResponse, BATCH,
41};
42
43/// Transaction broadcast protocol
44mod protocol_tx;
45pub use protocol_tx::{ProtocolTxHandler, ProtocolTxHandlerPtr};
46
47/// Atomic pointer to the Darkfid P2P protocols handler.
48pub type DarkfidP2pHandlerPtr = Arc<DarkfidP2pHandler>;
49
50/// Darkfid P2P protocols handler.
51pub struct DarkfidP2pHandler {
52    /// P2P network pointer
53    pub p2p: P2pPtr,
54    /// `ProtocolProposal` messages handler
55    proposals: ProtocolProposalHandlerPtr,
56    /// `ProtocolSync` messages handler
57    sync: ProtocolSyncHandlerPtr,
58    /// `ProtocolTx` messages handler
59    txs: ProtocolTxHandlerPtr,
60}
61
62impl DarkfidP2pHandler {
63    /// Initialize a Darkfid P2P protocols handler.
64    ///
65    /// A new P2P instance is generated using provided settings and all
66    /// corresponding protocols are registered.
67    pub async fn init(settings: &Settings, executor: &ExecutorPtr) -> Result<DarkfidP2pHandlerPtr> {
68        info!(
69            target: "darkfid::proto::mod::DarkfidP2pHandler::init",
70            "Initializing a new Darkfid P2P handler..."
71        );
72
73        // Generate a new P2P instance
74        let p2p = P2p::new(settings.clone(), executor.clone()).await?;
75
76        // Generate a new `ProtocolProposal` messages handler
77        let proposals = ProtocolProposalHandler::init(&p2p).await;
78
79        // Generate a new `ProtocolSync` messages handler
80        let sync = ProtocolSyncHandler::init(&p2p).await;
81
82        // Generate a new `ProtocolTx` messages handler
83        let txs = ProtocolTxHandler::init(&p2p).await;
84
85        info!(
86            target: "darkfid::proto::mod::DarkfidP2pHandler::init",
87            "Darkfid P2P handler generated successfully!"
88        );
89
90        Ok(Arc::new(Self { p2p, proposals, sync, txs }))
91    }
92
93    /// Start the Darkfid P2P protocols handler for provided node.
94    pub async fn start(&self, executor: &ExecutorPtr, node: &DarkfiNodePtr) -> Result<()> {
95        info!(
96            target: "darkfid::proto::mod::DarkfidP2pHandler::start",
97            "Starting the Darkfid P2P handler..."
98        );
99
100        // Start the `ProtocolProposal` messages handler
101        self.proposals.start(executor, node).await?;
102
103        // Start the `ProtocolSync` messages handler
104        self.sync.start(executor, &node.validator).await?;
105
106        // Start the `ProtocolTx` messages handler
107        let subscriber = node.subscribers.get("txs").unwrap().clone();
108        self.txs.start(executor, &node.validator, subscriber).await?;
109
110        // Start the P2P instance
111        self.p2p.clone().start().await?;
112
113        info!(
114            target: "darkfid::proto::mod::DarkfidP2pHandler::start",
115            "Darkfid P2P handler started successfully!"
116        );
117
118        Ok(())
119    }
120
121    /// Stop the Darkfid P2P protocols handler.
122    pub async fn stop(&self) {
123        info!(target: "darkfid::proto::mod::DarkfidP2pHandler::stop", "Terminating Darkfid P2P handler...");
124
125        // Stop the P2P instance
126        self.p2p.stop().await;
127
128        // Start the `ProtocolTx` messages handler
129        self.txs.stop().await;
130
131        // Start the `ProtocolSync` messages handler
132        self.sync.stop().await;
133
134        // Start the `ProtocolProposal` messages handler
135        self.proposals.stop().await;
136
137        info!(target: "darkfid::proto::mod::DarkfidP2pHandler::stop", "Darkfid P2P handler terminated successfully!");
138    }
139}