1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
//! This module provides the interfaces for external interaction with Subcoin network.

use crate::sync::PeerSync;
use crate::PeerId;
use bitcoin::{Transaction, Txid};
use sc_utils::mpsc::TracingUnboundedSender;
use serde::{Deserialize, Serialize};
use std::sync::atomic::AtomicBool;
use std::sync::Arc;
use tokio::sync::oneshot;

/// Represents the sync status of node.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum SyncStatus {
    /// The node is idle and not currently major syncing.
    Idle,
    /// The node is downloading blocks from peers.
    ///
    /// `target` specifies the block number the node aims to reach.
    /// `peers` is a list of peers from which the node is downloading.
    Downloading { target: u32, peers: Vec<PeerId> },
    /// The node is importing downloaded blocks into the local database.
    Importing { target: u32, peers: Vec<PeerId> },
}

/// Represents the status of network.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct NetworkStatus {
    /// The number of peers currently connected to the node.
    pub num_connected_peers: usize,
    /// The total number of bytes received from the network.
    pub total_bytes_inbound: u64,
    /// The total number of bytes sent to the network.
    pub total_bytes_outbound: u64,
    /// Current sync status of the node.
    pub sync_status: SyncStatus,
}

/// Represents the result of submitting a transaction to the network.
#[derive(Debug, Clone, serde::Serialize, serde::Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum SendTransactionResult {
    /// Transaction was submitted successfully.
    Success(Txid),
    /// An error occurred during the transaction submission.
    Failure(String),
}

/// An incoming transaction from RPC or network.
#[derive(Debug)]
pub(crate) struct IncomingTransaction {
    pub(crate) txid: Txid,
    pub(crate) transaction: Transaction,
}

/// Represents the different messages that can be sent to the network worker.
#[derive(Debug)]
pub(crate) enum NetworkWorkerMessage {
    /// Request the current network status.
    NetworkStatus(oneshot::Sender<NetworkStatus>),
    /// Request the sync peers.
    SyncPeers(oneshot::Sender<Vec<PeerSync>>),
    /// Request the number of inbound connected peers.
    InboundPeersCount(oneshot::Sender<usize>),
    /// Request a specific transaction by its Txid.
    GetTransaction((Txid, oneshot::Sender<Option<Transaction>>)),
    /// Submit a transaction to the transaction manager.
    SendTransaction((IncomingTransaction, oneshot::Sender<SendTransactionResult>)),
    /// Enable the block sync within the chain sync component.
    StartBlockSync,
}

/// A handle for interacting with the network worker.
///
/// This handle allows sending messages to the network worker and provides a simple
/// way to check if the node is performing a major synchronization.
#[derive(Debug, Clone)]
pub struct NetworkHandle {
    pub(crate) worker_msg_sender: TracingUnboundedSender<NetworkWorkerMessage>,
    // A simple flag to know whether the node is doing the major sync.
    pub(crate) is_major_syncing: Arc<AtomicBool>,
}

impl NetworkHandle {
    /// Provides high-level status information about network.
    ///
    /// Returns None if the `NetworkWorker` is no longer running.
    pub async fn status(&self) -> Option<NetworkStatus> {
        let (sender, receiver) = oneshot::channel();

        self.worker_msg_sender
            .unbounded_send(NetworkWorkerMessage::NetworkStatus(sender))
            .ok();

        receiver.await.ok()
    }

    /// Returns the currently syncing peers.
    pub async fn sync_peers(&self) -> Vec<PeerSync> {
        let (sender, receiver) = oneshot::channel();

        if self
            .worker_msg_sender
            .unbounded_send(NetworkWorkerMessage::SyncPeers(sender))
            .is_err()
        {
            return Vec::new();
        }

        receiver.await.unwrap_or_default()
    }

    /// Retrieves a transaction by its Txid.
    pub async fn get_transaction(&self, txid: Txid) -> Option<Transaction> {
        let (sender, receiver) = oneshot::channel();

        if self
            .worker_msg_sender
            .unbounded_send(NetworkWorkerMessage::GetTransaction((txid, sender)))
            .is_err()
        {
            return None;
        }

        receiver.await.ok().flatten()
    }

    /// Sends a transaction to the network.
    pub async fn send_transaction(&self, transaction: Transaction) -> SendTransactionResult {
        let (sender, receiver) = oneshot::channel();

        let txid = transaction.compute_txid();
        let incoming_transaction = IncomingTransaction { txid, transaction };

        if self
            .worker_msg_sender
            .unbounded_send(NetworkWorkerMessage::SendTransaction((
                incoming_transaction,
                sender,
            )))
            .is_err()
        {
            return SendTransactionResult::Failure(format!(
                "Failed to send transaction ({txid}) to worker"
            ));
        }

        receiver
            .await
            .unwrap_or(SendTransactionResult::Failure("Internal error".to_string()))
    }

    /// Starts the block sync in chain sync component.
    pub fn start_block_sync(&self) -> bool {
        self.worker_msg_sender
            .unbounded_send(NetworkWorkerMessage::StartBlockSync)
            .is_ok()
    }

    /// Returns a flag indicating whether the node is actively performing a major sync.
    pub fn is_major_syncing(&self) -> Arc<AtomicBool> {
        self.is_major_syncing.clone()
    }
}