From 040ac648ebf73805f99a8e5579ab497ab38a4492 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Thu, 15 Mar 2018 14:30:30 +0100 Subject: [PATCH] Alpha: better error message when registering delegates --- .../lib_protocol/src/delegate_storage.ml | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/proto_alpha/lib_protocol/src/delegate_storage.ml b/src/proto_alpha/lib_protocol/src/delegate_storage.ml index ffe6d4dba..34c7c46ab 100644 --- a/src/proto_alpha/lib_protocol/src/delegate_storage.ml +++ b/src/proto_alpha/lib_protocol/src/delegate_storage.ml @@ -12,6 +12,7 @@ type error += | No_deletion of Ed25519.Public_key_hash.t (* `Permanent *) | Active_delegate (* `Temporary *) | Current_delegate (* `Temporary *) + | Empty_delegate_account of Ed25519.Public_key_hash.t (* `Temporary *) let () = register_error_kind @@ -58,7 +59,20 @@ let () = "The contract is already delegated to the same delegate") Data_encoding.empty (function Current_delegate -> Some () | _ -> None) - (fun () -> Current_delegate) + (fun () -> Current_delegate) ; + register_error_kind + `Permanent + ~id:"delegate.empty_delegate_account" + ~title:"Empty delegate account" + ~description:"Cannot register a delegate when its implicit account is empty" + ~pp:(fun ppf delegate -> + Format.fprintf ppf + "Delegate registration is forbidden when the delegate + implicit account is empty (%a)" + Ed25519.Public_key_hash.pp delegate) + Data_encoding.(obj1 (req "delegate" Ed25519.Public_key_hash.encoding)) + (function Empty_delegate_account c -> Some c | _ -> None) + (fun c -> Empty_delegate_account c) let is_delegatable c contract = match Contract_repr.is_implicit contract with @@ -144,6 +158,10 @@ let set c contract delegate = fail Current_delegate | None | Some _ -> return () end >>=? fun () -> + Storage.Contract.Balance.mem c contract >>= fun exists -> + fail_when + (self_delegation && not exists) + (Empty_delegate_account delegate) >>=? fun () -> Storage.Contract.Balance.get c contract >>=? fun balance -> unlink c contract balance >>=? fun c -> Storage.Contract.Delegate.init_set c contract delegate >>= fun c ->