2016-09-08 21:13:10 +04:00
|
|
|
(**************************************************************************)
|
|
|
|
(* *)
|
2018-02-06 00:17:03 +04:00
|
|
|
(* Copyright (c) 2014 - 2018. *)
|
2016-09-08 21:13:10 +04:00
|
|
|
(* Dynamic Ledger Solutions, Inc. <contact@tezos.com> *)
|
|
|
|
(* *)
|
|
|
|
(* All rights reserved. No warranty, explicit or implicit, provided. *)
|
|
|
|
(* *)
|
|
|
|
(**************************************************************************)
|
|
|
|
|
2018-01-29 04:06:47 +04:00
|
|
|
open Proto_alpha
|
2018-02-11 22:17:39 +04:00
|
|
|
open Alpha_context
|
2017-11-27 09:13:12 +04:00
|
|
|
open Tezos_micheline
|
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-04 03:16:05 +04:00
|
|
|
type t = Michelson_v1_parser.parsed Micheline_parser.parsing_result
|
2017-07-22 02:37:33 +04:00
|
|
|
let encoding =
|
2017-11-02 21:57:17 +04:00
|
|
|
Data_encoding.conv
|
2017-11-04 03:16:05 +04:00
|
|
|
(fun ({ Michelson_v1_parser.source }, _) -> source)
|
|
|
|
(fun source -> Michelson_v1_parser.parse_toplevel source)
|
2017-11-02 21:57:17 +04:00
|
|
|
Data_encoding.string
|
2017-12-05 18:09:36 +04:00
|
|
|
let of_source source =
|
2017-11-04 03:16:05 +04:00
|
|
|
return (Michelson_v1_parser.parse_toplevel source)
|
2017-12-05 18:09:36 +04:00
|
|
|
let to_source ({ Michelson_v1_parser.source }, _) = return source
|
2016-09-08 21:13:10 +04:00
|
|
|
let name = "program"
|
|
|
|
end)
|
|
|
|
|
2018-02-16 21:10:18 +04:00
|
|
|
let print_errors (cctxt : #Client_context.printer) errs ~show_source ~parsed =
|
2017-11-07 20:38:11 +04:00
|
|
|
cctxt#warning "%a"
|
|
|
|
(Michelson_v1_error_reporter.report_errors
|
|
|
|
~details:false
|
|
|
|
~show_source
|
|
|
|
~parsed) errs >>= fun () ->
|
|
|
|
cctxt#error "error running program" >>= fun () ->
|
|
|
|
return ()
|
|
|
|
|
2017-12-14 19:45:04 +04:00
|
|
|
let print_big_map_diff ppf = function
|
|
|
|
| None -> ()
|
|
|
|
| Some diff ->
|
|
|
|
Format.fprintf ppf
|
|
|
|
"@[<v 2>map diff:@,%a@]@,"
|
|
|
|
(Format.pp_print_list
|
|
|
|
~pp_sep:Format.pp_print_space
|
|
|
|
(fun ppf (key, value) ->
|
2018-04-08 22:24:25 +04:00
|
|
|
Format.fprintf ppf "%s %s%a"
|
2017-12-14 19:45:04 +04:00
|
|
|
(match value with
|
|
|
|
| None -> "-"
|
|
|
|
| Some _ -> "+")
|
2018-04-08 22:24:25 +04:00
|
|
|
key
|
2017-12-14 19:45:04 +04:00
|
|
|
(fun ppf -> function
|
|
|
|
| None -> ()
|
|
|
|
| Some x -> Format.fprintf ppf "-> %a" print_expr x)
|
|
|
|
value))
|
|
|
|
diff
|
|
|
|
|
2018-02-16 21:10:18 +04:00
|
|
|
let print_run_result (cctxt : #Client_context.printer) ~show_source ~parsed = function
|
2018-04-05 19:17:27 +04:00
|
|
|
| Ok (storage, operations, maybe_diff) ->
|
|
|
|
cctxt#message "@[<v 0>@[<v 2>storage@,%a@]@,@[<v 2>emitted operations@,%a@]@,@[%a@]@]@."
|
2017-11-07 20:38:11 +04:00
|
|
|
print_expr storage
|
2018-04-22 12:50:34 +04:00
|
|
|
(Format.pp_print_list Operation_result.pp_internal_operation) operations
|
2017-12-14 19:45:04 +04:00
|
|
|
print_big_map_diff maybe_diff >>= fun () ->
|
2017-11-07 20:38:11 +04:00
|
|
|
return ()
|
|
|
|
| Error errs ->
|
|
|
|
print_errors cctxt errs ~show_source ~parsed
|
|
|
|
|
2018-02-16 21:10:18 +04:00
|
|
|
let print_trace_result (cctxt : #Client_context.printer) ~show_source ~parsed =
|
2017-11-07 20:38:11 +04:00
|
|
|
function
|
2018-04-05 19:17:27 +04:00
|
|
|
| Ok (storage, operations, trace, maybe_big_map_diff) ->
|
2017-11-07 20:38:11 +04:00
|
|
|
cctxt#message
|
|
|
|
"@[<v 0>@[<v 2>storage@,%a@]@,\
|
2018-04-05 19:17:27 +04:00
|
|
|
@[<v 2>emitted operations@,%a@]@,%a@[<v 2>@[<v 2>trace@,%a@]@]@."
|
2017-11-07 20:38:11 +04:00
|
|
|
print_expr storage
|
2018-04-22 12:50:34 +04:00
|
|
|
(Format.pp_print_list Operation_result.pp_internal_operation) operations
|
2017-12-14 19:45:04 +04:00
|
|
|
print_big_map_diff maybe_big_map_diff
|
2017-11-07 20:38:11 +04:00
|
|
|
(Format.pp_print_list
|
|
|
|
(fun ppf (loc, gas, stack) ->
|
|
|
|
Format.fprintf ppf
|
2017-11-06 18:22:58 +04:00
|
|
|
"- @[<v 0>location: %d (remaining gas: %a)@,\
|
2017-11-07 20:38:11 +04:00
|
|
|
[ @[<v 0>%a ]@]@]"
|
2017-11-06 18:22:58 +04:00
|
|
|
loc Gas.pp gas
|
2017-11-07 20:38:11 +04:00
|
|
|
(Format.pp_print_list print_expr)
|
|
|
|
stack))
|
|
|
|
trace >>= fun () ->
|
|
|
|
return ()
|
|
|
|
| Error errs ->
|
|
|
|
print_errors cctxt errs ~show_source ~parsed
|
|
|
|
|
2018-04-16 02:44:24 +04:00
|
|
|
let get_contract cctxt ?(chain = `Main) block contract =
|
2018-04-04 18:20:03 +04:00
|
|
|
match contract with
|
|
|
|
| Some contract -> return contract
|
|
|
|
| None ->
|
|
|
|
(* TODO use local contract by default *)
|
2018-04-16 02:44:24 +04:00
|
|
|
Alpha_services.Contract.list cctxt (chain, block) >>|? List.hd
|
2018-04-04 18:20:03 +04:00
|
|
|
|
2017-11-07 20:38:11 +04:00
|
|
|
let run
|
2018-04-16 02:44:24 +04:00
|
|
|
(cctxt : #Proto_alpha.rpc_context)
|
|
|
|
?(chain = `Main)
|
|
|
|
block
|
2018-04-04 18:20:03 +04:00
|
|
|
?contract
|
2017-11-29 21:06:17 +04:00
|
|
|
?(amount = Tez.fifty_cents)
|
2017-11-07 20:38:11 +04:00
|
|
|
~(program : Michelson_v1_parser.parsed)
|
|
|
|
~(storage : Michelson_v1_parser.parsed)
|
|
|
|
~(input : Michelson_v1_parser.parsed)
|
2018-04-16 02:44:24 +04:00
|
|
|
() =
|
|
|
|
get_contract cctxt ~chain block contract >>=? fun contract ->
|
2018-02-11 22:17:40 +04:00
|
|
|
Alpha_services.Helpers.run_code cctxt
|
2018-04-16 02:44:24 +04:00
|
|
|
(chain, block)
|
|
|
|
program.expanded (storage.expanded, input.expanded, amount, contract)
|
2017-11-07 20:38:11 +04:00
|
|
|
|
|
|
|
let trace
|
2018-04-16 02:44:24 +04:00
|
|
|
(cctxt : #Proto_alpha.rpc_context)
|
|
|
|
?(chain = `Main)
|
|
|
|
block
|
2018-04-04 18:20:03 +04:00
|
|
|
?contract
|
2017-11-29 21:06:17 +04:00
|
|
|
?(amount = Tez.fifty_cents)
|
2017-11-07 20:38:11 +04:00
|
|
|
~(program : Michelson_v1_parser.parsed)
|
|
|
|
~(storage : Michelson_v1_parser.parsed)
|
|
|
|
~(input : Michelson_v1_parser.parsed)
|
2018-04-16 02:44:24 +04:00
|
|
|
() =
|
|
|
|
get_contract cctxt ~chain block contract >>=? fun contract ->
|
2018-02-11 22:17:40 +04:00
|
|
|
Alpha_services.Helpers.trace_code cctxt
|
2018-04-16 02:44:24 +04:00
|
|
|
(chain, block)
|
|
|
|
program.expanded (storage.expanded, input.expanded, amount, contract)
|
2017-11-07 20:38:11 +04:00
|
|
|
|
2018-04-16 02:44:24 +04:00
|
|
|
let hash_and_sign
|
|
|
|
cctxt
|
|
|
|
?(chain = `Main)
|
|
|
|
block
|
|
|
|
?gas
|
|
|
|
(data : Michelson_v1_parser.parsed)
|
|
|
|
(typ : Michelson_v1_parser.parsed)
|
|
|
|
sk =
|
|
|
|
Alpha_services.Helpers.hash_data
|
|
|
|
cctxt (chain, block) (data.expanded, typ.expanded, gas) >>=? fun (hash, gas) ->
|
2018-05-26 15:22:47 +04:00
|
|
|
Client_keys.sign sk (MBytes.of_string hash) >>=? fun signature ->
|
2018-05-17 19:25:54 +04:00
|
|
|
return (hash, Signature.to_b58check signature, gas)
|
2017-11-07 20:38:11 +04:00
|
|
|
|
|
|
|
let typecheck_data
|
2018-04-16 02:44:24 +04:00
|
|
|
cctxt
|
|
|
|
?(chain = `Main)
|
|
|
|
block
|
2018-02-10 07:28:32 +04:00
|
|
|
?gas
|
2017-11-07 20:38:11 +04:00
|
|
|
~(data : Michelson_v1_parser.parsed)
|
|
|
|
~(ty : Michelson_v1_parser.parsed)
|
2018-04-16 02:44:24 +04:00
|
|
|
() =
|
|
|
|
Alpha_services.Helpers.typecheck_data
|
|
|
|
cctxt (chain, block)
|
|
|
|
(data.expanded, ty.expanded, gas)
|
2017-11-07 20:38:11 +04:00
|
|
|
|
2018-04-16 02:44:24 +04:00
|
|
|
let typecheck_program
|
|
|
|
cctxt
|
|
|
|
?(chain = `Main)
|
|
|
|
block
|
|
|
|
?gas
|
|
|
|
(program : Michelson_v1_parser.parsed) =
|
|
|
|
Alpha_services.Helpers.typecheck_code cctxt (chain, block) (program.expanded, gas)
|
2017-11-07 20:38:11 +04:00
|
|
|
|
|
|
|
let print_typecheck_result
|
2018-03-24 05:03:03 +04:00
|
|
|
~emacs ~show_types ~print_source_on_error
|
2018-02-16 21:10:18 +04:00
|
|
|
program res (cctxt : #Client_context.printer) =
|
2017-11-07 20:38:11 +04:00
|
|
|
if emacs then
|
2018-02-10 07:28:32 +04:00
|
|
|
let type_map, errs, _gas = match res with
|
|
|
|
| Ok (type_map, gas) -> (type_map, [], Some gas)
|
2018-02-11 22:17:39 +04:00
|
|
|
| Error (Alpha_environment.Ecoproto_error
|
2018-02-21 23:58:53 +04:00
|
|
|
(Script_tc_errors.Ill_typed_contract (_, type_map ))
|
2017-11-07 20:38:11 +04:00
|
|
|
:: _ as errs) ->
|
2018-02-10 07:28:32 +04:00
|
|
|
(type_map, errs, None)
|
2017-11-07 20:38:11 +04:00
|
|
|
| Error errs ->
|
2018-02-10 07:28:32 +04:00
|
|
|
([], errs, None) in
|
2017-11-07 20:38:11 +04:00
|
|
|
cctxt#message
|
|
|
|
"(@[<v 0>(types . %a)@ (errors . %a)@])"
|
|
|
|
Michelson_v1_emacs.print_type_map (program, type_map)
|
|
|
|
Michelson_v1_emacs.report_errors (program, errs) >>= fun () ->
|
|
|
|
return ()
|
|
|
|
else
|
|
|
|
match res with
|
2018-02-10 07:28:32 +04:00
|
|
|
| Ok (type_map, gas) ->
|
2017-11-07 20:38:11 +04:00
|
|
|
let program = Michelson_v1_printer.inject_types type_map program in
|
2018-03-24 05:03:03 +04:00
|
|
|
cctxt#message "@[<v 0>Well typed@,Gas remaining: %a@]"
|
2018-02-10 07:28:32 +04:00
|
|
|
Gas.pp gas >>= fun () ->
|
2017-11-07 20:38:11 +04:00
|
|
|
if show_types then
|
|
|
|
cctxt#message "%a" Micheline_printer.print_expr program >>= fun () ->
|
|
|
|
return ()
|
|
|
|
else return ()
|
|
|
|
| Error errs ->
|
|
|
|
cctxt#warning "%a"
|
|
|
|
(Michelson_v1_error_reporter.report_errors
|
|
|
|
~details: show_types
|
|
|
|
~show_source:print_source_on_error
|
|
|
|
~parsed:program) errs >>= fun () ->
|
|
|
|
cctxt#error "ill-typed program"
|