Skip to content

Commit

Permalink
feat(watcher): add exit detection feature in WatcherDB
Browse files Browse the repository at this point in the history
  • Loading branch information
Pawel Nowosielski authored and Pawel Nowosielski committed Oct 8, 2018
1 parent f28923d commit 15e2222
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 1 deletion.
8 changes: 7 additions & 1 deletion apps/omg_watcher/lib/application.ex
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ defmodule OMG.Watcher.Application do
synced_height_update_key: :last_fast_exit_eth_height,
service_name: :fast_validator,
get_events_callback: &OMG.Eth.RootChain.get_exits/2,
process_events_callback: OMG.Watcher.ExitValidator.Validator.challenge_invalid_exits(fn _ -> :ok end),
process_events_callback: &exit_events_callback/1,
get_last_synced_height_callback: &OMG.DB.last_fast_exit_eth_height/0
}
],
Expand Down Expand Up @@ -115,4 +115,10 @@ defmodule OMG.Watcher.Application do
_ = OMG.Watcher.DB.EthEventDB.insert_deposits(deposits)
:ok
end

defp exit_events_callback(exits) do
:ok = OMG.Watcher.ExitValidator.Validator.challenge_invalid_exits(fn _ -> :ok end).(exits)
_ = OMG.Watcher.DB.EthEventDB.insert_exits(exits)
:ok
end
end
24 changes: 24 additions & 0 deletions apps/omg_watcher/lib/db/eth_event_db.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,30 @@ defmodule OMG.Watcher.DB.EthEventDB do
|> Repo.insert()
end

@spec insert_exits([OMG.API.State.Core.exit_t()]) :: [{:ok, %__MODULE__{}} | {:error, Ecto.Changeset.t()}]
def insert_exits(exits) do
exits
|> Enum.map(fn %{utxo_pos: utxo_pos} ->
position = Utxo.Position.decode(utxo_pos)
{:ok, _} = insert_exit(position)
end)
end

@spec insert_exit(Utxo.Position.t()) :: {:ok, %__MODULE__{}} | {:error, Ecto.Changeset.t()}
defp insert_exit(Utxo.position(blknum, txindex, _oindex) = position) do
utxo = TxOutputDB.get_by_position(position)

{:ok, _} =
%__MODULE__{
hash: exit_key(position),
blknum: blknum,
txindex: txindex,
event_type: :exit,
exited_utxo: utxo
}
|> Repo.insert()
end

@doc """
Good candidate for deposit/exit primary key is a pair (Utxo.position, event_type).
Switching to composite key requires carefull consideration of data types and schema change,
Expand Down

0 comments on commit 15e2222

Please sign in to comment.