Merge pull request #2913 from poanetwork/ab-clean-reorgs-in-pending-block-ops-by-schedule
clean reorgs in pending block ops by schedulepull/2921/head
commit
764a95c7e5
@ -0,0 +1,45 @@ |
||||
defmodule Indexer.PendingOpsCleaner do |
||||
@moduledoc """ |
||||
Peiodically cleans non-consensus pending ops. |
||||
""" |
||||
|
||||
use GenServer |
||||
|
||||
require Logger |
||||
|
||||
alias Explorer.Chain |
||||
|
||||
@interval :timer.minutes(60) |
||||
|
||||
def start_link([init_opts, gen_server_opts]) do |
||||
start_link(init_opts, gen_server_opts) |
||||
end |
||||
|
||||
def start_link(init_opts, gen_server_opts) do |
||||
GenServer.start_link(__MODULE__, init_opts, gen_server_opts) |
||||
end |
||||
|
||||
def init(opts) do |
||||
interval = opts[:interval] || @interval |
||||
|
||||
Process.send_after(self(), :clean_nonconsensus_pending_ops, interval) |
||||
|
||||
{:ok, %{interval: interval}} |
||||
end |
||||
|
||||
def handle_info(:clean_nonconsensus_pending_ops, %{interval: interval} = state) do |
||||
Logger.debug(fn -> "Cleaning non-consensus pending ops" end) |
||||
|
||||
clean_nonconsensus_pending_ops() |
||||
|
||||
Process.send_after(self(), :clean_nonconsensus_pending_ops, interval) |
||||
|
||||
{:noreply, state} |
||||
end |
||||
|
||||
defp clean_nonconsensus_pending_ops do |
||||
:ok = Chain.remove_nonconsensus_blocks_from_pending_ops() |
||||
|
||||
Logger.debug(fn -> "Non-consensus pending ops are cleaned" end) |
||||
end |
||||
end |
@ -0,0 +1,34 @@ |
||||
defmodule Indexer.PendingOpsCleanerTest do |
||||
use Explorer.DataCase |
||||
|
||||
alias Explorer.Chain.PendingBlockOperation |
||||
alias Indexer.PendingOpsCleaner |
||||
|
||||
describe "init/1" do |
||||
test "deletes non-consensus pending ops on init" do |
||||
block = insert(:block, consensus: false) |
||||
|
||||
insert(:pending_block_operation, block_hash: block.hash, fetch_internal_transactions: true) |
||||
|
||||
assert Repo.one(from(block in PendingBlockOperation, where: block.block_hash == ^block.hash)) |
||||
|
||||
start_supervised!({PendingOpsCleaner, [[interval: :timer.seconds(1)], [name: :PendingOpsTest]]}) |
||||
|
||||
Process.sleep(2_000) |
||||
|
||||
assert is_nil(Repo.one(from(block in PendingBlockOperation, where: block.block_hash == ^block.hash))) |
||||
end |
||||
|
||||
test "re-schedules deletion" do |
||||
start_supervised!({PendingOpsCleaner, [[interval: :timer.seconds(1)], [name: :PendingOpsTest]]}) |
||||
|
||||
block = insert(:block, consensus: false) |
||||
|
||||
insert(:pending_block_operation, block_hash: block.hash, fetch_internal_transactions: true) |
||||
|
||||
Process.sleep(2_000) |
||||
|
||||
assert is_nil(Repo.one(from(block in PendingBlockOperation, where: block.block_hash == ^block.hash))) |
||||
end |
||||
end |
||||
end |
Loading…
Reference in new issue