fix: ignore block_submitting that are above mined blocks
MODIFY
apps/omg_child_chain/lib/omg_child_chain/block_queue/submission_monitor.ex
apps/omg_child_chain/lib/omg_child_chain/block_queue/submission_monitor.ex
@@ -21,6 +21,7 @@ defmodule OMG.ChildChain.BlockQueue.SubmissionMonitor do
require Logger
defstruct pending_blocks: [],
+ last_mined_blknum: 0,
root_chain_height: 0,
stall_threshold_blocks: 4,
alarm_module: nil,
@@ -31,6 +32,7 @@ defmodule OMG.ChildChain.BlockQueue.SubmissionMonitor do
@type t() :: %__MODULE__{
pending_blocks: [pending_block()],
+ last_mined_blknum: non_neg_integer(),
root_chain_height: non_neg_integer(),
stall_threshold_blocks: pos_integer(),
alarm_module: module(),
@@ -87,16 +89,26 @@ defmodule OMG.ChildChain.BlockQueue.SubmissionMonitor do
{:noreply, %{state | root_chain_height: new_height}}
end
- # Listens for a block being submitted and add it to monitoring if it hasn't been tracked
+ # Listens for :block_submitting and adds its blknum to monitoring if it's not being monitored.
+ #
+ # Sometimes a block is resubmitted even after it's been mined on the rootchain due to
+ # rootchain node and network latency. So we ignore the submitting blknums that are below
+ # :last_mined_blknum while add the rest to monitoring.
+ def handle_info({:internal_event_bus, :block_submitting, blknum}, %{last_mined_blknum: mined} = state)
+ when blknum <= mined do
+ {:noreply, state}
+ end
+
+ # Monitors the rest of :block_submitting that are not mined yet
def handle_info({:internal_event_bus, :block_submitting, blknum}, state) do
pending_blocks = add_new_blknum(state.pending_blocks, blknum, state.root_chain_height)
{:noreply, %{state | pending_blocks: pending_blocks}}
end
- # Listens for a block that got submitted and drop it from monitoring
+ # Listens for a block that got mined and drops it from monitoring
def handle_info({:internal_event_bus, :block_submitted, blknum}, state) do
pending_blocks = remove_blknum(state.pending_blocks, blknum)
- {:noreply, %{state | pending_blocks: pending_blocks}}
+ {:noreply, %{state | pending_blocks: pending_blocks, last_mined_blknum: blknum}}
end
# Ignore unrelated events
@@ -131,9 +143,10 @@ defmodule OMG.ChildChain.BlockQueue.SubmissionMonitor do
end
end
+ # Remove all pending_blknum that are below or equal to the given mined blknum
@spec remove_blknum([{blknum(), any()}], blknum()) :: [pending_block()]
- defp remove_blknum(pending_blocks, blknum) do
- Enum.reject(pending_blocks, fn {pending_blknum, _} -> pending_blknum == blknum end)
+ defp remove_blknum(pending_blocks, mined_blknum) do
+ Enum.reject(pending_blocks, fn {pending_blknum, _} -> pending_blknum <= mined_blknum end)
end
#

MODIFY
apps/omg_child_chain/test/omg_child_chain/block_queue/submission_monitor_test.exs
apps/omg_child_chain/test/omg_child_chain/block_queue/submission_monitor_test.exs
@@ -64,7 +64,23 @@ defmodule OMG.ChildChain.BlockQueue.SubmissionMonitorTest do
# Push the height just below the stalling height
_ = send(context.monitor, {:internal_event_bus, :ethereum_new_height, context.stall_threshold_blocks - 1})
- # Wait for 10x the check interval to make sure it really does not get raised again.
+ # Wait for 10x the check interval to make sure the alarm really does not get raised.
+ refute_receive(:got_raise_alarm, context.check_interval_ms * 10)
+ end)
+ end
+
+ test "does not raise :block_submit_stalled alarm when the submitting block is below the last mined block", context do
+ capture_log(fn ->
+ # Inform the monitor of a mined block
+ _ = send(context.monitor, {:internal_event_bus, :block_submitted, 2000})
+
+ # Inform the monitor of a pending block lower than the mined block
+ _ = send(context.monitor, {:internal_event_bus, :block_submitting, 1000})
+
+ # Push the height to the stalling height
+ _ = send(context.monitor, {:internal_event_bus, :ethereum_new_height, context.stall_threshold_blocks})
+
+ # Wait for 10x the check interval to make sure the alarm really does not get raised.
refute_receive(:got_raise_alarm, context.check_interval_ms * 10)
end)
end

fix: ignore block_submitting that are above mined blocks (#1703)