diff --git a/src/proto_alpha/lib_benchmarks_proto/carbonated_map_benchmarks.ml b/src/proto_alpha/lib_benchmarks_proto/carbonated_map_benchmarks.ml index 4e16ebb2653d3f91b2abde611270fe7884068be1..a4788a34a9fa37b529b3a7e45a5477c127638f84 100644 --- a/src/proto_alpha/lib_benchmarks_proto/carbonated_map_benchmarks.ml +++ b/src/proto_alpha/lib_benchmarks_proto/carbonated_map_benchmarks.ml @@ -60,6 +60,12 @@ let register (module BM : Benchmark.S) = BM.name (Model.For_codegen model)) +module Alpha_context_gas = struct + type context = Alpha_context.context + + let consume = Alpha_context.Gas.consume +end + (** Benchmarks the [fold] functions of [Carbonated_map]. This benchmark does not depend on the size of the keys or types of elements. @@ -89,7 +95,7 @@ module Fold_benchmark : Benchmark.S = struct let models = [("carbonated_map", fold_model)] let benchmark rng_state config () = - let module M = Carbonated_map.Make (Int) in + let module M = Carbonated_map.Make (Alpha_context_gas) (Int) in let (_, list) = let sampler rng_state = let key = Base_samplers.int rng_state ~size:{min = 1; max = 5} in @@ -187,12 +193,15 @@ module Make (CS : COMPARABLE_SAMPLER) = struct module Find = struct include Config_and_workload - module M = Carbonated_map.Make (struct - include CS + module M = + Carbonated_map.Make + (Alpha_context_gas) + (struct + include CS - (** Dummy cost*) - let compare_cost _ = Saturation_repr.safe_int 0 - end) + (** Dummy cost*) + let compare_cost _ = Saturation_repr.safe_int 0 + end) let name = carbonated_map_cost_name "find" @@ -284,12 +293,15 @@ module Make (CS : COMPARABLE_SAMPLER) = struct let workload_to_vector () = Sparse_vec.String.of_list [] - module M = Carbonated_map.Make (struct - include CS + module M = + Carbonated_map.Make + (Alpha_context_gas) + (struct + include CS - (** Dummy cost*) - let compare_cost _ = Saturation_repr.safe_int 0 - end) + (** Dummy cost*) + let compare_cost _ = Saturation_repr.safe_int 0 + end) let name = carbonated_map_cost_name "find_intercept" diff --git a/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL b/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL index f6016bcb1d124652d81a1577aef9bbc566b08a89..de2fcb055bad03e1f0c51cdd2a2d9aa72d18877b 100644 --- a/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL +++ b/src/proto_alpha/lib_protocol/TEZOS_PROTOCOL @@ -75,6 +75,8 @@ "Receipt_repr", "Migration_repr", "Sc_rollup_tick_repr", + "Carbonated_map_costs", + "Carbonated_map", "Raw_context_intf", "Raw_context", @@ -132,8 +134,6 @@ "Script_string", "Script_int", "Script_timestamp", - "Carbonated_map_costs", - "Carbonated_map", "Tx_rollup_l2_storage_sig", "Tx_rollup_l2_context_sig", diff --git a/src/proto_alpha/lib_protocol/carbonated_map.ml b/src/proto_alpha/lib_protocol/carbonated_map.ml index ceda9cd2c8537068eebe21fde3acb7907951b078..4e62c663c1a82d37e8a1da4486c6e33365c7deba 100644 --- a/src/proto_alpha/lib_protocol/carbonated_map.ml +++ b/src/proto_alpha/lib_protocol/carbonated_map.ml @@ -1,7 +1,7 @@ (*****************************************************************************) (* *) (* Open Source License *) -(* Copyright (c) 2021 Trili Tech, *) +(* Copyright (c) 2021-2022 Trili Tech, *) (* *) (* Permission is hereby granted, free of charge, to any person obtaining a *) (* copy of this software and associated documentation files (the "Software"),*) @@ -23,13 +23,13 @@ (* *) (*****************************************************************************) -open Alpha_context - module type S = sig type 'a t type key + type context + val empty : 'a t val singleton : key -> 'a -> 'a t @@ -74,15 +74,30 @@ module type S = sig ('state * context) tzresult end +module type GAS = sig + type context + + val consume : + context -> + Saturation_repr.may_saturate Saturation_repr.t -> + context tzresult +end + module type COMPARABLE = sig include Compare.COMPARABLE - val compare_cost : t -> Gas.cost + (** [compare_cost k] returns the cost of comparing the given key [k] with + another value of the same type. *) + val compare_cost : t -> Saturation_repr.may_saturate Saturation_repr.t end -module Make (C : COMPARABLE) = struct +module Make (G : GAS) (C : COMPARABLE) = struct module M = Map.Make (C) + type context = G.context + + type key = C.t + type 'a t = {map : 'a M.t; size : int} let empty = {map = M.empty; size = 0} @@ -100,41 +115,40 @@ module Make (C : COMPARABLE) = struct ~size let find ctxt key {map; size} = - Gas.consume ctxt (find_cost ~key ~size) >|? fun ctxt -> - (M.find key map, ctxt) + G.consume ctxt (find_cost ~key ~size) >|? fun ctxt -> (M.find key map, ctxt) let update ctxt key f {map; size} = let find_cost = find_cost ~key ~size in let update_cost = update_cost ~key ~size in (* Consume gas for looking up the old value *) - Gas.consume ctxt find_cost >>? fun ctxt -> + G.consume ctxt find_cost >>? fun ctxt -> let old_val_opt = M.find key map in (* The call to [f] must also account for gas *) f ctxt old_val_opt >>? fun (new_val_opt, ctxt) -> match (old_val_opt, new_val_opt) with | (Some _, Some new_val) -> (* Consume gas for adding to the map *) - Gas.consume ctxt update_cost >|? fun ctxt -> + G.consume ctxt update_cost >|? fun ctxt -> ({map = M.add key new_val map; size}, ctxt) | (Some _, None) -> (* Consume gas for removing from the map *) - Gas.consume ctxt update_cost >|? fun ctxt -> + G.consume ctxt update_cost >|? fun ctxt -> ({map = M.remove key map; size = size - 1}, ctxt) | (None, Some new_val) -> (* Consume gas for adding to the map *) - Gas.consume ctxt update_cost >|? fun ctxt -> + G.consume ctxt update_cost >|? fun ctxt -> ({map = M.add key new_val map; size = size + 1}, ctxt) | (None, None) -> ok ({map; size}, ctxt) let to_list ctxt {map; size} = - Gas.consume ctxt (Carbonated_map_costs.fold_cost ~size) >|? fun ctxt -> + G.consume ctxt (Carbonated_map_costs.fold_cost ~size) >|? fun ctxt -> (M.bindings map, ctxt) let add ctxt ~merge_overlap key value {map; size} = (* Consume gas for looking up the element *) - Gas.consume ctxt (find_cost ~key ~size) >>? fun ctxt -> + G.consume ctxt (find_cost ~key ~size) >>? fun ctxt -> (* Consume gas for adding the element *) - Gas.consume ctxt (update_cost ~key ~size) >>? fun ctxt -> + G.consume ctxt (update_cost ~key ~size) >>? fun ctxt -> match M.find key map with | Some old_val -> (* Invoking [merge_overlap] must also account for gas *) @@ -156,14 +170,14 @@ module Make (C : COMPARABLE) = struct (* To be on the safe side, pay an upfront gas cost for traversing the map. Each step of the fold is accounted for separately. *) - Gas.consume ctxt (Carbonated_map_costs.fold_cost ~size) >>? fun ctxt -> + G.consume ctxt (Carbonated_map_costs.fold_cost ~size) >>? fun ctxt -> M.fold_e (fun key value (map, ctxt) -> add ctxt ~merge_overlap key value map) map (map1, ctxt) let fold ctxt f empty {map; size} = - Gas.consume ctxt (Carbonated_map_costs.fold_cost ~size) >>? fun ctxt -> + G.consume ctxt (Carbonated_map_costs.fold_cost ~size) >>? fun ctxt -> M.fold_e (fun key value (acc, ctxt) -> (* Invoking [f] must also account for gas. *) @@ -180,7 +194,7 @@ module Make (C : COMPARABLE) = struct (* Invoking [f] must also account for gas. *) f ctxt key value >>? fun (value, ctxt) -> (* Consume gas for adding the element. *) - Gas.consume ctxt (update_cost ~key ~size) >|? fun ctxt -> + G.consume ctxt (update_cost ~key ~size) >|? fun ctxt -> (M.add key value map, ctxt)) M.empty {map; size} diff --git a/src/proto_alpha/lib_protocol/carbonated_map.mli b/src/proto_alpha/lib_protocol/carbonated_map.mli index e9419b95241bcbe255e92e3cc2cd7103293414af..71e7cb9f002dda79872d2fa84bf282c1f0a0b12e 100644 --- a/src/proto_alpha/lib_protocol/carbonated_map.mli +++ b/src/proto_alpha/lib_protocol/carbonated_map.mli @@ -1,7 +1,7 @@ (*****************************************************************************) (* *) (* Open Source License *) -(* Copyright (c) 2021 Trili Tech, *) +(* Copyright (c) 2021-2022 Trili Tech, *) (* *) (* Permission is hereby granted, free of charge, to any person obtaining a *) (* copy of this software and associated documentation files (the "Software"),*) @@ -26,12 +26,16 @@ (** An in-memory data-structure for a key-value map where all operations account for gas costs. *) + module type S = sig type 'a t (** The type of keys in the map. *) type key + (** The type used for the context. *) + type context + (** [empty] an empty map. *) val empty : 'a t @@ -44,32 +48,23 @@ module type S = sig (** [find ctxt k m] looks up the value with key [k] in the given map [m] and also consumes the gas associated with the lookup. The complexity is logarithmic in the size of the map. *) - val find : - Alpha_context.context -> - key -> - 'a t -> - ('a option * Alpha_context.context) tzresult + val find : context -> key -> 'a t -> ('a option * context) tzresult (** [update ctxt k f map] updates or adds the value of the key [k] using [f]. The function accounts for the gas cost for finding the element. The updating function [f] should also account for its own gas cost. The complexity is logarithmic in the size of the map. *) val update : - Alpha_context.context -> + context -> key -> - (Alpha_context.context -> - 'a option -> - ('a option * Alpha_context.context) tzresult) -> + (context -> 'a option -> ('a option * context) tzresult) -> 'a t -> - ('a t * Alpha_context.context) tzresult + ('a t * context) tzresult (** [to_list m] transforms a map [m] into a list. It also accounts for the gas cost for traversing the elements. The complexity is linear in the size of the map. *) - val to_list : - Alpha_context.context -> - 'a t -> - ((key * 'a) list * Alpha_context.context) tzresult + val to_list : context -> 'a t -> ((key * 'a) list * context) tzresult (** [of_list ctxt ~merge_overlaps m] creates a map from a list of key-value pairs. In case there are overlapping keys, their values are combined @@ -78,14 +73,10 @@ module type S = sig cost. The complexity is [n * log n] in the size of the list. *) val of_list : - Alpha_context.context -> - merge_overlap: - (Alpha_context.context -> - 'a -> - 'a -> - ('a * Alpha_context.context) tzresult) -> + context -> + merge_overlap:(context -> 'a -> 'a -> ('a * context) tzresult) -> (key * 'a) list -> - ('a t * Alpha_context.context) tzresult + ('a t * context) tzresult (** [merge ctxt ~merge_overlap m1 m2] merges the maps [m1] and [m2]. In case there are overlapping keys, their values are combined using the @@ -94,54 +85,62 @@ module type S = sig cost. The complexity is [n * log n], where [n] is [size m1 + size m2]. *) val merge : - Alpha_context.context -> - merge_overlap: - (Alpha_context.context -> - 'a -> - 'a -> - ('a * Alpha_context.context) tzresult) -> + context -> + merge_overlap:(context -> 'a -> 'a -> ('a * context) tzresult) -> 'a t -> 'a t -> - ('a t * Alpha_context.context) tzresult + ('a t * context) tzresult (** [map ctxt f m] maps over all key-value pairs in the map [m] using the function [f]. It accounts for gas costs associated with traversing the elements. The mapping function [f] should also account for its own gas cost. The complexity is linear in the size of the map [m]. *) val map : - Alpha_context.context -> - (Alpha_context.context -> - key -> - 'a -> - ('b * Alpha_context.context) tzresult) -> + context -> + (context -> key -> 'a -> ('b * context) tzresult) -> 'a t -> - ('b t * Alpha_context.context) tzresult + ('b t * context) tzresult (** [fold ctxt f z m] folds over the key-value pairs of the given map [m], accumulating values using [f], with [z] as the initial state. The function [f] must account for its own gas cost. The complexity is linear in the size of the map [m]. *) val fold : - Alpha_context.context -> - (Alpha_context.context -> - 'state -> - key -> - 'value -> - ('state * Alpha_context.context) tzresult) -> + context -> + (context -> 'state -> key -> 'value -> ('state * context) tzresult) -> 'state -> 'value t -> - ('state * Alpha_context.context) tzresult + ('state * context) tzresult +end + +(** This module is used to provide the function for consuming gas when + constructing carbonated maps. *) +module type GAS = sig + (* The context type. *) + type context + + (** [consume ctxt cost] returns a context where [cost] has been consumed. *) + val consume : + context -> + Saturation_repr.may_saturate Saturation_repr.t -> + context tzresult end -(** A module type for comparable values that also includes a cost function - for metering gas depending on the cost of comparing values. *) +(** Standard [Compare.COMPARE] extended with a [compare_cost] function + specifying the cost for comparing values. *) module type COMPARABLE = sig include Compare.COMPARABLE (** [compare_cost k] returns the cost of comparing the given key [k] with another value of the same type. *) - val compare_cost : t -> Alpha_context.Gas.cost + val compare_cost : t -> Saturation_repr.may_saturate Saturation_repr.t end -(** A functor for building gas metered maps. *) -module Make (O : COMPARABLE) : S with type key := O.t +(** A functor for building gas metered maps. When building a gas metered map via + [Make(G)(C)], [C] is a [COMPARABLE] required to construct a the map while + [G] is a module providing the gas consuming functions. The type of the + context on which the gas consuming function operates is + determined by [G.context]. +*) +module Make (G : GAS) (C : COMPARABLE) : + S with type key = C.t and type context = G.context diff --git a/src/proto_alpha/lib_protocol/carbonated_map_costs.ml b/src/proto_alpha/lib_protocol/carbonated_map_costs.ml index 5f94d49b9f1e37d0aae91606d6e3a59b0bdc5059..b484552e6916fb70d5ecf41c6879af338f07eea4 100644 --- a/src/proto_alpha/lib_protocol/carbonated_map_costs.ml +++ b/src/proto_alpha/lib_protocol/carbonated_map_costs.ml @@ -1,7 +1,7 @@ (*****************************************************************************) (* *) (* Open Source License *) -(* Copyright (c) 2021 Trili Tech, *) +(* Copyright (c) 2022 Trili Tech, *) (* *) (* Permission is hereby granted, free of charge, to any person obtaining a *) (* copy of this software and associated documentation files (the "Software"),*) @@ -22,8 +22,11 @@ (* DEALINGS IN THE SOFTWARE. *) (* *) (*****************************************************************************) -open Alpha_context + module S = Saturation_repr +open Gas_limit_repr + +type cost = Saturation_repr.may_saturate Saturation_repr.t (** This is a good enough approximation *) let log2 x = S.safe_int (1 + S.numbits x) @@ -40,7 +43,6 @@ let log2 x = S.safe_int (1 + S.numbits x) - [traversal_overhead] is for the overhead of log2 steps walking the tree *) let find_cost ~compare_key_cost ~size = - let open Gas in let intercept = S.safe_int 50 in let size = S.safe_int size in let compare_cost = log2 size *@ compare_key_cost in @@ -58,11 +60,11 @@ let find_cost ~compare_key_cost ~size = providing an overestimate by doubling the cost of [find]. *) let update_cost ~compare_key_cost ~size = - Gas.(S.safe_int 2 *@ find_cost ~compare_key_cost ~size) + S.safe_int 2 *@ find_cost ~compare_key_cost ~size (** Collect benchmark from [Carbonated_map_benchmarks.Fold_benchmark]. The cost of producing a list of elements is linear in the size of the map and does not depend on the size of the elements nor keys. *) -let fold_cost ~size = Gas.(S.safe_int 50 +@ (S.safe_int 24 *@ S.safe_int size)) +let fold_cost ~size = S.safe_int 50 +@ (S.safe_int 24 *@ S.safe_int size) diff --git a/src/proto_alpha/lib_protocol/carbonated_map_costs.mli b/src/proto_alpha/lib_protocol/carbonated_map_costs.mli index eec763b5bc5b7dc1ca294888e1ea69cadabb2492..c40a26465e8d8271fe81e3f351e9576196a44a23 100644 --- a/src/proto_alpha/lib_protocol/carbonated_map_costs.mli +++ b/src/proto_alpha/lib_protocol/carbonated_map_costs.mli @@ -1,7 +1,7 @@ (*****************************************************************************) (* *) (* Open Source License *) -(* Copyright (c) 2021 Trili Tech, *) +(* Copyright (c) 2021-2022 Trili Tech, *) (* *) (* Permission is hereby granted, free of charge, to any person obtaining a *) (* copy of this software and associated documentation files (the "Software"),*) @@ -23,24 +23,25 @@ (* *) (*****************************************************************************) +(** The type of the cost.*) +type cost = Saturation_repr.may_saturate Saturation_repr.t + (** The [Carbonated_map_costs] module contains gas cost functions for [Carbonated_map]. -*) + *) (** [find_cost ~compare_key_cost ~size] returns the gas cost for looking up an element from a map of size [size]. The user of this function is responsible for providing a correct value of [compare_key_cost], representing the cost of comparing elements with a given key. -*) -val find_cost : - compare_key_cost:Alpha_context.Gas.cost -> size:int -> Alpha_context.Gas.cost + *) +val find_cost : compare_key_cost:cost -> size:int -> cost (** [update_cost ~compare_key_cost ~size] returns the gas cost for updating an element in a map of size [size]. The user of this function is responsible for providing a correct value of [compare_key_cost], representing the cost of comparing elements with a given key. *) -val update_cost : - compare_key_cost:Alpha_context.Gas.cost -> size:int -> Alpha_context.Gas.cost +val update_cost : compare_key_cost:cost -> size:int -> cost (** [fold_cost ~size] returns the cost of folding over a list of size [size]. *) -val fold_cost : size:int -> Alpha_context.Gas.cost +val fold_cost : size:int -> cost diff --git a/src/proto_alpha/lib_protocol/dune.inc b/src/proto_alpha/lib_protocol/dune.inc index f3d48717682382168153dee9d1e82e0f30a2d8a0..cd2d02f738092ea2c0ce706c3df42531dcd8e9e8 100644 --- a/src/proto_alpha/lib_protocol/dune.inc +++ b/src/proto_alpha/lib_protocol/dune.inc @@ -100,6 +100,8 @@ module CamlinternalFormatBasics = struct include CamlinternalFormatBasics end receipt_repr.mli receipt_repr.ml migration_repr.mli migration_repr.ml sc_rollup_tick_repr.mli sc_rollup_tick_repr.ml + carbonated_map_costs.mli carbonated_map_costs.ml + carbonated_map.mli carbonated_map.ml raw_context_intf.ml raw_context.mli raw_context.ml storage_costs.mli storage_costs.ml @@ -149,8 +151,6 @@ module CamlinternalFormatBasics = struct include CamlinternalFormatBasics end script_string.mli script_string.ml script_int.mli script_int.ml script_timestamp.mli script_timestamp.ml - carbonated_map_costs.mli carbonated_map_costs.ml - carbonated_map.mli carbonated_map.ml tx_rollup_l2_storage_sig.ml tx_rollup_l2_context_sig.ml tx_rollup_l2_context.ml @@ -287,6 +287,8 @@ module CamlinternalFormatBasics = struct include CamlinternalFormatBasics end receipt_repr.mli receipt_repr.ml migration_repr.mli migration_repr.ml sc_rollup_tick_repr.mli sc_rollup_tick_repr.ml + carbonated_map_costs.mli carbonated_map_costs.ml + carbonated_map.mli carbonated_map.ml raw_context_intf.ml raw_context.mli raw_context.ml storage_costs.mli storage_costs.ml @@ -336,8 +338,6 @@ module CamlinternalFormatBasics = struct include CamlinternalFormatBasics end script_string.mli script_string.ml script_int.mli script_int.ml script_timestamp.mli script_timestamp.ml - carbonated_map_costs.mli carbonated_map_costs.ml - carbonated_map.mli carbonated_map.ml tx_rollup_l2_storage_sig.ml tx_rollup_l2_context_sig.ml tx_rollup_l2_context.ml @@ -474,6 +474,8 @@ module CamlinternalFormatBasics = struct include CamlinternalFormatBasics end receipt_repr.mli receipt_repr.ml migration_repr.mli migration_repr.ml sc_rollup_tick_repr.mli sc_rollup_tick_repr.ml + carbonated_map_costs.mli carbonated_map_costs.ml + carbonated_map.mli carbonated_map.ml raw_context_intf.ml raw_context.mli raw_context.ml storage_costs.mli storage_costs.ml @@ -523,8 +525,6 @@ module CamlinternalFormatBasics = struct include CamlinternalFormatBasics end script_string.mli script_string.ml script_int.mli script_int.ml script_timestamp.mli script_timestamp.ml - carbonated_map_costs.mli carbonated_map_costs.ml - carbonated_map.mli carbonated_map.ml tx_rollup_l2_storage_sig.ml tx_rollup_l2_context_sig.ml tx_rollup_l2_context.ml @@ -683,6 +683,8 @@ include Tezos_raw_protocol_alpha.Main Receipt_repr Migration_repr Sc_rollup_tick_repr + Carbonated_map_costs + Carbonated_map Raw_context_intf Raw_context Storage_costs @@ -732,8 +734,6 @@ include Tezos_raw_protocol_alpha.Main Script_string Script_int Script_timestamp - Carbonated_map_costs - Carbonated_map Tx_rollup_l2_storage_sig Tx_rollup_l2_context_sig Tx_rollup_l2_context @@ -911,6 +911,8 @@ include Tezos_raw_protocol_alpha.Main receipt_repr.mli receipt_repr.ml migration_repr.mli migration_repr.ml sc_rollup_tick_repr.mli sc_rollup_tick_repr.ml + carbonated_map_costs.mli carbonated_map_costs.ml + carbonated_map.mli carbonated_map.ml raw_context_intf.ml raw_context.mli raw_context.ml storage_costs.mli storage_costs.ml @@ -960,8 +962,6 @@ include Tezos_raw_protocol_alpha.Main script_string.mli script_string.ml script_int.mli script_int.ml script_timestamp.mli script_timestamp.ml - carbonated_map_costs.mli carbonated_map_costs.ml - carbonated_map.mli carbonated_map.ml tx_rollup_l2_storage_sig.ml tx_rollup_l2_context_sig.ml tx_rollup_l2_context.ml diff --git a/src/proto_alpha/lib_protocol/test/pbt/test_carbonated_map.ml b/src/proto_alpha/lib_protocol/test/pbt/test_carbonated_map.ml index 05ebbf38eeaaf4afabe5522741705728376f2bfe..e0a588968a301735664a549cc9fa4ff19c218084 100644 --- a/src/proto_alpha/lib_protocol/test/pbt/test_carbonated_map.ml +++ b/src/proto_alpha/lib_protocol/test/pbt/test_carbonated_map.ml @@ -51,7 +51,14 @@ module Compare_int = struct let compare_cost _ = Saturation_repr.safe_int 10 end -module CM = Carbonated_map.Make (Compare_int) +module CM = + Carbonated_map.Make + (struct + type context = Alpha_context.context + + let consume = Alpha_context.Gas.consume + end) + (Compare_int) let unsafe_new_context () = Result.value_f diff --git a/src/proto_alpha/lib_protocol/ticket_lazy_storage_diff.ml b/src/proto_alpha/lib_protocol/ticket_lazy_storage_diff.ml index eb19db16ab7eb574838d757660bf52efaa89ac30..f77a1cd82870375caefad26516fd1b55f10d90ec 100644 --- a/src/proto_alpha/lib_protocol/ticket_lazy_storage_diff.ml +++ b/src/proto_alpha/lib_protocol/ticket_lazy_storage_diff.ml @@ -95,13 +95,21 @@ let collect_token_diffs_of_node ctxt has_tickets node ~get_token_and_amount acc (** A module for keeping track of script-key-hashes. It's used for looking up keys for multiple big-map updates referencing the same key. *) -module Key_hash_map = Carbonated_map.Make (struct - type t = Script_expr_hash.t - let compare = Script_expr_hash.compare +module Key_hash_map = + Carbonated_map.Make + (struct + type context = Alpha_context.context - let compare_cost _ = Ticket_costs.Constants.cost_compare_ticket_hash -end) + let consume = Alpha_context.Gas.consume + end) + (struct + type t = Script_expr_hash.t + + let compare = Script_expr_hash.compare + + let compare_cost _ = Ticket_costs.Constants.cost_compare_ticket_hash + end) (** Collects all ticket-token diffs from a big-map update and prepends them to the accumulator [acc]. *) diff --git a/src/proto_alpha/lib_protocol/ticket_operations_diff.ml b/src/proto_alpha/lib_protocol/ticket_operations_diff.ml index 60837e56e172407d20d3b6b0ba1c38026d6b6107..74893aa1a27a3985226c7c570d98a5f604fb0abf 100644 --- a/src/proto_alpha/lib_protocol/ticket_operations_diff.ml +++ b/src/proto_alpha/lib_protocol/ticket_operations_diff.ml @@ -66,18 +66,25 @@ let () = (fun () -> Contract_not_originated) (** A carbonated map where the keys are destination (contract or tx_rollup). *) -module Destination_map = Carbonated_map.Make (struct - type t = Destination.t +module Destination_map = + Carbonated_map.Make + (struct + type context = Alpha_context.context - let compare = Destination.compare + let consume = Alpha_context.Gas.consume + end) + (struct + type t = Destination.t - (* TODO: #2667 - Change cost-function to one for comparing destinations. - Not expected to have any performance impact but we should update for - completeness. - *) - let compare_cost _ = Ticket_costs.Constants.cost_compare_key_contract -end) + let compare = Destination.compare + + (* TODO: #2667 + Change cost-function to one for comparing destinations. + Not expected to have any performance impact but we should update for + completeness. + *) + let compare_cost _ = Ticket_costs.Constants.cost_compare_key_contract + end) (** A module for mapping ticket-tokens to a map of contract destinations and amounts. The values specify how to distribute the spending of a ticket-token diff --git a/src/proto_alpha/lib_protocol/ticket_token_map.ml b/src/proto_alpha/lib_protocol/ticket_token_map.ml index fd66f169381bc1056b20982f9f806a2ff2aa63a4..68df78a89909dd595b42ba71a519817ab6dfdd8c 100644 --- a/src/proto_alpha/lib_protocol/ticket_token_map.ml +++ b/src/proto_alpha/lib_protocol/ticket_token_map.ml @@ -26,13 +26,20 @@ open Alpha_context (** A carbonated map where the keys are [Ticket_hash.t] values. *) -module Ticket_token_map = Carbonated_map.Make (struct - type t = Ticket_hash.t +module Ticket_token_map = + Carbonated_map.Make + (struct + type context = Alpha_context.context - let compare = Ticket_hash.compare + let consume = Alpha_context.Gas.consume + end) + (struct + type t = Ticket_hash.t - let compare_cost _ = Ticket_costs.Constants.cost_compare_ticket_hash -end) + let compare = Ticket_hash.compare + + let compare_cost _ = Ticket_costs.Constants.cost_compare_ticket_hash + end) (** Conceptually a map from [Ticket_token.ex_token] to values. Since ticket-tokens are expensive to compare we use [Ticket_hash.t] keys instead,