package tezos-protocol-demo-counter

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

Source file main.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
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.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.                                                 *)
(*                                                                           *)
(*****************************************************************************)

let max_block_length = 100

let max_operation_data_length = 100

let validation_passes = Updater.[{max_size = 1000; max_op = None}]

let acceptable_pass _op = Some 0

type block_header_data = Header.t

type block_header = {
  shell : Block_header.shell_header;
  protocol_data : block_header_data;
}

let block_header_data_encoding = Header.encoding

type block_header_metadata = State.t

let block_header_metadata_encoding_with_legacy_attestation_name =
  State.encoding

let block_header_metadata_encoding = State.encoding

type operation_data = Proto_operation.t

let operation_data_encoding = Proto_operation.encoding

let operation_data_encoding_with_legacy_attestation_name =
  operation_data_encoding

type operation_receipt = Receipt.t

let operation_receipt_encoding = Receipt.encoding

let operation_receipt_encoding_with_legacy_attestation_name =
  operation_receipt_encoding

let operation_data_and_receipt_encoding =
  (* we could merge data and receipt encoding for a lighter json *)
  Data_encoding.(
    obj2 (req "data" Proto_operation.encoding) (req "receipt" Receipt.encoding))

let operation_data_and_receipt_encoding_with_legacy_attestation_name =
  operation_data_and_receipt_encoding

type operation = {
  shell : Operation.shell_header;
  protocol_data : operation_data;
}

type validation_state = {context : Context.t; fitness : Fitness.t}

type application_state = validation_state

type mode =
  | Application of block_header
  | Partial_validation of block_header
  | Construction of {
      predecessor_hash : Block_hash.t;
      timestamp : Time.t;
      block_header_data : block_header_data;
    }
  | Partial_construction of {
      predecessor_hash : Block_hash.t;
      timestamp : Time.t;
    }

let mode_str = function
  | Application _ -> "application"
  | Partial_validation _ -> "partial_validation"
  | Construction _ -> "construction"
  | Partial_construction _ -> "partial_construction"

let validation_or_application_str = function
  | `Validation -> "validation"
  | `Application -> "application"

let begin_any_application_mode validation_or_application mode context
    ~(predecessor : Block_header.shell_header) (block_header : block_header) =
  let fitness = block_header.shell.fitness in
  Logging.log
    Notice
    "begin_%s (%s mode): pred_fitness = %a  block_fitness = %a%!"
    (validation_or_application_str validation_or_application)
    (mode_str mode)
    Fitness.pp
    predecessor.fitness
    Fitness.pp
    fitness ;
  (* Note: Logging is only available for debugging purposes and should
     not appear in a real protocol. *)
  return {context; fitness}

(* we use here the same fitness format than proto alpha,
   but with higher [version_number] to allow testing
   migration from alpha to demo_counter. *)
let version_number = "\255"

let int64_to_bytes i =
  let b = Bytes.make 8 '\000' in
  TzEndian.set_int64 b 0 i ;
  b

let fitness_from_level level =
  [
    Bytes.of_string version_number;
    Bytes.of_string "\000";
    Bytes.of_string "\000";
    Bytes.of_string "\000";
    int64_to_bytes level;
  ]

let begin_any_construction_mode validation_or_application mode context
    ~(predecessor : Block_header.shell_header) =
  let fitness = fitness_from_level Int64.(succ (of_int32 predecessor.level)) in
  Logging.log
    Notice
    "begin_%s (%s mode): pred_fitness = %a  constructed fitness = %a%!"
    (validation_or_application_str validation_or_application)
    (mode_str mode)
    Fitness.pp
    predecessor.fitness
    Fitness.pp
    fitness ;
  return {context; fitness}

let begin_validation_or_application validation_or_application ctxt _chain_id
    mode ~predecessor =
  match mode with
  | Application block_header | Partial_validation block_header ->
      begin_any_application_mode
        validation_or_application
        mode
        ctxt
        ~predecessor
        block_header
  | Construction _ | Partial_construction _ ->
      begin_any_construction_mode
        validation_or_application
        mode
        ctxt
        ~predecessor

let begin_validation = begin_validation_or_application `Validation

let begin_application = begin_validation_or_application `Application

let apply_operation_aux application_state operation =
  let open Lwt_result_syntax in
  let {context; fitness} = application_state in
  let*! state = State.get_state context in
  match Apply.apply state operation.protocol_data with
  | None -> Error_monad.tzfail Error.Invalid_operation
  | Some state ->
    let*! context = State.update_state context state in
    return {context; fitness}

let validate_operation ?check_signature:_ validation_state _oph operation =
  Logging.log Notice "validate_operation" ;
  apply_operation_aux validation_state operation

let apply_operation application_state _oph operation =
  let open Lwt_result_syntax in
  Logging.log Notice "apply_operation" ;
  let* application_state = apply_operation_aux application_state operation in
  let receipt = Receipt.create "operation applied successfully" in
  return (application_state, receipt)

let log_finalize validation_or_application validation_state =
  Logging.log
    Notice
    "finalize_%s: fitness = %a%!"
    (validation_or_application_str validation_or_application)
    Fitness.pp
    validation_state.fitness

let finalize_validation validation_state =
  log_finalize `Validation validation_state ;
  return_unit

let finalize_application application_state _shell_header =
  let open Lwt_result_syntax in
  log_finalize `Application application_state ;
  let fitness = application_state.fitness in
  let message = Some (Format.asprintf "fitness <- %a" Fitness.pp fitness) in
  let context = application_state.context in
  let*! state = State.get_state context in
  return
    ( {
        Updater.message;
        context;
        fitness;
        max_operations_ttl = 0;
        last_finalized_block_level = 0l;
        last_preserved_block_level = 0l;
      },
      state )

let decode_json json =
  match Proto_params.from_json json with
  | exception _ ->
    tzfail Error.Invalid_protocol_parameters
  | proto_params ->
    return proto_params

let get_init_state context : State.t tzresult Lwt.t =
  let open Lwt_result_syntax in
  let protocol_params_key = ["protocol_parameters"] in
  let*! params_bytes = Context.find context protocol_params_key in
  let* Proto_params.{init_a; init_b} =
    match params_bytes with
      | None ->
        return Proto_params.default
      | Some bytes -> (
          match Data_encoding.Binary.of_bytes_opt Data_encoding.json bytes with
          | None ->
            tzfail (Error.Failed_to_parse_parameter bytes)
          | Some json ->
            decode_json json )
  in
  match State.create init_a init_b with
  | None ->
    tzfail Error.Invalid_protocol_parameters
  | Some state ->
    return state

let init _chain_id context block_header =
  let open Lwt_result_syntax in
  let open Block_header in
  let fitness = block_header.fitness in
  Logging.log Notice "init: fitness = %a%!" Fitness.pp fitness ;
  let* init_state = get_init_state context in
  let*! init_context = State.update_state context init_state in
  return
    {
      Updater.message = None;
      context = init_context;
      fitness;
      max_operations_ttl = 0;
      last_preserved_block_level = block_header.level;
      last_finalized_block_level = Compare.Int32.max 0l Int32.(sub block_header.level 2l) ;
    }

let compare_operations _ _ = 0

type Context.Cache.value += Demo of int

let value_of_key ~chain_id:_ ~predecessor_context:_ ~predecessor_timestamp:_
    ~predecessor_level:_ ~predecessor_fitness:_ ~predecessor:_ ~timestamp:_ =
  return (fun _ -> return (Demo 123))

let rpc_services = Services.rpc_services

module Mempool = struct
  type t = State.t

  type validation_info = unit

  type conflict_handler =
    existing_operation:Operation_hash.t * operation ->
    new_operation:Operation_hash.t * operation ->
    [`Keep | `Replace]

  type operation_conflict =
    | Operation_conflict of {
        existing : Operation_hash.t;
        new_operation : Operation_hash.t;
      }

  type add_result =
    | Added
    | Replaced of {removed : Operation_hash.t}
    | Unchanged

  type add_error =
    | Validation_error of error trace
    | Add_conflict of operation_conflict

  type merge_error =
    | Incompatible_mempool
    | Merge_conflict of operation_conflict

  let init ctxt _chain_id ~head_hash:_ ~(head : Block_header.shell_header) =
    let open Lwt_result_syntax in
    Logging.log
      Notice
      "Mempool.init: head fitness = %a%!"
      Fitness.pp
      head.fitness ;
    let*! state = State.get_state ctxt in
    return ((), state)

  let encoding = State.encoding

  let add_operation ?check_signature:_ ?conflict_handler:_
      (_info : validation_info) state ((_oph : Operation_hash.t), op) =
    match Apply.apply state op.protocol_data with
    | None ->
        Lwt.return_error
          (Validation_error (trace_of_error Error.Invalid_operation))
    | Some state -> return (state, Added)

  (* This mempool does not currently support removing an operation. *)
  let remove_operation _ _ = assert false

  (* This mempool does not currently support merging. *)
  let merge ?conflict_handler:_ _ _ = assert false

  (* This function is not currently used in the context of
     [proto_demo_counter]. If it is needed in the future, the type [t]
     will need to be extended to remember all added operations. *)
  let operations _ = assert false
end
OCaml

Innovation. Community. Security.