Commit 29452a1c authored by Benjamin Canou's avatar Benjamin Canou Committed by Seb Mondet

Shell: 'Backport' prevalidator filters in master

parent 1f0963ab
......@@ -23,6 +23,9 @@
(* *)
(*****************************************************************************)
let () =
Prevalidator_filters.register (module Tezos_mempool_005_PsBabyM1.Filter)
(** Commands *)
let show (args : Node_shared_arg.t) =
......
......@@ -326,17 +326,27 @@ let broadcast_head w ~previous block =
Lwt.return_unit )
else Distributed_db.Advertise.current_branch nv.parameters.chain_db
let safe_get_protocol hash =
match Registered_protocol.get hash with
| None ->
(* FIXME. *)
(* This should not happen: it should be handled in the validator. *)
failwith
"chain_validator: missing protocol '%a' for the current block."
Protocol_hash.pp_short
hash
| Some protocol ->
return protocol
let safe_get_prevalidator_filter hash =
match Prevalidator_filters.find hash with
| Some filter ->
return filter
| None -> (
match Registered_protocol.get hash with
| None ->
(* FIXME. *)
(* This should not happen: it should be handled in the validator. *)
failwith
"chain_validator: missing protocol '%a' for the current block."
Protocol_hash.pp_short
hash
| Some protocol ->
Log.log_notice
"no prevalidator filter found for protocol '%a'"
Protocol_hash.pp_short
hash ;
let (module Proto) = protocol in
let module Filter = Prevalidator_filters.No_filter (Proto) in
return (module Filter : Prevalidator_filters.FILTER) )
let on_request (type a) w start_testchain active_chains spawn_child
(req : a Request.t) : a tzresult Lwt.t =
......@@ -375,12 +385,12 @@ let on_request (type a) w start_testchain active_chains spawn_child
>>=? fun new_protocol ->
let old_protocol = Prevalidator.protocol_hash old_prevalidator in
if not (Protocol_hash.equal old_protocol new_protocol) then (
safe_get_protocol new_protocol
>>=? fun (module Proto) ->
safe_get_prevalidator_filter new_protocol
>>=? fun (module Filter) ->
let (limits, chain_db) = Prevalidator.parameters old_prevalidator in
(* TODO inject in the new prevalidator the operation
from the previous one. *)
Prevalidator.create limits (module Proto) chain_db
Prevalidator.create limits (module Filter) chain_db
>>= function
| Error errs ->
Log.lwt_log_error
......@@ -447,7 +457,7 @@ let on_launch start_prevalidator w _ parameters =
>>= fun head ->
State.Block.protocol_hash head
>>=? fun head_hash ->
safe_get_protocol head_hash
safe_get_prevalidator_filter head_hash
>>= function
| Ok (module Proto) -> (
Prevalidator.create
......
......@@ -68,6 +68,8 @@ module type T = sig
val status : t -> status tzresult Lwt.t
val validation_state : t -> Proto.validation_state
val pp_result : Format.formatter -> result -> unit
end
......@@ -212,6 +214,8 @@ struct
>>=? fun (block_result, block_metadata) ->
return {block_metadata; block_result; applied_operations = pv.applied}
let validation_state {state; _} = state
let pp_result ppf =
let open Format in
function
......
......@@ -71,6 +71,8 @@ module type T = sig
val status : t -> status tzresult Lwt.t
val validation_state : t -> Proto.validation_state
val pp_result : Format.formatter -> result -> unit
end
......
This diff is collapsed.
......@@ -59,7 +59,7 @@ type limits = {
(** Creates/tear-down a new prevalidator context. *)
val create :
limits ->
(module Registered_protocol.T) ->
(module Prevalidator_filters.FILTER) ->
Distributed_db.chain_db ->
t tzresult Lwt.t
......
(*****************************************************************************)
(* *)
(* Open Source License *)
(* Copyright (c) 2018 Nomadic Development. <[email protected]> *)
(* *)
(* Permission is hereby granted, free of charge, to any person obtaining a *)
(* copy of this software and associated documentation files (the "Software"),*)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)
(* and/or sell copies of the Software, and to permit persons to whom the *)
(* Software is furnished to do so, subject to the following conditions: *)
(* *)
(* The above copyright notice and this permission notice shall be included *)
(* in all copies or substantial portions of the Software. *)
(* *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)
(* DEALINGS IN THE SOFTWARE. *)
(* *)
(*****************************************************************************)
module type FILTER = sig
type config
val config_encoding : config Data_encoding.t
val default_config : config
module Proto : Registered_protocol.T
val pre_filter : config -> Proto.operation_data -> bool
val post_filter :
config ->
validation_state_before:Proto.validation_state ->
validation_state_after:Proto.validation_state ->
Proto.operation_data * Proto.operation_receipt ->
bool Lwt.t
end
module No_filter (Proto : Registered_protocol.T) = struct
type config = unit
let config_encoding = Data_encoding.empty
let default_config = ()
module Proto = Proto
let pre_filter _ _ = true
let post_filter _ ~validation_state_before:_ ~validation_state_after:_ _ =
Lwt.return_true
end
let table : (module FILTER) Protocol_hash.Table.t =
Protocol_hash.Table.create 5
let register (module Filter : FILTER) =
assert (not (Protocol_hash.Table.mem table Filter.Proto.hash)) ;
Protocol_hash.Table.add table Filter.Proto.hash (module Filter)
let find = Protocol_hash.Table.find_opt table
(*****************************************************************************)
(* *)
(* Open Source License *)
(* Copyright (c) 2018 Nomadic Development. <[email protected]> *)
(* *)
(* Permission is hereby granted, free of charge, to any person obtaining a *)
(* copy of this software and associated documentation files (the "Software"),*)
(* to deal in the Software without restriction, including without limitation *)
(* the rights to use, copy, modify, merge, publish, distribute, sublicense, *)
(* and/or sell copies of the Software, and to permit persons to whom the *)
(* Software is furnished to do so, subject to the following conditions: *)
(* *)
(* The above copyright notice and this permission notice shall be included *)
(* in all copies or substantial portions of the Software. *)
(* *)
(* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*)
(* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *)
(* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *)
(* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*)
(* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *)
(* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *)
(* DEALINGS IN THE SOFTWARE. *)
(* *)
(*****************************************************************************)
(** Type of a protocol-specific mempool filter plug-in. *)
module type FILTER = sig
(** Type of protocol-specific mempool configuration, as specifiable
in the node's configuration file, and updatable via RPCs. *)
type config
(** Formatting of {!config} for the configuration file and RPCs. *)
val config_encoding : config Data_encoding.t
(** Default configuration value, used when none is specified. *)
val default_config : config
(** The protocol this plug-in understands. *)
module Proto : Registered_protocol.T
(** Tells if an operation should be kept and propagated before even running it. *)
val pre_filter : config -> Proto.operation_data -> bool
(** Tells if an operation should be kept and propagated considering its result. *)
val post_filter :
config ->
validation_state_before:Proto.validation_state ->
validation_state_after:Proto.validation_state ->
Proto.operation_data * Proto.operation_receipt ->
bool Lwt.t
end
(** Dummy filter that does nothing *)
module No_filter (Proto : Registered_protocol.T) :
FILTER with module Proto = Proto
(** Registers a mempool plug-in for a specific protocol (according to its [Proto.hash]). *)
val register : (module FILTER) -> unit
(** Looks for a mempool plug-in for a specific protocol. *)
val find : Protocol_hash.t -> (module FILTER) option
......@@ -832,6 +832,21 @@ module Make (Proto : PROTO) (Next_proto : PROTO) = struct
~output:(list next_operation_encoding)
RPC_path.(path / "monitor_operations")
let get_filter path =
RPC_service.get_service
~description:"Get the configuration of the mempool filter."
~query:RPC_query.empty
~output:json
RPC_path.(path / "filter")
let set_filter path =
RPC_service.post_service
~description:"Set the configuration of the mempool filter."
~query:RPC_query.empty
~input:json
~output:unit
RPC_path.(path / "filter")
let request_operations path =
RPC_service.post_service
~description:"Request the operations of your peers."
......
......@@ -499,6 +499,14 @@ module Make (Proto : PROTO) (Next_proto : PROTO) : sig
Next_proto.operation list )
RPC_service.t
val get_filter :
('a, 'b) RPC_path.t ->
([`GET], 'a, 'b, unit, unit, Data_encoding.json) RPC_service.t
val set_filter :
('a, 'b) RPC_path.t ->
([`POST], 'a, 'b, unit, Data_encoding.json, unit) RPC_service.t
val request_operations :
('a, 'b) RPC_path.t ->
([`POST], 'a, 'b, unit, unit, unit) RPC_service.t
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment