From ad78a4d57aaa0540ad776dc20df229c629cbd797 Mon Sep 17 00:00:00 2001 From: varasev <33550681+varasev@users.noreply.github.com> Date: Mon, 15 Jul 2024 11:15:11 +0300 Subject: [PATCH] Add Brotli decompression to Indexer.Fetcher.Optimism.TxnBatch (#10410) * Add Brotli decompression to Indexer.Fetcher.Optimism.TxnBatch * Update spelling --------- Co-authored-by: POA <33550681+poa@users.noreply.github.com> --- .../lib/indexer/fetcher/optimism/txn_batch.ex | 14 ++++++++++---- apps/indexer/mix.exs | 2 ++ cspell.json | 1 + mix.lock | 1 + 4 files changed, 14 insertions(+), 4 deletions(-) diff --git a/apps/indexer/lib/indexer/fetcher/optimism/txn_batch.ex b/apps/indexer/lib/indexer/fetcher/optimism/txn_batch.ex index 0904241577..c122d1713c 100644 --- a/apps/indexer/lib/indexer/fetcher/optimism/txn_batch.ex +++ b/apps/indexer/lib/indexer/fetcher/optimism/txn_batch.ex @@ -47,6 +47,8 @@ defmodule Indexer.Fetcher.Optimism.TxnBatch do # Optimism chain block time is a constant (2 seconds) @op_chain_block_time 2 + @compressor_brotli 1 + def child_spec(start_link_arguments) do spec = %{ id: __MODULE__, @@ -1097,15 +1099,20 @@ defmodule Indexer.Fetcher.Optimism.TxnBatch do genesis_block_l2, json_rpc_named_arguments_l2 ) do - uncompressed_bytes = zlib_decompress(bytes) + uncompressed_bytes = + if first_byte(bytes) == @compressor_brotli do + {:ok, uncompressed} = :brotli.decode(binary_part(bytes, 1, byte_size(bytes) - 1)) + uncompressed + else + zlib_decompress(bytes) + end batches = Enum.reduce_while(Stream.iterate(0, &(&1 + 1)), {uncompressed_bytes, []}, fn _i, {remainder, batch_acc} -> try do {decoded, new_remainder} = ExRLP.decode(remainder, stream: true) - <> = binary_part(decoded, 0, 1) - content = binary_part(decoded, 1, byte_size(decoded) - 1) + <> = decoded new_batch_acc = cond do @@ -1310,7 +1317,6 @@ defmodule Indexer.Fetcher.Optimism.TxnBatch do start_block = quantity_to_integer(Enum.at(responses, 0).result) "0x000000000000000000000000" <> batch_inbox = Enum.at(responses, 1).result "0x000000000000000000000000" <> batch_submitter = Enum.at(responses, 2).result - {start_block, String.downcase("0x" <> batch_inbox), String.downcase("0x" <> batch_submitter)} _ -> diff --git a/apps/indexer/mix.exs b/apps/indexer/mix.exs index 7bccbcfcab..74226a9065 100644 --- a/apps/indexer/mix.exs +++ b/apps/indexer/mix.exs @@ -51,6 +51,8 @@ defmodule Indexer.MixProject do {:decorator, "~> 1.4"}, # JSONRPC access to Nethermind for `Explorer.Indexer` {:ethereum_jsonrpc, in_umbrella: true}, + # Brotli compression/decompression + {:brotli, "~> 0.3.2"}, # RLP encoding {:ex_rlp, "~> 0.6.0"}, # Importing to database diff --git a/cspell.json b/cspell.json index 579cb8d3cd..2f572a7d60 100644 --- a/cspell.json +++ b/cspell.json @@ -61,6 +61,7 @@ "blockscoutuser", "bools", "bridgedtokenlist", + "brotli", "browserconfig", "bsdr", "Btvk", diff --git a/mix.lock b/mix.lock index 6502c9d0f0..bbb6d6ace0 100644 --- a/mix.lock +++ b/mix.lock @@ -9,6 +9,7 @@ "benchee": {:hex, :benchee, "1.3.1", "c786e6a76321121a44229dde3988fc772bca73ea75170a73fd5f4ddf1af95ccf", [:mix], [{:deep_merge, "~> 1.0", [hex: :deep_merge, repo: "hexpm", optional: false]}, {:statistex, "~> 1.0", [hex: :statistex, repo: "hexpm", optional: false]}, {:table, "~> 0.1.0", [hex: :table, repo: "hexpm", optional: true]}], "hexpm", "76224c58ea1d0391c8309a8ecbfe27d71062878f59bd41a390266bf4ac1cc56d"}, "benchee_csv": {:hex, :benchee_csv, "1.0.0", "0b3b9223290bfcb8003552705bec9bcf1a89b4a83b70bd686e45295c264f3d16", [:mix], [{:benchee, ">= 0.99.0 and < 2.0.0", [hex: :benchee, repo: "hexpm", optional: false]}, {:csv, "~> 2.0", [hex: :csv, repo: "hexpm", optional: false]}], "hexpm", "cdefb804c021dcf7a99199492026584be9b5a21d6644ac0d01c81c5d97c520d5"}, "briefly": {:git, "https://github.com/CargoSense/briefly.git", "4836ba322ffb504a102a15cc6e35d928ef97120e", []}, + "brotli": {:hex, :brotli, "0.3.2", "59cf45a399098516f1d34f70d8e010e5c9bf326659d3ef34c7cc56793339002b", [:rebar3], [], "hexpm", "9ec3ef9c753f80d0c657b4905193c55e5198f169fa1d1c044d8601d4d931a2ad"}, "bunt": {:hex, :bunt, "1.0.0", "081c2c665f086849e6d57900292b3a161727ab40431219529f13c4ddcf3e7a44", [:mix], [], "hexpm", "dc5f86aa08a5f6fa6b8096f0735c4e76d54ae5c9fa2c143e5a1fc7c1cd9bb6b5"}, "bureaucrat": {:hex, :bureaucrat, "0.2.10", "b0de157dad540e40007b663b683f716ced21f85ff0591093aadb209ad0d967e1", [:mix], [{:inflex, ">= 1.10.0", [hex: :inflex, repo: "hexpm", optional: false]}, {:phoenix, ">= 1.2.0", [hex: :phoenix, repo: "hexpm", optional: true]}, {:plug, ">= 1.0.0", [hex: :plug, repo: "hexpm", optional: false]}, {:poison, "~> 1.5 or ~> 2.0 or ~> 3.0 or ~> 4.0 or ~> 5.0", [hex: :poison, repo: "hexpm", optional: true]}], "hexpm", "bc7e5162b911c29c8ebefee87a2c16fbf13821a58f448a8fd024eb6c17fae15c"}, "bypass": {:hex, :bypass, "2.1.0", "909782781bf8e20ee86a9cabde36b259d44af8b9f38756173e8f5e2e1fabb9b1", [:mix], [{:plug, "~> 1.7", [hex: :plug, repo: "hexpm", optional: false]}, {:plug_cowboy, "~> 2.0", [hex: :plug_cowboy, repo: "hexpm", optional: false]}, {:ranch, "~> 1.3", [hex: :ranch, repo: "hexpm", optional: false]}], "hexpm", "d9b5df8fa5b7a6efa08384e9bbecfe4ce61c77d28a4282f79e02f1ef78d96b80"},