From f5ecbb10739bcbc4f8952f7f5b01df26c972dcde Mon Sep 17 00:00:00 2001 From: Victor Allombert Date: Thu, 13 Jul 2023 09:46:23 +0200 Subject: [PATCH 1/2] Naming: improve lockfile naming --- src/lib_store/shared/naming.ml | 2 +- src/lib_store/shared/naming.mli | 2 +- src/lib_store/unix/store.ml | 2 +- 3 files changed, 3 insertions(+), 3 deletions(-) diff --git a/src/lib_store/shared/naming.ml b/src/lib_store/shared/naming.ml index 1619672296e6..d23f17ffa1cf 100644 --- a/src/lib_store/shared/naming.ml +++ b/src/lib_store/shared/naming.ml @@ -70,7 +70,7 @@ let chain_config_file dir = chain_config_encoding chain_config_equal -let lock_file dir = mk_file dir "lock" +let lockfile dir = mk_file dir "lock" let gc_lockfile dir = mk_file dir "gc_lock" diff --git a/src/lib_store/shared/naming.mli b/src/lib_store/shared/naming.mli index b3af94856ead..9cdcbc3f4830 100644 --- a/src/lib_store/shared/naming.mli +++ b/src/lib_store/shared/naming.mli @@ -75,7 +75,7 @@ val chain_dir : Chain_id.t -> [`Chain_dir] directory -val lock_file : [`Chain_dir] directory -> [`Lockfile] file +val lockfile : [`Chain_dir] directory -> [`Lockfile] file val gc_lockfile : [`Chain_dir] directory -> [`Gc_lockfile] file diff --git a/src/lib_store/unix/store.ml b/src/lib_store/unix/store.ml index c1b55d75a5cf..3392ec9b0d3a 100644 --- a/src/lib_store/unix/store.ml +++ b/src/lib_store/unix/store.ml @@ -226,7 +226,7 @@ let create_lockfile chain_dir = protect (fun () -> let* fd = Lwt_unix.openfile - (Naming.lock_file chain_dir |> Naming.file_path) + (Naming.lockfile chain_dir |> Naming.file_path) [Unix.O_CREAT; O_RDWR; O_CLOEXEC; O_SYNC] 0o644 in -- GitLab From 20543b8fe56d2d820573d54a9a0b0b86ab2d9482 Mon Sep 17 00:00:00 2001 From: Victor Allombert Date: Fri, 24 Nov 2023 10:20:08 +0100 Subject: [PATCH 2/2] Store: update protocol table as soon as a block is stored --- src/lib_shell/chain_validator.ml | 42 ++------------- src/lib_store/unix/store.ml | 90 ++++++++++++++++++++++---------- 2 files changed, 66 insertions(+), 66 deletions(-) diff --git a/src/lib_shell/chain_validator.ml b/src/lib_shell/chain_validator.ml index 3b62408185be..3ed904e096a3 100644 --- a/src/lib_shell/chain_validator.ml +++ b/src/lib_shell/chain_validator.ml @@ -46,7 +46,6 @@ module Request = struct peer : P2p_peer_id.t option; (* The peer who sent the block if it was not injected locally. *) block : Store.Block.t; - resulting_context_hash : Context_hash.t; } -> (update, error trace) t | Notify_branch : P2p_peer.Id.t * Block_locator.t -> (unit, Empty.t) t @@ -156,7 +155,7 @@ let check_and_update_synchronisation_state w (hash, block) peer_id : unit Lwt.t else Lwt.return_unit (* Called for every validated block. *) -let notify_new_block w peer {Block_validator.block; resulting_context_hash} = +let notify_new_block w peer {Block_validator.block; _} = let nv = Worker.state w in Option.iter (fun id -> @@ -167,9 +166,7 @@ let notify_new_block w peer {Block_validator.block; resulting_context_hash} = nv.parameters.parent ; Lwt_watcher.notify nv.valid_block_input block ; Lwt_watcher.notify nv.parameters.global_valid_block_input block ; - Worker.Queue.push_request_now - w - (Validated {peer; block; resulting_context_hash}) + Worker.Queue.push_request_now w (Validated {peer; block}) let with_activated_peer_validator w peer_id f = let open Lwt_syntax in @@ -198,33 +195,6 @@ let with_activated_peer_validator w peer_id f = | Worker_types.Launching _ -> return_ok_unit) -let may_update_protocol_level chain_store block resulting_context_hash = - let open Lwt_result_syntax in - let* pred = Store.Block.read_predecessor chain_store block in - let prev_proto_level = Store.Block.proto_level pred in - let new_proto_level = Store.Block.proto_level block in - if Compare.Int.(prev_proto_level < new_proto_level) then - let context_index = - Store.context_index (Store.Chain.global_store chain_store) - in - let* resulting_context = - protect (fun () -> - let*! c = - Context_ops.checkout_exn context_index resulting_context_hash - in - return c) - in - let*! new_protocol = Context_ops.get_protocol resulting_context in - let* (module NewProto) = Registered_protocol.get_result new_protocol in - Store.Chain.may_update_protocol_level - chain_store - ~pred - ~protocol_level:new_proto_level - ~expect_predecessor_context: - (NewProto.expected_context_hash = Predecessor_resulting_context) - (block, new_protocol) - else return_unit - let may_switch_test_chain w active_chains spawn_child block = let open Lwt_result_syntax in let nv = Worker.state w in @@ -495,7 +465,7 @@ let may_synchronise_context synchronisation_state chain_store = else Lwt.return_unit let on_validation_request w peer start_testchain active_chains spawn_child block - resulting_context_hash = + = let open Lwt_result_syntax in let*! () = Option.iter_s @@ -518,9 +488,6 @@ let on_validation_request w peer start_testchain active_chains spawn_child block if is_bootstrapped nv then Distributed_db.Advertise.current_head nv.chain_db block in - let* () = - may_update_protocol_level chain_store block resulting_context_hash - in let*! () = if start_testchain then may_switch_test_chain w active_chains spawn_child block @@ -617,7 +584,7 @@ let on_request (type a b) w start_testchain active_chains spawn_child Prometheus.Counter.inc_one nv.parameters.metrics.worker_counters.worker_request_count ; match req with - | Request.Validated {peer; block; resulting_context_hash} -> + | Request.Validated {peer; block} -> on_validation_request w peer @@ -625,7 +592,6 @@ let on_request (type a b) w start_testchain active_chains spawn_child active_chains spawn_child block - resulting_context_hash | Request.Notify_branch (peer_id, locator) -> on_notify_branch w peer_id locator | Request.Notify_head (peer_id, hash, header, mempool) -> diff --git a/src/lib_store/unix/store.ml b/src/lib_store/unix/store.ml index 3392ec9b0d3a..9867361b6b44 100644 --- a/src/lib_store/unix/store.ml +++ b/src/lib_store/unix/store.ml @@ -467,6 +467,35 @@ module Block = struct operations_data_lengths = to_string ops_metadata; } )) + let set_protocol_level chain_store ~protocol_level + (block, protocol_hash, expect_predecessor_context) = + let open Lwt_result_syntax in + Shared.locked_use chain_store.chain_state (fun {protocol_levels_data; _} -> + let* () = + Stored_data.update_with protocol_levels_data (fun protocol_levels -> + let activation_block = descriptor block in + Lwt.return + Protocol_levels.( + add + protocol_level + { + protocol = protocol_hash; + activation_block; + expect_predecessor_context; + } + protocol_levels)) + in + let*! () = + Store_events.( + emit + update_protocol_table + ( protocol_hash, + protocol_level, + Block_repr.hash block, + Block_repr.level block )) + in + return_unit) + let store_block chain_store ~block_header ~operations validation_result = let open Lwt_result_syntax in let { @@ -603,6 +632,38 @@ module Block = struct block resulting_context_hash in + let protocol_level = Block_repr.proto_level block in + let* pred_block = + read_block chain_store (Block_repr.predecessor block) + in + let pred_proto_level = Block_repr.proto_level pred_block in + (* We update the protocol_table when a block contains a + protocol level change. *) + let* () = + if Compare.Int.(pred_proto_level < protocol_level) then + let context_index = chain_store.global_store.context_index in + let* resulting_context = + protect (fun () -> + let*! c = + Context_ops.checkout_exn + context_index + resulting_context_hash + in + return c) + in + let*! protocol_hash = Context_ops.get_protocol resulting_context in + let* (module NewProto) = + Registered_protocol.get_result protocol_hash + in + set_protocol_level + chain_store + ~protocol_level + ( block, + protocol_hash, + NewProto.expected_context_hash = Predecessor_resulting_context + ) + else return_unit + in let*! () = Store_events.(emit store_block) (hash, block_header.shell.level) in @@ -2556,34 +2617,7 @@ module Chain = struct let expect_predecessor_context_hash chain_store ~protocol_level = expect_predecessor_context_hash chain_store ~protocol_level - let set_protocol_level chain_store ~protocol_level - (block, protocol_hash, expect_predecessor_context) = - let open Lwt_result_syntax in - Shared.locked_use chain_store.chain_state (fun {protocol_levels_data; _} -> - let* () = - Stored_data.update_with protocol_levels_data (fun protocol_levels -> - let activation_block = Block.descriptor block in - Lwt.return - Protocol_levels.( - add - protocol_level - { - protocol = protocol_hash; - activation_block; - expect_predecessor_context; - } - protocol_levels)) - in - let*! () = - Store_events.( - emit - update_protocol_table - ( protocol_hash, - protocol_level, - Block.hash block, - Block.level block )) - in - return_unit) + let set_protocol_level = Block.set_protocol_level let may_update_protocol_level chain_store ?pred ?protocol_level ~expect_predecessor_context (block, protocol_hash) = -- GitLab