From f880917c5be8830bbe4c3012c5de1a3244ebadc8 Mon Sep 17 00:00:00 2001 From: Roman Krasiuk Date: Wed, 11 Sep 2024 16:16:11 +0200 Subject: [PATCH 1/2] fix(tree): avoid holding two locks simultaneously --- crates/chain-state/src/in_memory.rs | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/crates/chain-state/src/in_memory.rs b/crates/chain-state/src/in_memory.rs index 25362cfd913b..c810f885a20b 100644 --- a/crates/chain-state/src/in_memory.rs +++ b/crates/chain-state/src/in_memory.rs @@ -103,10 +103,8 @@ impl InMemoryState { /// Returns the current chain head state. pub(crate) fn head_state(&self) -> Option> { - self.numbers - .read() - .last_key_value() - .and_then(|(_, hash)| self.blocks.read().get(hash).cloned()) + let hash = *self.numbers.read().last_key_value()?.1; + self.blocks.read().get(&hash).cloned() } /// Returns the pending state corresponding to the current head plus one, From e585436fe22deaf4b61dc2fde55b0b84f56687f7 Mon Sep 17 00:00:00 2001 From: Roman Krasiuk Date: Wed, 11 Sep 2024 16:21:55 +0200 Subject: [PATCH 2/2] nit --- crates/chain-state/src/in_memory.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/chain-state/src/in_memory.rs b/crates/chain-state/src/in_memory.rs index c810f885a20b..f64df5b2ef4d 100644 --- a/crates/chain-state/src/in_memory.rs +++ b/crates/chain-state/src/in_memory.rs @@ -104,7 +104,7 @@ impl InMemoryState { /// Returns the current chain head state. pub(crate) fn head_state(&self) -> Option> { let hash = *self.numbers.read().last_key_value()?.1; - self.blocks.read().get(&hash).cloned() + self.state_by_hash(hash) } /// Returns the pending state corresponding to the current head plus one,