Improves error messages and defaults for transfer -arg

This commit is contained in:
Milo Davis 2017-08-02 10:22:50 +02:00 committed by Benjamin Canou
parent 58b53d79c5
commit 4c31d084e1
2 changed files with 59 additions and 22 deletions

View File

@ -275,6 +275,23 @@ let report_errors cctxt errs =
| Some s -> Format.fprintf ppf "%s " s) | Some s -> Format.fprintf ppf "%s " s)
name name
(print_expr locations) expr (print_expr locations) expr
| Apply.Bad_contract_parameter (c, None, _) ->
cctxt.warning
"@[<v 0>Account %a is not a smart contract, it does not take arguments.@,\
The `-arg' flag cannot be used when transferring to an account.@]"
Contract.pp c
| Apply.Bad_contract_parameter (c, Some expected, None) ->
cctxt.warning
"@[<v 0>Contract %a expected an argument of type@, %a@,but no argument was provided.@,\
The `-arg' flag can be used when transferring to a smart contract.@]"
Contract.pp c
(print_expr_unwrapped no_locations) expected
| Apply.Bad_contract_parameter (c, Some expected, Some argument) ->
cctxt.warning
"@[<v 0>Contract %a expected an argument of type@, %a@but received@, %a@]"
Contract.pp c
(print_expr_unwrapped no_locations) expected
(print_expr_unwrapped no_locations) argument
| Ill_typed_contract (expr, arg_ty, ret_ty, storage_ty, type_map) -> | Ill_typed_contract (expr, arg_ty, ret_ty, storage_ty, type_map) ->
cctxt.warning cctxt.warning
"@[<v 2>Ill typed contract:@ %a@]" "@[<v 2>Ill typed contract:@ %a@]"

View File

@ -13,6 +13,7 @@ open Tezos_context
type error += Wrong_voting_period of Voting_period.t * Voting_period.t (* `Temporary *) type error += Wrong_voting_period of Voting_period.t * Voting_period.t (* `Temporary *)
type error += Wrong_endorsement_predecessor of Block_hash.t * Block_hash.t (* `Temporary *) type error += Wrong_endorsement_predecessor of Block_hash.t * Block_hash.t (* `Temporary *)
type error += Bad_contract_parameter of Contract.t * Script.expr option * Script.expr option (* `Permanent *)
let () = let () =
register_error_kind register_error_kind
@ -42,7 +43,21 @@ let () =
(req "current" Voting_period.encoding) (req "current" Voting_period.encoding)
(req "provided" Voting_period.encoding)) (req "provided" Voting_period.encoding))
(function Wrong_voting_period (e, p) -> Some (e, p) | _ -> None) (function Wrong_voting_period (e, p) -> Some (e, p) | _ -> None)
(fun (e, p) -> Wrong_voting_period (e, p)) (fun (e, p) -> Wrong_voting_period (e, p));
register_error_kind
`Permanent
~id:"badContractParameter"
~title:"Contract supplied an invalid parameter"
~description:"Either no parameter was supplied to a contract, \
a parameter was passed to an account, \
or a parameter was supplied of the wrong type"
Data_encoding.(obj3
(req "contract" Contract.encoding)
(opt "expectedType" Script.expr_encoding)
(opt "providedArgument" Script.expr_encoding))
(function Bad_contract_parameter (c, expected, supplied) ->
Some (c, expected, supplied) | _ -> None)
(fun (c, expected, supplied) -> Bad_contract_parameter (c, expected, supplied))
let apply_delegate_operation_content let apply_delegate_operation_content
ctxt delegate pred_block block_priority = function ctxt delegate pred_block block_priority = function
@ -68,9 +83,6 @@ let apply_delegate_operation_content
(Wrong_voting_period (level.voting_period, period)) >>=? fun () -> (Wrong_voting_period (level.voting_period, period)) >>=? fun () ->
Amendment.record_ballot ctxt delegate proposal ballot Amendment.record_ballot ctxt delegate proposal ballot
type error += Non_scripted_contract_with_parameter
type error += Scripted_contract_without_paramater
let apply_manager_operation_content let apply_manager_operation_content
ctxt origination_nonce source = function ctxt origination_nonce source = function
| Transaction { amount ; parameters ; destination } -> begin | Transaction { amount ; parameters ; destination } -> begin
@ -81,15 +93,13 @@ let apply_manager_operation_content
match parameters with match parameters with
| None | Some (Prim (_, "Unit", [])) -> | None | Some (Prim (_, "Unit", [])) ->
return (ctxt, origination_nonce, None) return (ctxt, origination_nonce, None)
| Some _ -> fail Non_scripted_contract_with_parameter | Some _ -> fail (Bad_contract_parameter (destination, None, parameters))
end end
| Some { code ; storage } -> | Some { code ; storage } ->
match parameters with let call_contract argument =
| None -> fail Scripted_contract_without_paramater
| Some parameters ->
Script_interpreter.execute Script_interpreter.execute
origination_nonce origination_nonce
source destination ctxt storage code amount parameters source destination ctxt storage code amount argument
(Constants.instructions_per_transaction ctxt) (Constants.instructions_per_transaction ctxt)
>>= function >>= function
| Ok (storage_res, _res, _steps, ctxt, origination_nonce) -> | Ok (storage_res, _res, _steps, ctxt, origination_nonce) ->
@ -100,7 +110,17 @@ let apply_manager_operation_content
Script_interpreter.dummy_storage_fee storage_res >>=? fun ctxt -> Script_interpreter.dummy_storage_fee storage_res >>=? fun ctxt ->
return (ctxt, origination_nonce, None) return (ctxt, origination_nonce, None)
| Error err -> | Error err ->
return (ctxt, origination_nonce, Some err) return (ctxt, origination_nonce, Some err) in
match parameters, code.arg_type with
| None, Prim (_, "unit", _) -> call_contract (Prim (0, "Unit", []))
| Some parameters, arg_type -> begin
Script_ir_translator.typecheck_data ctxt (parameters, arg_type) >>= function
| Ok () -> call_contract parameters
| Error errs ->
let err = Bad_contract_parameter (destination, Some arg_type, Some parameters) in
return (ctxt, origination_nonce, Some ((err :: errs)))
end
| None, arg_type -> fail (Bad_contract_parameter (destination, Some arg_type, None))
end end
| Origination { manager ; delegate ; script ; | Origination { manager ; delegate ; script ;
spendable ; delegatable ; credit } -> spendable ; delegatable ; credit } ->