Source file shell_plugin.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2018 Nomadic Development. <contact@tezcore.com>             *)
(* Copyright (c) 2018-2022 Nomadic Labs, <contact@nomadic-labs.com>          *)
(*                                                                           *)
(* 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
  module Proto : Registered_protocol.T

  module Mempool : sig
    type config

    val config_encoding : config Data_encoding.t

    val default_config : config

    type state

    val init :
      Tezos_protocol_environment.Context.t ->
      head:Tezos_base.Block_header.shell_header ->
      state tzresult Lwt.t

    val flush :
      state -> head:Tezos_base.Block_header.shell_header -> state tzresult Lwt.t

    val remove : filter_state:state -> Operation_hash.t -> state

    val syntactic_check : Proto.operation -> [`Well_formed | `Ill_formed] Lwt.t

    val pre_filter :
      config ->
      filter_state:state ->
      Proto.operation ->
      [ `Passed_prefilter of [`High | `Medium | `Low of Q.t list]
      | `Branch_delayed of tztrace
      | `Branch_refused of tztrace
      | `Refused of tztrace
      | `Outdated of tztrace ]
      Lwt.t

    val add_operation_and_enforce_mempool_bound :
      ?replace:Operation_hash.t ->
      config ->
      state ->
      Operation_hash.t * Proto.operation ->
      ( state
        * [ `No_replace
          | `Replace of
            Operation_hash.t
            * [ `Branch_delayed of tztrace
              | `Branch_refused of tztrace
              | `Refused of tztrace
              | `Outdated of tztrace ] ],
        [ `Branch_delayed of tztrace
        | `Branch_refused of tztrace
        | `Refused of tztrace
        | `Outdated of tztrace ] )
      result
      Lwt.t

    val conflict_handler : config -> Proto.Mempool.conflict_handler
  end
end

module type RPC = sig
  module Proto : Registered_protocol.T

  val rpc_services :
    Tezos_protocol_environment.rpc_context Tezos_rpc.Directory.directory
end

module No_filter (Proto : Registered_protocol.T) :
  FILTER with module Proto = Proto and type Mempool.state = unit = struct
  module Proto = Proto

  module Mempool = struct
    type config = unit

    let config_encoding = Data_encoding.empty

    let default_config = ()

    type state = unit

    let init _ ~head:_ = Lwt_result_syntax.return_unit

    let remove ~filter_state _ = filter_state

    let flush _ ~head:_ = Lwt_result_syntax.return_unit

    let syntactic_check _ = Lwt.return `Well_formed

    let pre_filter _ ~filter_state:_ _ =
      Lwt.return @@ `Passed_prefilter (`Low [])

    let add_operation_and_enforce_mempool_bound ?replace:_ _ filter_state _ =
      Lwt_result.return (filter_state, `No_replace)

    let conflict_handler _ ~existing_operation ~new_operation =
      if Proto.compare_operations existing_operation new_operation < 0 then
        `Replace
      else `Keep
  end
end

module type METRICS = sig
  val hash : Protocol_hash.t

  val update_metrics :
    protocol_metadata:bytes ->
    Fitness.t ->
    (cycle:float -> consumed_gas:float -> round:float -> unit) ->
    unit Lwt.t
end

module Undefined_metrics_plugin (Proto : sig
  val hash : Protocol_hash.t
end) =
struct
  let hash = Proto.hash

  let update_metrics ~protocol_metadata:_ _ _ = Lwt.return_unit
end

let rpc_table : (module RPC) Protocol_hash.Table.t =
  Protocol_hash.Table.create 5

let metrics_table : (module METRICS) Protocol_hash.Table.t =
  Protocol_hash.Table.create 5

let register_rpc (module Rpc : RPC) =
  assert (not (Protocol_hash.Table.mem rpc_table Rpc.Proto.hash)) ;
  Protocol_hash.Table.add rpc_table Rpc.Proto.hash (module Rpc)

let register_metrics (module Metrics : METRICS) =
  Protocol_hash.Table.replace metrics_table Metrics.hash (module Metrics)

let find_rpc = Protocol_hash.Table.find rpc_table

let find_metrics = Protocol_hash.Table.find metrics_table

let safe_find_metrics hash =
  match find_metrics hash with
  | Some proto_metrics -> Lwt.return proto_metrics
  | None ->
      let module Metrics = Undefined_metrics_plugin (struct
        let hash = hash
      end) in
      Lwt.return (module Metrics : METRICS)

let filter_table : (module FILTER) Protocol_hash.Table.t =
  Protocol_hash.Table.create 5

let add_to_filter_table proto_hash filter =
  assert (not (Protocol_hash.Table.mem filter_table proto_hash)) ;
  Protocol_hash.Table.add filter_table proto_hash filter

let register_filter (module Filter : FILTER) =
  add_to_filter_table Filter.Proto.hash (module Filter)

let validator_filter_not_found =
  Internal_event.Simple.declare_1
    ~section:["block"; "validation"]
    ~name:"protocol_filter_not_found"
    ~msg:"no protocol filter found for protocol {protocol_hash}"
    ~level:Warning
    ~pp1:Protocol_hash.pp
    ("protocol_hash", Protocol_hash.encoding)

let find_filter ~block_hash protocol_hash =
  let open Lwt_result_syntax in
  match Protocol_hash.Table.find filter_table protocol_hash with
  | Some filter -> return filter
  | None -> (
      match Registered_protocol.get protocol_hash with
      | None ->
          tzfail
            (Block_validator_errors.Unavailable_protocol
               {block = block_hash; protocol = protocol_hash})
      | Some (module Proto : Registered_protocol.T) ->
          let*! () =
            match Proto.environment_version with
            | V0 ->
                (* This is normal for protocols Genesis and 000
                   because they don't have a plugin. *)
                Lwt.return_unit
            | _ ->
                Internal_event.Simple.(emit validator_filter_not_found)
                  protocol_hash
          in
          return (module No_filter (Proto) : FILTER))