package octez-protocol-019-PtParisB-libs

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

Source file client_proto_programs.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
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
(*****************************************************************************)
(*                                                                           *)
(* Open Source License                                                       *)
(* Copyright (c) 2018 Dynamic Ledger Solutions, Inc. <contact@tezos.com>     *)
(* Copyright (c) 2019 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 Tezos_micheline
open Michelson_v1_printer

module Program = Client_aliases.Alias (struct
  type t = Michelson_v1_parser.parsed Micheline_parser.parsing_result

  include Compare.Make (struct
    type nonrec t = t

    let compare = Micheline_parser.compare Michelson_v1_parser.compare_parsed
  end)

  let encoding =
    Data_encoding.conv
      (fun ({Michelson_v1_parser.source; _}, _) -> source)
      (fun source -> Michelson_v1_parser.parse_toplevel source)
      Data_encoding.string

  let of_source source =
    let open Lwt_result_syntax in
    return (Michelson_v1_parser.parse_toplevel source)

  let to_source ({Michelson_v1_parser.source; _}, _) =
    let open Lwt_result_syntax in
    return source

  let name = "script"
end)

let print_errors ?parsed (cctxt : #Protocol_client_context.full) errs
    ~show_source =
  let open Lwt_result_syntax in
  let parsed = Option.map Michelson_v1_parser.unrecognize_prims parsed in
  let*! errs =
    Michelson_v1_error_reporter.enrich_runtime_errors
      cctxt
      ~chain:cctxt#chain
      ~block:cctxt#block
      ~parsed
      errs
  in
  let*! () =
    cctxt#warning
      "%a"
      (Michelson_v1_error_reporter.report_errors
         ~details:false
         ?parsed
         ~show_source)
      errs
  in
  let*! () = cctxt#error "error running script" in
  return_unit

let print_view_result (cctxt : #Protocol_client_context.full) =
  let open Lwt_result_syntax in
  function
  | Ok expr ->
      let*! () = cctxt#message "%a" print_expr expr in
      return_unit
  | Error errs -> print_errors cctxt ~show_source:false errs

let print_run_result (cctxt : Protocol_client_context.full) ~show_source ~parsed
    =
  let open Lwt_result_syntax in
  function
  | Ok (storage, operations, maybe_lazy_storage_diff) ->
      let* operations =
        List.map_es
          (Operation_result.normalize_internal_operation cctxt Readable)
          operations
      in
      let*! () =
        cctxt#message
          "@[<v 0>@[<v 2>storage@,\
           %a@]@,\
           @[<v 2>emitted operations@,\
           %a@]@,\
           @[<v 2>big_map diff@,\
           %a@]@]@."
          print_expr
          storage
          (Format.pp_print_list Operation_result.pp_internal_operation)
          operations
          (fun ppf -> function
            | None -> ()
            | Some diff -> print_big_map_diff ppf diff)
          maybe_lazy_storage_diff
      in
      return_unit
  | Error errs -> print_errors cctxt errs ~show_source ~parsed

let print_trace_result (cctxt : #Client_context.printer) ~show_source ~parsed =
  let open Lwt_result_syntax in
  function
  | Ok (storage, operations, trace, maybe_lazy_storage_diff) ->
      let*! () =
        cctxt#message
          "@[<v 0>@[<v 2>storage@,\
           %a@]@,\
           @[<v 2>emitted operations@,\
           %a@]@,\
           @[<v 2>big_map diff@,\
           %a@]@,\
           @[<v 2>trace@,\
           %a@]@]@."
          print_expr
          storage
          (Format.pp_print_list Operation_result.pp_internal_operation)
          operations
          (fun ppf -> function
            | None -> ()
            | Some diff -> print_big_map_diff ppf diff)
          maybe_lazy_storage_diff
          print_execution_trace
          trace
      in
      return_unit
  | Error errs -> print_errors cctxt errs ~show_source ~parsed

let print_run_instr_result (cctxt : #Client_context.printer) ~show_source
    ~parsed =
  let open Lwt_result_syntax in
  function
  | Ok (stack, gas) ->
      let*! () =
        cctxt#message
          "@[<v 0>@[<v 2>Result@,%a@]@,@[<v 2>Gas remaining: %a@]@]@."
          Michelson_v1_printer.print_typed_stack
          stack
          Alpha_context.Gas.pp
          gas
      in
      return_unit
  | Error errs -> print_errors cctxt errs ~show_source ~parsed

type simulation_params = {
  input : Michelson_v1_parser.parsed;
  unparsing_mode : Script_ir_unparser.unparsing_mode;
  now : Script_timestamp.t option;
  level : Script_int.n Script_int.num option;
  sender : Contract.t option;
  payer : Signature.public_key_hash option;
  gas : Gas.Arith.integral option;
  other_contracts : RPC.Scripts.S.other_contract_description list option;
  extra_big_maps : RPC.Scripts.S.extra_big_map_description list option;
}

type run_view_params = {
  shared_params : simulation_params;
  contract : Contract_hash.t;
  entrypoint : Entrypoint.t;
}

type run_script_view_params = {
  shared_params : simulation_params;
  contract : Contract_hash.t;
  view : string;
  unlimited_gas : bool;
}

type run_params = {
  shared_params : simulation_params;
  amount : Tez.t option;
  balance : Tez.t option;
  program : Michelson_v1_parser.parsed;
  storage : Michelson_v1_parser.parsed;
  entrypoint : Entrypoint.t option;
  self : Contract_hash.t option;
}

type run_instr_params = {
  shared_params : simulation_params;
  amount : Tez.t;
  balance : Tez.t option;
  stack : (Script.expr * Script.expr) list;
  self : Contract_hash.t option;
  parameter : Script.expr option;
  legacy : bool;
}

let run_view (cctxt : #Protocol_client_context.rpc_context)
    ~(chain : Chain_services.chain) ~block (params : run_view_params) =
  let open Lwt_result_syntax in
  let {
    shared_params =
      {
        input;
        unparsing_mode;
        now;
        level;
        sender;
        payer;
        gas;
        other_contracts;
        extra_big_maps;
      };
    contract;
    entrypoint;
  } =
    params
  in
  let* chain_id = Chain_services.chain_id cctxt ~chain () in
  Plugin.RPC.Scripts.run_tzip4_view
    cctxt
    (chain, block)
    ~gas
    ~contract
    ~entrypoint
    ~input:input.expanded
    ~chain_id
    ~sender
    ~payer
    ~unparsing_mode
    ~now
    ~level
    ~other_contracts
    ~extra_big_maps

let run_script_view (cctxt : #Protocol_client_context.rpc_context)
    ~(chain : Chain_services.chain) ~block (params : run_script_view_params) =
  let open Lwt_result_syntax in
  let {
    shared_params =
      {
        input;
        unparsing_mode;
        now;
        level;
        sender;
        payer;
        gas;
        other_contracts;
        extra_big_maps;
      };
    contract;
    view;
    unlimited_gas;
  } =
    params
  in
  let* chain_id = Chain_services.chain_id cctxt ~chain () in
  Plugin.RPC.Scripts.run_script_view
    cctxt
    (chain, block)
    ~gas
    ~contract
    ~view
    ~input:input.expanded
    ~unlimited_gas
    ~chain_id
    ~sender
    ~payer
    ~unparsing_mode
    ~now
    ~level
    ~other_contracts
    ~extra_big_maps

let run (cctxt : #Protocol_client_context.rpc_context)
    ~(chain : Chain_services.chain) ~block (params : run_params) =
  let open Lwt_result_syntax in
  let* chain_id = Chain_services.chain_id cctxt ~chain () in
  let {
    shared_params =
      {
        input;
        unparsing_mode;
        now;
        level;
        sender;
        payer;
        gas;
        other_contracts;
        extra_big_maps;
      };
    program;
    amount;
    balance;
    storage;
    entrypoint;
    self;
  } =
    params
  in
  let amount = Option.value ~default:Tez.fifty_cents amount in
  let entrypoint = Option.value ~default:Entrypoint.default entrypoint in
  Plugin.RPC.Scripts.run_code
    cctxt
    (chain, block)
    ~gas
    ~entrypoint
    ~unparsing_mode:(Some unparsing_mode)
    ~script:program.expanded
    ~storage:storage.expanded
    ~input:input.expanded
    ~amount
    ~balance
    ~chain_id
    ~sender
    ~payer
    ~self
    ~now
    ~level
    ~other_contracts
    ~extra_big_maps

let trace (cctxt : #Protocol_client_context.rpc_context)
    ~(chain : Chain_services.chain) ~block (params : run_params) =
  let open Lwt_result_syntax in
  let* chain_id = Chain_services.chain_id cctxt ~chain () in
  let {
    shared_params =
      {
        input;
        unparsing_mode;
        now;
        level;
        sender;
        payer;
        gas;
        other_contracts;
        extra_big_maps;
      };
    program;
    amount;
    balance;
    storage;
    entrypoint;
    self;
  } =
    params
  in
  let amount = Option.value ~default:Tez.fifty_cents amount in
  let entrypoint = Option.value ~default:Entrypoint.default entrypoint in
  Plugin.RPC.Scripts.trace_code
    cctxt
    (chain, block)
    ~gas
    ~entrypoint
    ~unparsing_mode:(Some unparsing_mode)
    ~script:program.expanded
    ~storage:storage.expanded
    ~input:input.expanded
    ~amount
    ~balance
    ~chain_id
    ~sender
    ~payer
    ~self
    ~now
    ~level
    ~other_contracts
    ~extra_big_maps

let run_instr (cctxt : #Protocol_client_context.rpc_context)
    ~(chain : Chain_services.chain) ~block (params : run_instr_params) =
  let open Lwt_result_syntax in
  let* chain_id = Chain_services.chain_id cctxt ~chain () in
  let {shared_params; amount; balance; stack; self; parameter; legacy} =
    params
  in
  let {
    input;
    unparsing_mode;
    now;
    level;
    sender;
    payer;
    gas;
    other_contracts;
    extra_big_maps;
  } =
    shared_params
  in
  Plugin.RPC.Scripts.run_instr
    ~gas
    ~legacy
    ~input:stack
    ~code:input.expanded
    ~chain_id
    ~now
    ~level
    ~unparsing_mode:(Some unparsing_mode)
    ~source:payer
    ~sender
    ~self
    ~parameter
    ~amount
    ~balance
    ~other_contracts
    ~extra_big_maps
    cctxt
    (chain, block)

let typecheck_data cctxt ~(chain : Chain_services.chain) ~block ~gas ~legacy
    ~(data : Michelson_v1_parser.parsed) ~(ty : Michelson_v1_parser.parsed) () =
  Plugin.RPC.Scripts.typecheck_data
    cctxt
    (chain, block)
    ~gas
    ~legacy
    ~data:data.expanded
    ~ty:ty.expanded

let typecheck_program cctxt ~(chain : Chain_services.chain) ~block ~gas ~legacy
    ~show_types (program : Michelson_v1_parser.parsed) =
  Plugin.RPC.Scripts.typecheck_code
    cctxt
    (chain, block)
    ~gas
    ~legacy
    ~script:program.expanded
    ~show_types

let script_size cctxt ~(chain : Chain_services.chain) ~block ~gas ~legacy
    ~(program : Michelson_v1_parser.parsed)
    ~(storage : Michelson_v1_parser.parsed) () =
  Plugin.RPC.Scripts.script_size
    cctxt
    (chain, block)
    ~gas
    ~legacy
    ~script:program.expanded
    ~storage:storage.expanded

let print_typecheck_result ~emacs ~show_types ~print_source_on_error
    ~display_names ~name program res (cctxt : #Client_context.printer) =
  let open Lwt_result_syntax in
  if emacs then
    let type_map, errs, _gas =
      match res with
      | Ok (type_map, gas) -> (type_map, [], Some gas)
      | Error
          (Environment.Ecoproto_error
             (Script_tc_errors.Ill_typed_contract (_, type_map))
           :: _ as errs) ->
          (type_map, errs, None)
      | Error errs -> ([], errs, None)
    in
    let*! () =
      cctxt#message
        "(@[<v 0>(types . %a)@ (errors . %a)@])"
        Michelson_v1_emacs.print_type_map
        (program, type_map)
        Michelson_v1_emacs.report_errors
        (program, errs)
    in
    return_unit
  else
    match res with
    | Ok (type_map, gas) ->
        let program = Michelson_v1_printer.inject_types type_map program in
        let*! () =
          cctxt#message
            "@[Well typed (Gas remaining: %a)\t%t@]"
            Gas.pp
            gas
            (fun fmt -> if display_names then Format.pp_print_string fmt name)
        in
        if show_types then
          let*! () = cctxt#message "%a" Micheline_printer.print_expr program in
          return_unit
        else return_unit
    | Error errs ->
        let*! () =
          cctxt#warning
            "%a"
            (Michelson_v1_error_reporter.report_errors
               ~details:show_types
               ~show_source:print_source_on_error
               ~parsed:(Michelson_v1_parser.unrecognize_prims program))
            errs
        in
        cctxt#error "script %S is ill-typed" name

let entrypoint_type cctxt ~(chain : Chain_services.chain) ~block
    (program : Michelson_v1_parser.parsed) ~entrypoint =
  Michelson_v1_entrypoints.script_entrypoint_type
    cctxt
    ~chain
    ~block
    program.expanded
    ~entrypoint

let print_entrypoint_type (cctxt : #Client_context.printer) ~emacs ?script_name
    ~show_source ~parsed ~entrypoint ty =
  Michelson_v1_entrypoints.print_entrypoint_type
    cctxt
    ~entrypoint
    ~emacs
    ?script_name
    ~on_errors:(print_errors cctxt ~show_source ~parsed)
    ty

let list_entrypoints cctxt ~(chain : Chain_services.chain) ~block
    (program : Michelson_v1_parser.parsed) =
  Michelson_v1_entrypoints.list_entrypoints cctxt ~chain ~block program.expanded

let print_entrypoints_list (cctxt : #Client_context.printer) ~emacs ?script_name
    ~show_source ~parsed ty =
  Michelson_v1_entrypoints.print_entrypoints_list
    cctxt
    ~emacs
    ?script_name
    ~on_errors:(print_errors cctxt ~show_source ~parsed)
    ty

let list_unreachables cctxt ~(chain : Chain_services.chain) ~block
    (program : Michelson_v1_parser.parsed) =
  Michelson_v1_entrypoints.list_unreachables
    cctxt
    ~chain
    ~block
    program.expanded

let print_unreachables (cctxt : #Client_context.printer) ~emacs ?script_name
    ~show_source ~parsed ty =
  Michelson_v1_entrypoints.print_unreachables
    cctxt
    ~emacs
    ?script_name
    ~on_errors:(print_errors cctxt ~show_source ~parsed)
    ty
OCaml

Innovation. Community. Security.