From 747cdb196382287a55d9f46eef6244e6e25a458b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Fri, 9 Jun 2017 17:54:08 +0200 Subject: [PATCH] Shell: export `Distributed_db.*.clear` --- src/node/shell/distributed_db.ml | 52 ++++++++++------------ src/node/shell/distributed_db.mli | 3 +- src/node/shell/distributed_db_functors.ml | 10 ++--- src/node/shell/distributed_db_functors.mli | 2 +- src/node/shell/validator.ml | 4 +- 5 files changed, 33 insertions(+), 38 deletions(-) diff --git a/src/node/shell/distributed_db.ml b/src/node/shell/distributed_db.ml index 8349ff078..fb45a0010 100644 --- a/src/node/shell/distributed_db.ml +++ b/src/node/shell/distributed_db.ml @@ -188,8 +188,8 @@ module Raw_operation_hashes = struct let read_all table hash n = map_p (fun i -> Table.read table (hash, i)) (0 -- (n-1)) - let remove_all table hash n = - Lwt_list.iter_p (fun i -> Table.remove table (hash, i)) (0 -- (n-1)) + let clear_all table hash n = + List.iter (fun i -> Table.clear table (hash, i)) (0 -- (n-1)) end @@ -250,8 +250,8 @@ module Raw_operations = struct let read_all table hash n = map_p (fun i -> Table.read table (hash, i)) (0 -- (n-1)) - let remove_all table hash n = - Lwt_list.iter_p (fun i -> Table.remove table (hash, i)) (0 -- (n-1)) + let clear_all table hash n = + List.iter (fun i -> Table.clear table (hash, i)) (0 -- (n-1)) end @@ -713,31 +713,27 @@ let commit_block net_db hash n validation_result = read_all_operations net_db hash n >>=? fun operations -> State.Block.store net_db.net_state header operations validation_result >>=? fun res -> - Raw_block_header.Table.remove - net_db.block_header_db.table hash >>= fun () -> - Raw_operation_hashes.remove_all - net_db.operation_hashes_db.table hash n >>= fun () -> - Raw_operations.remove_all - net_db.operations_db.table hash n >>= fun () -> + Raw_block_header.Table.clear net_db.block_header_db.table hash ; + Raw_operation_hashes.clear_all + net_db.operation_hashes_db.table hash n ; + Raw_operations.clear_all + net_db.operations_db.table hash n ; (* TODO: proper handling of the operations table by the prevalidator. *) - Lwt_list.iter_p - (Lwt_list.iter_p - (fun op -> Raw_operation.Table.remove + List.iter + (List.iter + (fun op -> Raw_operation.Table.clear net_db.operation_db.table (Operation.hash op))) - operations >>= fun () -> + operations ; return res let commit_invalid_block net_db hash n = Raw_block_header.Table.read net_db.block_header_db.table hash >>=? fun header -> State.Block.store_invalid net_db.net_state header >>=? fun res -> - Raw_block_header.Table.remove - net_db.block_header_db.table hash >>= fun () -> - Raw_operation_hashes.remove_all - net_db.operation_hashes_db.table hash n >>= fun () -> - Raw_operations.remove_all - net_db.operations_db.table hash n >>= fun () -> + Raw_block_header.Table.clear net_db.block_header_db.table hash ; + Raw_operation_hashes.clear_all net_db.operation_hashes_db.table hash n ; + Raw_operations.clear_all net_db.operations_db.table hash n ; return res let inject_operation net_db h op = @@ -753,7 +749,7 @@ let inject_protocol db h p = let commit_protocol db h = Raw_protocol.Table.read db.protocol_db.table h >>=? fun p -> State.Protocol.store db.disk p >>= fun res -> - Raw_protocol.Table.remove db.protocol_db.table h >>= fun () -> + Raw_protocol.Table.clear db.protocol_db.table h ; return (res <> None) type operation = @@ -803,12 +799,10 @@ let inject_block db bytes operations = net_db.operations_db.table hash operations >>= fun _ -> return (hash, block) -let remove_block net_db hash n = - Raw_operations.remove_all - net_db.operations_db.table hash n >>= fun () -> - Raw_operation_hashes.remove_all - net_db.operation_hashes_db.table hash n >>= fun () -> - Raw_block_header.Table.remove net_db.block_header_db.table hash +let clear_block net_db hash n = + Raw_operations.clear_all net_db.operations_db.table hash n ; + Raw_operation_hashes.clear_all net_db.operation_hashes_db.table hash n ; + Raw_block_header.Table.clear net_db.block_header_db.table hash let broadcast_head net_db head mempool = let msg : Message.t = @@ -848,6 +842,7 @@ module type DISTRIBUTED_DB = sig val watch: t -> (key * value) Lwt_stream.t * Watcher.stopper val prefetch: t -> ?peer:P2p.Peer_id.t -> key -> param -> unit val fetch: t -> ?peer:P2p.Peer_id.t -> key -> param -> value Lwt.t + val clear: t -> key -> unit end module Make @@ -867,9 +862,10 @@ module Make let read_exn t k = Table.read_exn (Kind.proj t) k let prefetch t ?peer k p = Table.prefetch (Kind.proj t) ?peer k p let fetch t ?peer k p = Table.fetch (Kind.proj t) ?peer k p - let remove t k = Table.remove (Kind.proj t) k + let clear t k = Table.clear (Kind.proj t) k let inject t k v = Table.inject (Kind.proj t) k v let watch t = Table.watch (Kind.proj t) + let clear t k = Table.clear (Kind.proj t) k end module Block_header = diff --git a/src/node/shell/distributed_db.mli b/src/node/shell/distributed_db.mli index e47a01b90..e8e8e5b7e 100644 --- a/src/node/shell/distributed_db.mli +++ b/src/node/shell/distributed_db.mli @@ -52,7 +52,7 @@ val commit_invalid_block: val inject_block: t -> MBytes.t -> operation list list -> (Block_hash.t * Block_header.t) tzresult Lwt.t -val remove_block: net_db -> Block_hash.t -> int -> unit Lwt.t +val clear_block: net_db -> Block_hash.t -> int -> unit val inject_operation: net_db -> Operation_hash.t -> Operation.t -> bool tzresult Lwt.t @@ -83,6 +83,7 @@ module type DISTRIBUTED_DB = sig val watch: t -> (key * value) Lwt_stream.t * Watcher.stopper val prefetch: t -> ?peer:P2p.Peer_id.t -> key -> param -> unit val fetch: t -> ?peer:P2p.Peer_id.t -> key -> param -> value Lwt.t + val clear: t -> key -> unit end module Block_header : diff --git a/src/node/shell/distributed_db_functors.ml b/src/node/shell/distributed_db_functors.ml index b8eed341a..7a3e08c93 100644 --- a/src/node/shell/distributed_db_functors.ml +++ b/src/node/shell/distributed_db_functors.ml @@ -24,7 +24,7 @@ module type DISTRIBUTED_DB = sig val prefetch: t -> ?peer:P2p.Peer_id.t -> key -> param -> unit val fetch: t -> ?peer:P2p.Peer_id.t -> key -> param -> value Lwt.t - val remove: t -> key -> unit Lwt.t + val clear: t -> key -> unit val inject: t -> key -> value -> bool Lwt.t val watch: t -> (key * value) Lwt_stream.t * Watcher.stopper @@ -190,13 +190,11 @@ end = struct | Found _ -> Lwt.return_false - let remove s k = + let clear s k = match Memory_table.find s.memory k with - | exception Not_found -> Lwt.return_unit + | exception Not_found -> () | Pending _ -> assert false - | Found _ -> - Memory_table.remove s.memory k ; - Lwt.return_unit + | Found _ -> Memory_table.remove s.memory k let watch s = Watcher.create_stream s.input diff --git a/src/node/shell/distributed_db_functors.mli b/src/node/shell/distributed_db_functors.mli index 370d484b1..dba1d6bf2 100644 --- a/src/node/shell/distributed_db_functors.mli +++ b/src/node/shell/distributed_db_functors.mli @@ -24,7 +24,7 @@ module type DISTRIBUTED_DB = sig val prefetch: t -> ?peer:P2p.Peer_id.t -> key -> param -> unit val fetch: t -> ?peer:P2p.Peer_id.t -> key -> param -> value Lwt.t - val remove: t -> key -> unit Lwt.t + val clear: t -> key -> unit val inject: t -> key -> value -> bool Lwt.t val watch: t -> (key * value) Lwt_stream.t * Watcher.stopper diff --git a/src/node/shell/validator.ml b/src/node/shell/validator.ml index e780ed6f2..1568e430f 100644 --- a/src/node/shell/validator.ml +++ b/src/node/shell/validator.ml @@ -832,8 +832,8 @@ let create_worker ?max_ttl state db = let validation = protect ~on_error: begin fun err -> - Distributed_db.remove_block - net.net_db hash (List.length operations) >>= fun () -> + Distributed_db.clear_block + net.net_db hash (List.length operations) ; Lwt.return (Error err) end begin fun () ->