Alpha: use 'Data_set' for storing the spendable flag

This commit is contained in:
Grégoire Henry 2018-02-23 09:43:07 -05:00 committed by Benjamin Canou
parent 9ff3be14b6
commit 4c1e4bc6cd
5 changed files with 20 additions and 26 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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