From 1b303c4e139a6e5a980871c9252e707926aba56d Mon Sep 17 00:00:00 2001 From: Victor Allombert Date: Thu, 22 Feb 2024 11:23:24 +0100 Subject: [PATCH 1/2] Shell: avoid head validation request on blocks know as current head --- src/lib_shell/chain_validator.ml | 23 ++++++++++++++++++----- 1 file changed, 18 insertions(+), 5 deletions(-) diff --git a/src/lib_shell/chain_validator.ml b/src/lib_shell/chain_validator.ml index 86ff747dc511..e55f63ea2a1e 100644 --- a/src/lib_shell/chain_validator.ml +++ b/src/lib_shell/chain_validator.ml @@ -556,14 +556,27 @@ let on_notify_branch w peer_id locator = Peer_validator.notify_branch pv locator ; return_ok_unit) -let on_notify_head w peer_id (hash, header) mempool = +let on_notify_head w peer_id (block_hash, header) mempool = let open Lwt_syntax in let nv = Worker.state w in - let* () = check_and_update_synchronisation_state w (hash, header) peer_id in + let* () = + check_and_update_synchronisation_state w (block_hash, header) peer_id + in + let* current_head = Store.Chain.current_head nv.parameters.chain_store in let* (r : (_, Empty.t) result) = - with_activated_peer_validator w peer_id (fun pv -> - Peer_validator.notify_head pv hash header ; - return_ok_unit) + (* To minimize unnecessary calls to notify_head, we do nothing + when the received block is actually the current head or it's + predecessor. *) + if + not + Block_hash.( + Store.Block.hash current_head = block_hash + || Store.Block.predecessor current_head = block_hash) + then + with_activated_peer_validator w peer_id (fun pv -> + Peer_validator.notify_head pv block_hash header ; + return_ok_unit) + else return_ok_unit in match r with | Ok () -> ( -- GitLab From a4685f3f15958cd5202fd57120fdeda598328a43 Mon Sep 17 00:00:00 2001 From: Victor Allombert Date: Thu, 22 Feb 2024 14:08:45 +0100 Subject: [PATCH 2/2] Store: improve Store.Block.is_known_valid --- src/lib_store/unix/store.ml | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/lib_store/unix/store.ml b/src/lib_store/unix/store.ml index 56406783ab54..84f66f04c5e2 100644 --- a/src/lib_store/unix/store.ml +++ b/src/lib_store/unix/store.ml @@ -252,12 +252,18 @@ module Block = struct (* I/O operations *) - let is_known_valid chain_store hash = + let is_known_valid chain_store block_hash = let open Lwt_syntax in let* current_head = current_head chain_store in - if Block_repr.hash current_head = hash then return_true + if + Block_hash.( + Block_repr.hash current_head = block_hash + || Block_repr.predecessor current_head = block_hash) + then return_true else - let* r = Block_store.(mem chain_store.block_store (Block (hash, 0))) in + let* r = + Block_store.(mem chain_store.block_store (Block (block_hash, 0))) + in match r with | Ok k -> return k | Error _ -> -- GitLab