From 4c1e4bc6cd585f7cc5553c81726eba025e7223c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gr=C3=A9goire=20Henry?= Date: Fri, 23 Feb 2018 09:43:07 -0500 Subject: [PATCH] Alpha: use 'Data_set' for storing the spendable flag --- .../lib_protocol/src/contract_storage.ml | 24 +++++++------------ src/proto_alpha/lib_protocol/src/storage.ml | 8 +------ src/proto_alpha/lib_protocol/src/storage.mli | 5 ++-- .../lib_protocol/src/storage_functors.ml | 6 +++++ .../lib_protocol/src/storage_sigs.ml | 3 +++ 5 files changed, 20 insertions(+), 26 deletions(-) diff --git a/src/proto_alpha/lib_protocol/src/contract_storage.ml b/src/proto_alpha/lib_protocol/src/contract_storage.ml index 524d440c9..c65d60434 100644 --- a/src/proto_alpha/lib_protocol/src/contract_storage.ml +++ b/src/proto_alpha/lib_protocol/src/contract_storage.ml @@ -192,13 +192,8 @@ let create_base c contract Storage.Contract.Delegate.init c contract delegate >>=? fun c -> link_delegate c contract delegate balance end >>=? fun c -> - Storage.Contract.Spendable.init c contract spendable >>=? fun c -> - begin - if delegatable then - Storage.Contract.Delegatable.add c contract - else - Lwt.return c - end >>= fun c -> + Storage.Contract.Spendable.set c contract spendable >>= fun c -> + Storage.Contract.Delegatable.set c contract delegatable >>= fun c -> Storage.Contract.Counter.init c contract counter >>=? fun c -> (match script with | Some ({ Script_repr.code ; storage }, (code_fees, storage_fees)) -> @@ -225,7 +220,7 @@ let delete c contract = Storage.Contract.Balance.delete c contract >>=? fun c -> Storage.Contract.Manager.delete c contract >>=? fun c -> Storage.Contract.Delegate.remove c contract >>= fun c -> - Storage.Contract.Spendable.delete c contract >>=? fun c -> + Storage.Contract.Spendable.del c contract >>= fun c -> Storage.Contract.Delegatable.del c contract >>= fun c -> Storage.Contract.Counter.delete c contract >>=? fun c -> Storage.Contract.Code.remove c contract >>= fun c -> @@ -332,13 +327,10 @@ let is_delegatable c contract = Storage.Contract.Delegatable.mem c contract >>= return let is_spendable c contract = - Storage.Contract.Spendable.get_option c contract >>=? function - | None -> begin - match Contract_repr.is_implicit contract with - | Some _ -> return true - | None -> failwith "is_spendable" - end - | Some v -> return v + match Contract_repr.is_implicit contract with + | Some _ -> return true + | None -> + Storage.Contract.Spendable.mem c contract >>= return let set_delegate c contract delegate = match delegate with @@ -442,7 +434,7 @@ let credit c contract amount = Roll_storage.Contract.add_amount c contract amount let spend c contract amount = - Storage.Contract.Spendable.get c contract >>=? fun spendable -> + is_spendable c contract >>=? fun spendable -> if not spendable then fail (Unspendable_contract contract) else spend_from_script c contract amount diff --git a/src/proto_alpha/lib_protocol/src/storage.ml b/src/proto_alpha/lib_protocol/src/storage.ml index ab278041a..7ea5d4a0a 100644 --- a/src/proto_alpha/lib_protocol/src/storage.ml +++ b/src/proto_alpha/lib_protocol/src/storage.ml @@ -14,11 +14,6 @@ module Int32 = struct let encoding = Data_encoding.int32 end -module Bool = struct - type t = bool - let encoding = Data_encoding.bool -end - module String_index = struct type t = string let path_length = 1 @@ -60,9 +55,8 @@ module Contract = struct (Make_value(Manager_repr)) module Spendable = - Indexed_context.Make_map + Indexed_context.Make_set (struct let name = ["spendable"] end) - (Make_value(Bool)) module Delegatable = Indexed_context.Make_set diff --git a/src/proto_alpha/lib_protocol/src/storage.mli b/src/proto_alpha/lib_protocol/src/storage.mli index d251054aa..7f7d5ae62 100644 --- a/src/proto_alpha/lib_protocol/src/storage.mli +++ b/src/proto_alpha/lib_protocol/src/storage.mli @@ -112,9 +112,8 @@ module Contract : sig with type elt = Contract_hash.t and type t = Raw_context.t * Contract_repr.t - module Spendable : Indexed_data_storage - with type key = Contract_repr.t - and type value = bool + module Spendable : Data_set_storage + with type elt = Contract_repr.t and type t := Raw_context.t module Delegatable : Data_set_storage diff --git a/src/proto_alpha/lib_protocol/src/storage_functors.ml b/src/proto_alpha/lib_protocol/src/storage_functors.ml index 58285de31..7b0b58cab 100644 --- a/src/proto_alpha/lib_protocol/src/storage_functors.ml +++ b/src/proto_alpha/lib_protocol/src/storage_functors.ml @@ -139,6 +139,9 @@ module Make_data_set_storage (C : Raw_context.T) (I : INDEX) let del s i = C.remove s (I.to_path i []) >>= fun t -> Lwt.return (C.project t) + let set s i = function + | true -> add s i + | false -> del s i let clear s = C.remove_rec s [] >>= fun t -> Lwt.return (C.project t) @@ -398,6 +401,9 @@ module Make_indexed_subcontext (C : Raw_context.T) (I : INDEX) let del s i = Raw_context.remove (s, i) N.name >>= fun (s, _) -> Lwt.return (C.project s) + let set s i = function + | true -> add s i + | false -> del s i let clear s = fold_keys s ~init:s diff --git a/src/proto_alpha/lib_protocol/src/storage_sigs.ml b/src/proto_alpha/lib_protocol/src/storage_sigs.ml index d59dfac20..31950da55 100644 --- a/src/proto_alpha/lib_protocol/src/storage_sigs.ml +++ b/src/proto_alpha/lib_protocol/src/storage_sigs.ml @@ -178,6 +178,9 @@ module type Data_set_storage = sig (** Removes a elt of the set ; does nothing if not a member *) val del: context -> elt -> Raw_context.t Lwt.t + (** Adds/Removes a elt of the set *) + val set: context -> elt -> bool -> Raw_context.t Lwt.t + (** Returns the elements of the set, deserialized in a list in no particular order. *) val elements: context -> elt list Lwt.t