From a64591a9f15ad61947bc9cbb5c16618dec24417b Mon Sep 17 00:00:00 2001 From: Arvid Jakobsson Date: Wed, 6 Apr 2022 13:26:58 +0200 Subject: [PATCH 1/3] Proto/tx_rollup: bench Tx_rollup_commitment_repr.Full.compact --- .../tx_rollup_benchmarks.ml | 75 +++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/src/proto_alpha/lib_benchmarks_proto/tx_rollup_benchmarks.ml b/src/proto_alpha/lib_benchmarks_proto/tx_rollup_benchmarks.ml index 9ebc90b9dfad..7feab0c50d2f 100644 --- a/src/proto_alpha/lib_benchmarks_proto/tx_rollup_benchmarks.ml +++ b/src/proto_alpha/lib_benchmarks_proto/tx_rollup_benchmarks.ml @@ -107,4 +107,79 @@ module Inbox_add_message : Benchmark.S = struct Generator.Plain {workload; closure} end +module Commitment_full_compact_bench : Benchmark.S = struct + open Tx_rollup_commitment_repr + + let name = "Commitment_full_compact_bench" + + let info = "Benchmark for Tx_rollup_commitment_repr.Full.compact" + + let tags = ["tx_rollup"; "merkle"; "commitment"; "compact"] + + type config = {max_messages : int} + + let default_config = {max_messages = 10000} + + let config_encoding = + let open Data_encoding in + conv + (fun {max_messages} -> max_messages) + (fun max_messages -> {max_messages}) + int31 + + type workload = Tx_rollup_commitment_repr.Full.t + + let workload_encoding = Tx_rollup_commitment_repr.Full.encoding + + let workload_to_vector {messages; _} = + Sparse_vec.String.of_list + [("n_messages", float_of_int @@ List.length messages)] + + let models = + let conv {messages; _} = (List.length messages, ()) in + [ + ( "tx_rollup", + Model.make + ~conv + ~model: + (Model.affine + ~intercept: + (Free_variable.of_string "full_compact_bench_intercept") + ~coeff:(Free_variable.of_string "n_messages_coeff")) ); + ] + + let create_benchmarks ~rng_state ~bench_num ({max_messages} : config) = + List.repeat bench_num @@ fun () -> + let n_messages = + Base_samplers.sample_in_interval + ~range:{min = 1; max = max_messages} + rng_state + in + let message_result_hash rng_state : Tx_rollup_message_result_hash_repr.t = + let size = Tx_rollup_message_result_hash_repr.size in + let bytes = + Base_samplers.bytes ~size:{min = size; max = size} rng_state + in + Tx_rollup_message_result_hash_repr.of_bytes_exn bytes + in + let workload = + let level = Tx_rollup_level_repr.root in + let predecessor = None in + let inbox_merkle_root = + Tx_rollup_inbox_repr.Merkle.root Tx_rollup_inbox_repr.Merkle.empty + in + (* AFAICT, the values above have no influence on the execution time of [compact] *) + let messages = List.repeat n_messages (message_result_hash rng_state) in + {level; messages; predecessor; inbox_merkle_root} + in + let closure () = + ignore + (Tx_rollup_commitment_repr.Full.compact workload + : Tx_rollup_commitment_repr.Compact.t) + in + Generator.Plain {workload; closure} +end + let () = Registration_helpers.register (module Inbox_add_message) + +let () = Registration_helpers.register (module Commitment_full_compact_bench) -- GitLab From 12363a65280d4375c2a4e4912c897f6192676cdb Mon Sep 17 00:00:00 2001 From: Arvid Jakobsson Date: Wed, 6 Apr 2022 13:51:28 +0200 Subject: [PATCH 2/3] Proto/tx_rollup: implement cost model derived from compact bench --- src/proto_alpha/lib_protocol/tx_rollup_gas.ml | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/src/proto_alpha/lib_protocol/tx_rollup_gas.ml b/src/proto_alpha/lib_protocol/tx_rollup_gas.ml index b7250caa97bd..41857f1c03e7 100644 --- a/src/proto_alpha/lib_protocol/tx_rollup_gas.ml +++ b/src/proto_alpha/lib_protocol/tx_rollup_gas.ml @@ -100,16 +100,15 @@ let consume_add_message_cost ctxt = in Raw_context.consume_gas ctxt cost -let compute_cost element_size size = - let ( + ) = S.add in - let ( * ) = S.mul in - hash_cost element_size >>? fun element_hash_cost -> - hash_cost 64 >>? fun inner_hash_cost -> - ok (element_hash_cost + (S.safe_int 2 * S.safe_int size * inner_hash_cost)) - -let consume_compact_commitment_cost ctxt n = - compute_cost Tx_rollup_prefixes.message_result_hash.hash_size n - >>? fun cost -> Raw_context.consume_gas ctxt cost +(** As generated by the model [model_commitment_full_compact] in + [lib_benchmarks_proto/tx_rollup_benchmarks.ml]. We add one to + [inbox_length] so that the cost is never zero. *) +let model_commitment_full_compact inbox_length = + S.mul (S.safe_int 811) (S.safe_int (1 + inbox_length)) + +let consume_compact_commitment_cost ctxt inbox_length = + let cost = model_commitment_full_compact inbox_length in + Raw_context.consume_gas ctxt cost let hash ~hash_f ctxt encoding input = match Data_encoding.Binary.to_bytes_opt encoding input with -- GitLab From 6ca1e83ac0c1f7a64dc01a821028f0a53b71afe8 Mon Sep 17 00:00:00 2001 From: Arvid Jakobsson Date: Wed, 6 Apr 2022 13:51:43 +0200 Subject: [PATCH 3/3] Proto/tx_rollup: update regressions traces due to gas model change --- .../tx_rollup_finalize_too_recent_commitment.out | 12 ++++++------ tezt/_regressions/tx_rollup_rpc_commitment.out | 12 ++++++------ .../tx_rollup_rpc_pending_bonded_commitments.out | 12 ++++++------ 3 files changed, 18 insertions(+), 18 deletions(-) diff --git a/tezt/_regressions/tx_rollup_finalize_too_recent_commitment.out b/tezt/_regressions/tx_rollup_finalize_too_recent_commitment.out index 4ed8708d9ffa..b179eecdf5ca 100644 --- a/tezt/_regressions/tx_rollup_finalize_too_recent_commitment.out +++ b/tezt/_regressions/tx_rollup_finalize_too_recent_commitment.out @@ -58,7 +58,7 @@ This sequence of operations was run: ./tezos-client --wait none submit tx rollup commitment 0 '[TX_ROLLUP_INBOX_HASH]' '[TX_ROLLUP_MESSAGE_RESULT_HASH]' to '[TX_ROLLUP_HASH]' from '[PUBLIC_KEY_HASH]' Node is bootstrapped. -Estimated gas: 3818.146 units (will add 100 for safety) +Estimated gas: 3694.298 units (will add 100 for safety) Estimated storage: no bytes added Operation successfully injected in the node. Operation hash is '[OPERATION_HASH]' @@ -69,20 +69,20 @@ and/or an external block explorer to make sure that it has been included. This sequence of operations was run: Manager signed operations: From: [PUBLIC_KEY_HASH] - Fee to the baker: ꜩ0.000709 + Fee to the baker: ꜩ0.000697 Expected counter: 3 - Gas limit: 3919 + Gas limit: 3795 Storage limit: 0 bytes Balance updates: - [PUBLIC_KEY_HASH] ... -ꜩ0.000709 - payload fees(the block proposer) ....... +ꜩ0.000709 + [PUBLIC_KEY_HASH] ... -ꜩ0.000697 + payload fees(the block proposer) ....... +ꜩ0.000697 Tx rollup commitment:[TX_ROLLUP_HASH], commitment 0 : messages = [TX_ROLLUP_MESSAGE_RESULT_HASH] predecessor for inbox with merkle root [TX_ROLLUP_INBOX_HASH] From: [PUBLIC_KEY_HASH] This tx rollup commit operation was successfully applied Balance updates: [PUBLIC_KEY_HASH] ....................................................... -ꜩ10000 Frozen_bonds([PUBLIC_KEY_HASH],[TX_ROLLUP_HASH]) ... +ꜩ10000 - Consumed gas: 3818.146 + Consumed gas: 3694.298 ./tezos-client --wait none submit tx rollup finalize commitment to '[TX_ROLLUP_HASH]' from '[PUBLIC_KEY_HASH]' diff --git a/tezt/_regressions/tx_rollup_rpc_commitment.out b/tezt/_regressions/tx_rollup_rpc_commitment.out index c1909fa28a01..60da03542f57 100644 --- a/tezt/_regressions/tx_rollup_rpc_commitment.out +++ b/tezt/_regressions/tx_rollup_rpc_commitment.out @@ -58,7 +58,7 @@ This sequence of operations was run: ./tezos-client --wait none submit tx rollup commitment 0 '[TX_ROLLUP_INBOX_HASH]' '[TX_ROLLUP_MESSAGE_RESULT_HASH]' to '[TX_ROLLUP_HASH]' from '[PUBLIC_KEY_HASH]' Node is bootstrapped. -Estimated gas: 3818.146 units (will add 100 for safety) +Estimated gas: 3694.298 units (will add 100 for safety) Estimated storage: no bytes added Operation successfully injected in the node. Operation hash is '[OPERATION_HASH]' @@ -69,20 +69,20 @@ and/or an external block explorer to make sure that it has been included. This sequence of operations was run: Manager signed operations: From: [PUBLIC_KEY_HASH] - Fee to the baker: ꜩ0.000709 + Fee to the baker: ꜩ0.000697 Expected counter: 3 - Gas limit: 3919 + Gas limit: 3795 Storage limit: 0 bytes Balance updates: - [PUBLIC_KEY_HASH] ... -ꜩ0.000709 - payload fees(the block proposer) ....... +ꜩ0.000709 + [PUBLIC_KEY_HASH] ... -ꜩ0.000697 + payload fees(the block proposer) ....... +ꜩ0.000697 Tx rollup commitment:[TX_ROLLUP_HASH], commitment 0 : messages = [TX_ROLLUP_MESSAGE_RESULT_HASH] predecessor for inbox with merkle root [TX_ROLLUP_INBOX_HASH] From: [PUBLIC_KEY_HASH] This tx rollup commit operation was successfully applied Balance updates: [PUBLIC_KEY_HASH] ....................................................... -ꜩ10000 Frozen_bonds([PUBLIC_KEY_HASH],[TX_ROLLUP_HASH]) ... +ꜩ10000 - Consumed gas: 3818.146 + Consumed gas: 3694.298 ./tezos-client rpc get '/chains/main/blocks/head/context/tx_rollup/[TX_ROLLUP_HASH]/commitment/0' diff --git a/tezt/_regressions/tx_rollup_rpc_pending_bonded_commitments.out b/tezt/_regressions/tx_rollup_rpc_pending_bonded_commitments.out index 0fd40e80ca03..6ec6b0aa13bb 100644 --- a/tezt/_regressions/tx_rollup_rpc_pending_bonded_commitments.out +++ b/tezt/_regressions/tx_rollup_rpc_pending_bonded_commitments.out @@ -58,7 +58,7 @@ This sequence of operations was run: ./tezos-client --wait none submit tx rollup commitment 0 '[TX_ROLLUP_INBOX_HASH]' '[TX_ROLLUP_MESSAGE_RESULT_HASH]' to '[TX_ROLLUP_HASH]' from '[PUBLIC_KEY_HASH]' Node is bootstrapped. -Estimated gas: 3818.146 units (will add 100 for safety) +Estimated gas: 3694.298 units (will add 100 for safety) Estimated storage: no bytes added Operation successfully injected in the node. Operation hash is '[OPERATION_HASH]' @@ -69,20 +69,20 @@ and/or an external block explorer to make sure that it has been included. This sequence of operations was run: Manager signed operations: From: [PUBLIC_KEY_HASH] - Fee to the baker: ꜩ0.000709 + Fee to the baker: ꜩ0.000697 Expected counter: 3 - Gas limit: 3919 + Gas limit: 3795 Storage limit: 0 bytes Balance updates: - [PUBLIC_KEY_HASH] ... -ꜩ0.000709 - payload fees(the block proposer) ....... +ꜩ0.000709 + [PUBLIC_KEY_HASH] ... -ꜩ0.000697 + payload fees(the block proposer) ....... +ꜩ0.000697 Tx rollup commitment:[TX_ROLLUP_HASH], commitment 0 : messages = [TX_ROLLUP_MESSAGE_RESULT_HASH] predecessor for inbox with merkle root [TX_ROLLUP_INBOX_HASH] From: [PUBLIC_KEY_HASH] This tx rollup commit operation was successfully applied Balance updates: [PUBLIC_KEY_HASH] ....................................................... -ꜩ10000 Frozen_bonds([PUBLIC_KEY_HASH],[TX_ROLLUP_HASH]) ... +ꜩ10000 - Consumed gas: 3818.146 + Consumed gas: 3694.298 ./tezos-client rpc get '/chains/main/blocks/head/context/tx_rollup/[TX_ROLLUP_HASH]/pending_bonded_commitments/[PUBLIC_KEY_HASH]' -- GitLab