Skip to content

Commit

Permalink
standard exit struct and warnings cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
InoMurko committed Jan 27, 2021
1 parent 1e47686 commit 7ad67f4
Show file tree
Hide file tree
Showing 14 changed files with 97 additions and 58 deletions.
1 change: 0 additions & 1 deletion apps/omg_eth/test/omg_eth/root_chain_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ defmodule OMG.Eth.RootChainTest do
alias OMG.Eth.Encoding
alias OMG.Eth.RootChain
alias OMG.Eth.RootChain.Abi
alias OMG.Eth.RootChain.AbiEventSelector
alias Support.DevHelper
alias Support.RootChainHelper

Expand Down
6 changes: 4 additions & 2 deletions apps/omg_watcher/lib/omg_watcher/exit_processor/core.ex
Original file line number Diff line number Diff line change
Expand Up @@ -193,9 +193,11 @@ defmodule OMG.Watcher.ExitProcessor.Core do
new_exits_map = Map.new(new_exits_kv_pairs)

new_exit_ids_map =
new_exits_map |> Enum.into(%{}, fn {utxo_pos, %ExitInfo{exit_id: exit_id}} -> {exit_id, utxo_pos} end)
Enum.into(new_exits_map, %{}, fn {utxo_pos, %ExitInfo{exit_id: exit_id}} -> {exit_id, utxo_pos} end)

{%{state | exits: Map.merge(exits, new_exits_map), exit_ids: Map.merge(exit_ids, new_exit_ids_map)}, db_updates}
exits = Map.merge(exits, new_exits_map)
exit_ids = Map.merge(exit_ids, new_exit_ids_map)
{%{state | exits: exits, exit_ids: exit_ids}, db_updates}
end

defdelegate finalize_exits(state, validities), to: ExitProcessor.Finalizations
Expand Down
26 changes: 12 additions & 14 deletions apps/omg_watcher/lib/omg_watcher/exit_processor/exit_info.ex
Original file line number Diff line number Diff line change
Expand Up @@ -59,17 +59,16 @@ defmodule OMG.Watcher.ExitProcessor.ExitInfo do
}

@spec new(map(), map()) :: t()
def new(
contract_status,
%{
eth_height: eth_height,
output_tx: txbytes,
exit_id: exit_id,
root_chain_txhash: root_chain_txhash,
scheduled_finalization_time: scheduled_finalization_time,
block_timestamp: block_timestamp
} = exit_event
) do
def new(contract_status, exit_event) do
%{
eth_height: eth_height,
output_tx: txbytes,
exit_id: exit_id,
root_chain_txhash: root_chain_txhash,
scheduled_finalization_time: scheduled_finalization_time,
block_timestamp: block_timestamp
} = exit_event

Utxo.position(_, _, oindex) = utxo_pos_for(exit_event)
{:ok, raw_tx} = Transaction.decode(txbytes)
%{amount: amount, currency: currency, owner: owner} = raw_tx |> Transaction.get_outputs() |> Enum.at(oindex)
Expand All @@ -90,8 +89,7 @@ defmodule OMG.Watcher.ExitProcessor.ExitInfo do

def new_key(exit_info), do: utxo_pos_for(exit_info)

defp utxo_pos_for(%{utxo_pos: utxo_pos_enc} = _exit_info),
do: Utxo.Position.decode!(utxo_pos_enc)
defp utxo_pos_for(%{utxo_pos: utxo_pos_enc} = _exit_info), do: Utxo.Position.decode!(utxo_pos_enc)

@spec do_new(map(), list(keyword())) :: t()
defp do_new(contract_status, fields) do
Expand Down Expand Up @@ -160,7 +158,7 @@ defmodule OMG.Watcher.ExitProcessor.ExitInfo do
# `exitable` will be `false` if the exit was challenged
# `exitable` will be `false` ALONG WITH the whole tuple holding zeroees, if the exit was processed successfully
# **NOTE** one can only rely on the zero-nonzero of this data, since for processed exits this data will be all zeros
defp parse_contract_exit_status({exitable, _, _, _, _, _}), do: exitable
defp parse_contract_exit_status({exitable, _, _, _, _, _, _}), do: exitable

# Based on the block number determines whether UTXO was created by a deposit.
defguardp is_deposit(blknum, child_block_interval) when rem(blknum, child_block_interval) != 0
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ defmodule OMG.Watcher.ExitProcessor.PersistenceTest do
alias OMG.State.Transaction
alias OMG.Utxo
alias OMG.Watcher.ExitProcessor.Core
alias OMG.Watcher.ExitProcessor.StandardExitIntegration

require Utxo

Expand Down Expand Up @@ -81,8 +82,12 @@ defmodule OMG.Watcher.ExitProcessor.PersistenceTest do
}
],
[
{true, Utxo.Position.encode(@utxo_pos1), Utxo.Position.encode(@utxo_pos1), alice.addr, 10, 0},
{false, Utxo.Position.encode(@utxo_pos2), Utxo.Position.encode(@utxo_pos2), alice.addr, 10, 0}
StandardExitIntegration.standard_exit_struct(
{true, Utxo.Position.encode(@utxo_pos1), Utxo.Position.encode(@utxo_pos1), alice.addr, 10, 0, 0}
),
StandardExitIntegration.standard_exit_struct(
{false, Utxo.Position.encode(@utxo_pos2), Utxo.Position.encode(@utxo_pos2), alice.addr, 10, 0, 0}
)
]}

{:ok, %{alice: alice, carol: carol, processor_empty: processor_empty, transactions: transactions, exits: exits}}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ defmodule OMG.Watcher.ExitProcessor.StandardExitTest do
alias OMG.Watcher.Event
alias OMG.Watcher.ExitProcessor
alias OMG.Watcher.ExitProcessor.Core
alias OMG.Watcher.ExitProcessor.StandardExitIntegration

require Utxo

Expand Down Expand Up @@ -545,7 +546,7 @@ defmodule OMG.Watcher.ExitProcessor.StandardExitTest do
test "can process challenged exits", %{processor_empty: processor, alice: alice} do
# see the contract and `Eth.RootChain.get_standard_exit_structs/1` for some explanation why like this
# this is what an exit looks like after a challenge
zero_status = {false, 0, 0, 0, 0, 0}
zero_status = StandardExitIntegration.standard_exit_struct({false, 0, 0, 0, 0, 0, 0})
standard_exit_tx = TestHelper.create_recovered([{1, 0, 0, alice}], @eth, [{alice, 10}])
processor = processor |> start_se_from(standard_exit_tx, @utxo_pos_deposit2, status: zero_status)

Expand All @@ -554,7 +555,7 @@ defmodule OMG.Watcher.ExitProcessor.StandardExitTest do
Core.determine_utxo_existence_to_get(%ExitProcessor.Request{blknum_now: @late_blknum}, processor)

# pinning because challenge shouldn't change the already challenged exit in the processor
{^processor, _} = processor |> Core.challenge_exits([%{utxo_pos: Utxo.Position.encode(@utxo_pos_deposit2)}])
{^processor, _} = Core.challenge_exits(processor, [%{utxo_pos: Utxo.Position.encode(@utxo_pos_deposit2)}])
end
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ defmodule OMG.Watcher.Integration.BlockGetter4Test do

require OMG.Utxo

alias OMG.Eth.RootChain
alias OMG.Eth.Support.BlockSubmission.Integration
alias OMG.Watcher.BlockGetter
alias OMG.Watcher.Event
Expand Down Expand Up @@ -77,7 +78,7 @@ defmodule OMG.Watcher.Integration.BlockGetter4Test do
gas_price = 20_000_000_000
nonce = RootChain.next_child_block() / 1000

{:ok, _txhash} = Integration.submit_block(block_overclaiming_fees.hash, round(nonce - 1), 20_000_000_000)
{:ok, _txhash} = Integration.submit_block(block_overclaiming_fees.hash, round(nonce - 1), gas_price)
IntegrationTest.wait_for_byzantine_events([%Event.InvalidBlock{}.name], @timeout)
end) =~ inspect({:tx_execution, :claimed_and_collected_amounts_mismatch})
end
Expand Down
44 changes: 22 additions & 22 deletions apps/omg_watcher/test/support/exit_processor/case.ex
Original file line number Diff line number Diff line change
Expand Up @@ -20,48 +20,48 @@ defmodule OMG.Watcher.ExitProcessor.Case do

alias OMG.Block
alias OMG.State.Transaction
alias OMG.TestHelper
alias OMG.Utxo
alias OMG.Watcher.ExitProcessor
alias OMG.Watcher.ExitProcessor.Core
alias OMG.Watcher.ExitProcessor.TestHelper

require Utxo

import OMG.Watcher.ExitProcessor.TestHelper

@default_min_exit_period_seconds 120
@default_child_block_interval 1000

@eth <<0::160>>
@not_eth <<1::size(160)>>

setup do
[alice, bob, carol] = 1..3 |> Enum.map(fn _ -> TestHelper.generate_entity() end)
[alice, bob, carol] = 1..3 |> Enum.map(fn _ -> OMG.TestHelper.generate_entity() end)

transactions = [
TestHelper.create_recovered([{1, 0, 0, alice}, {1, 2, 1, carol}], [{alice, @eth, 1}, {carol, @eth, 2}]),
TestHelper.create_recovered([{2, 1, 0, alice}, {2, 2, 1, carol}], [{alice, @not_eth, 1}, {carol, @not_eth, 2}])
OMG.TestHelper.create_recovered([{1, 0, 0, alice}, {1, 2, 1, carol}], [{alice, @eth, 1}, {carol, @eth, 2}]),
OMG.TestHelper.create_recovered([{2, 1, 0, alice}, {2, 2, 1, carol}], [{alice, @not_eth, 1}, {carol, @not_eth, 2}])
]

competing_tx =
TestHelper.create_recovered([{10, 2, 1, alice}, {1, 0, 0, alice}], [{bob, @eth, 2}, {carol, @eth, 1}])
OMG.TestHelper.create_recovered([{10, 2, 1, alice}, {1, 0, 0, alice}], [{bob, @eth, 2}, {carol, @eth, 1}])

unrelated_tx =
TestHelper.create_recovered([{20, 1, 0, alice}, {20, 20, 1, alice}], [{bob, @eth, 2}, {carol, @eth, 1}])
OMG.TestHelper.create_recovered([{20, 1, 0, alice}, {20, 20, 1, alice}], [{bob, @eth, 2}, {carol, @eth, 1}])

{:ok, processor_empty} = Core.init([], [], [], @default_min_exit_period_seconds, @default_child_block_interval)

in_flight_exit_events =
transactions |> Enum.zip([2, 4]) |> Enum.map(fn {tx, eth_height} -> ife_event(tx, eth_height: eth_height) end)
transactions
|> Enum.zip([2, 4])
|> Enum.map(fn {tx, eth_height} -> TestHelper.ife_event(tx, eth_height: eth_height) end)

ife_tx_hashes = transactions |> Enum.map(&Transaction.raw_txhash/1)
ife_tx_hashes = Enum.map(transactions, &Transaction.raw_txhash/1)

processor_filled =
transactions
|> Enum.zip([1, 4])
|> Enum.reduce(processor_empty, fn {tx, idx}, processor ->
# use the idx as both two distinct ethereum heights and two distinct exit_ids arriving from the root chain
start_ife_from(processor, tx, eth_height: idx, exit_id: idx)
TestHelper.start_ife_from(processor, tx, eth_height: idx, exit_id: idx)
end)

{:ok,
Expand Down Expand Up @@ -91,26 +91,26 @@ defmodule OMG.Watcher.ExitProcessor.Case do

state =
state
|> start_ife_from(competing_tx)
|> piggyback_ife_from(ife_id, 0, :input)
|> TestHelper.start_ife_from(competing_tx)
|> TestHelper.piggyback_ife_from(ife_id, 0, :input)
|> Core.find_ifes_in_blocks(request)

%{
state: state,
request: request,
ife_input_index: 0,
ife_txbytes: txbytes(tx),
spending_txbytes: txbytes(competing_tx),
ife_txbytes: TestHelper.txbytes(tx),
spending_txbytes: TestHelper.txbytes(competing_tx),
spending_input_index: 1,
spending_sig: sig(competing_tx)
spending_sig: TestHelper.sig(competing_tx)
}
end

defp invalid_piggyback_on_output(alice, state, [tx | _], [ife_id | _]) do
# the piggybacked-output-spending tx is going to be included in a block, which requires more back&forth
# 1. transaction which is, ife'd, output piggybacked, and included in a block
# 2. transaction which spends that piggybacked output
comp = TestHelper.create_recovered([{3000, 0, 0, alice}], [{alice, @eth, 1}])
comp = OMG.TestHelper.create_recovered([{3000, 0, 0, alice}], [{alice, @eth, 1}])

tx_blknum = 3000
comp_blknum = 4000
Expand All @@ -126,20 +126,20 @@ defmodule OMG.Watcher.ExitProcessor.Case do
# 3. stuff happens in the contract; output #4 is a double-spend; #5 is OK
state =
state
|> piggyback_ife_from(ife_id, 0, :output)
|> piggyback_ife_from(ife_id, 1, :output)
|> TestHelper.piggyback_ife_from(ife_id, 0, :output)
|> TestHelper.piggyback_ife_from(ife_id, 1, :output)
|> Core.find_ifes_in_blocks(request)

%{
state: state,
request: request,
ife_good_pb_index: 5,
ife_txbytes: txbytes(tx),
ife_txbytes: TestHelper.txbytes(tx),
ife_output_pos: Utxo.position(tx_blknum, 0, 0),
ife_proof: Block.inclusion_proof(block, 0),
spending_txbytes: txbytes(comp),
spending_txbytes: TestHelper.txbytes(comp),
spending_input_index: 0,
spending_sig: sig(comp),
spending_sig: TestHelper.sig(comp),
ife_input_index: 4
}
end
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
# Copyright 2019-2020 OMG Network Pte Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

defmodule OMG.Watcher.ExitProcessor.StandardExitIntegration do
alias OMG.Eth.RootChain.AbiFunctionSelector

def standard_exit_struct(struct) do
true =
AbiFunctionSelector.standard_exits().returns |> Keyword.get(:array) |> elem(1) |> length() == tuple_size(struct)

struct
end
end
18 changes: 13 additions & 5 deletions apps/omg_watcher/test/support/exit_processor/test_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ defmodule OMG.Watcher.ExitProcessor.TestHelper do
alias OMG.State.Transaction
alias OMG.Utxo
alias OMG.Watcher.ExitProcessor.Core
alias OMG.Watcher.ExitProcessor.StandardExitIntegration

require Utxo

Expand Down Expand Up @@ -61,8 +62,13 @@ defmodule OMG.Watcher.ExitProcessor.TestHelper do
fake_output_id = enc_pos
amount = nil
bond_size = nil
bounty_size = nil

status = Keyword.get(opts, :status) || {exitable, enc_pos, fake_output_id, owner, amount, bond_size}
status =
Keyword.get(opts, :status) ||
StandardExitIntegration.standard_exit_struct(
{exitable, enc_pos, fake_output_id, owner, amount, bond_size, bounty_size}
)

{event, status}
end
Expand Down Expand Up @@ -97,7 +103,7 @@ defmodule OMG.Watcher.ExitProcessor.TestHelper do

def ife_event(tx, opts \\ []) do
sigs = Keyword.get(opts, :sigs) || sigs(tx)
input_utxos_pos = Transaction.get_inputs(tx) |> Enum.map(&Utxo.Position.encode/1)
input_utxos_pos = tx |> Transaction.get_inputs() |> Enum.map(&Utxo.Position.encode/1)

input_txs = Keyword.get(opts, :input_txs) || List.duplicate("input_tx", length(input_utxos_pos))

Expand All @@ -112,8 +118,9 @@ defmodule OMG.Watcher.ExitProcessor.TestHelper do
}
end

def ife_response(tx, position),
do: %{tx_hash: Transaction.raw_txhash(tx), challenge_position: Utxo.Position.encode(position)}
def ife_response(tx, position) do
%{tx_hash: Transaction.raw_txhash(tx), challenge_position: Utxo.Position.encode(position)}
end

def ife_challenge(tx, comp, opts \\ []) do
competitor_position = Keyword.get(opts, :competitor_position)
Expand Down Expand Up @@ -171,7 +178,8 @@ defmodule OMG.Watcher.ExitProcessor.TestHelper do

defp not_included_competitor_pos() do
<<long::256>> =
List.duplicate(<<255::8>>, 32)
<<255::8>>
|> List.duplicate(32)
|> Enum.reduce(fn val, acc -> val <> acc end)

long
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ defmodule OMG.WatcherInfo.OrderFeeFetcherTest do
use ExUnit.Case, async: true
use OMG.WatcherInfo.Fixtures

alias OMG.Eth
alias OMG.Utils.HttpRPC.Encoding
alias OMG.WatcherInfo.OrderFeeFetcher
alias OMG.WatcherInfo.TestServer
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ defmodule OMG.WatcherInfo.TransactionTest do
use ExUnit.Case, async: true
use OMG.Fixtures

alias OMG.Eth
alias OMG.Utxo
alias OMG.WatcherInfo.DB
alias OMG.WatcherInfo.Transaction
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@ defmodule OMG.WatcherInfo.UtxoSelectionTest do

assert {:ok,
%{
@eth => [eth_utxo],
@other_token => [other_token_utxo]
@eth => [^eth_utxo],
@other_token => [^other_token_utxo]
}} = UtxoSelection.review_selected_utxos(constructed_argument)
end
end
Expand All @@ -129,7 +129,9 @@ defmodule OMG.WatcherInfo.UtxoSelectionTest do

utxos = DB.TxOutput.get_sorted_grouped_utxos(@alice, :desc)

assert [{@eth, {-200, utxos}}] = UtxoSelection.select_utxos(net_amount, utxos)
[{@eth, {-200, utxos2}}] = UtxoSelection.select_utxos(net_amount, utxos)

assert [] = Map.get(utxos, @eth) -- utxos2
end

@tag fixtures: [:phoenix_ecto_sandbox]
Expand All @@ -142,7 +144,9 @@ defmodule OMG.WatcherInfo.UtxoSelectionTest do

utxos = DB.TxOutput.get_sorted_grouped_utxos(@alice, :desc)

assert [{@eth, {0, utxos}}] = UtxoSelection.select_utxos(net_amount, utxos)
assert [{@eth, {0, utxos2}}] = UtxoSelection.select_utxos(net_amount, utxos)

assert [] = Map.get(utxos, @eth) -- utxos2
end

@tag fixtures: [:phoenix_ecto_sandbox]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ defmodule OMG.WatcherRPC.Web.Controller.DepositTest do
"creating_txhash" => _,
"oindex" => _,
"otype" => _,
"owner" => address,
"owner" => ^address,
"spending_txhash" => _,
"txindex" => _
}
Expand Down

0 comments on commit 7ad67f4

Please sign in to comment.