Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

simplify event pulling #1801

Merged
merged 5 commits into from
Dec 11, 2020
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 16 additions & 15 deletions apps/omg/lib/omg/ethereum_event_listener.ex
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ defmodule OMG.EthereumEventListener do

case RootChainCoordinator.get_sync_info() do
:nosync ->
:ok = RootChainCoordinator.check_in(Core.get_height_to_check_in(state), state.service_name)
:ok = RootChainCoordinator.check_in(state.synced_height, state.service_name)
{:ok, _} = schedule_get_events(state.ethereum_events_check_interval_ms)
{:noreply, {state, callbacks}}

Expand All @@ -158,31 +158,32 @@ defmodule OMG.EthereumEventListener do
end

# see `handle_info/2`, clause for `:sync`
@decorate span(service: :ethereum_event_listener, type: :backend, name: "sync_height/2")
@decorate span(service: :ethereum_event_listener, type: :backend, name: "sync_height/3")
defp sync_height(state, callbacks, sync_guide) do
{:ok, events, db_updates, height_to_check_in, new_state} =
Core.get_events_range_for_download(state, sync_guide)
|> maybe_update_event_cache(callbacks.get_ethereum_events_callback)
|> Core.get_events(sync_guide.sync_height)
{events, new_state} =
state
|> Core.calc_events_range_set_height(sync_guide)
|> get_events(callbacks.get_ethereum_events_callback)

:ok = :telemetry.execute([:process, __MODULE__], %{events: events}, state)
db_update = [{:put, new_state.synced_height_update_key, new_state.synced_height}]
:ok = :telemetry.execute([:process, __MODULE__], %{events: events}, new_state)

{:ok, db_updates_from_callback} = callbacks.process_events_callback.(events)
:ok = publish_events(events)
:ok = OMG.DB.multi_update(db_updates ++ db_updates_from_callback)
:ok = RootChainCoordinator.check_in(height_to_check_in, state.service_name)
:ok = OMG.DB.multi_update(db_update ++ db_updates_from_callback)
:ok = RootChainCoordinator.check_in(new_state.synced_height, new_state.service_name)

new_state
end

@decorate span(service: :ethereum_event_listener, type: :backend, name: "maybe_update_event_cache/2")
defp maybe_update_event_cache({:get_events, {from, to}, %Core{} = state}, get_ethereum_events_callback) do
{:ok, new_events} = get_ethereum_events_callback.(from, to)
Core.add_new_events(state, new_events)
defp get_events({{from, to}, state}, get_events_callback) do
{:ok, new_events} = get_events_callback.(from, to)
{new_events, state}
end

@decorate span(service: :ethereum_event_listener, type: :backend, name: "maybe_update_event_cache/2")
defp maybe_update_event_cache({:dont_fetch_events, %Core{} = state}, _callback), do: state
defp get_events({:dont_fetch_events, state}, _callback) do
{[], state}
end

defp schedule_get_events(ethereum_events_check_interval_ms) do
:timer.send_after(ethereum_events_check_interval_ms, self(), :sync)
Expand Down
121 changes: 32 additions & 89 deletions apps/omg/lib/omg/ethereum_event_listener/core.ex
Original file line number Diff line number Diff line change
Expand Up @@ -28,29 +28,22 @@ defmodule OMG.EthereumEventListener.Core do

use Spandex.Decorators

# synced_height is what's being exchanged with `RootChainCoordinator`.
# The point in root chain until where it processed
defstruct synced_height_update_key: nil,
service_name: nil,
# what's being exchanged with `RootChainCoordinator` - the point in root chain until where it processed
synced_height: 0,
ethereum_events_check_interval_ms: nil,
cached: %{
data: [],
request_max_size: 1000,
# until which height the events have been pulled and cached
events_upper_bound: 0
}
request_max_size: 1000

@type event :: %{eth_height: non_neg_integer()}

@type t() :: %__MODULE__{
synced_height_update_key: atom(),
service_name: atom(),
cached: %{
data: list(event),
request_max_size: pos_integer(),
events_upper_bound: non_neg_integer()
},
ethereum_events_check_interval_ms: non_neg_integer()
synced_height: integer(),
ethereum_events_check_interval_ms: non_neg_integer(),
request_max_size: pos_integer()
}

@doc """
Expand All @@ -70,89 +63,39 @@ defmodule OMG.EthereumEventListener.Core do
synced_height_update_key: update_key,
synced_height: last_synced_ethereum_height,
service_name: service_name,
cached: %{
data: [],
request_max_size: request_max_size,
events_upper_bound: last_synced_ethereum_height
},
request_max_size: request_max_size,
ethereum_events_check_interval_ms: ethereum_events_check_interval_ms
}

{initial_state, get_height_to_check_in(initial_state)}
{initial_state, last_synced_ethereum_height}
end

@doc """
Provides a uniform way to get the height to check in.
Returns the events range -
- from (inclusive!),
- to (inclusive!)
that needs to be scraped and sets synced_height in the state.

Every call to RootChainCoordinator.check_in should use value taken from this, after all mutations to the state
"""
@spec get_height_to_check_in(t()) :: non_neg_integer()
def get_height_to_check_in(%__MODULE__{synced_height: synced_height}), do: synced_height

@doc """
Returns range Ethereum height to download
"""
@decorate span(service: :ethereum_event_listener, type: :backend, name: "get_events_range_for_download/2")
@spec get_events_range_for_download(t(), SyncGuide.t()) ::
{:dont_fetch_events, t()} | {:get_events, {non_neg_integer, non_neg_integer}, t()}
def get_events_range_for_download(%__MODULE__{cached: %{events_upper_bound: upper}} = state, %SyncGuide{
sync_height: sync_height
})
when sync_height <= upper,
do: {:dont_fetch_events, state}

@decorate span(service: :ethereum_event_listener, type: :backend, name: "get_events_range_for_download/2")
def get_events_range_for_download(
%__MODULE__{
cached: %{request_max_size: request_max_size, events_upper_bound: old_upper_bound} = cached_data
} = state,
%SyncGuide{root_chain_height: root_chain_height, sync_height: sync_height}
) do
# grab as much as allowed, but not higher than current root_chain_height and at least as much as needed to sync
# NOTE: both root_chain_height and sync_height are assumed to have any required finality margins applied by caller
next_upper_bound =
min(root_chain_height, old_upper_bound + request_max_size)
|> max(sync_height)

new_state = %__MODULE__{
state
| cached: %{cached_data | events_upper_bound: next_upper_bound}
}

{:get_events, {old_upper_bound + 1, next_upper_bound}, new_state}
end

@doc """
Stores the freshly fetched ethereum events into a memory-cache
"""
@decorate span(service: :ethereum_event_listener, type: :backend, name: "add_new_events/2")
@spec add_new_events(t(), list(event)) :: t()
def add_new_events(
%__MODULE__{cached: %{data: data} = cached_data} = state,
new_events
) do
%__MODULE__{state | cached: %{cached_data | data: data ++ new_events}}
end

@doc """
Pop some ethereum events stored in the memory-cache, up to a certain height
"""
@decorate span(service: :ethereum_event_listener, type: :backend, name: "get_events/2")
@spec get_events(t(), non_neg_integer) :: {:ok, list(event), list(), non_neg_integer, t()}
def get_events(
%__MODULE__{synced_height_update_key: update_key, cached: %{data: data}} = state,
new_sync_height
) do
{events, new_data} = Enum.split_while(data, fn %{eth_height: height} -> height <= new_sync_height end)

new_state =
state
|> Map.update!(:synced_height, &max(&1, new_sync_height))
|> Map.update!(:cached, &%{&1 | data: new_data})
|> struct!()

height_to_check_in = get_height_to_check_in(new_state)
db_update = [{:put, update_key, height_to_check_in}]
{:ok, events, db_update, height_to_check_in, new_state}
@decorate span(service: :ethereum_event_listener, type: :backend, name: "calc_events_range_set_height/2")
@spec calc_events_range_set_height(t(), SyncGuide.t()) ::
{:dont_fetch_events, t()} | {{non_neg_integer, non_neg_integer}, t()}
def calc_events_range_set_height(state, sync_guide) do
case sync_guide.sync_height <= state.synced_height do
true ->
{:dont_fetch_events, state}

_ ->
# if sync_guide.sync_height has applied margin (reorg protection)
# the only thing we need to be aware of is that we don't go pass that!
# but we want to move as fast as possible so we try to fetch as much as we can (request_max_size)
first_not_visited = state.synced_height + 1
# if first not visited = 1, and request max size is 10
# it means we can scrape AT MOST request_max_size events
max_height = state.request_max_size - 1
upper_bound = min(sync_guide.sync_height, first_not_visited + max_height)

{{first_not_visited, upper_bound}, %{state | synced_height: upper_bound}}
end
end
end
Loading