cuprated/txpool/
incoming_tx.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
use std::{
    collections::HashSet,
    sync::Arc,
    task::{Context, Poll},
};

use bytes::Bytes;
use futures::{future::BoxFuture, FutureExt};
use monero_serai::transaction::Transaction;
use tower::{Service, ServiceExt};

use cuprate_consensus::{
    transactions::new_tx_verification_data, BlockChainContextRequest, BlockChainContextResponse,
    BlockChainContextService, ExtendedConsensusError, VerifyTxRequest,
};
use cuprate_dandelion_tower::{
    pool::{DandelionPoolService, IncomingTxBuilder},
    State, TxState,
};
use cuprate_helper::asynch::rayon_spawn_async;
use cuprate_p2p::NetworkInterface;
use cuprate_p2p_core::ClearNet;
use cuprate_txpool::{
    service::{
        interface::{
            TxpoolReadRequest, TxpoolReadResponse, TxpoolWriteRequest, TxpoolWriteResponse,
        },
        TxpoolReadHandle, TxpoolWriteHandle,
    },
    transaction_blob_hash,
};
use cuprate_types::TransactionVerificationData;

use crate::{
    blockchain::ConcreteTxVerifierService,
    constants::PANIC_CRITICAL_SERVICE_ERROR,
    p2p::CrossNetworkInternalPeerId,
    signals::REORG_LOCK,
    txpool::{
        dandelion,
        txs_being_handled::{TxsBeingHandled, TxsBeingHandledLocally},
    },
};

/// An error that can happen handling an incoming tx.
pub enum IncomingTxError {
    Parse(std::io::Error),
    Consensus(ExtendedConsensusError),
    DuplicateTransaction,
}

/// Incoming transactions.
pub struct IncomingTxs {
    /// The raw bytes of the transactions.
    pub txs: Vec<Bytes>,
    /// The routing state of the transactions.
    pub state: TxState<CrossNetworkInternalPeerId>,
}

///  The transaction type used for dandelion++.
#[derive(Clone)]
pub struct DandelionTx(pub Bytes);

/// A transaction ID/hash.
pub(super) type TxId = [u8; 32];

/// The service than handles incoming transaction pool transactions.
///
/// This service handles everything including verifying the tx, adding it to the pool and routing it to other nodes.
pub struct IncomingTxHandler {
    /// A store of txs currently being handled in incoming tx requests.
    pub(super) txs_being_handled: TxsBeingHandled,
    /// The blockchain context cache.
    pub(super) blockchain_context_cache: BlockChainContextService,
    /// The dandelion txpool manager.
    pub(super) dandelion_pool_manager:
        DandelionPoolService<DandelionTx, TxId, CrossNetworkInternalPeerId>,
    /// The transaction verifier service.
    pub(super) tx_verifier_service: ConcreteTxVerifierService,
    /// The txpool write handle.
    pub(super) txpool_write_handle: TxpoolWriteHandle,
    /// The txpool read handle.
    pub(super) txpool_read_handle: TxpoolReadHandle,
}

impl IncomingTxHandler {
    /// Initialize the [`IncomingTxHandler`].
    #[expect(clippy::significant_drop_tightening)]
    pub fn init(
        clear_net: NetworkInterface<ClearNet>,
        txpool_write_handle: TxpoolWriteHandle,
        txpool_read_handle: TxpoolReadHandle,
        blockchain_context_cache: BlockChainContextService,
        tx_verifier_service: ConcreteTxVerifierService,
    ) -> Self {
        let dandelion_router = dandelion::dandelion_router(clear_net);

        let dandelion_pool_manager = dandelion::start_dandelion_pool_manager(
            dandelion_router,
            txpool_read_handle.clone(),
            txpool_write_handle.clone(),
        );

        Self {
            txs_being_handled: TxsBeingHandled::new(),
            blockchain_context_cache,
            dandelion_pool_manager,
            tx_verifier_service,
            txpool_write_handle,
            txpool_read_handle,
        }
    }
}

impl Service<IncomingTxs> for IncomingTxHandler {
    type Response = ();
    type Error = IncomingTxError;
    type Future = BoxFuture<'static, Result<Self::Response, Self::Error>>;

    fn poll_ready(&mut self, cx: &mut Context<'_>) -> Poll<Result<(), Self::Error>> {
        Poll::Ready(Ok(()))
    }

    fn call(&mut self, req: IncomingTxs) -> Self::Future {
        handle_incoming_txs(
            req,
            self.txs_being_handled.clone(),
            self.blockchain_context_cache.clone(),
            self.tx_verifier_service.clone(),
            self.txpool_write_handle.clone(),
            self.txpool_read_handle.clone(),
            self.dandelion_pool_manager.clone(),
        )
        .boxed()
    }
}

/// Handles the incoming txs.
async fn handle_incoming_txs(
    IncomingTxs { txs, state }: IncomingTxs,
    txs_being_handled: TxsBeingHandled,
    mut blockchain_context_cache: BlockChainContextService,
    mut tx_verifier_service: ConcreteTxVerifierService,
    mut txpool_write_handle: TxpoolWriteHandle,
    mut txpool_read_handle: TxpoolReadHandle,
    mut dandelion_pool_manager: DandelionPoolService<DandelionTx, TxId, CrossNetworkInternalPeerId>,
) -> Result<(), IncomingTxError> {
    let _reorg_guard = REORG_LOCK.read().await;

    let (txs, stem_pool_txs, txs_being_handled_guard) =
        prepare_incoming_txs(txs, txs_being_handled, &mut txpool_read_handle).await?;

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

    let context = context.unchecked_blockchain_context();

    tx_verifier_service
        .ready()
        .await
        .expect(PANIC_CRITICAL_SERVICE_ERROR)
        .call(VerifyTxRequest::Prepped {
            txs: txs.clone(),
            current_chain_height: context.chain_height,
            top_hash: context.top_hash,
            time_for_time_lock: context.current_adjusted_timestamp_for_time_lock(),
            hf: context.current_hf,
        })
        .await
        .map_err(IncomingTxError::Consensus)?;

    for tx in txs {
        handle_valid_tx(
            tx,
            state.clone(),
            &mut txpool_write_handle,
            &mut dandelion_pool_manager,
        )
        .await;
    }

    // Re-relay any txs we got in the block that were already in our stem pool.
    for stem_tx in stem_pool_txs {
        rerelay_stem_tx(
            &stem_tx,
            state.clone(),
            &mut txpool_read_handle,
            &mut dandelion_pool_manager,
        )
        .await;
    }

    Ok(())
}

/// Prepares the incoming transactions for verification.
///
/// This will filter out all transactions already in the pool or txs already being handled in another request.
///
/// Returns in order:
///   - The [`TransactionVerificationData`] for all the txs we did not already have
///   - The Ids of the transactions in the incoming message that are in our stem-pool
///   - A [`TxsBeingHandledLocally`] guard that prevents verifying the same tx at the same time across 2 tasks.
async fn prepare_incoming_txs(
    tx_blobs: Vec<Bytes>,
    txs_being_handled: TxsBeingHandled,
    txpool_read_handle: &mut TxpoolReadHandle,
) -> Result<
    (
        Vec<Arc<TransactionVerificationData>>,
        Vec<TxId>,
        TxsBeingHandledLocally,
    ),
    IncomingTxError,
> {
    let mut tx_blob_hashes = HashSet::new();
    let mut txs_being_handled_locally = txs_being_handled.local_tracker();

    // Compute the blob hash for each tx and filter out the txs currently being handled by another incoming tx batch.
    let txs = tx_blobs
        .into_iter()
        .filter_map(|tx_blob| {
            let tx_blob_hash = transaction_blob_hash(&tx_blob);

            // If a duplicate is in here the incoming tx batch contained the same tx twice.
            if !tx_blob_hashes.insert(tx_blob_hash) {
                return Some(Err(IncomingTxError::DuplicateTransaction));
            }

            // If a duplicate is here it is being handled in another batch.
            if !txs_being_handled_locally.try_add_tx(tx_blob_hash) {
                return None;
            }

            Some(Ok((tx_blob_hash, tx_blob)))
        })
        .collect::<Result<Vec<_>, _>>()?;

    // Filter the txs already in the txpool out.
    // This will leave the txs already in the pool in [`TxBeingHandledLocally`] but that shouldn't be an issue.
    let TxpoolReadResponse::FilterKnownTxBlobHashes {
        unknown_blob_hashes,
        stem_pool_hashes,
    } = txpool_read_handle
        .ready()
        .await
        .expect(PANIC_CRITICAL_SERVICE_ERROR)
        .call(TxpoolReadRequest::FilterKnownTxBlobHashes(tx_blob_hashes))
        .await
        .expect(PANIC_CRITICAL_SERVICE_ERROR)
    else {
        unreachable!()
    };

    // Now prepare the txs for verification.
    rayon_spawn_async(move || {
        let txs = txs
            .into_iter()
            .filter_map(|(tx_blob_hash, tx_blob)| {
                if unknown_blob_hashes.contains(&tx_blob_hash) {
                    Some(tx_blob)
                } else {
                    None
                }
            })
            .map(|bytes| {
                let tx = Transaction::read(&mut bytes.as_ref()).map_err(IncomingTxError::Parse)?;

                let tx = new_tx_verification_data(tx)
                    .map_err(|e| IncomingTxError::Consensus(e.into()))?;

                Ok(Arc::new(tx))
            })
            .collect::<Result<Vec<_>, IncomingTxError>>()?;

        Ok((txs, stem_pool_hashes, txs_being_handled_locally))
    })
    .await
}

/// Handle a verified tx.
///
/// This will add the tx to the txpool and route it to the network.
async fn handle_valid_tx(
    tx: Arc<TransactionVerificationData>,
    state: TxState<CrossNetworkInternalPeerId>,
    txpool_write_handle: &mut TxpoolWriteHandle,
    dandelion_pool_manager: &mut DandelionPoolService<
        DandelionTx,
        TxId,
        CrossNetworkInternalPeerId,
    >,
) {
    let incoming_tx =
        IncomingTxBuilder::new(DandelionTx(Bytes::copy_from_slice(&tx.tx_blob)), tx.tx_hash);

    let TxpoolWriteResponse::AddTransaction(double_spend) = txpool_write_handle
        .ready()
        .await
        .expect(PANIC_CRITICAL_SERVICE_ERROR)
        .call(TxpoolWriteRequest::AddTransaction {
            tx,
            state_stem: state.is_stem_stage(),
        })
        .await
        .expect("TODO")
    else {
        unreachable!()
    };

    // TODO: track double spends to quickly ignore them from their blob hash.
    if let Some(tx_hash) = double_spend {
        return;
    };

    // TODO: There is a race condition possible if a tx and block come in at the same time: <https://github.com/Cuprate/cuprate/issues/314>.

    let incoming_tx = incoming_tx
        .with_routing_state(state)
        .with_state_in_db(None)
        .build()
        .unwrap();

    dandelion_pool_manager
        .ready()
        .await
        .expect(PANIC_CRITICAL_SERVICE_ERROR)
        .call(incoming_tx)
        .await
        .expect(PANIC_CRITICAL_SERVICE_ERROR);
}

/// Re-relay a tx that was already in our stem pool.
async fn rerelay_stem_tx(
    tx_hash: &TxId,
    state: TxState<CrossNetworkInternalPeerId>,
    txpool_read_handle: &mut TxpoolReadHandle,
    dandelion_pool_manager: &mut DandelionPoolService<
        DandelionTx,
        TxId,
        CrossNetworkInternalPeerId,
    >,
) {
    let Ok(TxpoolReadResponse::TxBlob { tx_blob, .. }) = txpool_read_handle
        .ready()
        .await
        .expect(PANIC_CRITICAL_SERVICE_ERROR)
        .call(TxpoolReadRequest::TxBlob(*tx_hash))
        .await
    else {
        // The tx could have been dropped from the pool.
        return;
    };

    let incoming_tx =
        IncomingTxBuilder::new(DandelionTx(Bytes::copy_from_slice(&tx_blob)), *tx_hash);

    let incoming_tx = incoming_tx
        .with_routing_state(state)
        .with_state_in_db(Some(State::Stem))
        .build()
        .unwrap();

    dandelion_pool_manager
        .ready()
        .await
        .expect(PANIC_CRITICAL_SERVICE_ERROR)
        .call(incoming_tx)
        .await
        .expect(PANIC_CRITICAL_SERVICE_ERROR);
}