cuprated/blockchain/
manager.rs

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
use std::{collections::HashMap, sync::Arc};

use futures::StreamExt;
use monero_serai::block::Block;
use tokio::sync::{mpsc, oneshot, Notify};
use tower::{Service, ServiceExt};
use tracing::error;

use cuprate_blockchain::service::{BlockchainReadHandle, BlockchainWriteHandle};
use cuprate_consensus::{
    BlockChainContextRequest, BlockChainContextResponse, BlockChainContextService,
    BlockVerifierService, ExtendedConsensusError, TxVerifierService, VerifyBlockRequest,
    VerifyBlockResponse, VerifyTxRequest, VerifyTxResponse,
};
use cuprate_consensus_context::RawBlockChainContext;
use cuprate_p2p::{
    block_downloader::{BlockBatch, BlockDownloaderConfig},
    BroadcastSvc, NetworkInterface,
};
use cuprate_p2p_core::ClearNet;
use cuprate_txpool::service::TxpoolWriteHandle;
use cuprate_types::{
    blockchain::{BlockchainReadRequest, BlockchainResponse},
    Chain, TransactionVerificationData,
};

use crate::{
    blockchain::{
        chain_service::ChainService,
        interface::COMMAND_TX,
        syncer,
        types::{ConcreteBlockVerifierService, ConsensusBlockchainReadHandle},
    },
    constants::PANIC_CRITICAL_SERVICE_ERROR,
};

mod commands;
mod handler;

pub use commands::{BlockchainManagerCommand, IncomingBlockOk};

/// Initialize the blockchain manager.
///
/// This function sets up the [`BlockchainManager`] and the [`syncer`] so that the functions in [`interface`](super::interface)
/// can be called.
pub async fn init_blockchain_manager(
    clearnet_interface: NetworkInterface<ClearNet>,
    blockchain_write_handle: BlockchainWriteHandle,
    blockchain_read_handle: BlockchainReadHandle,
    txpool_write_handle: TxpoolWriteHandle,
    mut blockchain_context_service: BlockChainContextService,
    block_verifier_service: ConcreteBlockVerifierService,
    block_downloader_config: BlockDownloaderConfig,
) {
    // TODO: find good values for these size limits
    let (batch_tx, batch_rx) = mpsc::channel(1);
    let stop_current_block_downloader = Arc::new(Notify::new());
    let (command_tx, command_rx) = mpsc::channel(3);

    COMMAND_TX.set(command_tx).unwrap();

    tokio::spawn(syncer::syncer(
        blockchain_context_service.clone(),
        ChainService(blockchain_read_handle.clone()),
        clearnet_interface.clone(),
        batch_tx,
        Arc::clone(&stop_current_block_downloader),
        block_downloader_config,
    ));

    let BlockChainContextResponse::Context(blockchain_context) = blockchain_context_service
        .ready()
        .await
        .expect(PANIC_CRITICAL_SERVICE_ERROR)
        .call(BlockChainContextRequest::Context)
        .await
        .expect(PANIC_CRITICAL_SERVICE_ERROR)
    else {
        unreachable!()
    };

    let manager = BlockchainManager {
        blockchain_write_handle,
        blockchain_read_handle,
        txpool_write_handle,
        blockchain_context_service,
        cached_blockchain_context: blockchain_context.unchecked_blockchain_context().clone(),
        block_verifier_service,
        stop_current_block_downloader,
        broadcast_svc: clearnet_interface.broadcast_svc(),
    };

    tokio::spawn(manager.run(batch_rx, command_rx));
}

/// The blockchain manager.
///
/// This handles all mutation of the blockchain, anything that changes the state of the blockchain must
/// go through this.
///
/// Other parts of Cuprate can interface with this by using the functions in [`interface`](super::interface).
pub struct BlockchainManager {
    /// The [`BlockchainWriteHandle`], this is the _only_ part of Cuprate where a [`BlockchainWriteHandle`]
    /// is held.
    blockchain_write_handle: BlockchainWriteHandle,
    /// A [`BlockchainReadHandle`].
    blockchain_read_handle: BlockchainReadHandle,
    /// A [`TxpoolWriteHandle`].
    txpool_write_handle: TxpoolWriteHandle,
    // TODO: Improve the API of the cache service.
    // TODO: rename the cache service -> `BlockchainContextService`.
    /// The blockchain context cache, this caches the current state of the blockchain to quickly calculate/retrieve
    /// values without needing to go to a [`BlockchainReadHandle`].
    blockchain_context_service: BlockChainContextService,
    /// A cached context representing the current state.
    cached_blockchain_context: RawBlockChainContext,
    /// The block verifier service, to verify incoming blocks.
    block_verifier_service: ConcreteBlockVerifierService,
    /// A [`Notify`] to tell the [syncer](syncer::syncer) that we want to cancel this current download
    /// attempt.
    stop_current_block_downloader: Arc<Notify>,
    /// The broadcast service, to broadcast new blocks.
    broadcast_svc: BroadcastSvc<ClearNet>,
}

impl BlockchainManager {
    /// The [`BlockchainManager`] task.
    pub async fn run(
        mut self,
        mut block_batch_rx: mpsc::Receiver<BlockBatch>,
        mut command_rx: mpsc::Receiver<BlockchainManagerCommand>,
    ) {
        loop {
            tokio::select! {
                Some(batch) = block_batch_rx.recv() => {
                    self.handle_incoming_block_batch(
                        batch,
                    ).await;
                }
                Some(incoming_command) = command_rx.recv() => {
                    self.handle_command(incoming_command).await;
                }
                else => {
                    todo!("TODO: exit the BC manager")
                }
            }
        }
    }
}