package octez-protocol-019-PtParisB-libs

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

Source file node_rpc.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
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2020 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.                                                 *)
(*                                                                           *)
(*****************************************************************************)

open Protocol
open Alpha_context
open Baking_cache
open Baking_state
module Block_services = Block_services.Make (Protocol) (Protocol)
module Events = Baking_events.Node_rpc

let inject_block cctxt ?(force = false) ~chain signed_block_header operations =
  let signed_shell_header_bytes =
    Data_encoding.Binary.to_bytes_exn Block_header.encoding signed_block_header
  in
  Shell_services.Injection.block
    ~async:true
    cctxt
    ~chain
    ~force
    signed_shell_header_bytes
    operations

let inject_operation cctxt ~chain operation =
  let encoded_op =
    Data_encoding.Binary.to_bytes_exn Operation.encoding operation
  in
  Shell_services.Injection.operation cctxt ~async:true ~chain encoded_op

let preapply_block cctxt ~chain ~head ~timestamp ~protocol_data operations =
  Block_services.Helpers.Preapply.block
    cctxt
    ~chain
    ~timestamp
    ~block:(`Hash (head, 0))
    operations
    ~protocol_data

let extract_prequorum preattestations =
  match preattestations with
  | h :: _ ->
      let ({protocol_data = {contents = Single (Preattestation content); _}; _})
          =
        (h : Kind.preattestation Operation.t)
      in
      Some
        {
          level = Raw_level.to_int32 content.level;
          round = content.round;
          block_payload_hash = content.block_payload_hash;
          preattestations;
        }
  | _ -> None

let info_of_header_and_ops ~in_protocol block_hash block_header operations =
  let open Result_syntax in
  let shell = block_header.Tezos_base.Block_header.shell in
  let dummy_payload_hash = Block_payload_hash.zero in
  let* round =
    Environment.wrap_tzresult @@ Fitness.round_from_raw shell.fitness
  in
  let payload_hash, payload_round, prequorum, quorum, payload =
    if not in_protocol then
      (* The first block in the protocol is baked using the previous
         protocol, the encodings might change. The baker's logic is to
         consider final the first block of a new protocol and not
         attest it. Therefore, we do not need to have the correct
         values here. *)
      (dummy_payload_hash, Round.zero, None, [], Operation_pool.empty_payload)
    else
      let payload_hash, payload_round =
        match
          Data_encoding.Binary.of_bytes_opt
            Protocol.block_header_data_encoding
            block_header.protocol_data
        with
        | Some {contents = {payload_hash; payload_round; _}; _} ->
            (payload_hash, payload_round)
        | None -> assert false
      in
      let preattestations, quorum, payload =
        WithExceptions.Option.get
          ~loc:__LOC__
          (Operation_pool.extract_operations_of_list_list operations)
      in
      let prequorum = Option.bind preattestations extract_prequorum in
      (payload_hash, payload_round, prequorum, quorum, payload)
  in
  return
    {
      hash = block_hash;
      shell;
      payload_hash;
      payload_round;
      round;
      prequorum;
      quorum;
      payload;
    }

let compute_block_info cctxt ~in_protocol ?operations ~chain block_hash
    block_header =
  let open Lwt_result_syntax in
  let* operations =
    match operations with
    | None when not in_protocol -> return_nil
    | None ->
        let open Protocol_client_context in
        let* operations =
          Alpha_block_services.Operations.operations
            cctxt
            ~chain
            ~block:(`Hash (block_hash, 0))
            ()
        in
        let packed_operations =
          List.map
            (fun l ->
              List.map
                (fun {Alpha_block_services.shell; protocol_data; _} ->
                  {Alpha_context.shell; protocol_data})
                l)
            operations
        in
        return packed_operations
    | Some operations ->
        let parse_op (raw_op : Tezos_base.Operation.t) =
          let protocol_data =
            Data_encoding.Binary.of_bytes_exn
              Operation.protocol_data_encoding
              raw_op.proto
          in
          {shell = raw_op.shell; protocol_data}
        in
        protect @@ fun () -> return (List.map (List.map parse_op) operations)
  in
  let*? block_info =
    info_of_header_and_ops ~in_protocol block_hash block_header operations
  in
  return block_info

let proposal cctxt ?(cache : block_info Block_cache.t option) ?operations ~chain
    block_hash (block_header : Tezos_base.Block_header.t) =
  let open Lwt_result_syntax in
  let predecessor_hash = block_header.shell.predecessor in
  let pred_block = `Hash (predecessor_hash, 0) in
  let predecessor_opt =
    Option.bind cache (fun cache -> Block_cache.find_opt cache predecessor_hash)
  in
  let* is_proposal_in_protocol, predecessor =
    match predecessor_opt with
    | Some predecessor ->
        return
          ( predecessor.shell.proto_level = block_header.shell.proto_level,
            predecessor )
    | None ->
        let* {
               current_protocol = pred_current_protocol;
               next_protocol = pred_next_protocol;
             } =
          Shell_services.Blocks.protocols cctxt ~chain ~block:pred_block ()
        in
        let is_proposal_in_protocol =
          Protocol_hash.(pred_next_protocol = Protocol.hash)
        in
        let* predecessor =
          let in_protocol =
            Protocol_hash.(pred_current_protocol = Protocol.hash)
          in
          let* raw_header_b =
            Shell_services.Blocks.raw_header cctxt ~chain ~block:pred_block ()
          in
          let predecessor_header =
            Data_encoding.Binary.of_bytes_exn
              Tezos_base.Block_header.encoding
              raw_header_b
          in
          compute_block_info
            cctxt
            ~in_protocol
            ~chain
            predecessor_hash
            predecessor_header
        in
        Option.iter
          (fun cache -> Block_cache.replace cache predecessor_hash predecessor)
          cache ;
        return (is_proposal_in_protocol, predecessor)
  in
  let block_opt =
    Option.bind cache (fun cache -> Block_cache.find_opt cache block_hash)
  in
  let* block =
    match block_opt with
    | Some pi -> return pi
    | None ->
        let* pi =
          compute_block_info
            cctxt
            ~in_protocol:is_proposal_in_protocol
            ?operations
            ~chain
            block_hash
            block_header
        in
        Option.iter (fun cache -> Block_cache.replace cache block_hash pi) cache ;
        return pi
  in
  return {block; predecessor}

let proposal cctxt ?cache ?operations ~chain block_hash block_header =
  protect @@ fun () ->
  proposal cctxt ?cache ?operations ~chain block_hash block_header

let monitor_valid_proposals cctxt ~chain ?cache () =
  let open Lwt_result_syntax in
  let next_protocols = [Protocol.hash] in
  let* block_stream, stopper =
    Monitor_services.validated_blocks cctxt ~chains:[chain] ~next_protocols ()
  in
  let stream =
    let map (_chain_id, block_hash, block_header, operations) =
      let*! map_result =
        proposal cctxt ?cache ~operations ~chain block_hash block_header
      in
      match map_result with
      | Ok proposal -> Lwt.return_some proposal
      | Error err ->
          let*! () = Events.(emit error_while_monitoring_valid_proposals err) in
          Lwt.return_none
    in
    Lwt_stream.filter_map_s map block_stream
  in
  return (stream, stopper)

let monitor_heads cctxt ~chain ?cache () =
  let open Lwt_result_syntax in
  let next_protocols = [Protocol.hash] in
  let* block_stream, stopper =
    Monitor_services.heads cctxt ~next_protocols chain
  in
  let stream =
    let map (block_hash, block_header) =
      let*! map_result = proposal cctxt ?cache ~chain block_hash block_header in
      match map_result with
      | Ok proposal -> Lwt.return_some proposal
      | Error err ->
          let*! () = Events.(emit error_while_monitoring_heads err) in
          Lwt.return_none
    in
    Lwt_stream.filter_map_s map block_stream
  in
  return (stream, stopper)

let await_protocol_activation cctxt ~chain () =
  let open Lwt_result_syntax in
  let* block_stream, stop =
    Monitor_services.heads cctxt ~next_protocols:[Protocol.hash] chain
  in
  let*! _ = Lwt_stream.get block_stream in
  stop () ;
  return_unit

let fetch_dal_config cctxt =
  let open Lwt_syntax in
  let* r = Config_services.dal_config cctxt in
  match r with
  | Error e -> return_error e
  | Ok dal_config -> return_ok dal_config

let get_attestable_slots dal_node_rpc_ctxt pkh ~attested_level =
  Tezos_rpc.Context.make_call
    Tezos_dal_node_services.Services.get_attestable_slots
    dal_node_rpc_ctxt
    (((), pkh), attested_level)
    ()
    ()

let register_dal_profiles dal_node_rpc_ctxt delegates =
  let profiles =
    List.map
      (fun consensus_key ->
        Tezos_dal_node_services.Types.Attester consensus_key.public_key_hash)
      delegates
  in
  Tezos_rpc.Context.make_call
    Tezos_dal_node_services.Services.patch_profiles
    dal_node_rpc_ctxt
    ()
    ()
    profiles
OCaml

Innovation. Community. Security.