Skip to content

Commit 36e39eb

Browse files
authored
fix(optimism): Compare parent hash and latest hash to invalidate cached flashblock (#18238)
1 parent 1d7fefe commit 36e39eb

File tree

1 file changed

+21
-11
lines changed

1 file changed

+21
-11
lines changed

crates/optimism/flashblocks/src/service.rs

Lines changed: 21 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,9 @@ use crate::{sequence::FlashBlockSequence, ExecutionPayloadBaseV1, FlashBlock};
22
use alloy_eips::BlockNumberOrTag;
33
use alloy_primitives::B256;
44
use futures_util::{FutureExt, Stream, StreamExt};
5-
use reth_chain_state::{CanonStateNotifications, CanonStateSubscriptions, ExecutedBlock};
5+
use reth_chain_state::{
6+
CanonStateNotification, CanonStateNotifications, CanonStateSubscriptions, ExecutedBlock,
7+
};
68
use reth_errors::RethError;
79
use reth_evm::{
810
execute::{BlockBuilder, BlockBuilderOutcome},
@@ -167,6 +169,12 @@ where
167169
},
168170
)))
169171
}
172+
173+
/// Takes out `current` [`PendingBlock`] if `state` is not preceding it.
174+
fn on_new_tip(&mut self, state: CanonStateNotification<N>) -> Option<PendingBlock<N>> {
175+
let latest = state.tip_checked()?.hash();
176+
self.current.take_if(|current| current.parent_hash() != latest)
177+
}
170178
}
171179

172180
impl<N, S, EvmConfig, Provider> Stream for FlashBlockService<N, S, EvmConfig, Provider>
@@ -199,22 +207,24 @@ where
199207
}
200208
}
201209

202-
// advance new canonical message, if any to reset flashblock
203-
{
210+
if let Poll::Ready(Ok(state)) = {
204211
let fut = this.canon_receiver.recv();
205212
pin!(fut);
206-
if fut.poll_unpin(cx).is_ready() {
207-
// if we have a new canonical message, we know the currently tracked flashblock is
208-
// invalidated
209-
if let Some(current) = this.current.take() {
210-
trace!(parent_hash=%current.block().parent_hash(), block_number=current.block().number(), "Clearing current flashblock on new canonical block");
211-
return Poll::Ready(Some(Ok(None)))
212-
}
213+
fut.poll_unpin(cx)
214+
} {
215+
if let Some(current) = this.on_new_tip(state) {
216+
trace!(
217+
parent_hash = %current.block().parent_hash(),
218+
block_number = current.block().number(),
219+
"Clearing current flashblock on new canonical block"
220+
);
221+
222+
return Poll::Ready(Some(Ok(None)))
213223
}
214224
}
215225

216226
if !this.rebuild && this.current.is_some() {
217-
return Poll::Pending;
227+
return Poll::Pending
218228
}
219229

220230
let now = Instant::now();

0 commit comments

Comments
 (0)