Skip to content

Commit 48a4fa8

Browse files
committed
review fixes
1 parent 5cae64f commit 48a4fa8

File tree

7 files changed

+61
-54
lines changed

7 files changed

+61
-54
lines changed

binaries/cuprated/src/blockchain/interface.rs

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,21 @@ pub async fn handle_incoming_block(
137137
return Ok(IncomingBlockOk::AlreadyHave);
138138
}
139139

140-
// From this point on we MUST not early return without removing the block hash from `BLOCKS_BEING_HANDLED`.
140+
// We must remove the block hash from `BLOCKS_BEING_HANDLED`.
141+
let _guard = {
142+
struct RemoveFromBlocksBeingHandled {
143+
block_hash: [u8; 32],
144+
}
145+
impl Drop for RemoveFromBlocksBeingHandled {
146+
fn drop(&mut self) {
147+
BLOCKS_BEING_HANDLED
148+
.lock()
149+
.unwrap()
150+
.remove(&self.block_hash);
151+
}
152+
}
153+
RemoveFromBlocksBeingHandled { block_hash }
154+
};
141155

142156
let (response_tx, response_rx) = oneshot::channel();
143157

@@ -150,15 +164,10 @@ pub async fn handle_incoming_block(
150164
.await
151165
.expect("TODO: don't actually panic here, an err means we are shutting down");
152166

153-
let res = response_rx
167+
response_rx
154168
.await
155169
.expect("The blockchain manager will always respond")
156-
.map_err(IncomingBlockError::InvalidBlock);
157-
158-
// Remove the block hash from the blocks being handled.
159-
BLOCKS_BEING_HANDLED.lock().unwrap().remove(&block_hash);
160-
161-
res
170+
.map_err(IncomingBlockError::InvalidBlock)
162171
}
163172

164173
/// Check if we have a block with the given hash.

binaries/cuprated/src/txpool.rs

Lines changed: 0 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,30 +13,3 @@ mod incoming_tx;
1313
mod txs_being_handled;
1414

1515
pub use incoming_tx::IncomingTxHandler;
16-
17-
/// Initialize the [`IncomingTxHandler`].
18-
#[expect(clippy::significant_drop_tightening)]
19-
pub fn incoming_tx_handler(
20-
clear_net: NetworkInterface<ClearNet>,
21-
txpool_write_handle: TxpoolWriteHandle,
22-
txpool_read_handle: TxpoolReadHandle,
23-
blockchain_context_cache: BlockChainContextService,
24-
tx_verifier_service: ConcreteTxVerifierService,
25-
) -> IncomingTxHandler {
26-
let dandelion_router = dandelion::dandelion_router(clear_net);
27-
28-
let dandelion_pool_manager = dandelion::start_dandelion_pool_manager(
29-
dandelion_router,
30-
txpool_read_handle.clone(),
31-
txpool_write_handle.clone(),
32-
);
33-
34-
IncomingTxHandler {
35-
txs_being_handled: txs_being_handled::TxsBeingHandled::new(),
36-
blockchain_context_cache,
37-
dandelion_pool_manager,
38-
tx_verifier_service,
39-
txpool_write_handle,
40-
txpool_read_handle,
41-
}
42-
}

binaries/cuprated/src/txpool/dandelion.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ mod tx_store;
1818

1919
/// The configuration used for [`cuprate_dandelion_tower`].
2020
///
21-
/// TODO: should we expose this? probably not.
21+
/// TODO: should we expose this to users of cuprated? probably not.
2222
const DANDELION_CONFIG: DandelionConfig = DandelionConfig {
2323
time_between_hop: Duration::from_millis(175),
2424
epoch_duration: Duration::from_secs(10 * 60),

binaries/cuprated/src/txpool/dandelion/diffuse_service.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ use cuprate_dandelion_tower::traits::DiffuseRequest;
1010
use cuprate_p2p::{BroadcastRequest, BroadcastSvc};
1111
use cuprate_p2p_core::ClearNet;
1212

13-
use super::DandelionTx;
13+
use crate::txpool::dandelion::DandelionTx;
1414

1515
/// The dandelion diffusion service.
1616
pub struct DiffuseService {
@@ -30,16 +30,14 @@ impl Service<DiffuseRequest<DandelionTx>> for DiffuseService {
3030

3131
fn call(&mut self, req: DiffuseRequest<DandelionTx>) -> Self::Future {
3232
// TODO: the dandelion crate should pass along where we got the tx from.
33-
// TODO: Use `into_inner` when 1.82.0 stabilizes.
34-
self.clear_net_broadcast_service
33+
let Ok(()) = self
34+
.clear_net_broadcast_service
3535
.call(BroadcastRequest::Transaction {
3636
tx_bytes: req.0 .0,
3737
direction: None,
3838
received_from: None,
3939
})
40-
.now_or_never()
41-
.unwrap()
42-
.expect("Broadcast service is Infallible");
40+
.into_inner();
4341

4442
ready(Ok(()))
4543
}

binaries/cuprated/src/txpool/dandelion/stem_service.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ use cuprate_p2p_core::{
1515
};
1616
use cuprate_wire::protocol::NewTransactions;
1717

18-
use super::DandelionTx;
19-
use crate::p2p::CrossNetworkInternalPeerId;
18+
use crate::{p2p::CrossNetworkInternalPeerId, txpool::dandelion::DandelionTx};
2019

2120
/// The dandelion outbound peer stream.
2221
pub struct OutboundPeerStream {

binaries/cuprated/src/txpool/incoming_tx.rs

Lines changed: 38 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@ use cuprate_dandelion_tower::{
1818
State, TxState,
1919
};
2020
use cuprate_helper::asynch::rayon_spawn_async;
21+
use cuprate_p2p::NetworkInterface;
22+
use cuprate_p2p_core::ClearNet;
2123
use cuprate_txpool::{
2224
service::{
2325
interface::{
@@ -34,7 +36,10 @@ use crate::{
3436
constants::PANIC_CRITICAL_SERVICE_ERROR,
3537
p2p::CrossNetworkInternalPeerId,
3638
signals::REORG_LOCK,
37-
txpool::txs_being_handled::{TxsBeingHandled, TxsBeingHandledLocally},
39+
txpool::{
40+
dandelion,
41+
txs_being_handled::{TxsBeingHandled, TxsBeingHandledLocally},
42+
},
3843
};
3944

4045
/// An error that can happen handling an incoming tx.
@@ -78,6 +83,35 @@ pub struct IncomingTxHandler {
7883
pub(super) txpool_read_handle: TxpoolReadHandle,
7984
}
8085

86+
impl IncomingTxHandler {
87+
/// Initialize the [`IncomingTxHandler`].
88+
#[expect(clippy::significant_drop_tightening)]
89+
pub fn init(
90+
clear_net: NetworkInterface<ClearNet>,
91+
txpool_write_handle: TxpoolWriteHandle,
92+
txpool_read_handle: TxpoolReadHandle,
93+
blockchain_context_cache: BlockChainContextService,
94+
tx_verifier_service: ConcreteTxVerifierService,
95+
) -> Self {
96+
let dandelion_router = dandelion::dandelion_router(clear_net);
97+
98+
let dandelion_pool_manager = dandelion::start_dandelion_pool_manager(
99+
dandelion_router,
100+
txpool_read_handle.clone(),
101+
txpool_write_handle.clone(),
102+
);
103+
104+
Self {
105+
txs_being_handled: TxsBeingHandled::new(),
106+
blockchain_context_cache,
107+
dandelion_pool_manager,
108+
tx_verifier_service,
109+
txpool_write_handle,
110+
txpool_read_handle,
111+
}
112+
}
113+
}
114+
81115
impl Service<IncomingTxs> for IncomingTxHandler {
82116
type Response = ();
83117
type Error = IncomingTxError;
@@ -89,8 +123,7 @@ impl Service<IncomingTxs> for IncomingTxHandler {
89123

90124
fn call(&mut self, req: IncomingTxs) -> Self::Future {
91125
handle_incoming_txs(
92-
req.txs,
93-
req.state,
126+
req,
94127
self.txs_being_handled.clone(),
95128
self.blockchain_context_cache.clone(),
96129
self.tx_verifier_service.clone(),
@@ -105,8 +138,7 @@ impl Service<IncomingTxs> for IncomingTxHandler {
105138
/// Handles the incoming txs.
106139
#[expect(clippy::too_many_arguments)]
107140
async fn handle_incoming_txs(
108-
txs: Vec<Bytes>,
109-
state: TxState<CrossNetworkInternalPeerId>,
141+
IncomingTxs { txs, state }: IncomingTxs,
110142
txs_being_handled: TxsBeingHandled,
111143
mut blockchain_context_cache: BlockChainContextService,
112144
mut tx_verifier_service: ConcreteTxVerifierService,
@@ -197,7 +229,7 @@ async fn prepare_incoming_txs(
197229
let txs = tx_blobs
198230
.into_iter()
199231
.filter_map(|tx_blob| {
200-
let tx_blob_hash = transaction_blob_hash(tx_blob.as_ref());
232+
let tx_blob_hash = transaction_blob_hash(&tx_blob);
201233

202234
// If a duplicate is in here the incoming tx batch contained the same tx twice.
203235
if !tx_blob_hashes.insert(tx_blob_hash) {

storage/txpool/src/service/interface.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,14 +94,10 @@ pub enum TxpoolWriteRequest {
9494
},
9595

9696
/// Remove a transaction with the given hash from the pool.
97-
///
98-
/// Returns [`TxpoolWriteResponse::Ok`].
9997
RemoveTransaction(TransactionHash),
10098

10199
/// Promote a transaction from the stem pool to the fluff pool.
102100
/// If the tx is already in the fluff pool this does nothing.
103-
///
104-
/// Returns [`TxpoolWriteResponse::Ok`].
105101
Promote(TransactionHash),
106102

107103
/// Tell the tx-pool about a new block.

0 commit comments

Comments
 (0)