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

refactor: move Account.get_exitable_utxos/1 into watcher security #1256

Merged
merged 4 commits into from
Jan 14, 2020
Merged
Show file tree
Hide file tree
Changes from all 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
30 changes: 30 additions & 0 deletions apps/omg_watcher/lib/omg_watcher/api/account.ex
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
# Copyright 2019 OmiseGO 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.API.Account do
@moduledoc """
Module provides operations related to plasma accounts.
"""

@doc """
Gets all utxos belonging to the given address. Slow operation.
"""
@spec get_exitable_utxos(OMG.Crypto.address_t()) :: list(OMG.State.Core.exitable_utxos())
def get_exitable_utxos(address) do
# OMG.DB.utxos() takes a while.
{:ok, utxos} = OMG.DB.utxos()
unnawut marked this conversation as resolved.
Show resolved Hide resolved

OMG.State.Core.standard_exitable_utxos(utxos, address)
end
end
67 changes: 67 additions & 0 deletions apps/omg_watcher/test/omg_watcher/api/account_test.exs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
# Copyright 2019 OmiseGO 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.API.AccountTest do
use ExUnitFixtures
use ExUnit.Case, async: false
use OMG.DB.Fixtures

alias OMG.TestHelper
alias OMG.Watcher.API.Account

@eth OMG.Eth.RootChain.eth_pseudo_address()
@payment_output_type OMG.WireFormatTypes.output_type_for(:output_payment_v1)

describe "get_exitable_utxos/1" do
@tag fixtures: [:db_initialized]
test "returns an empty list if the address does not have any utxo" do
alice = TestHelper.generate_entity()
assert Account.get_exitable_utxos(alice.addr) == []
end

@tag fixtures: [:db_initialized]
test "returns utxos for the given address" do
alice = TestHelper.generate_entity()
bob = TestHelper.generate_entity()

blknum = 1927
txindex = 78
oindex = 1

_ =
OMG.DB.multi_update([
{:put, :utxo,
{
{blknum, txindex, oindex},
%{
output: %{amount: 333, currency: @eth, owner: alice.addr, output_type: @payment_output_type},
creating_txhash: nil
}
}},
{:put, :utxo,
{
{blknum, txindex, oindex + 1},
%{
output: %{amount: 999, currency: @eth, owner: bob.addr, output_type: @payment_output_type},
creating_txhash: nil
}
}}
])

[utxo] = Account.get_exitable_utxos(alice.addr)

assert %{blknum: ^blknum, txindex: ^txindex, oindex: ^oindex} = utxo
end
end
end
13 changes: 0 additions & 13 deletions apps/omg_watcher_info/lib/omg_watcher_info/api/account.ex
Original file line number Diff line number Diff line change
Expand Up @@ -34,17 +34,4 @@ defmodule OMG.WatcherInfo.API.Account do
def get_utxos(address) do
DB.TxOutput.get_utxos(address)
end

@doc """
Gets all utxos belonging to the given address.
Slow operation, compatible with security-critical.
"""
# TODO this seems weird and a breach of decoupling
@spec get_exitable_utxos(OMG.Crypto.address_t()) :: list(OMG.State.Core.exitable_utxos())
def get_exitable_utxos(address) do
# OMG.DB.utxos() takes a while.
{:ok, utxos} = OMG.DB.utxos()

OMG.State.Core.standard_exitable_utxos(utxos, address)
end
end
9 changes: 5 additions & 4 deletions apps/omg_watcher_rpc/lib/web/controllers/account.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,32 @@ defmodule OMG.WatcherRPC.Web.Controller.Account do

use OMG.WatcherRPC.Web, :controller

alias OMG.WatcherInfo.API.Account
alias OMG.Watcher.API, as: SecurityAPI
alias OMG.WatcherInfo.API, as: InfoAPI

@doc """
Gets plasma account balance
"""
def get_balance(conn, params) do
with {:ok, address} <- expect(params, "address", :address) do
address
|> Account.get_balance()
|> InfoAPI.Account.get_balance()
|> api_response(conn, :balance)
end
end

def get_utxos(conn, params) do
with {:ok, address} <- expect(params, "address", :address) do
address
|> Account.get_utxos()
|> InfoAPI.Account.get_utxos()
|> api_response(conn, :utxos)
end
end

def get_exitable_utxos(conn, params) do
with {:ok, address} <- expect(params, "address", :address) do
address
|> Account.get_exitable_utxos()
|> SecurityAPI.Account.get_exitable_utxos()
|> api_response(conn, :utxos)
end
end
Expand Down