Skip to content

Commit 205e0aa

Browse files
committed
fix(p2p): Broadcast produced block only after successful injection
1 parent 9c1ead0 commit 205e0aa

File tree

2 files changed

+29
-8
lines changed

2 files changed

+29
-8
lines changed

node/src/block_producer/block_producer_reducer.rs

Lines changed: 22 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -338,13 +338,6 @@ impl BlockProducerEnabled {
338338
}
339339
)),
340340
});
341-
342-
#[cfg(feature = "p2p-libp2p")]
343-
{
344-
use mina_p2p_messages::gossip::GossipNetMessageV2;
345-
let message = Box::new(GossipNetMessageV2::NewState(best_tip.block.clone()));
346-
dispatcher.push(P2pNetworkPubsubAction::Broadcast { message });
347-
}
348341
}
349342
BlockProducerAction::BlockInjected => {
350343
if let BlockProducerCurrentState::Produced {
@@ -365,7 +358,10 @@ impl BlockProducerEnabled {
365358
bug_condition!("Invalid state for `BlockProducerAction::BlockInjected` expected: `BlockProducerCurrentState::Produced`, found: {:?}", state.current);
366359
}
367360

368-
let dispatcher = state_context.into_dispatcher();
361+
let (dispatcher, global_state) = state_context.into_dispatcher_and_state();
362+
363+
#[cfg(feature = "p2p-libp2p")]
364+
broadcast_injected_block(global_state, dispatcher);
369365

370366
dispatcher.push(BlockProducerAction::WonSlotSearch);
371367
}
@@ -757,6 +753,24 @@ impl BlockProducerEnabled {
757753
}
758754
}
759755

756+
#[cfg(feature = "p2p-libp2p")]
757+
fn broadcast_injected_block(global_state: &State, dispatcher: &mut Dispatcher<Action, State>) {
758+
use mina_p2p_messages::gossip::GossipNetMessageV2;
759+
760+
let Some(block) = global_state
761+
.block_producer
762+
.as_ref()
763+
.and_then(|bp| bp.current.injected_block())
764+
.map(|pb| pb.block.clone())
765+
else {
766+
// Should be impossible, we call this immediately after having injected the block.
767+
return;
768+
};
769+
770+
let message = Box::new(GossipNetMessageV2::NewState(block));
771+
dispatcher.push(P2pNetworkPubsubAction::Broadcast { message });
772+
}
773+
760774
fn can_apply_supercharged_coinbase(
761775
block_stake_winner: &v2::NonZeroCurvePoint,
762776
stake_proof_sparse_ledger: &v2::MinaBaseSparseLedgerBaseStableV2,

node/src/block_producer/block_producer_state.rs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -372,6 +372,13 @@ impl BlockProducerCurrentState {
372372
}
373373
}
374374

375+
pub fn injected_block(&self) -> Option<&ArcBlockWithHash> {
376+
match self {
377+
Self::Injected { block, .. } => Some(block),
378+
_ => None,
379+
}
380+
}
381+
375382
pub fn produced_block_with_chain(&self) -> Option<(&ArcBlockWithHash, &[AppliedBlock])> {
376383
match self {
377384
Self::Produced { chain, block, .. } => Some((block, chain)),

0 commit comments

Comments
 (0)