2016-09-08 21:13:10 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
|
|
|
(* Copyright (c) 2014 - 2016. *)
|
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
2016-11-14 18:55:24 +04:00
|
|
|
module Ed25519 = Environment.Ed25519
|
2016-09-08 21:13:10 +04:00
|
|
|
open Client_proto_args
|
2017-10-05 19:29:57 +04:00
|
|
|
|
2017-11-02 21:57:17 +04:00
|
|
|
open Michelson_v1_printer
|
2016-11-10 20:16:37 +04:00
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
module Program = Client_aliases.Alias (struct
|
2017-11-02 21:57:17 +04:00
|
|
|
type t = Michelson_v1_parser.parsed
|
2017-07-22 02:37:33 +04:00
|
|
|
let encoding =
|
2017-11-02 21:57:17 +04:00
|
|
|
Data_encoding.conv
|
|
|
|
(fun { Michelson_v1_parser.source } -> source)
|
|
|
|
(fun source ->
|
|
|
|
match Michelson_v1_parser.parse_toplevel source with
|
|
|
|
| Ok parsed -> parsed
|
|
|
|
| Error _ -> Pervasives.failwith "could not decode Michelson program alias")
|
|
|
|
Data_encoding.string
|
|
|
|
let of_source _cctxt source =
|
|
|
|
Lwt.return (Michelson_v1_parser.parse_toplevel source)
|
|
|
|
let to_source _ { Michelson_v1_parser.source } = return source
|
2016-09-08 21:13:10 +04:00
|
|
|
let name = "program"
|
|
|
|
end)
|
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
let group =
|
|
|
|
{ Cli_entries.name = "programs" ;
|
|
|
|
title = "Commands for managing the record of known programs" }
|
|
|
|
|
2017-09-27 11:55:20 +04:00
|
|
|
let data_parameter =
|
2017-11-02 21:57:17 +04:00
|
|
|
Cli_entries.parameter (fun _ data -> Lwt.return (Michelson_v1_parser.parse_expression data))
|
2017-09-27 11:55:20 +04:00
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
let commands () =
|
|
|
|
let open Cli_entries in
|
2017-09-19 13:31:35 +04:00
|
|
|
let show_types_switch =
|
|
|
|
switch
|
|
|
|
~parameter:"-details"
|
|
|
|
~doc:"Show the types of each instruction" in
|
|
|
|
let emacs_mode_switch =
|
|
|
|
switch
|
|
|
|
~parameter:"-emacs"
|
|
|
|
~doc:"Output in michelson-mode.el compatible format" in
|
|
|
|
let trace_stack_switch =
|
|
|
|
switch
|
|
|
|
~parameter:"-trace-stack"
|
|
|
|
~doc:"Show the stack after each step" in
|
|
|
|
let amount_arg =
|
2017-07-19 13:35:01 +04:00
|
|
|
Client_proto_args.tez_arg
|
2017-09-19 13:31:35 +04:00
|
|
|
~parameter:"-amount"
|
|
|
|
~doc:"The amount of the transfer in \xEA\x9C\xA9."
|
|
|
|
~default:"0.05" in
|
2016-09-08 21:13:10 +04:00
|
|
|
[
|
2017-04-05 03:02:10 +04:00
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
command ~group ~desc: "lists all known programs"
|
2017-09-19 13:31:35 +04:00
|
|
|
no_options
|
2016-09-08 21:13:10 +04:00
|
|
|
(fixed [ "list" ; "known" ; "programs" ])
|
2017-09-19 13:31:35 +04:00
|
|
|
(fun () cctxt ->
|
2017-04-05 03:02:10 +04:00
|
|
|
Program.load cctxt >>=? fun list ->
|
|
|
|
Lwt_list.iter_s (fun (n, _) -> cctxt.message "%s" n) list >>= fun () ->
|
|
|
|
return ()) ;
|
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
command ~group ~desc: "remember a program under some name"
|
2017-09-19 13:31:35 +04:00
|
|
|
no_options
|
2016-09-08 21:13:10 +04:00
|
|
|
(prefixes [ "remember" ; "program" ]
|
|
|
|
@@ Program.fresh_alias_param
|
|
|
|
@@ Program.source_param
|
|
|
|
@@ stop)
|
2017-09-19 13:31:35 +04:00
|
|
|
(fun () name hash cctxt -> Program.add cctxt name hash) ;
|
2017-04-05 03:02:10 +04:00
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
command ~group ~desc: "forget a remembered program"
|
2017-09-19 13:31:35 +04:00
|
|
|
no_options
|
2016-09-08 21:13:10 +04:00
|
|
|
(prefixes [ "forget" ; "program" ]
|
|
|
|
@@ Program.alias_param
|
|
|
|
@@ stop)
|
2017-09-19 13:31:35 +04:00
|
|
|
(fun () (name, _) cctxt -> Program.del cctxt name) ;
|
2017-04-05 03:02:10 +04:00
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
command ~group ~desc: "display a program"
|
2017-09-19 13:31:35 +04:00
|
|
|
no_options
|
2016-09-08 21:13:10 +04:00
|
|
|
(prefixes [ "show" ; "known" ; "program" ]
|
|
|
|
@@ Program.alias_param
|
|
|
|
@@ stop)
|
2017-09-19 13:31:35 +04:00
|
|
|
(fun () (_, program) cctxt ->
|
2017-04-05 03:02:10 +04:00
|
|
|
Program.to_source cctxt program >>=? fun source ->
|
2017-04-05 01:35:41 +04:00
|
|
|
cctxt.message "%s\n" source >>= fun () ->
|
|
|
|
return ()) ;
|
2017-04-05 03:02:10 +04:00
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
command ~group ~desc: "ask the node to run a program"
|
2017-11-03 14:53:54 +04:00
|
|
|
(args3 trace_stack_switch amount_arg no_print_source_flag)
|
2016-11-15 18:58:18 +04:00
|
|
|
(prefixes [ "run" ; "program" ]
|
|
|
|
@@ Program.source_param
|
|
|
|
@@ prefixes [ "on" ; "storage" ]
|
2017-04-05 12:22:41 +04:00
|
|
|
@@ Cli_entries.param ~name:"storage" ~desc:"the storage data"
|
2017-09-27 11:55:20 +04:00
|
|
|
data_parameter
|
2016-11-15 18:58:18 +04:00
|
|
|
@@ prefixes [ "and" ; "input" ]
|
2017-04-05 12:22:41 +04:00
|
|
|
@@ Cli_entries.param ~name:"storage" ~desc:"the input data"
|
2017-09-27 11:55:20 +04:00
|
|
|
data_parameter
|
2016-11-15 18:58:18 +04:00
|
|
|
@@ stop)
|
2017-11-03 14:53:54 +04:00
|
|
|
(fun (trace_stack, amount, no_print_source) program storage input cctxt ->
|
2017-07-25 16:03:49 +04:00
|
|
|
let print_errors errs =
|
2017-11-02 21:57:17 +04:00
|
|
|
cctxt.warning "%a"
|
|
|
|
(Michelson_v1_error_reporter.report_errors
|
|
|
|
~details:false
|
2017-11-03 14:53:54 +04:00
|
|
|
~show_source: (not no_print_source)
|
2017-11-02 21:57:17 +04:00
|
|
|
~parsed:program) errs >>= fun () ->
|
2017-07-25 16:03:49 +04:00
|
|
|
cctxt.error "error running program" >>= fun () ->
|
|
|
|
return () in
|
2017-09-19 13:31:35 +04:00
|
|
|
if trace_stack then
|
2017-04-05 01:35:41 +04:00
|
|
|
Client_proto_rpcs.Helpers.trace_code cctxt.rpc_config
|
2017-11-02 21:57:17 +04:00
|
|
|
cctxt.config.block program.expanded (storage.expanded, input.expanded, amount) >>= function
|
2016-11-16 18:05:02 +04:00
|
|
|
| Ok (storage, output, trace) ->
|
2017-04-05 03:02:10 +04:00
|
|
|
cctxt.message
|
|
|
|
"@[<v 0>@[<v 2>storage@,%a@]@,\
|
|
|
|
@[<v 2>output@,%a@]@,@[<v 2>trace@,%a@]@]@."
|
2017-11-02 21:57:17 +04:00
|
|
|
print_expr storage
|
|
|
|
print_expr output
|
2016-11-16 18:05:02 +04:00
|
|
|
(Format.pp_print_list
|
|
|
|
(fun ppf (loc, gas, stack) ->
|
|
|
|
Format.fprintf ppf
|
2017-04-05 03:02:10 +04:00
|
|
|
"- @[<v 0>location: %d (remaining gas: %d)@,\
|
2017-07-22 02:37:33 +04:00
|
|
|
[ @[<v 0>%a ]@]@]"
|
2016-11-16 18:05:02 +04:00
|
|
|
loc gas
|
2017-11-02 21:57:17 +04:00
|
|
|
(Format.pp_print_list print_expr)
|
2016-11-16 18:05:02 +04:00
|
|
|
stack))
|
2017-04-05 01:35:41 +04:00
|
|
|
trace >>= fun () ->
|
|
|
|
return ()
|
2017-07-25 16:03:49 +04:00
|
|
|
| Error errs -> print_errors errs
|
2016-11-16 18:05:02 +04:00
|
|
|
else
|
2017-04-05 01:35:41 +04:00
|
|
|
Client_proto_rpcs.Helpers.run_code cctxt.rpc_config
|
2017-11-02 21:57:17 +04:00
|
|
|
cctxt.config.block program.expanded (storage.expanded, input.expanded, amount) >>= function
|
2016-11-16 18:05:02 +04:00
|
|
|
| Ok (storage, output) ->
|
2016-12-03 16:05:02 +04:00
|
|
|
cctxt.message "@[<v 0>@[<v 2>storage@,%a@]@,@[<v 2>output@,%a@]@]@."
|
2017-11-02 21:57:17 +04:00
|
|
|
print_expr storage
|
|
|
|
print_expr output >>= fun () ->
|
2017-04-05 01:35:41 +04:00
|
|
|
return ()
|
2016-11-16 18:05:02 +04:00
|
|
|
| Error errs ->
|
2017-07-25 16:03:49 +04:00
|
|
|
print_errors errs);
|
2017-04-05 03:02:10 +04:00
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
command ~group ~desc: "ask the node to typecheck a program"
|
2017-11-03 14:53:54 +04:00
|
|
|
(args3 show_types_switch emacs_mode_switch no_print_source_flag)
|
2016-09-08 21:13:10 +04:00
|
|
|
(prefixes [ "typecheck" ; "program" ]
|
|
|
|
@@ Program.source_param
|
|
|
|
@@ stop)
|
2017-11-03 14:53:54 +04:00
|
|
|
(fun (show_types, emacs_mode, no_print_source) program cctxt ->
|
2017-07-22 02:37:33 +04:00
|
|
|
Client_proto_rpcs.Helpers.typecheck_code
|
2017-11-02 21:57:17 +04:00
|
|
|
cctxt.rpc_config cctxt.config.block program.expanded >>= fun res ->
|
2017-09-19 13:31:35 +04:00
|
|
|
if emacs_mode then
|
2017-11-02 21:57:17 +04:00
|
|
|
let type_map, errs = match res with
|
|
|
|
| Ok type_map -> type_map, []
|
|
|
|
| Error (Environment.Ecoproto_error
|
|
|
|
(Script_ir_translator.Ill_typed_contract (_, type_map ) :: _)
|
|
|
|
:: _ as errs) ->
|
|
|
|
type_map, errs
|
2017-07-22 02:37:33 +04:00
|
|
|
| Error errs ->
|
2017-11-02 21:57:17 +04:00
|
|
|
[], errs in
|
2017-07-22 02:37:33 +04:00
|
|
|
cctxt.message
|
2017-11-02 21:57:17 +04:00
|
|
|
"(@[<v 0>(types . %a)@ (errors . %a)@])"
|
|
|
|
Michelson_v1_emacs.print_type_map (program, type_map)
|
|
|
|
Michelson_v1_emacs.report_errors (program, errs) >>= fun () ->
|
2017-07-22 02:37:33 +04:00
|
|
|
return ()
|
|
|
|
else
|
|
|
|
match res with
|
|
|
|
| Ok type_map ->
|
2017-11-02 21:57:17 +04:00
|
|
|
let program = inject_types type_map program in
|
2017-07-22 02:37:33 +04:00
|
|
|
cctxt.message "Well typed" >>= fun () ->
|
2017-09-19 13:31:35 +04:00
|
|
|
if show_types then
|
2017-11-02 21:57:17 +04:00
|
|
|
cctxt.message "%a" Micheline_printer.print_expr program >>= fun () ->
|
2017-07-22 02:37:33 +04:00
|
|
|
return ()
|
|
|
|
else return ()
|
|
|
|
| Error errs ->
|
2017-11-02 21:57:17 +04:00
|
|
|
cctxt.warning "%a"
|
|
|
|
(Michelson_v1_error_reporter.report_errors
|
|
|
|
~details: show_types
|
2017-11-03 14:53:54 +04:00
|
|
|
~show_source: (not no_print_source)
|
2017-11-02 21:57:17 +04:00
|
|
|
~parsed:program) errs >>= fun () ->
|
2017-07-25 16:03:49 +04:00
|
|
|
cctxt.error "ill-typed program") ;
|
2017-04-05 03:02:10 +04:00
|
|
|
|
2017-01-11 20:42:54 +04:00
|
|
|
command ~group ~desc: "ask the node to typecheck a data expression"
|
2017-11-03 14:53:54 +04:00
|
|
|
(args1 no_print_source_flag)
|
2016-09-12 16:06:23 +04:00
|
|
|
(prefixes [ "typecheck" ; "data" ]
|
2017-04-05 12:22:41 +04:00
|
|
|
@@ Cli_entries.param ~name:"data" ~desc:"the data to typecheck"
|
2017-09-27 11:55:20 +04:00
|
|
|
data_parameter
|
2016-09-12 16:06:23 +04:00
|
|
|
@@ prefixes [ "against" ; "type" ]
|
2017-04-05 12:22:41 +04:00
|
|
|
@@ Cli_entries.param ~name:"type" ~desc:"the expected type"
|
2017-09-27 11:55:20 +04:00
|
|
|
data_parameter
|
2016-09-12 16:06:23 +04:00
|
|
|
@@ stop)
|
2017-11-03 14:53:54 +04:00
|
|
|
(fun no_print_source data exp_ty cctxt ->
|
2017-04-05 12:22:41 +04:00
|
|
|
Client_proto_rpcs.Helpers.typecheck_data cctxt.Client_commands.rpc_config
|
2017-11-02 21:57:17 +04:00
|
|
|
cctxt.config.block (data.expanded, exp_ty.expanded) >>= function
|
2016-09-12 16:06:23 +04:00
|
|
|
| Ok () ->
|
2017-04-05 01:35:41 +04:00
|
|
|
cctxt.message "Well typed" >>= fun () ->
|
|
|
|
return ()
|
2016-09-12 16:06:23 +04:00
|
|
|
| Error errs ->
|
2017-11-02 21:57:17 +04:00
|
|
|
cctxt.warning "%a"
|
|
|
|
(Michelson_v1_error_reporter.report_errors
|
|
|
|
~details:false
|
2017-11-03 14:53:54 +04:00
|
|
|
~show_source:(not no_print_source)
|
2017-11-02 21:57:17 +04:00
|
|
|
?parsed:None) errs >>= fun () ->
|
2017-07-25 16:03:49 +04:00
|
|
|
cctxt.error "ill-typed data") ;
|
2017-04-05 03:02:10 +04:00
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
command ~group
|
2017-01-11 20:42:54 +04:00
|
|
|
~desc: "ask the node to compute the hash of a data expression \
|
2016-09-12 16:06:23 +04:00
|
|
|
using the same algorithm as script instruction H"
|
2017-09-19 13:31:35 +04:00
|
|
|
no_options
|
2016-09-12 16:06:23 +04:00
|
|
|
(prefixes [ "hash" ; "data" ]
|
2017-04-05 12:22:41 +04:00
|
|
|
@@ Cli_entries.param ~name:"data" ~desc:"the data to hash"
|
2017-09-27 11:55:20 +04:00
|
|
|
data_parameter
|
2016-09-12 16:06:23 +04:00
|
|
|
@@ stop)
|
2017-09-19 13:31:35 +04:00
|
|
|
(fun () data cctxt ->
|
2017-04-05 12:22:41 +04:00
|
|
|
Client_proto_rpcs.Helpers.hash_data cctxt.Client_commands.rpc_config
|
2017-11-02 21:57:17 +04:00
|
|
|
cctxt.config.block (data.expanded) >>= function
|
2016-09-12 16:06:23 +04:00
|
|
|
| Ok hash ->
|
2017-04-05 01:35:41 +04:00
|
|
|
cctxt.message "%S" hash >>= fun () ->
|
|
|
|
return ()
|
2016-09-12 16:06:23 +04:00
|
|
|
| Error errs ->
|
2017-01-12 22:37:18 +04:00
|
|
|
cctxt.warning "%a" pp_print_error errs >>= fun () ->
|
2017-07-25 16:03:49 +04:00
|
|
|
cctxt.error "ill-formed data") ;
|
2017-04-05 03:02:10 +04:00
|
|
|
|
2016-12-03 16:05:02 +04:00
|
|
|
command ~group
|
2017-01-11 20:42:54 +04:00
|
|
|
~desc: "ask the node to compute the hash of a data expression \
|
2016-09-12 16:06:23 +04:00
|
|
|
using the same algorithm as script instruction H, sign it using \
|
|
|
|
a given secret key, and display it using the format expected by \
|
|
|
|
script instruction CHECK_SIGNATURE"
|
2017-09-19 13:31:35 +04:00
|
|
|
no_options
|
2016-09-12 16:06:23 +04:00
|
|
|
(prefixes [ "hash" ; "and" ; "sign" ; "data" ]
|
2017-04-05 12:22:41 +04:00
|
|
|
@@ Cli_entries.param ~name:"data" ~desc:"the data to hash"
|
2017-09-27 11:55:20 +04:00
|
|
|
data_parameter
|
2016-09-12 16:06:23 +04:00
|
|
|
@@ prefixes [ "for" ]
|
|
|
|
@@ Client_keys.Secret_key.alias_param
|
|
|
|
@@ stop)
|
2017-09-19 13:31:35 +04:00
|
|
|
(fun () data (_, key) cctxt ->
|
2017-04-05 01:35:41 +04:00
|
|
|
Client_proto_rpcs.Helpers.hash_data cctxt.rpc_config
|
2017-11-02 21:57:17 +04:00
|
|
|
cctxt.config.block (data.expanded) >>= function
|
2016-09-12 16:06:23 +04:00
|
|
|
| Ok hash ->
|
|
|
|
let signature = Ed25519.sign key (MBytes.of_string hash) in
|
2016-12-03 16:05:02 +04:00
|
|
|
cctxt.message "Hash: %S@.Signature: %S"
|
2016-09-12 16:06:23 +04:00
|
|
|
hash
|
|
|
|
(signature |>
|
2017-02-28 05:56:40 +04:00
|
|
|
Data_encoding.Binary.to_bytes Ed25519.Signature.encoding |>
|
2017-04-05 01:35:41 +04:00
|
|
|
Hex_encode.hex_of_bytes) >>= fun () ->
|
|
|
|
return ()
|
2016-09-12 16:06:23 +04:00
|
|
|
| Error errs ->
|
2017-01-12 22:37:18 +04:00
|
|
|
cctxt.warning "%a" pp_print_error errs >>= fun () ->
|
2017-07-25 16:03:49 +04:00
|
|
|
cctxt.error "ill-formed data") ;
|
2017-04-05 03:02:10 +04:00
|
|
|
|
2016-09-08 21:13:10 +04:00
|
|
|
]
|