darkfid/proto/
mod.rs

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
19use std::{collections::HashMap, sync::Arc};
20
21use darkfi::{
22    net::{P2p, P2pPtr, Settings},
23    rpc::jsonrpc::JsonSubscriber,
24    system::ExecutorPtr,
25    validator::ValidatorPtr,
26    Result,
27};
28use log::info;
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 validator.
94    pub async fn start(
95        &self,
96        executor: &ExecutorPtr,
97        validator: &ValidatorPtr,
98        subscribers: &HashMap<&'static str, JsonSubscriber>,
99    ) -> Result<()> {
100        info!(
101            target: "darkfid::proto::mod::DarkfidP2pHandler::start",
102            "Starting the Darkfid P2P handler..."
103        );
104
105        // Start the `ProtocolProposal` messages handler
106        let proposals_sub = subscribers.get("proposals").unwrap().clone();
107        let blocks_sub = subscribers.get("blocks").unwrap().clone();
108        self.proposals.start(executor, validator, &self.p2p, proposals_sub, blocks_sub).await?;
109
110        // Start the `ProtocolSync` messages handler
111        self.sync.start(executor, validator).await?;
112
113        // Start the `ProtocolTx` messages handler
114        let subscriber = subscribers.get("txs").unwrap().clone();
115        self.txs.start(executor, validator, subscriber).await?;
116
117        // Start the P2P instance
118        self.p2p.clone().start().await?;
119
120        info!(
121            target: "darkfid::proto::mod::DarkfidP2pHandler::start",
122            "Darkfid P2P handler started successfully!"
123        );
124
125        Ok(())
126    }
127
128    /// Stop the Darkfid P2P protocols handler.
129    pub async fn stop(&self) {
130        info!(target: "darkfid::proto::mod::DarkfidP2pHandler::stop", "Terminating Darkfid P2P handler...");
131
132        // Stop the P2P instance
133        self.p2p.stop().await;
134
135        // Start the `ProtocolTx` messages handler
136        self.txs.stop().await;
137
138        // Start the `ProtocolSync` messages handler
139        self.sync.stop().await;
140
141        // Start the `ProtocolProposal` messages handler
142        self.proposals.stop().await;
143
144        info!(target: "darkfid::proto::mod::DarkfidP2pHandler::stop", "Darkfid P2P handler terminated successfully!");
145    }
146}