2019-12-18 16:34:29 +01:00
|
|
|
open Tezos_utils
|
|
|
|
open Proto_alpha_utils
|
|
|
|
open Trace
|
|
|
|
|
2019-12-31 19:14:42 +01:00
|
|
|
module Errors = struct
|
|
|
|
let title_type_check_msg () = "Invalid contract"
|
|
|
|
let bad_parameter () =
|
|
|
|
let message () =
|
2020-01-01 14:15:49 -08:00
|
|
|
"bad contract parameter type (some michelson types are forbidden as contract parameter)" in
|
2019-12-31 19:14:42 +01:00
|
|
|
error title_type_check_msg message
|
|
|
|
let bad_storage () =
|
|
|
|
let message () =
|
2020-01-01 14:15:49 -08:00
|
|
|
"bad storage type (some michelson types are forbidden as contract storage)" in
|
2019-12-31 19:14:42 +01:00
|
|
|
error title_type_check_msg message
|
|
|
|
let bad_contract () =
|
|
|
|
let message () =
|
2020-01-01 14:15:49 -08:00
|
|
|
"bad contract type (contract entry point is expected to be of the form \"parameter * storage -> list(operation) * storage\")" in
|
2019-12-31 19:14:42 +01:00
|
|
|
error title_type_check_msg message
|
|
|
|
let unknown () =
|
|
|
|
let message () =
|
|
|
|
"unknown error" in
|
|
|
|
error title_type_check_msg message
|
|
|
|
end
|
|
|
|
|
2019-12-18 16:34:29 +01:00
|
|
|
let build_contract : Compiler.compiled_expression -> Michelson.michelson result =
|
|
|
|
fun compiled ->
|
|
|
|
let%bind ((Ex_ty _param_ty),(Ex_ty _storage_ty)) = Self_michelson.fetch_contract_inputs compiled.expr_ty in
|
|
|
|
let%bind param_michelson =
|
2019-12-31 19:14:42 +01:00
|
|
|
Trace.trace_tzresult_lwt (simple_error "Could not unparse parameter") @@
|
2019-12-18 16:34:29 +01:00
|
|
|
Proto_alpha_utils.Memory_proto_alpha.unparse_ty_michelson _param_ty in
|
|
|
|
let%bind storage_michelson =
|
2019-12-31 19:14:42 +01:00
|
|
|
Trace.trace_tzresult_lwt (simple_error "Could not unparse storage") @@
|
2019-12-18 16:34:29 +01:00
|
|
|
Proto_alpha_utils.Memory_proto_alpha.unparse_ty_michelson _storage_ty in
|
|
|
|
let contract = Michelson.contract param_michelson storage_michelson compiled.expr in
|
2020-01-01 14:15:49 -08:00
|
|
|
let%bind res =
|
2019-12-31 19:14:42 +01:00
|
|
|
Trace.trace_tzresult_lwt (simple_error "Could not typecheck the code") @@
|
2019-12-18 16:34:29 +01:00
|
|
|
Proto_alpha_utils.Memory_proto_alpha.typecheck_contract contract in
|
2019-12-31 19:14:42 +01:00
|
|
|
match res with
|
|
|
|
| Type_checked -> ok contract
|
|
|
|
| Err_parameter -> fail @@ Errors.bad_parameter ()
|
|
|
|
| Err_storage -> fail @@ Errors.bad_storage ()
|
|
|
|
| Err_contract -> fail @@ Errors.bad_contract ()
|
|
|
|
| Err_unknown -> fail @@ Errors.unknown ()
|
2019-12-18 16:34:29 +01:00
|
|
|
|
|
|
|
type check_type = Check_parameter | Check_storage
|
|
|
|
let assert_equal_contract_type : check_type -> Compiler.compiled_expression -> Compiler.compiled_expression -> unit result =
|
|
|
|
fun c compiled_prg compiled_param ->
|
|
|
|
let%bind (Ex_ty expected_ty) =
|
|
|
|
let%bind (c_param_ty,c_storage_ty) = Self_michelson.fetch_contract_inputs compiled_prg.expr_ty in
|
|
|
|
match c with
|
|
|
|
| Check_parameter -> ok c_param_ty
|
|
|
|
| Check_storage -> ok c_storage_ty in
|
|
|
|
let (Ex_ty actual_ty) = compiled_param.expr_ty in
|
|
|
|
let%bind _ =
|
|
|
|
Trace.trace_tzresult (simple_error "Passed parameter does not match the contract type") @@
|
|
|
|
Proto_alpha_utils.Memory_proto_alpha.assert_equal_michelson_type expected_ty actual_ty in
|
2020-01-01 14:15:49 -08:00
|
|
|
ok ()
|