From 6d900e3e52bdf3252eb2f24fd1941fc53560418d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Wed, 21 Feb 2018 22:08:12 +0100 Subject: [PATCH] Alpha: remove index of public keys For delegate keys, we now use the revealed key of the associated delegate contract. --- .../lib_client/client_proto_contracts.ml | 14 ------ .../lib_client/client_proto_contracts.mli | 7 --- .../lib_protocol/src/TEZOS_PROTOCOL | 1 - .../lib_protocol/src/alpha_context.ml | 2 - .../lib_protocol/src/alpha_context.mli | 21 ++------ src/proto_alpha/lib_protocol/src/apply.ml | 2 +- .../lib_protocol/src/bootstrap_storage.ml | 1 - .../lib_protocol/src/contract_storage.ml | 16 +----- .../lib_protocol/src/delegate_services.ml | 50 ------------------- .../lib_protocol/src/delegate_services.mli | 13 ----- .../lib_protocol/src/helpers_services.ml | 4 +- .../lib_protocol/src/public_key_storage.ml | 50 ------------------- .../lib_protocol/src/public_key_storage.mli | 24 --------- .../lib_protocol/src/roll_storage.ml | 32 +++++++++--- .../lib_protocol/src/roll_storage.mli | 5 ++ src/proto_alpha/lib_protocol/src/storage.ml | 10 ---- src/proto_alpha/lib_protocol/src/storage.mli | 8 --- 17 files changed, 39 insertions(+), 221 deletions(-) delete mode 100644 src/proto_alpha/lib_protocol/src/public_key_storage.ml delete mode 100644 src/proto_alpha/lib_protocol/src/public_key_storage.mli diff --git a/src/proto_alpha/lib_client/client_proto_contracts.ml b/src/proto_alpha/lib_client/client_proto_contracts.ml index 334dcd7f0..d6ca6dce1 100644 --- a/src/proto_alpha/lib_client/client_proto_contracts.ml +++ b/src/proto_alpha/lib_client/client_proto_contracts.ml @@ -154,17 +154,3 @@ let may_check_key sourcePubKey sourcePubKeyHash = (failure "Invalid public key in `client_proto_endorsement`") | None -> return () - -let check_public_key cctxt block ?src_pk src_pk_hash = - Alpha_services.Delegate.Key.get cctxt block src_pk_hash >>= function - | Error errors -> - begin - match src_pk with - | None -> - failwith "Unknown public key@ %a" pp_print_error errors - | Some key -> - may_check_key src_pk src_pk_hash >>=? fun () -> - return (Some key) - end - | Ok _ -> return None - diff --git a/src/proto_alpha/lib_client/client_proto_contracts.mli b/src/proto_alpha/lib_client/client_proto_contracts.mli index 9b3bf9964..0de327619 100644 --- a/src/proto_alpha/lib_client/client_proto_contracts.mli +++ b/src/proto_alpha/lib_client/client_proto_contracts.mli @@ -53,13 +53,6 @@ val get_delegate: Contract.t -> public_key_hash tzresult Lwt.t -val check_public_key : - #Proto_alpha.rpc_context -> - Block_services.block -> - ?src_pk:public_key -> - public_key_hash -> - public_key option tzresult Lwt.t - module Contract_tags : module type of Client_tags.Tags (struct let name = "contract" end) diff --git a/src/proto_alpha/lib_protocol/src/TEZOS_PROTOCOL b/src/proto_alpha/lib_protocol/src/TEZOS_PROTOCOL index 411a342b0..6642838d4 100644 --- a/src/proto_alpha/lib_protocol/src/TEZOS_PROTOCOL +++ b/src/proto_alpha/lib_protocol/src/TEZOS_PROTOCOL @@ -37,7 +37,6 @@ "Level_storage", "Nonce_storage", "Seed_storage", - "Public_key_storage", "Roll_storage", "Contract_storage", "Reward_storage", diff --git a/src/proto_alpha/lib_protocol/src/alpha_context.ml b/src/proto_alpha/lib_protocol/src/alpha_context.ml index f785a4df8..77b3e1681 100644 --- a/src/proto_alpha/lib_protocol/src/alpha_context.ml +++ b/src/proto_alpha/lib_protocol/src/alpha_context.ml @@ -94,8 +94,6 @@ module Constants = struct constants.michelson_maximum_type_size end -module Delegates_pubkey = Public_key_storage - module Voting_period = Voting_period_repr module Level = struct diff --git a/src/proto_alpha/lib_protocol/src/alpha_context.mli b/src/proto_alpha/lib_protocol/src/alpha_context.mli index 9317bacaa..c49d9c1a5 100644 --- a/src/proto_alpha/lib_protocol/src/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/src/alpha_context.mli @@ -275,23 +275,6 @@ module Constants : sig val michelson_maximum_type_size: context -> int end -(** Global storage for all delegates public keys *) -module Delegates_pubkey : sig - - val get: - context -> public_key_hash -> public_key tzresult Lwt.t - val get_option: - context -> public_key_hash -> public_key option tzresult Lwt.t - val reveal: - context -> public_key_hash -> public_key -> context tzresult Lwt.t - val remove: - context -> public_key_hash -> context Lwt.t - - val list: - context -> (public_key_hash * public_key) list Lwt.t - -end - module Voting_period : sig include BASIC_DATA @@ -423,6 +406,7 @@ module Contract : sig val get_delegate_opt: context -> contract -> public_key_hash option tzresult Lwt.t + val is_delegatable: context -> contract -> bool tzresult Lwt.t val is_spendable: @@ -705,6 +689,9 @@ module Roll : sig val endorsement_rights_owner: context -> Level.t -> slot:int -> public_key tzresult Lwt.t + val delegate_pubkey: + context -> public_key_hash -> public_key tzresult Lwt.t + end module Reward : sig diff --git a/src/proto_alpha/lib_protocol/src/apply.ml b/src/proto_alpha/lib_protocol/src/apply.ml index 2b53b76d2..8d0e2ab51 100644 --- a/src/proto_alpha/lib_protocol/src/apply.ml +++ b/src/proto_alpha/lib_protocol/src/apply.ml @@ -254,7 +254,7 @@ let apply_sourced_operation pred_block block_prio operation content >>=? fun ctxt -> return (ctxt, origination_nonce, None) | Amendment_operation { source ; operation = content } -> - Delegates_pubkey.get ctxt source >>=? fun delegate -> + Roll.delegate_pubkey ctxt source >>=? fun delegate -> Operation.check_signature delegate operation >>=? fun () -> (* TODO, see how to extract the public key hash after this operation to pass it to apply_delegate_operation_content *) diff --git a/src/proto_alpha/lib_protocol/src/bootstrap_storage.ml b/src/proto_alpha/lib_protocol/src/bootstrap_storage.ml index 703e95bfa..9e6a83b60 100644 --- a/src/proto_alpha/lib_protocol/src/bootstrap_storage.ml +++ b/src/proto_alpha/lib_protocol/src/bootstrap_storage.ml @@ -13,7 +13,6 @@ type account = { } let init_account ctxt account = - Storage.Public_key.init ctxt account.public_key_hash account.public_key >>=? fun ctxt -> let contract = Contract_repr.implicit_contract account.public_key_hash in Contract_storage.credit ctxt contract Constants_repr.bootstrap_wealth >>=? fun ctxt -> diff --git a/src/proto_alpha/lib_protocol/src/contract_storage.ml b/src/proto_alpha/lib_protocol/src/contract_storage.ml index a5628ca21..ea23738e3 100644 --- a/src/proto_alpha/lib_protocol/src/contract_storage.ml +++ b/src/proto_alpha/lib_protocol/src/contract_storage.ml @@ -18,7 +18,6 @@ type error += | Inconsistent_public_key of Ed25519.Public_key.t * Ed25519.Public_key.t (* `Permanent *) | Missing_public_key of Ed25519.Public_key_hash.t (* `Permanent *) | Failure of string (* `Permanent *) - | Unregistred_delegate of Ed25519.Public_key_hash.t (* `Permanent *) let () = register_error_kind @@ -151,18 +150,7 @@ let () = ~pp:(fun ppf s -> Format.fprintf ppf "Contract_storage.Failure %S" s) Data_encoding.(obj1 (req "message" string)) (function Failure s -> Some s | _ -> None) - (fun s -> Failure s) ; - register_error_kind - `Permanent - ~id:"contract.manager.unregistred_delegate" - ~title:"Unregistred delegate" - ~description:"A contract cannot be delegated to an unregistred delegate" - ~pp:(fun ppf (k) -> - Format.fprintf ppf "The delegate public key (with hash %a) is missing" - Ed25519.Public_key_hash.pp k) - Data_encoding.(obj1 (req "hash" Ed25519.Public_key_hash.encoding)) - (function Unregistred_delegate (k) -> Some (k) | _ -> None) - (fun (k) -> Unregistred_delegate (k)) + (fun s -> Failure s) let failwith msg = fail (Failure msg) @@ -354,7 +342,7 @@ let set_delegate c contract delegate = | Some pkh -> Ed25519.Public_key_hash.equal pkh delegate | None -> false in if not known_delegate || not (registred_delegate || self_delegation) then - fail (Unregistred_delegate delegate) + fail (Roll_storage.Unregistred_delegate delegate) else if not (delegatable || self_delegation) then fail (Non_delegatable_contract contract) else diff --git a/src/proto_alpha/lib_protocol/src/delegate_services.ml b/src/proto_alpha/lib_protocol/src/delegate_services.ml index e2db6a314..35c46d6b0 100644 --- a/src/proto_alpha/lib_protocol/src/delegate_services.ml +++ b/src/proto_alpha/lib_protocol/src/delegate_services.ml @@ -321,55 +321,5 @@ module Endorser = struct end -module Key = struct - - module S = struct - - open Data_encoding - - let custom_root = - RPC_path.(open_root / "context" / "key") - - let pk_encoding = - (obj2 - (req "hash" Ed25519.Public_key_hash.encoding) - (req "public_key" Ed25519.Public_key.encoding)) - - let list = - RPC_service.post_service - ~description: "List the known public keys" - ~query: RPC_query.empty - ~input: empty - ~output: (list pk_encoding) - custom_root - - let get = - RPC_service.post_service - ~description: "Fetch the stored public key" - ~query: RPC_query.empty - ~input: empty - ~output: pk_encoding - RPC_path.(custom_root /: Ed25519.Public_key_hash.rpc_arg ) - - end - - let () = - let open Services_registration in - register1 S.get begin fun ctxt hash () () -> - Delegates_pubkey.get ctxt hash >>=? fun pk -> - return (hash, pk) - end ; - register0 S.list begin fun ctxt () () -> - Delegates_pubkey.list ctxt >>= return - end - - let list ctxt block = - RPC_context.make_call0 S.list ctxt block () () - - let get ctxt block pkh = - RPC_context.make_call1 S.get ctxt block pkh () () - -end - let baking_rights = Baker.I.baking_rights let endorsement_rights = Endorser.I.endorsement_rights diff --git a/src/proto_alpha/lib_protocol/src/delegate_services.mli b/src/proto_alpha/lib_protocol/src/delegate_services.mli index ce0a631e5..9a000799f 100644 --- a/src/proto_alpha/lib_protocol/src/delegate_services.mli +++ b/src/proto_alpha/lib_protocol/src/delegate_services.mli @@ -45,19 +45,6 @@ module Endorser : sig end -module Key : sig - - val list: - 'a #RPC_context.simple -> 'a -> - (Ed25519.Public_key_hash.t * Ed25519.Public_key.t) list shell_tzresult Lwt.t - - val get: - 'a #RPC_context.simple -> 'a -> - Ed25519.Public_key_hash.t -> - (Ed25519.Public_key_hash.t * Ed25519.Public_key.t) shell_tzresult Lwt.t - -end - (* temporary export *) val endorsement_rights: Alpha_context.t -> diff --git a/src/proto_alpha/lib_protocol/src/helpers_services.ml b/src/proto_alpha/lib_protocol/src/helpers_services.ml index c77055cac..7a42f6b74 100644 --- a/src/proto_alpha/lib_protocol/src/helpers_services.ml +++ b/src/proto_alpha/lib_protocol/src/helpers_services.ml @@ -477,7 +477,7 @@ module Parse = struct | Some key -> return key | None -> Contract.get_manager ctxt op.source >>=? fun manager -> - Delegates_pubkey.get ctxt manager + Roll.delegate_pubkey ctxt manager end >>=? fun public_key -> Operation.check_signature public_key { signature ; shell ; contents ; hash = Operation_hash.zero } @@ -487,7 +487,7 @@ module Parse = struct Operation.check_signature public_key { signature ; shell ; contents ; hash = Operation_hash.zero } | Sourced_operations (Amendment_operation { source ; _ }) -> - Delegates_pubkey.get ctxt source >>=? fun source -> + Roll.delegate_pubkey ctxt source >>=? fun source -> Operation.check_signature source { signature ; shell ; contents ; hash = Operation_hash.zero } | Sourced_operations (Dictator_operation _) -> diff --git a/src/proto_alpha/lib_protocol/src/public_key_storage.ml b/src/proto_alpha/lib_protocol/src/public_key_storage.ml deleted file mode 100644 index aa6ddcebf..000000000 --- a/src/proto_alpha/lib_protocol/src/public_key_storage.ml +++ /dev/null @@ -1,50 +0,0 @@ -(**************************************************************************) -(* *) -(* Copyright (c) 2014 - 2018. *) -(* Dynamic Ledger Solutions, Inc. *) -(* *) -(* All rights reserved. No warranty, explicit or implicit, provided. *) -(* *) -(**************************************************************************) - -open Ed25519 - -type error += Inconsistent_hash of Public_key.t * Public_key_hash.t * Public_key_hash.t - -let () = - register_error_kind - `Permanent - ~id:"public_key.inconsistent_hash" - ~title:"Inconsistent public key hash" - ~description:"A revealed public key is inconsistent with the announced hash" - ~pp:(fun ppf (k, eh, ph) -> - Format.fprintf ppf "Hash of public key %s is not %a as announced but %a" - (Public_key.to_b58check k) - Public_key_hash.pp ph - Public_key_hash.pp eh) - Data_encoding.(obj3 - (req "public_key" Public_key.encoding) - (req "expected_hash" Public_key_hash.encoding) - (req "provided_hash" Public_key_hash.encoding)) - (function Inconsistent_hash (k, eh, ph) -> Some (k, eh, ph) | _ -> None) - (fun (k, eh, ph) -> Inconsistent_hash (k, eh, ph)) - -let get = Storage.Public_key.get - -let get_option = Storage.Public_key.get_option - -let reveal c hash key = - let actual_hash = Ed25519.Public_key.hash key in - if Ed25519.Public_key_hash.equal hash actual_hash then - Storage.Public_key.init_set c hash key >>= return - else - fail (Inconsistent_hash (key, actual_hash, hash)) - -let remove = Storage.Public_key.remove - -let list ctxt = - Storage.Public_key.fold ctxt - ~init:[] - ~f:begin fun pk_h pk acc -> - Lwt.return @@ (pk_h, pk) :: acc - end diff --git a/src/proto_alpha/lib_protocol/src/public_key_storage.mli b/src/proto_alpha/lib_protocol/src/public_key_storage.mli deleted file mode 100644 index 758b0eb90..000000000 --- a/src/proto_alpha/lib_protocol/src/public_key_storage.mli +++ /dev/null @@ -1,24 +0,0 @@ -(**************************************************************************) -(* *) -(* Copyright (c) 2014 - 2018. *) -(* Dynamic Ledger Solutions, Inc. *) -(* *) -(* All rights reserved. No warranty, explicit or implicit, provided. *) -(* *) -(**************************************************************************) - -open Ed25519 - -type error += Inconsistent_hash of Public_key.t * Public_key_hash.t * Public_key_hash.t - -val get: - Raw_context.t -> Public_key_hash.t -> Public_key.t tzresult Lwt.t -val get_option: - Raw_context.t -> Public_key_hash.t -> Public_key.t option tzresult Lwt.t -val reveal: - Raw_context.t -> Public_key_hash.t -> Public_key.t -> Raw_context.t tzresult Lwt.t -val remove: - Raw_context.t -> Public_key_hash.t -> Raw_context.t Lwt.t - -val list: - Raw_context.t -> (Public_key_hash.t * Public_key.t) list Lwt.t diff --git a/src/proto_alpha/lib_protocol/src/roll_storage.ml b/src/proto_alpha/lib_protocol/src/roll_storage.ml index 60a6c0375..1f6719dd1 100644 --- a/src/proto_alpha/lib_protocol/src/roll_storage.ml +++ b/src/proto_alpha/lib_protocol/src/roll_storage.ml @@ -12,14 +12,35 @@ type error += | No_roll_in_contract | Deleted_contract_owning_rolls | No_roll_snapshot_for_cycle of Cycle_repr.t + | Unregistred_delegate of Ed25519.Public_key_hash.t (* `Permanent *) + +let () = + register_error_kind + `Permanent + ~id:"contract.manager.unregistred_delegate" + ~title:"Unregistred delegate" + ~description:"A contract cannot be delegated to an unregistred delegate" + ~pp:(fun ppf (k) -> + Format.fprintf ppf "The provided public key (with hash %a) is \ + \ not registred as valid delegate key." + Ed25519.Public_key_hash.pp k) + Data_encoding.(obj1 (req "hash" Ed25519.Public_key_hash.encoding)) + (function Unregistred_delegate (k) -> Some (k) | _ -> None) + (fun (k) -> Unregistred_delegate (k)) let get_contract_delegate c contract = Storage.Contract.Delegate.get_option c contract let get_contract_delegate_at_cycle c cycle contract = - match Contract_repr.is_implicit contract with - | Some manager -> return (Some manager) - | None -> Storage.Contract.Delegate.Snapshot.get_option c (cycle, contract) + Storage.Contract.Delegate.Snapshot.get_option c (cycle, contract) + +let delegate_pubkey ctxt delegate = + Storage.Contract.Manager.get_option ctxt + (Contract_repr.implicit_contract delegate) >>=? function + | None | Some (Manager_repr.Hash _) -> + fail (Unregistred_delegate delegate) + | Some (Manager_repr.Public_key pk) -> + return pk let clear_cycle c cycle = Storage.Roll.Last_for_cycle.delete c cycle >>=? fun c -> @@ -78,10 +99,7 @@ module Random = struct | None -> loop sequence | Some delegate -> - Public_key_storage.get_option c delegate >>=? function - | None -> loop sequence - | Some delegate -> return delegate - in + delegate_pubkey c delegate in Storage.Roll.Owner.snapshot_exists c cycle >>= fun snapshot_exists -> fail_unless snapshot_exists (No_roll_snapshot_for_cycle cycle) >>=? fun () -> loop sequence diff --git a/src/proto_alpha/lib_protocol/src/roll_storage.mli b/src/proto_alpha/lib_protocol/src/roll_storage.mli index cc49770af..8eb638bd4 100644 --- a/src/proto_alpha/lib_protocol/src/roll_storage.mli +++ b/src/proto_alpha/lib_protocol/src/roll_storage.mli @@ -20,6 +20,7 @@ type error += | Consume_roll_change | No_roll_in_contract + | Unregistred_delegate of Ed25519.Public_key_hash.t (* `Permanent *) val init : Raw_context.t -> Raw_context.t tzresult Lwt.t @@ -57,6 +58,10 @@ module Contract : sig end +val delegate_pubkey: + Raw_context.t -> Ed25519.Public_key_hash.t -> + Ed25519.Public_key.t tzresult Lwt.t + (**/**) val get_contract_delegate: diff --git a/src/proto_alpha/lib_protocol/src/storage.ml b/src/proto_alpha/lib_protocol/src/storage.ml index ae5194b0b..dfbc7ba46 100644 --- a/src/proto_alpha/lib_protocol/src/storage.ml +++ b/src/proto_alpha/lib_protocol/src/storage.ml @@ -308,16 +308,6 @@ module Vote = struct end -(** Keys *) - -module Public_key = - Make_indexed_data_storage - (Make_subcontext - (Raw_context) - (struct let name = ["public_keys"; "ed25519"] end)) - (Ed25519.Public_key_hash) - (Make_value(Ed25519.Public_key)) - (** Seed *) module Seed = struct diff --git a/src/proto_alpha/lib_protocol/src/storage.mli b/src/proto_alpha/lib_protocol/src/storage.mli index 21bee92d4..791f6ba9e 100644 --- a/src/proto_alpha/lib_protocol/src/storage.mli +++ b/src/proto_alpha/lib_protocol/src/storage.mli @@ -188,14 +188,6 @@ module Vote : sig end - -(** Keys *) - -module Public_key : Indexed_data_storage - with type key = Ed25519.Public_key_hash.t - and type value = Ed25519.Public_key.t - and type t := Raw_context.t - (** Seed *) module Seed : sig