diff --git a/lib/ae_mdw/db/sync/block.ex b/lib/ae_mdw/db/sync/block.ex index db8f95af7..1667b4cc5 100644 --- a/lib/ae_mdw/db/sync/block.ex +++ b/lib/ae_mdw/db/sync/block.ex @@ -49,11 +49,11 @@ defmodule AeMdw.Db.Sync.Block do Txs.txi(), Blocks.height() | Blocks.block_hash() ) :: - {[height_mutations()], Txs.txi()} + Enumerable.t() def blocks_mutations(from_height, from_mbi, from_txi, to_height_or_hash) do from_height |> Db.get_blocks_per_height(to_height_or_hash) - |> Enum.flat_map_reduce(from_txi, fn {key_block, micro_blocks, next_kb_hash}, txi -> + |> Stream.transform(from_txi, fn {key_block, micro_blocks, next_kb_hash}, txi -> height = :aec_blocks.height(key_block) kb_header = :aec_blocks.to_key_header(key_block) {:ok, kb_hash} = :aec_headers.hash_header(kb_header) diff --git a/lib/ae_mdw/sync/server.ex b/lib/ae_mdw/sync/server.ex index 0ec7c9239..876304811 100644 --- a/lib/ae_mdw/sync/server.ex +++ b/lib/ae_mdw/sync/server.ex @@ -245,17 +245,14 @@ defmodule AeMdw.Sync.Server do end spawn_task(fn -> - {mutations_time, {gens_mutations, _next_txi}} = - :timer.tc(fn -> - Block.blocks_mutations(from_height, from_mbi, from_txi, to_height) - end) - - MutationsCache.clear() + gens_mutations = Block.blocks_mutations(from_height, from_mbi, from_txi, to_height) {exec_time, new_state} = :timer.tc(fn -> exec_db_mutations(gens_mutations, db_state, clear_mem?) end) - gens_per_min = (to_height + 1 - from_height) * 60_000_000 / (mutations_time + exec_time) + MutationsCache.clear() + + gens_per_min = (to_height + 1 - from_height) * 60_000_000 / exec_time Status.set_gens_per_min(gens_per_min) new_state @@ -273,15 +270,8 @@ defmodule AeMdw.Sync.Server do :none -> -1 end - {mutations_time, {gens_mutations, _next_txi}} = - :timer.tc(fn -> - Block.blocks_mutations(from_height, from_mbi, from_txi, last_hash) - end) - - {exec_time, _new_state} = :timer.tc(fn -> exec_mem_mutations(gens_mutations, mem_state) end) - - gens_per_min = length(gens_mutations) * 60_000_000 / (mutations_time + exec_time) - Status.set_gens_per_min(gens_per_min) + gens_mutations = Block.blocks_mutations(from_height, from_mbi, from_txi, last_hash) + _new_state = exec_mem_mutations(gens_mutations, mem_state) last_hash end) diff --git a/test/ae_mdw/db/sync/block_test.exs b/test/ae_mdw/db/sync/block_test.exs index 4c018a536..201ba7b54 100644 --- a/test/ae_mdw/db/sync/block_test.exs +++ b/test/ae_mdw/db/sync/block_test.exs @@ -70,8 +70,8 @@ defmodule AeMdw.Db.Sync.BlockTest do last_mb_hash = AeMdw.Validate.id!(last_mb_hash) assert mem1 = :ets.info(:sync_hashes, :memory) - assert {height_blocks, txi} = Block.blocks_mutations(0, 0, 0, last_mb_hash) - assert {^height_blocks, ^txi} = Block.blocks_mutations(0, 0, 0, last_mb_hash) + assert height_blocks = 0 |> Block.blocks_mutations(0, 0, last_mb_hash) |> Enum.to_list() + assert ^height_blocks = 0 |> Block.blocks_mutations(0, 0, last_mb_hash) |> Enum.to_list() assert length(height_blocks) == 11 assert mem2 = :ets.info(:sync_hashes, :memory) assert mem2 > mem1 diff --git a/test/ae_mdw/sync/block_test.exs b/test/ae_mdw/sync/block_test.exs deleted file mode 100644 index 1c990cf5b..000000000 --- a/test/ae_mdw/sync/block_test.exs +++ /dev/null @@ -1,81 +0,0 @@ -defmodule AeMdw.Sync.BlockTest do - use ExUnit.Case - - alias AeMdw.Db.Model - alias AeMdw.Db.Sync.Block - - import AeMdwWeb.BlockchainSim, - only: [with_blockchain: 3, tx: 3, spend_tx: 3, name_tx: 3, name_tx: 4] - - import Mock - - require Model - - describe "blocks_mutations/4" do - test "returns the mutations for a generation with many transactions" do - # credo:disable-for-next-line - accounts = Map.new(0..100, fn i -> {String.to_atom("user#{i}"), 100_000_000} end) - - first_microblock = { - :mb0, - List.flatten( - Enum.map(1..30, fn i -> - # credo:disable-for-next-line - tx_tag = String.to_atom("tx#{i}") - user = String.to_existing_atom("user#{i}") - name = "name#{i}.chain" - {tx_tag, name_tx(:name_claim_tx, user, name)} - end) ++ - Enum.map(31..40, fn i -> - # credo:disable-for-next-line - tx_tag = String.to_atom("tx#{i}") - user = String.to_existing_atom("user#{i}") - {tx_tag, tx(:oracle_register_tx, user, %{})} - end) ++ - Enum.map(41..100, fn i -> - # credo:disable-for-next-line - tx_tag = String.to_atom("tx#{i}") - user = String.to_existing_atom("user#{i}") - {tx_tag, spend_tx(user, user, 1_000)} - end) - ) - } - - microblocks = - Enum.map(1..10, fn i -> - # credo:disable-for-next-line - mb_tag = String.to_atom("mb#{i}") - user = String.to_existing_atom("user#{i}") - oracle_id = :aeser_id.create(:oracle, <>) - - { - mb_tag, - [ - tx1: - name_tx(:name_update_tx, user, "name#{i}.chain", %{ - pointers: [{:pointer, "account_pubkey", user}] - }), - tx2: {:oracle_query_tx, user, oracle_id, %{}} - ] ++ - Enum.map(3..50, fn i -> - tx_tag = String.to_existing_atom("tx#{i}") - user = String.to_existing_atom("user#{i}") - {tx_tag, spend_tx(user, user, 1_000)} - end) - } - end) - - with_blockchain accounts, [first_microblock | microblocks] do - %{hash: last_mb_hash, height: 10} = blocks[:mb10] - last_mb_hash = AeMdw.Validate.id!(last_mb_hash) - - assert mem1 = :ets.info(:sync_hashes, :memory) - assert {height_blocks, txi} = Block.blocks_mutations(0, 0, 0, last_mb_hash) - assert {^height_blocks, ^txi} = Block.blocks_mutations(0, 0, 0, last_mb_hash) - assert length(height_blocks) == 11 - assert mem2 = :ets.info(:sync_hashes, :memory) - assert mem2 > mem1 - end - end - end -end