From 206ca8153d6e0a43e0853cdfe97549bf628ffb7f Mon Sep 17 00:00:00 2001 From: Matthias Seitz Date: Fri, 26 Jul 2024 15:59:21 +0200 Subject: [PATCH] feat: add CanonicalInMemoryState::set_pending_block (#9834) --- crates/chain-state/src/in_memory.rs | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) diff --git a/crates/chain-state/src/in_memory.rs b/crates/chain-state/src/in_memory.rs index cec9321773..e04eee9cbf 100644 --- a/crates/chain-state/src/in_memory.rs +++ b/crates/chain-state/src/in_memory.rs @@ -140,14 +140,24 @@ impl CanonicalInMemoryState { self.state_by_hash(hash).map(|block| block.block().block.header.clone()) } + /// Updates the pending block with the given block. + /// + /// Note: This assumes that the parent block of the pending block is canonical. + pub fn set_pending_block(&self, pending: ExecutedBlock) { + // fetch the state of the pending block's parent block + let parent = self.state_by_hash(pending.block().parent_hash); + let pending = BlockState::with_parent(pending, parent.map(|p| (*p).clone())); + *self.inner.in_memory_state.pending.write() = Some(pending); + } + /// Append new blocks to the in memory state. fn update_blocks(&self, new_blocks: I, reorged: I) where I: IntoIterator, { // acquire all locks - let mut blocks = self.inner.in_memory_state.blocks.write(); let mut numbers = self.inner.in_memory_state.numbers.write(); + let mut blocks = self.inner.in_memory_state.blocks.write(); let mut pending = self.inner.in_memory_state.pending.write(); // we first remove the blocks from the reorged chain