package octez-shell-libs

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file prevalidator_worker_state.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
213
214
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com>     *)
(* Copyright (c) 2018-2021 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 Request = struct
  type ('a, 'b) t =
    | Flush :
        Block_hash.t
        * Chain_validator_worker_state.update
        * Block_hash.Set.t
        * Operation_hash.Set.t
        -> (unit, error trace) t
    | Notify : P2p_peer.Id.t * Mempool.t -> (unit, Empty.t) t
    | Leftover : (unit, Empty.t) t
    | Inject : {op : Operation.t; force : bool} -> (unit, error trace) t
    | Arrived : Operation_hash.t * Operation.t -> (unit, Empty.t) t
    | Advertise : (unit, Empty.t) t
    | Ban : Operation_hash.t -> (unit, error trace) t

  type view = View : _ t -> view

  let view req = View req

  let encoding =
    let open Data_encoding in
    union
      [
        case
          (Tag 0)
          ~title:"Flush"
          (obj3
             (req "request" (constant "flush"))
             (req "block" Block_hash.encoding)
             (req "event" Chain_validator_worker_state.update_encoding))
          (function
            | View (Flush (hash, event, _, _)) -> Some ((), hash, event)
            | _ -> None)
          (fun ((), hash, event) ->
            View
              (Flush
                 (hash, event, Block_hash.Set.empty, Operation_hash.Set.empty)));
        case
          (Tag 1)
          ~title:"Notify"
          (obj3
             (req "request" (constant "notify"))
             (req "peer" P2p_peer.Id.encoding)
             (req "mempool" Mempool.encoding))
          (function
            | View (Notify (peer, mempool)) -> Some ((), peer, mempool)
            | _ -> None)
          (fun ((), peer, mempool) -> View (Notify (peer, mempool)));
        case
          (Tag 2)
          ~title:"Inject"
          (obj3
             (req "request" (constant "inject"))
             (req "operation" Operation.encoding)
             (req "force" bool))
          (function
            | View (Inject {op; force}) -> Some ((), op, force) | _ -> None)
          (fun ((), op, force) -> View (Inject {op; force}));
        case
          (Tag 3)
          ~title:"Arrived"
          (obj3
             (req "request" (constant "arrived"))
             (req "operation_hash" Operation_hash.encoding)
             (req "operation" Operation.encoding))
          (function
            | View (Arrived (oph, op)) -> Some ((), oph, op) | _ -> None)
          (fun ((), oph, op) -> View (Arrived (oph, op)));
        case
          (Tag 4)
          ~title:"Advertise"
          (obj1 (req "request" (constant "advertise")))
          (function View Advertise -> Some () | _ -> None)
          (fun () -> View Advertise);
        case
          (Tag 5)
          ~title:"Leftover"
          (obj1 (req "request" (constant "leftover")))
          (function View Leftover -> Some () | _ -> None)
          (fun () -> View Leftover);
        case
          (Tag 6)
          ~title:"Ban"
          (obj2
             (req "request" (constant "ban"))
             (req "operation_hash" Operation_hash.encoding))
          (function View (Ban oph) -> Some ((), oph) | _ -> None)
          (fun ((), oph) -> View (Ban oph));
      ]

  let pp ppf (View r) =
    match r with
    | Flush (hash, _, _, _) ->
        Format.fprintf ppf "switching to new head %a" Block_hash.pp hash
    | Notify (id, {Mempool.known_valid; pending}) ->
        Format.fprintf
          ppf
          "@[<v 2>notified by %a of operations"
          P2p_peer.Id.pp
          id ;
        Operation_hash.Set.iter
          (fun oph ->
            Format.fprintf ppf "@,%a (known_valid)" Operation_hash.pp oph)
          known_valid ;
        Operation_hash.Set.iter
          (fun oph -> Format.fprintf ppf "@,%a (pending)" Operation_hash.pp oph)
          pending ;
        Format.fprintf ppf "@]"
    | Leftover -> Format.fprintf ppf "process next batch of operation"
    | Inject {op; force} ->
        Format.fprintf
          ppf
          "injecting operation %a (force:%b)"
          Operation_hash.pp
          (Operation.hash op)
          force
    | Arrived (oph, _) ->
        Format.fprintf ppf "operation %a arrived" Operation_hash.pp oph
    | Advertise -> Format.fprintf ppf "advertising pending operations"
    | Ban oph -> Format.fprintf ppf "banning operation %a" Operation_hash.pp oph
end

module Operation_encountered = struct
  type situation =
    | Injected
    | Arrived
    | Notified of P2p_peer_id.t option
    | Other

  type t = situation * Operation_hash.t

  let encoding =
    let open Data_encoding in
    union
      ~tag_size:`Uint8
      [
        case
          (Tag 0)
          ~title:"injected"
          (obj2
             (req "situation" (constant "injected"))
             (req "operation" Operation_hash.encoding))
          (function Injected, oph -> Some ((), oph) | _ -> None)
          (fun ((), oph) -> (Injected, oph));
        case
          (Tag 1)
          ~title:"arrived"
          (obj2
             (req "situation" (constant "arrived"))
             (req "operation" Operation_hash.encoding))
          (function Arrived, oph -> Some ((), oph) | _ -> None)
          (fun ((), oph) -> (Arrived, oph));
        case
          (Tag 2)
          ~title:"notified"
          (obj3
             (req "situation" (constant "notified"))
             (req "operation" Operation_hash.encoding)
             (req "peer" (option P2p_peer_id.encoding)))
          (function Notified peer, oph -> Some ((), oph, peer) | _ -> None)
          (fun ((), oph, peer) -> (Notified peer, oph));
        case
          (Tag 3)
          ~title:"other"
          (obj2
             (req "situation" (constant "other"))
             (req "operation" Operation_hash.encoding))
          (function Other, hash -> Some ((), hash) | _ -> None)
          (fun ((), oph) -> (Other, oph));
      ]

  let situation_pp ppf = function
    | Injected -> Format.fprintf ppf "injected"
    | Arrived -> Format.fprintf ppf "arrived"
    | Notified None -> Format.fprintf ppf "notified from the previous run"
    | Notified (Some pid) ->
        Format.fprintf ppf "notified from %a" P2p_peer_id.pp pid
    | Other -> Format.fprintf ppf "encountered"

  let pp ppf (situation, oph) =
    Format.fprintf
      ppf
      "operation %a: %a"
      situation_pp
      situation
      Operation_hash.pp
      oph
end
OCaml

Innovation. Community. Security.