diff --git a/src/proto_alpha/lib_protocol/alpha_context.mli b/src/proto_alpha/lib_protocol/alpha_context.mli index 8de1667d2212873022c4dd7175c5a02d0cdad7e9..2a464b7c70520458597572912d2e7446168b843b 100644 --- a/src/proto_alpha/lib_protocol/alpha_context.mli +++ b/src/proto_alpha/lib_protocol/alpha_context.mli @@ -2705,13 +2705,13 @@ module Sc_rollup : sig (** See {!Sc_rollup_inbox_message_repr}. *) module Message : sig - type t = - | Internal of { - payload : Script.expr; - sender : Contract.t; - source : Signature.public_key_hash; - } - | External of string + type internal_inbox_message = { + payload : Script.expr; + sender : Contract.t; + source : Signature.public_key_hash; + } + + type t = Internal of internal_inbox_message | External of string type serialized = private string diff --git a/src/proto_alpha/lib_protocol/sc_rollup_costs.ml b/src/proto_alpha/lib_protocol/sc_rollup_costs.ml index a4212a0842211f13fd547330f5c75dbdcbf06cf5..570a42142116aa9fcc6a9edf3280b0aae3b5b798 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_costs.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_costs.ml @@ -45,6 +45,12 @@ module Constants = struct let cost_add_inbox_per_level = S.safe_int 15 let cost_update_num_and_size_of_messages = S.safe_int 15 + + (* equal to Michelson_v1_gas.Cost_of.Unparsing.contract_optimized *) + let cost_decoding_contract_optimized = S.safe_int 70 + + (* equal to Michelson_v1_gas.Cost_of.Unparsing.key_hash_optimized *) + let cost_decoding_key_hash_optimized = S.safe_int 50 end (* We assume that the gas cost of adding messages [[ m_1; ... ; m_n]] at level @@ -70,3 +76,20 @@ let is_valid_parameters_ty_cost ~ty_size = let fixed_cost = S.safe_int 10 in let coeff = S.safe_int 6 in S.add fixed_cost (S.mul coeff ty_size) + +let cost_serialize_internal_inbox_message + Sc_rollup_inbox_message_repr.{payload; sender = _; source = _} = + let lexpr = Script_repr.lazy_expr payload in + let expr_cost = Script_repr.force_bytes_cost lexpr in + S_syntax.( + expr_cost + Constants.cost_decoding_contract_optimized + + Constants.cost_decoding_key_hash_optimized) + +(** We assume that the cost of deserializing an expression of [bytes_len] is + greater by a notch to the real cost here. + + TODO: checks if the estimated cost is close to the more precise cost: To + check the real cost we could traverse the list of expression in the + deserialized output message. *) +let cost_deserialize_outbox_message ~bytes_len = + Script_repr.deserialization_cost_estimated_from_bytes bytes_len diff --git a/src/proto_alpha/lib_protocol/sc_rollup_costs.mli b/src/proto_alpha/lib_protocol/sc_rollup_costs.mli index 2c33496f10788a9de5067795f54aca78e27f9758..3e75d0e43a1fb4ba96bc5a693c26540d449a3907 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_costs.mli +++ b/src/proto_alpha/lib_protocol/sc_rollup_costs.mli @@ -47,3 +47,20 @@ val is_valid_parameters_ty_cost : function is used internally in the [Sc_rollup_storage] module. *) val cost_add_external_messages : num_messages:int -> total_messages_size:int -> int32 -> Gas_limit_repr.cost + +(** [cost_serialize_internal_inbox_message internal_inbox_message] is the cost + of the serialization of an internal inbox message. It's equal to the cost of + serializing the script expression, with {!Script_repr.force_bytes_cost} plus + a fixed amount for the serialized addresses. + + It traverses the payload expression to find the precise cost. It is safe to + use {!Script_repr.force_bytes_cost} because the payload of an internal inbox + message is bounded. +*) +val cost_serialize_internal_inbox_message : + Sc_rollup_inbox_message_repr.internal_inbox_message -> Gas_limit_repr.cost + +(** [cost_deserialize_outbox_message ~bytes_len] is the cost of the + deserialization of an outbox message. It's equal to the cost of + deserializing script expression of size [bytes_len]. *) +val cost_deserialize_outbox_message : bytes_len:int -> Gas_limit_repr.cost diff --git a/src/proto_alpha/lib_protocol/sc_rollup_inbox_message_repr.ml b/src/proto_alpha/lib_protocol/sc_rollup_inbox_message_repr.ml index 6cc9bc1c1f1bbd5abc5869c0d142ed5d669853ac..146856b63f91d75c43330d16f24bf67b35bb687c 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_inbox_message_repr.ml +++ b/src/proto_alpha/lib_protocol/sc_rollup_inbox_message_repr.ml @@ -54,15 +54,15 @@ let () = (function Error_decode_inbox_message -> Some () | _ -> None) (fun () -> Error_decode_inbox_message) -type t = - | Internal of { - payload : Script_repr.expr; - (** A Micheline value containing the parameters passed to the rollup. *) - sender : Contract_repr.t; (** The L1 caller contract. *) - source : Signature.public_key_hash; - (** The implicit account that originated the transaction. *) - } - | External of string +type internal_inbox_message = { + payload : Script_repr.expr; + (** A Micheline value containing the parameters passed to the rollup. *) + sender : Contract_repr.t; (** The L1 caller contract. *) + source : Signature.public_key_hash; + (** The implicit account that originated the transaction. *) +} + +type t = Internal of internal_inbox_message | External of string let encoding = let open Data_encoding in diff --git a/src/proto_alpha/lib_protocol/sc_rollup_inbox_message_repr.mli b/src/proto_alpha/lib_protocol/sc_rollup_inbox_message_repr.mli index 8f1a68b71488124160b1df7e1b97703a6684ff18..0ccac6a068955f63245505d33c4e84304d56e013 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_inbox_message_repr.mli +++ b/src/proto_alpha/lib_protocol/sc_rollup_inbox_message_repr.mli @@ -43,18 +43,21 @@ for decoding and interpreting these messages. *) +(** [internal_inbox_message] represent an internal message in a inbox (L1 -> + L2). This is not inline so it can easily be used by + {!Sc_rollup_costs.cost_serialize_internal_inbox_message}. *) +type internal_inbox_message = { + payload : Script_repr.expr; + (** A Micheline value containing the parameters passed to the rollup. *) + sender : Contract_repr.t; (** The L1 caller contract. *) + source : Signature.public_key_hash; + (** The implicit account that originated the transaction. *) +} + (** A type representing messages from Layer 1 to Layer 2. Internal ones are originated from Layer 1 smart-contracts and external ones are messages from an external manager operation. *) -type t = - | Internal of { - payload : Script_repr.expr; - (** A Micheline value containing the parameters passed to the rollup. *) - sender : Contract_repr.t; (** The L1 caller contract. *) - source : Signature.public_key_hash; - (** The implicit account that originated the transaction. *) - } - | External of string +type t = Internal of internal_inbox_message | External of string (** A typed version of a message serialized in binary format. *) type serialized = private string @@ -62,6 +65,7 @@ type serialized = private string (** [to_bytes msg] encodes the inbox message [msg] in binary format. *) val to_bytes : t -> serialized tzresult +(** Module containing functions exposed so they can be used in test. *) module Internal_for_tests : sig (** [of_bytes bs] decodes [bs] as an [inbox_message]. *) val of_bytes : string -> t tzresult diff --git a/src/proto_alpha/lib_protocol/sc_rollup_outbox_message_repr.mli b/src/proto_alpha/lib_protocol/sc_rollup_outbox_message_repr.mli index 2fe02e040a17995c62d51877d68d316c633a5c5a..43d1f4b91479cf5ab238b778c2e17c284c9ae836 100644 --- a/src/proto_alpha/lib_protocol/sc_rollup_outbox_message_repr.mli +++ b/src/proto_alpha/lib_protocol/sc_rollup_outbox_message_repr.mli @@ -50,6 +50,7 @@ type t = Atomic_transaction_batch of {transactions : transaction list} typed values. *) val of_bytes : string -> t tzresult +(** Module containing functions exposed so they can be used in test. *) module Internal_for_tests : sig (** [to_bytes msg] returns the bytes of the given outbox message [msg]. *)