From 3d4f9cb2a6b98f0850f148e8c57ce1e8c49bf06e Mon Sep 17 00:00:00 2001 From: Kirill Fedoseev Date: Mon, 26 Aug 2024 20:01:15 +0400 Subject: [PATCH] fix: logs list serialization (#10565) * fix: logs factory usage * fix: type conversions * chore: mix format * fix: log factory --- .../api/rpc/eth_controller_test.exs | 81 ++++++++++++++-- .../api/rpc/logs_controller_test.exs | 28 +++++- apps/explorer/lib/explorer/etherscan/logs.ex | 1 + apps/explorer/test/explorer/chain_test.exs | 11 +-- .../test/explorer/etherscan/logs_test.exs | 97 +++++++++++++++++-- 5 files changed, 190 insertions(+), 28 deletions(-) diff --git a/apps/block_scout_web/test/block_scout_web/controllers/api/rpc/eth_controller_test.exs b/apps/block_scout_web/test/block_scout_web/controllers/api/rpc/eth_controller_test.exs index 48646b342e..1e25c003ff 100644 --- a/apps/block_scout_web/test/block_scout_web/controllers/api/rpc/eth_controller_test.exs +++ b/apps/block_scout_web/test/block_scout_web/controllers/api/rpc/eth_controller_test.exs @@ -256,6 +256,7 @@ defmodule BlockScoutWeb.API.RPC.EthControllerTest do insert(:log, block: block, + block_number: block.number, address: address, transaction: transaction, data: "0x020202", @@ -333,13 +334,37 @@ defmodule BlockScoutWeb.API.RPC.EthControllerTest do transaction3 = insert(:transaction, from_address: address) |> with_block(block3) transaction4 = insert(:transaction, from_address: address) |> with_block(block4) - insert(:log, address: address, transaction: transaction1, data: "0x010101", block_number: block1.number) + insert(:log, + address: address, + transaction: transaction1, + data: "0x010101", + block: block1, + block_number: block1.number + ) - insert(:log, address: address, transaction: transaction2, data: "0x020202", block_number: block2.number) + insert(:log, + address: address, + transaction: transaction2, + data: "0x020202", + block: block2, + block_number: block2.number + ) - insert(:log, address: address, transaction: transaction3, data: "0x030303", block_number: block3.number) + insert(:log, + address: address, + transaction: transaction3, + data: "0x030303", + block: block3, + block_number: block3.number + ) - insert(:log, address: address, transaction: transaction4, data: "0x040404", block_number: block4.number) + insert(:log, + address: address, + transaction: transaction4, + data: "0x040404", + block: block4, + block_number: block4.number + ) params = params(api_params, [%{"address" => to_string(address.hash), "fromBlock" => 1, "toBlock" => 2}]) @@ -363,11 +388,29 @@ defmodule BlockScoutWeb.API.RPC.EthControllerTest do transaction2 = insert(:transaction, from_address: address) |> with_block(block2) transaction3 = insert(:transaction, from_address: address) |> with_block(block3) - insert(:log, address: address, transaction: transaction1, data: "0x010101", block_number: block1.number) + insert(:log, + address: address, + transaction: transaction1, + data: "0x010101", + block: block1, + block_number: block1.number + ) - insert(:log, address: address, transaction: transaction2, data: "0x020202", block_number: block2.number) + insert(:log, + address: address, + transaction: transaction2, + data: "0x020202", + block: block2, + block_number: block2.number + ) - insert(:log, address: address, transaction: transaction3, data: "0x030303", block_number: block3.number) + insert(:log, + address: address, + transaction: transaction3, + data: "0x030303", + block: block3, + block_number: block3.number + ) params = params(api_params, [%{"address" => to_string(address.hash), "blockHash" => to_string(block2.hash)}]) @@ -391,11 +434,29 @@ defmodule BlockScoutWeb.API.RPC.EthControllerTest do transaction2 = insert(:transaction, from_address: address) |> with_block(block2) transaction3 = insert(:transaction, from_address: address) |> with_block(block3) - insert(:log, address: address, transaction: transaction1, data: "0x010101", block_number: block1.number) + insert(:log, + address: address, + transaction: transaction1, + data: "0x010101", + block: block1, + block_number: block1.number + ) - insert(:log, address: address, transaction: transaction2, data: "0x020202", block_number: block2.number) + insert(:log, + address: address, + transaction: transaction2, + data: "0x020202", + block: block2, + block_number: block2.number + ) - insert(:log, address: address, transaction: transaction3, data: "0x030303", block_number: block3.number) + insert(:log, + address: address, + transaction: transaction3, + data: "0x030303", + block: block3, + block_number: block3.number + ) params = params(api_params, [%{"address" => to_string(address.hash), "fromBlock" => "earliest", "toBlock" => "earliest"}]) diff --git a/apps/block_scout_web/test/block_scout_web/controllers/api/rpc/logs_controller_test.exs b/apps/block_scout_web/test/block_scout_web/controllers/api/rpc/logs_controller_test.exs index 2691f1e7e8..df82a98a6a 100644 --- a/apps/block_scout_web/test/block_scout_web/controllers/api/rpc/logs_controller_test.exs +++ b/apps/block_scout_web/test/block_scout_web/controllers/api/rpc/logs_controller_test.exs @@ -296,7 +296,13 @@ defmodule BlockScoutWeb.API.RPC.LogsControllerTest do |> insert(to_address: contract_address) |> with_block() - log = insert(:log, address: contract_address, transaction: transaction, block_number: transaction.block_number) + log = + insert(:log, + address: contract_address, + transaction: transaction, + block: block, + block_number: transaction.block_number + ) params = %{ "module" => "logs", @@ -353,12 +359,14 @@ defmodule BlockScoutWeb.API.RPC.LogsControllerTest do insert(:log, address: contract_address, transaction: transaction_block1, + block: transaction_block1.block, block_number: transaction_block1.block_number ) insert(:log, address: contract_address, transaction: transaction_block2, + block: transaction_block2.block, block_number: transaction_block2.block_number ) @@ -406,12 +414,14 @@ defmodule BlockScoutWeb.API.RPC.LogsControllerTest do insert(:log, address: contract_address, transaction: transaction_block1, + block: transaction_block1.block, block_number: transaction_block1.block_number ) insert(:log, address: contract_address, transaction: transaction_block2, + block: transaction_block2.block, block_number: transaction_block2.block_number ) @@ -450,12 +460,16 @@ defmodule BlockScoutWeb.API.RPC.LogsControllerTest do log1_details = [ address: contract_address, transaction: transaction, + block: block, + block_number: block.number, first_topic: topic(@first_topic_hex_string_1) ] log2_details = [ address: contract_address, transaction: transaction, + block: block, + block_number: block.number, first_topic: topic(@first_topic_hex_string_2) ] @@ -508,6 +522,8 @@ defmodule BlockScoutWeb.API.RPC.LogsControllerTest do log1_details = [ address: contract_address, transaction: transaction, + block: block, + block_number: block.number, first_topic: topic(@first_topic_hex_string_1), second_topic: topic(@second_topic_hex_string_1) ] @@ -515,6 +531,8 @@ defmodule BlockScoutWeb.API.RPC.LogsControllerTest do log2_details = [ address: contract_address, transaction: transaction, + block: block, + block_number: block.number, first_topic: topic(@first_topic_hex_string_2), second_topic: topic(@second_topic_hex_string_2) ] @@ -557,6 +575,8 @@ defmodule BlockScoutWeb.API.RPC.LogsControllerTest do log1_details = [ address: contract_address, transaction: transaction, + block: block, + block_number: block.number, first_topic: topic(@first_topic_hex_string_1), second_topic: topic(@second_topic_hex_string_1) ] @@ -564,6 +584,8 @@ defmodule BlockScoutWeb.API.RPC.LogsControllerTest do log2_details = [ address: contract_address, transaction: transaction, + block: block, + block_number: block.number, first_topic: topic(@first_topic_hex_string_2), second_topic: topic(@second_topic_hex_string_2) ] @@ -605,6 +627,8 @@ defmodule BlockScoutWeb.API.RPC.LogsControllerTest do log1_details = [ address: contract_address, transaction: transaction, + block: block, + block_number: block.number, first_topic: topic(@first_topic_hex_string_1), second_topic: topic(@second_topic_hex_string_1), third_topic: topic(@third_topic_hex_string_1), @@ -614,6 +638,8 @@ defmodule BlockScoutWeb.API.RPC.LogsControllerTest do log2_details = [ address: contract_address, transaction: transaction, + block: block, + block_number: block.number, first_topic: topic(@first_topic_hex_string_1), second_topic: topic(@second_topic_hex_string_1), third_topic: topic(@third_topic_hex_string_1), diff --git a/apps/explorer/lib/explorer/etherscan/logs.ex b/apps/explorer/lib/explorer/etherscan/logs.ex index b22dbbd3b8..3cce390ea5 100644 --- a/apps/explorer/lib/explorer/etherscan/logs.ex +++ b/apps/explorer/lib/explorer/etherscan/logs.ex @@ -103,6 +103,7 @@ defmodule Explorer.Etherscan.Logs do ) all_transaction_logs_query + |> Chain.wrapped_union_subquery() |> order_by([log], asc: log.block_number, asc: log.index) |> Repo.replica().all() else diff --git a/apps/explorer/test/explorer/chain_test.exs b/apps/explorer/test/explorer/chain_test.exs index 7ec3a65922..bf8a8bb17d 100644 --- a/apps/explorer/test/explorer/chain_test.exs +++ b/apps/explorer/test/explorer/chain_test.exs @@ -280,9 +280,7 @@ defmodule Explorer.ChainTest do |> insert(to_address: address) |> with_block() - _log1 = insert(:log, transaction: transaction, index: 1, address: address, block_number: transaction.block_number) - - 2..51 + 1..51 |> Enum.map(fn index -> insert(:log, block: transaction.block, @@ -292,7 +290,6 @@ defmodule Explorer.ChainTest do block_number: transaction.block_number ) end) - |> Enum.map(& &1.index) paging_options1 = %PagingOptions{page_size: 1} @@ -3039,7 +3036,7 @@ defmodule Explorer.ChainTest do |> insert() |> with_block(block) - insert(:log, address: address, transaction: transaction) + insert(:log, address: address, transaction: transaction, block: block, block_number: block.number) balance = insert(:unfetched_balance, address_hash: address.hash, block_number: block.number) @@ -3229,7 +3226,7 @@ defmodule Explorer.ChainTest do |> insert() |> with_block(log_block) - insert(:log, address: miner, transaction: log_transaction) + insert(:log, address: miner, transaction: log_transaction, block: log_block, block_number: log_block.number) insert(:unfetched_balance, address_hash: miner.hash, block_number: log_block.number) from_internal_transaction_block = insert(:block) @@ -3310,7 +3307,7 @@ defmodule Explorer.ChainTest do |> insert() |> with_block(block) - insert(:log, address: miner, transaction: log_transaction) + insert(:log, address: miner, transaction: log_transaction, block: block, block_number: block.number) from_internal_transaction_transaction = :transaction diff --git a/apps/explorer/test/explorer/etherscan/logs_test.exs b/apps/explorer/test/explorer/etherscan/logs_test.exs index b5953407f8..02cc8f3e02 100644 --- a/apps/explorer/test/explorer/etherscan/logs_test.exs +++ b/apps/explorer/test/explorer/etherscan/logs_test.exs @@ -65,7 +65,7 @@ defmodule Explorer.Etherscan.LogsTest do |> insert(to_address: contract_address, block_timestamp: block.timestamp) |> with_block(block) - log = insert(:log, address: contract_address, block_number: block.number, transaction: transaction) + log = insert(:log, address: contract_address, block: block, block_number: block.number, transaction: transaction) filter = %{ from_block: block.number, @@ -99,7 +99,12 @@ defmodule Explorer.Etherscan.LogsTest do |> insert(to_address: contract_address) |> with_block() - insert_list(2, :log, address: contract_address, transaction: transaction, block_number: block.number) + insert_list(2, :log, + address: contract_address, + transaction: transaction, + block_number: block.number, + block: block + ) filter = %{ from_block: block.number, @@ -130,8 +135,19 @@ defmodule Explorer.Etherscan.LogsTest do |> insert(to_address: contract_address) |> with_block(second_block) - insert(:log, address: contract_address, transaction: transaction_block1, block_number: first_block.number) - insert(:log, address: contract_address, transaction: transaction_block2, block_number: second_block.number) + insert(:log, + address: contract_address, + transaction: transaction_block1, + block: first_block, + block_number: first_block.number + ) + + insert(:log, + address: contract_address, + transaction: transaction_block2, + block: second_block, + block_number: second_block.number + ) filter = %{ from_block: second_block.number, @@ -163,8 +179,19 @@ defmodule Explorer.Etherscan.LogsTest do |> insert(to_address: contract_address) |> with_block(second_block) - insert(:log, address: contract_address, transaction: transaction_block1, block_number: first_block.number) - insert(:log, address: contract_address, transaction: transaction_block2, block_number: second_block.number) + insert(:log, + address: contract_address, + transaction: transaction_block1, + block: first_block, + block_number: first_block.number + ) + + insert(:log, + address: contract_address, + transaction: transaction_block2, + block: second_block, + block_number: second_block.number + ) filter = %{ from_block: first_block.number, @@ -188,7 +215,12 @@ defmodule Explorer.Etherscan.LogsTest do |> with_block() inserted_records = - insert_list(2000, :log, address: contract_address, transaction: transaction, block_number: block.number) + insert_list(2000, :log, + address: contract_address, + transaction: transaction, + block_number: block.number, + block: block + ) filter = %{ from_block: block.number, @@ -230,12 +262,16 @@ defmodule Explorer.Etherscan.LogsTest do log1_details = [ address: contract_address, transaction: transaction, + block: block, + block_number: block.number, first_topic: topic(@first_topic_hex_string_1) ] log2_details = [ address: contract_address, transaction: transaction, + block: block, + block_number: block.number, first_topic: topic(@first_topic_hex_string_2) ] @@ -266,6 +302,8 @@ defmodule Explorer.Etherscan.LogsTest do log1_details = [ address: contract_address, transaction: transaction, + block: block, + block_number: block.number, first_topic: topic(@first_topic_hex_string_1), second_topic: topic(@second_topic_hex_string_1) ] @@ -273,6 +311,8 @@ defmodule Explorer.Etherscan.LogsTest do log2_details = [ address: contract_address, transaction: transaction, + block: block, + block_number: block.number, first_topic: topic(@first_topic_hex_string_1), second_topic: topic(@second_topic_hex_string_2) ] @@ -307,6 +347,8 @@ defmodule Explorer.Etherscan.LogsTest do log1_details = [ address: contract_address, transaction: transaction, + block: block, + block_number: block.number, first_topic: topic(@first_topic_hex_string_1), second_topic: topic(@second_topic_hex_string_1) ] @@ -314,6 +356,8 @@ defmodule Explorer.Etherscan.LogsTest do log2_details = [ address: contract_address, transaction: transaction, + block: block, + block_number: block.number, first_topic: topic(@first_topic_hex_string_2), second_topic: topic(@second_topic_hex_string_2) ] @@ -346,6 +390,7 @@ defmodule Explorer.Etherscan.LogsTest do log1_details = [ address: contract_address, transaction: transaction, + block: block, first_topic: topic(@first_topic_hex_string_1), block_number: block.number ] @@ -353,6 +398,7 @@ defmodule Explorer.Etherscan.LogsTest do log2_details = [ address: contract_address, transaction: transaction, + block: block, first_topic: topic(@first_topic_hex_string_2), block_number: block.number ] @@ -385,6 +431,7 @@ defmodule Explorer.Etherscan.LogsTest do log1_details = [ address: contract_address, transaction: transaction, + block: block, first_topic: topic(@first_topic_hex_string_1), second_topic: topic(@second_topic_hex_string_1), block_number: block.number @@ -393,6 +440,7 @@ defmodule Explorer.Etherscan.LogsTest do log2_details = [ address: contract_address, transaction: transaction, + block: block, first_topic: topic(@first_topic_hex_string_2), second_topic: topic(@second_topic_hex_string_2), block_number: block.number @@ -428,6 +476,7 @@ defmodule Explorer.Etherscan.LogsTest do log1_details = [ address: contract_address, transaction: transaction, + block: block, first_topic: topic(@first_topic_hex_string_1), second_topic: topic(@second_topic_hex_string_1), third_topic: topic(@third_topic_hex_string_1), @@ -437,6 +486,7 @@ defmodule Explorer.Etherscan.LogsTest do log2_details = [ address: contract_address, transaction: transaction, + block: block, first_topic: topic(@first_topic_hex_string_2), second_topic: topic(@second_topic_hex_string_2), third_topic: topic(@third_topic_hex_string_2), @@ -446,6 +496,7 @@ defmodule Explorer.Etherscan.LogsTest do log3_details = [ address: contract_address, transaction: transaction, + block: block, first_topic: topic(@first_topic_hex_string_3), second_topic: topic(@second_topic_hex_string_3), third_topic: topic(@third_topic_hex_string_3), @@ -488,6 +539,7 @@ defmodule Explorer.Etherscan.LogsTest do log1_details = [ address: contract_address, transaction: transaction, + block: block, first_topic: topic(@first_topic_hex_string_1), second_topic: topic(@second_topic_hex_string_1), third_topic: topic(@third_topic_hex_string_1), @@ -497,6 +549,7 @@ defmodule Explorer.Etherscan.LogsTest do log2_details = [ address: contract_address, transaction: transaction, + block: block, first_topic: topic(@first_topic_hex_string_2), second_topic: topic(@second_topic_hex_string_2), third_topic: topic(@third_topic_hex_string_2), @@ -506,6 +559,7 @@ defmodule Explorer.Etherscan.LogsTest do log3_details = [ address: contract_address, transaction: transaction, + block: block, first_topic: topic(@first_topic_hex_string_3), second_topic: topic(@second_topic_hex_string_3), third_topic: topic(@third_topic_hex_string_3), @@ -548,6 +602,7 @@ defmodule Explorer.Etherscan.LogsTest do log1_details = [ address: contract_address, transaction: transaction, + block: block, first_topic: topic(@first_topic_hex_string_1), second_topic: topic(@second_topic_hex_string_1), third_topic: topic(@third_topic_hex_string_1), @@ -557,6 +612,7 @@ defmodule Explorer.Etherscan.LogsTest do log2_details = [ address: contract_address, transaction: transaction, + block: block, first_topic: topic(@first_topic_hex_string_1), second_topic: topic(@second_topic_hex_string_2), third_topic: topic(@third_topic_hex_string_1), @@ -566,6 +622,7 @@ defmodule Explorer.Etherscan.LogsTest do log3_details = [ address: contract_address, transaction: transaction, + block: block, first_topic: topic(@first_topic_hex_string_1), second_topic: topic(@second_topic_hex_string_1), third_topic: topic(@third_topic_hex_string_1), @@ -608,6 +665,7 @@ defmodule Explorer.Etherscan.LogsTest do log1_details = [ address: contract_address, transaction: transaction, + block: block, first_topic: topic(@first_topic_hex_string_1), second_topic: topic(@second_topic_hex_string_1), block_number: block.number @@ -616,6 +674,7 @@ defmodule Explorer.Etherscan.LogsTest do log2_details = [ address: contract_address, transaction: transaction, + block: block, first_topic: topic(@first_topic_hex_string_2), second_topic: topic(@second_topic_hex_string_2), third_topic: topic(@third_topic_hex_string_2), @@ -626,6 +685,7 @@ defmodule Explorer.Etherscan.LogsTest do log3_details = [ address: contract_address, transaction: transaction, + block: block, first_topic: topic(@first_topic_hex_string_1), second_topic: topic(@second_topic_hex_string_1), third_topic: topic(@third_topic_hex_string_1), @@ -686,9 +746,26 @@ defmodule Explorer.Etherscan.LogsTest do |> insert(to_address: contract_address) |> with_block(third_block) - insert(:log, address: contract_address, transaction: transaction_block3) - insert(:log, address: contract_address, transaction: transaction_block1) - insert(:log, address: contract_address, transaction: transaction_block2) + insert(:log, + address: contract_address, + transaction: transaction_block3, + block: third_block, + block_number: third_block.number + ) + + insert(:log, + address: contract_address, + transaction: transaction_block1, + block: first_block, + block_number: first_block.number + ) + + insert(:log, + address: contract_address, + transaction: transaction_block2, + block: second_block, + block_number: second_block.number + ) filter = %{ from_block: first_block.number,