package tezos-store

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

Source file block_repr.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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2020-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.                                                 *)
(*                                                                           *)
(*****************************************************************************)

open Store_errors

type contents = {
  header : Block_header.t;
  operations : Operation.t list list;
  block_metadata_hash : Block_metadata_hash.t option;
  operations_metadata_hashes : Operation_metadata_hash.t list list option;
}

type metadata = {
  message : string option;
  max_operations_ttl : int;
  last_allowed_fork_level : Int32.t;
  block_metadata : Bytes.t;
  operations_metadata : Block_validation.operation_metadata list list;
}

type legacy_metadata = {
  legacy_message : string option;
  legacy_max_operations_ttl : int;
  legacy_last_allowed_fork_level : Int32.t;
  legacy_block_metadata : Bytes.t;
  legacy_operations_metadata : Bytes.t list list;
}

type legacy_block = {
  legacy_hash : Block_hash.t;
  legacy_contents : contents;
  mutable legacy_metadata : legacy_metadata option;
      (* allows updating metadata field when loading cemented metadata *)
}

type block = {
  hash : Block_hash.t;
  contents : contents;
  mutable metadata : metadata option;
      (* allows updating metadata field when loading cemented metadata *)
}

type t = block

let create_genesis_block ~genesis context =
  let shell : Block_header.shell_header =
    {
      level = 0l;
      proto_level = 0;
      predecessor = genesis.Genesis.block;
      (* genesis' predecessor is genesis *)
      timestamp = genesis.Genesis.time;
      fitness = [];
      validation_passes = 0;
      operations_hash = Operation_list_list_hash.empty;
      context;
    }
  in
  let header : Block_header.t = {shell; protocol_data = Bytes.create 0} in
  let contents =
    {
      header;
      operations = [];
      block_metadata_hash = None;
      operations_metadata_hashes = None;
    }
  in
  let metadata =
    Some
      {
        message = Some "Genesis";
        max_operations_ttl = 0;
        last_allowed_fork_level = 0l;
        block_metadata = Bytes.create 0;
        operations_metadata = [];
      }
  in
  {hash = genesis.block; contents; metadata}

let contents_encoding =
  let open Data_encoding in
  def "store.block_repr.contents"
  @@ conv
       (fun {
              header;
              operations;
              block_metadata_hash;
              operations_metadata_hashes;
            } ->
         (header, operations, block_metadata_hash, operations_metadata_hashes))
       (fun (header, operations, block_metadata_hash, operations_metadata_hashes)
            ->
         {header; operations; block_metadata_hash; operations_metadata_hashes})
       (obj4
          (req "header" (dynamic_size Block_header.encoding))
          (req "operations" (list (list (dynamic_size Operation.encoding))))
          (opt "block_metadata_hash" Block_metadata_hash.encoding)
          (opt
             "operations_metadata_hashes"
             (list (list Operation_metadata_hash.encoding))))

let metadata_encoding : metadata Data_encoding.t =
  let open Data_encoding in
  def "store.block_repr.metadata"
  @@ conv
       (fun {
              message;
              max_operations_ttl;
              last_allowed_fork_level;
              block_metadata;
              operations_metadata;
            } ->
         ( message,
           max_operations_ttl,
           last_allowed_fork_level,
           block_metadata,
           operations_metadata ))
       (fun ( message,
              max_operations_ttl,
              last_allowed_fork_level,
              block_metadata,
              operations_metadata ) ->
         {
           message;
           max_operations_ttl;
           last_allowed_fork_level;
           block_metadata;
           operations_metadata;
         })
       (obj5
          (opt "message" string)
          (req "max_operations_ttl" uint16)
          (req "last_allowed_fork_level" int32)
          (req "block_metadata" bytes)
          (req
             "operations_metadata"
             (list (list Block_validation.operation_metadata_encoding))))

let legacy_metadata_encoding : legacy_metadata Data_encoding.t =
  let open Data_encoding in
  def "store.block_repr.legacy_metadata"
  @@ conv
       (fun {
              legacy_message;
              legacy_max_operations_ttl;
              legacy_last_allowed_fork_level;
              legacy_block_metadata;
              legacy_operations_metadata;
            } ->
         ( legacy_message,
           legacy_max_operations_ttl,
           legacy_last_allowed_fork_level,
           legacy_block_metadata,
           legacy_operations_metadata ))
       (fun ( legacy_message,
              legacy_max_operations_ttl,
              legacy_last_allowed_fork_level,
              legacy_block_metadata,
              legacy_operations_metadata ) ->
         {
           legacy_message;
           legacy_max_operations_ttl;
           legacy_last_allowed_fork_level;
           legacy_block_metadata;
           legacy_operations_metadata;
         })
       (obj5
          (opt "legacy_message" string)
          (req "legacy_max_operations_ttl" uint16)
          (req "legacy_last_allowed_fork_level" int32)
          (req "legacy_block_metadata" bytes)
          (req "legacy_operations_metadata" (list (list bytes))))

let encoding =
  let open Data_encoding in
  def "store.block_repr"
  @@ conv
       (fun {hash; contents; metadata} -> (hash, contents, metadata))
       (fun (hash, contents, metadata) -> {hash; contents; metadata})
       (dynamic_size
          ~kind:`Uint30
          (obj3
             (req "hash" Block_hash.encoding)
             (req "contents" contents_encoding)
             (varopt "metadata" metadata_encoding)))

let legacy_encoding =
  let open Data_encoding in
  def "store.legacy_block_repr"
  @@ conv
       (fun {legacy_hash; legacy_contents; legacy_metadata} ->
         (legacy_hash, legacy_contents, legacy_metadata))
       (fun (legacy_hash, legacy_contents, legacy_metadata) ->
         {legacy_hash; legacy_contents; legacy_metadata})
       (dynamic_size
          ~kind:`Uint30
          (obj3
             (req "legacy_hash" Block_hash.encoding)
             (req "legacy_contents" contents_encoding)
             (varopt "legacy_metadata" legacy_metadata_encoding)))

let with_contents
    {header; operations; block_metadata_hash; operations_metadata_hashes} f =
  f header operations block_metadata_hash operations_metadata_hashes
  [@@ocaml.inline]

let with_metadata
    {
      message;
      max_operations_ttl;
      last_allowed_fork_level;
      block_metadata;
      operations_metadata;
    } f =
  f
    message
    max_operations_ttl
    last_allowed_fork_level
    block_metadata
    operations_metadata
  [@@ocaml.inline]

let contents_equal c1 c2 =
  with_contents c1 @@ fun h1 o1 b1 omh1 ->
  with_contents c2 @@ fun h2 o2 b2 omh2 ->
  Block_header.equal h1 h2
  && List.equal (List.equal Operation.equal) o1 o2
  && Option.equal Block_metadata_hash.equal b1 b2
  && Option.equal
       (List.equal (List.equal Operation_metadata_hash.equal))
       omh1
       omh2

let metadata_equal m1 m2 =
  with_metadata m1 @@ fun m1 mot1 lafl1 bm1 om1 ->
  with_metadata m2 @@ fun m2 mot2 lafl2 bm2 om2 ->
  Option.equal String.equal m1 m2
  && Int.equal mot1 mot2 && Int32.equal lafl1 lafl2 && Bytes.equal bm1 bm2
  && List.equal (List.equal Block_validation.operation_metadata_equal) om1 om2

let equal b1 b2 =
  let {hash = h1; contents = c1; metadata = m1} = b1 in
  let {hash = h2; contents = c2; metadata = m2} = b2 in
  Block_hash.equal h1 h2 && contents_equal c1 c2
  && Option.equal metadata_equal m1 m2

let pp_json fmt b =
  let json = Data_encoding.Json.construct encoding b in
  Data_encoding.Json.pp fmt json

(* Contents accessors *)

let descriptor blk = (blk.hash, blk.contents.header.Block_header.shell.level)

let hash blk = blk.hash

let header blk = blk.contents.header

let operations blk = blk.contents.operations

let block_metadata_hash blk = blk.contents.block_metadata_hash

let operations_metadata_hashes blk = blk.contents.operations_metadata_hashes

let shell_header blk = blk.contents.header.Block_header.shell

let level blk = blk.contents.header.Block_header.shell.level

let proto_level blk = blk.contents.header.Block_header.shell.proto_level

let predecessor blk = blk.contents.header.Block_header.shell.predecessor

let timestamp blk = blk.contents.header.Block_header.shell.timestamp

let validation_passes blk =
  blk.contents.header.Block_header.shell.validation_passes

let operations_hash blk = blk.contents.header.Block_header.shell.operations_hash

let fitness blk = blk.contents.header.Block_header.shell.fitness

let context blk = blk.contents.header.Block_header.shell.context

let protocol_data blk = blk.contents.header.Block_header.protocol_data

(* Metadata accessors *)

let metadata blk = blk.metadata

let message metadata = metadata.message

let max_operations_ttl metadata = metadata.max_operations_ttl

let last_allowed_fork_level metadata = metadata.last_allowed_fork_level

let block_metadata metadata = metadata.block_metadata

let operations_metadata metadata = metadata.operations_metadata

let check_block_consistency ?genesis_hash ?pred_block block =
  let open Lwt_result_syntax in
  let block_header = header block in
  let block_hash = hash block in
  let result_hash = Block_header.hash block_header in
  let* () =
    fail_unless
      (Block_hash.equal block_hash result_hash
      ||
      match genesis_hash with
      | Some genesis_hash -> Block_hash.equal block_hash genesis_hash
      | None -> false)
      (Inconsistent_block_hash
         {
           level = level block;
           expected_hash = block_hash;
           computed_hash = result_hash;
         })
  in
  let* () =
    match pred_block with
    | None -> return_unit
    | Some pred_block ->
        fail_unless
          (Block_hash.equal (hash pred_block) (predecessor block)
          && Compare.Int32.(level block = Int32.succ (level pred_block)))
          (Inconsistent_block_predecessor
             {
               block_hash;
               level = level block;
               expected_hash = hash pred_block;
               computed_hash = predecessor block;
             })
  in
  let computed_operations_hash =
    Operation_list_list_hash.compute
      (List.map
         Operation_list_hash.compute
         (List.map (List.map Operation.hash) (operations block)))
  in
  let* () =
    fail_unless
      (Operation_list_list_hash.equal
         computed_operations_hash
         (operations_hash block))
      (Store_errors.Inconsistent_operations_hash
         {expected = operations_hash block; got = computed_operations_hash})
  in
  return_unit

let convert_legacy_metadata (legacy_metadata : legacy_metadata) : metadata =
  let {
    legacy_message;
    legacy_max_operations_ttl;
    legacy_last_allowed_fork_level;
    legacy_block_metadata;
    legacy_operations_metadata;
  } =
    legacy_metadata
  in
  {
    message = legacy_message;
    max_operations_ttl = legacy_max_operations_ttl;
    last_allowed_fork_level = legacy_last_allowed_fork_level;
    block_metadata = legacy_block_metadata;
    operations_metadata =
      List.map
        (List.map (fun b -> Block_validation.Metadata b))
        legacy_operations_metadata;
  }

let decode_metadata b =
  Data_encoding.Binary.of_string_opt metadata_encoding b |> function
  | Some metadata -> Some metadata
  | None ->
      Option.map
        convert_legacy_metadata
        (Data_encoding.Binary.of_string_opt legacy_metadata_encoding b)
OCaml

Innovation. Community. Security.