package octez-shell-libs

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

Source file distributed_db_requester.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
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com>     *)
(* Copyright (c) 2019-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 Message = Distributed_db_message
module Requester_event = Distributed_db_event.Requester_event

type 'a request_param = {
  p2p : (Message.t, Peer_metadata.t, Connection_metadata.t) P2p.t;
  data : 'a;
  active : unit -> P2p_peer.Set.t;
  send : P2p_peer.Id.t -> Message.t -> unit;
}

module type EXTENDED_REQUESTER = sig
  include Requester.FULL_REQUESTER

  val state_of_t :
    t -> Chain_validator_worker_state.Distributed_db_state.table_scheduler
end

module type EXTENDED_REQUESTER_2 = sig
  include EXTENDED_REQUESTER

  val clear_all : t -> Block_hash.t -> int -> unit
end

module type REQUEST_MESSAGE = sig
  type param

  type hash

  val max_length : int

  val initial_delay : Time.System.Span.t

  val forge : param -> hash list -> Message.t
end

module Make_raw
    (Hash : Requester.HASH)
    (Disk_table : Requester.DISK_TABLE with type key := Hash.t)
    (Memory_table : Hashtbl.SeededS with type key := Hash.t)
    (Request_message : REQUEST_MESSAGE with type hash := Hash.t)
    (Probe : Requester.PROBE
               with type key := Hash.t
                and type value := Disk_table.value) :
  EXTENDED_REQUESTER
    with type key = Hash.t
     and type value = Disk_table.value
     and type request_param = Request_message.param request_param
     and type store = Disk_table.store
     and type param = Probe.param
     and type notified_value = Probe.notified_value = struct
  module Request = struct
    type param = Request_message.param request_param

    let active {active; _} = active ()

    let initial_delay = Request_message.initial_delay

    let rec send state gid keys =
      let first_keys, keys = List.split_n Request_message.max_length keys in
      let msg = Request_message.forge state.data first_keys in
      state.send gid msg ;
      let open Peer_metadata in
      let (req : requests_kind) =
        match msg with
        | Get_current_branch _ -> Branch
        | Get_current_head _ -> Head
        | Get_block_headers _ -> Block_header
        | Get_operations _ -> Operations
        | Get_protocols _ -> Protocols
        | Get_operations_for_blocks _ -> Operations_for_block
        | _ -> Other
      in
      let meta = P2p.get_peer_metadata state.p2p gid in
      Peer_metadata.incr meta @@ Scheduled_request req ;
      if keys <> [] then send state gid keys
  end

  module Monitored_memory_table = struct
    type 'a t = {
      table : 'a Memory_table.t;
      metrics : Shell_metrics.Distributed_db.t;
    }

    let create ~entry_type ?random s =
      {
        table = Memory_table.create ?random s;
        metrics = Shell_metrics.Distributed_db.init ~kind:Hash.name ~entry_type;
      }

    let find t x = Memory_table.find t.table x

    let add t k x =
      Memory_table.add t.table k x ;
      Shell_metrics.Distributed_db.update
        t.metrics
        ~length:(Memory_table.length t.table)

    let replace t k x =
      Memory_table.replace t.table k x ;
      Shell_metrics.Distributed_db.update
        t.metrics
        ~length:(Memory_table.length t.table)

    let remove t k =
      Memory_table.remove t.table k ;
      Shell_metrics.Distributed_db.update
        t.metrics
        ~length:(Memory_table.length t.table)

    let length t = Memory_table.length t.table

    let fold f t x = Memory_table.fold f t.table x
  end

  module Table =
    Requester.Make (Hash) (Disk_table) (Monitored_memory_table) (Request)
      (Probe)
  include Table

  let state_of_t t =
    let table_length = Table.memory_table_length t in
    let scheduler_length = Table.pending_requests t in
    {
      Chain_validator_worker_state.Distributed_db_state.table_length;
      scheduler_length;
    }

  let create ?random_table ?global_input request_param disk =
    Table.create ?random_table ?global_input request_param disk

  let shutdown t =
    let open Lwt_syntax in
    let* () = Requester_event.(emit shutting_down_requester) () in
    Table.shutdown t
end

module Fake_operation_storage = struct
  type store = Store.chain_store

  type value = Operation.t

  let known _ _ = Lwt.return_false

  let read _ _ = fail_with_exn Not_found

  let read_opt _ _ = Lwt.return_none
end

module Raw_operation =
  Make_raw
    (struct
      include Operation_hash

      let name = "operation"
    end)
    (Fake_operation_storage)
    (Operation_hash.Table)
    (struct
      type param = unit

      let max_length = 10

      let initial_delay = Time.System.Span.of_seconds_exn 0.5

      let forge () keys = Message.Get_operations keys
    end)
    (struct
      type param = unit

      type notified_value = Operation.t

      let probe _ _ v = Some v
    end)

module Block_header_storage = struct
  type store = Store.chain_store

  type value = Block_header.t

  let known chain_store hash =
    let open Lwt_syntax in
    let* b = Store.Block.is_known_valid chain_store hash in
    match b with
    | true -> Lwt.return_true
    | false -> Store.Block.is_known_validated chain_store hash

  let read chain_store h =
    let open Lwt_result_syntax in
    let* b =
      let*! r = Store.Block.read_block chain_store h in
      match r with
      | Ok b -> return b
      | Error _ -> Store.Block.read_validated_block chain_store h
    in
    return (Store.Block.header b)

  let read_opt chain_store h =
    let open Lwt_syntax in
    let* b =
      let* o = Store.Block.read_block_opt chain_store h in
      match o with
      | Some b -> Lwt.return_some b
      | None -> Store.Block.read_validated_block_opt chain_store h
    in
    Lwt.return (Option.map Store.Block.header b)
end

module Raw_block_header =
  Make_raw
    (struct
      include Block_hash

      let name = "block_header"
    end)
    (Block_header_storage)
    (Block_hash.Table)
    (struct
      type param = unit

      let max_length = 10

      let initial_delay = Time.System.Span.of_seconds_exn 0.5

      let forge () keys = Message.Get_block_headers keys
    end)
    (struct
      type param = unit

      type notified_value = Block_header.t

      let probe _ _ v = Some v
    end)

module Operations_table = Hashtbl.MakeSeeded (struct
  type t = Block_hash.t * int

  (* See [src/lib_base/tzPervasives.ml] for an explanation *)
  [@@@ocaml.warning "-32"]

  let hash = Hashtbl.seeded_hash

  let seeded_hash = Hashtbl.seeded_hash

  [@@@ocaml.warning "+32"]

  let equal (b1, i1) (b2, i2) = Block_hash.equal b1 b2 && i1 = i2
end)

module Operations_storage = struct
  type store = Store.chain_store

  type value = Operation.t list

  let known chain_store (h, _) = Store.Block.is_known_valid chain_store h

  let read chain_store (h, i) =
    let open Lwt_result_syntax in
    let* b = Store.Block.read_block chain_store h in
    let ops =
      List.nth (Store.Block.operations b) i
      |> WithExceptions.Option.to_exn ~none:Not_found
    in
    return ops

  let read_opt chain_store (h, i) =
    let open Lwt_syntax in
    let* o = Store.Block.read_block_opt chain_store h in
    match o with
    | None -> Lwt.return_none
    | Some b -> Lwt.return (List.nth (Store.Block.operations b) i)
end

module Raw_operations = struct
  include
    Make_raw
      (struct
        type t = Block_hash.t * int

        let name = "operations"

        let pp ppf (h, n) = Format.fprintf ppf "%a:%d" Block_hash.pp h n

        let encoding =
          let open Data_encoding in
          obj2 (req "block" Block_hash.encoding) (req "index" uint16)
      end)
      (Operations_storage)
      (Operations_table)
      (struct
        type param = unit

        let max_length = 10

        let initial_delay = Time.System.Span.of_seconds_exn 1.

        let forge () keys = Message.Get_operations_for_blocks keys
      end)
      (struct
        type param = Operation_list_list_hash.t

        type notified_value = Operation.t list * Operation_list_list_hash.path

        let probe (_block, expected_ofs) expected_hash (ops, path) =
          let received_hash, received_ofs =
            Operation_list_list_hash.check_path
              path
              (Operation_list_hash.compute (List.map Operation.hash ops))
          in
          if
            received_ofs = expected_ofs
            && Operation_list_list_hash.compare expected_hash received_hash = 0
          then Some ops
          else None
      end)

  let clear_all table hash n =
    List.iter (fun i -> clear_or_cancel table (hash, i)) (0 -- (n - 1))
end

module Protocol_storage = struct
  type store = Store.store

  type value = Protocol.t

  let known store ph = Lwt.return (Store.Protocol.mem store ph)

  let read_opt store ph = Store.Protocol.read store ph

  let read store ph =
    let open Lwt_syntax in
    let* o = read_opt store ph in
    match o with None -> fail_with_exn Not_found | Some p -> return_ok p
end

module Raw_protocol =
  Make_raw
    (struct
      include Protocol_hash

      let name = "protocol"
    end)
    (Protocol_storage)
    (Protocol_hash.Table)
    (struct
      type param = unit

      let initial_delay = Time.System.Span.of_seconds_exn 10.

      let max_length = 10

      let forge () keys = Message.Get_protocols keys
    end)
    (struct
      type param = unit

      type notified_value = Protocol.t

      let probe _ _ v = Some v
    end)
OCaml

Innovation. Community. Security.