-
Notifications
You must be signed in to change notification settings - Fork 59
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
feat : transaction.merge endpoint #1715
Merged
Merged
Changes from 168 commits
Commits
Show all changes
169 commits
Select commit
Hold shift + click to select a range
1ab5d4b
test: Init
8cee04f
test: create `generate_utxos_map/3`
3d8cd23
test: put test in describe
db4e77e
test: added `needed_funds/2` tests
0ed41d7
add tests and light refactor for get_sorted_grouped_utxos
9379147
add tests and light refactor [2]
aa01847
test: refactor `create_advice/2`
9fb398f
add spec and clarify funds_sufficient
8fe68f0
refactor: `needed_funds`
eb61669
Merge branches 'ripzery/optimize-transaction-create' and 'ripzery/opt…
3e47a08
refactor: use shorthanded type
816465a
Add @doc and readabilty edits
cb35988
add: tests for funds_sufficient/1
3f0fdfd
refactor: simplify use of 'alice'
f433eb4
refactor: move test inside describe
4c1a713
test: add `select_utxo/2` tests
88abe16
refactor: remove extra line
ef8b4da
fix: spec
bde05f2
Merge branch 'master' of https://github.com/omisego/elixir-omg into r…
36789fc
feat: implement `add_merge_utxos`
9ab4f39
test: add test for `add_merge_utxos/2`
02aa0de
feat: add prioritize_merge_utxos/2
5c164a8
test: add test for `prioritize_merge_utxos/2`
c08deea
refactor: pipe one more time
2474b85
refactor and spec stealth merge
61d1fce
refactor: test name
85deb42
Merge branch 'ripzery/optimize-transaction-create' of github.com:omgn…
dd86791
add min_length to base validators
nicholasmueller 4e439e9
add merge route and controller
nicholasmueller 2ebe660
fix compile errors
nicholasmueller 4cef457
add: tests and doc for `add_utxos_for_stealth_merge`
0a3100d
test address/currency and utxo positions constraint
nicholasmueller 5eb4ee4
feat: integrate add_merge_utxos to create_advice
692cdbc
test: add test for create_advice
a64aba8
Merge conflict
4b91019
add: doc content for add_utxos_for_stealth_merge
6cbcdbc
Merge branch 'ripzery/optimize-transaction-create' of https://github.…
6b268a6
refactor and test prioritisation of UTXOs for stealth merge
e6abf77
test utxo constraint
nicholasmueller 50730b8
add other required utxo data
nicholasmueller fd17e79
fetch utxo logic
nicholasmueller 908db37
start transaction_test
nicholasmueller 528f86e
finaly get basic transaction api test working
nicholasmueller 48861f1
merge p
nicholasmueller 12e6680
refactor: split transaction operation
bb1f5f8
Fix dialyzer type
2a96ec0
refactor: dialyzer
9089a0e
fix: typo
dd0dec1
test: add utxo_selection test
412dfd2
debug: transaction create
bfaae7a
feat: handle when original inputs are empty
e419ab8
tests: remove transaction merge tests
22ea3d7
Merge branch 'master' into ripzery/optimize-transaction-create
85ec55d
tests: add tests for utxo selection
08a3345
tests: handle when too many inputs to satisfy payments and fee
32b0c6d
refactor: remove todo
97f5b0c
refactor: parameter validation for transaction.merge
c1c8f8d
Merge branch 'ripzery/optimize-transaction-create' of https://github.…
aaade0b
Merge branch 'master' of https://github.com/omisego/elixir-omg into n…
4df2ddb
optional desc/asc for UTXO fetch
ef95ea2
WIP: merge functionality
7c07d83
feat: move `create_advice` from `utxo_selection.ex` to `transaction.…
1c2a4e4
test: add transaction tests
e2123c2
WIP: return multiple merge transactions if address/ccy params
f90b2f7
Merge branch 'ripzery/optimize-transaction-create' of https://github.…
cc6427c
test: change port `9657` to `9656`
4994fa5
cleanup: remove unused var
26448df
lint: remove @doc for defp
a7689a5
test: revert back port `9656` to `9657`
37820e4
lint: fix lint
97f3588
refactor: remove result from response
ed48bef
Merge branch 'master' of https://github.com/omisego/elixir-omg into n…
ab5de0d
Merge branch 'ripzery/optimize-transaction-create' of https://github.…
758a2cb
test: add prepare_test_server
b96787c
WIP: merge when given utxo positions
898ef00
refactor: fix per reviews
8189f7a
tests: add test case where 2 input use different currencies
135d928
refactor: remove alice, bob from test context
2b2db83
test: fix mismatch function signature
f90dbcc
fix: dialyzer type
625c130
test for merge with address and currency, refactor create merge so no…
nicholasmueller 7b0eda1
more happy path tests
nicholasmueller eb607d0
refactor: Cleanup
ae0ebe9
fix: dialyzer
ada001c
fix: credo
89f664d
remove unusued alias
0b5d31d
remove unaccepted merge constraints
nicholasmueller 2be71ee
remove max 4 merge constratint
nicholasmueller 652e1df
match tx.create response shape
nicholasmueller 8aa0e93
refactor: remove comments
6a95742
lint: fix
7d253e7
tests: add tests for transaction.create apis
78daf72
test: delete unnecessary function
be1ca39
address/currency merge controller tests
nicholasmueller 8bc1973
utxo position happy path
nicholasmueller 5bbd8b9
add multiple tx for utxo pos test
nicholasmueller 374ac55
Merge branch 'master' into ripzery/optimize-transaction-create
0e6843a
refactor: simplified `select_inputs` flow
2dedd6f
start merge view test
nicholasmueller 3aee87d
revert merge view since controler test makes this redundant
nicholasmueller a0f2a77
Merge branch 'master' into nm-transaction-merge-endpoint
nicholasmueller 69b95ff
Merge branch 'ripzery/optimize-transaction-create' into nm-transactio…
nicholasmueller 6be39d4
remove default argument in get_sorted_grouped_utxos and reflect change
0b652c2
refactor: remove "respond"
e055ceb
update swagger for merge endpoint
nicholasmueller aac5515
generate auto swagger docs
nicholasmueller 20a3ffc
move private below public funcs
nicholasmueller 73aa38a
change list first for match
nicholasmueller ca32f4c
refactor: fix follows pr review
f96403d
tests: fix wrong function
7a0ee66
lint: add alias
6bfb498
refactor: remove trailing comma
83df000
refactor: var name
a64f699
fixes: tests, dialyzer, credo
3ff17c3
remove single pipes
0e2e21a
docs: two examples for transaction.merge
3cbcf11
Merge branch 'ripzery/optimize-transaction-create' of https://github.…
d236051
switch branch for cabbage test
a80a2a8
refactor: move functions out of controller
08428c5
Merge branch 'ripzery/optimize-transaction-create' of github.com:omgn…
211cd59
test: add outputs to assertion
7f7b59b
test: add tests for `transaction.create/2`
c6ed37d
test: implement tests for include_typed_data/1
f85a70a
refactor: remove single pipe
c3d4891
tests: add tests for `select_inputs/2`
c8bcd40
refactor: cleanup typespec
6b1f10a
lint: formatting
c4e7ec7
lint: remove unused alias
f4b6461
Merge branch 'master' into ripzery/optimize-transaction-create
03d9714
lint: reorder the alias
cc89117
Merge branch 'ripzery/optimize-transaction-create' of github.com:omgn…
77773b0
refactor: move spec and simplified logic
11d9ca4
refactor: simplify pattern matching
0c33028
refactor: uniformize param
6babc77
Merge branch 'ripzery/optimize-transaction-create' into nm-transactio…
nicholasmueller 42f03e0
revert: bring back merge transaction
9d0be6b
fixes
cd7ecda
refactor: lift out respond
f46ce14
Merge branch 'ripzery/optimize-transaction-create' of github.com:omgn…
b81d83a
Merge branch 'ripzery/optimize-transaction-create' of https://github.…
175bddf
fix: is_required_merge doesn't work
d8c1fc1
refactor: transaction api
4c5e040
Merge branch 'ripzery/optimize-transaction-create' of https://github.…
4b0709c
resolve initial conflicts
f635fc4
tighten specification and refactor tests
1da9055
add: dialyzer specs
a16390e
refactor: merge constraints
547faac
refactor: accept max. 4 utxo positions
1b80e3b
refactor/add tests
920e7ed
refactor: merge_constraints limit max. positions to 4
df1650c
refactor: replace Alice and Bob fixtures with setup
a47e76d
refactor: return keyword lists from parameter validation
6e70f62
update documentation
d60473e
Merge branch 'master' of https://github.com/omisego/elixir-omg into n…
dd4ebc3
Merge branch 'master' of https://github.com/omisego/elixir-omg into n…
4bac755
Merge branch 'master' of https://github.com/omisego/elixir-omg into n…
e6988a7
post-merge: fix failing tests
ed73ccf
post-merge: fix failing tests
033869f
fix: dialyzer
b4b087d
fix (lint): remove unused aliases
4ccfcea
post-merge: remove unintended spacing
e5a7315
add: fallback descriptions
84d1dd9
remove `result` key from body schema for /merge
5f1c11b
reinstate result key where removed.
ece9a5a
rename tests
3c6ed93
fix: duplicate test typo
b28c836
Merge branch 'master' of https://github.com/omisego/elixir-omg into n…
cde2109
remove empty line
56387a7
Merge branch 'master' of https://github.com/omisego/elixir-omg into n…
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
191 changes: 191 additions & 0 deletions
191
apps/omg_watcher_info/test/omg_watcher_info/api/transaction_test.exs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,191 @@ | ||
# Copyright 2019-2020 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.WatcherInfo.API.TransactionTest do | ||
use ExUnitFixtures | ||
use ExUnit.Case, async: false | ||
use OMG.WatcherInfo.Fixtures | ||
|
||
alias OMG.Utxo.Position | ||
alias OMG.WatcherInfo.API.Transaction | ||
|
||
import OMG.WatcherInfo.Factory | ||
|
||
@alice <<1::160>> | ||
@bob <<2::160>> | ||
@currency_1 <<3::160>> | ||
@currency_2 <<4::160>> | ||
|
||
describe "merge/1 with address and currency parameters" do | ||
@tag fixtures: [:phoenix_ecto_sandbox] | ||
test "merge with address and currency forms multiple merge transactions if possible" do | ||
insert_initial_utxo() | ||
|
||
_ = insert(:txoutput, currency: @currency_1, owner: @alice, amount: 1) | ||
_ = insert(:txoutput, currency: @currency_1, owner: @alice, amount: 1) | ||
_ = insert(:txoutput, currency: @currency_1, owner: @alice, amount: 1) | ||
_ = insert(:txoutput, currency: @currency_1, owner: @alice, amount: 1) | ||
_ = insert(:txoutput, currency: @currency_1, owner: @alice, amount: 1) | ||
_ = insert(:txoutput, currency: @currency_1, owner: @alice, amount: 1) | ||
_ = insert(:txoutput, currency: @currency_1, owner: @alice, amount: 1) | ||
|
||
assert {:ok, [%{inputs: [_, _, _, _], outputs: [output_1]}, %{inputs: [_, _, _], outputs: [output_2]}]} = | ||
Transaction.merge(%{address: @alice, currency: @currency_1}) | ||
|
||
assert output_1 === %{amount: 4, currency: @currency_1, owner: @alice} | ||
assert output_2 === %{amount: 3, currency: @currency_1, owner: @alice} | ||
end | ||
|
||
@tag fixtures: [:phoenix_ecto_sandbox] | ||
test "fetches inputs for the given addresss only" do | ||
insert_initial_utxo() | ||
|
||
_ = insert(:txoutput, currency: @currency_1, owner: @bob, amount: 1) | ||
_ = insert(:txoutput, currency: @currency_1, owner: @bob, amount: 1) | ||
_ = insert(:txoutput, currency: @currency_1, owner: @alice, amount: 1) | ||
_ = insert(:txoutput, currency: @currency_1, owner: @alice, amount: 1) | ||
_ = insert(:txoutput, currency: @currency_1, owner: @alice, amount: 1) | ||
_ = insert(:txoutput, currency: @currency_1, owner: @alice, amount: 1) | ||
|
||
assert {:ok, [alice_merge]} = Transaction.merge(%{address: @alice, currency: @currency_1}) | ||
assert %{inputs: [_, _, _, _], outputs: [%{amount: 4, currency: @currency_1, owner: @alice}]} = alice_merge | ||
end | ||
|
||
@tag fixtures: [:phoenix_ecto_sandbox] | ||
test "fetches inputs for the given currency only" do | ||
insert_initial_utxo() | ||
|
||
_ = insert(:txoutput, currency: @currency_1, owner: @alice, amount: 1) | ||
_ = insert(:txoutput, currency: @currency_1, owner: @alice, amount: 1) | ||
_ = insert(:txoutput, currency: @currency_2, owner: @alice, amount: 1) | ||
_ = insert(:txoutput, currency: @currency_2, owner: @alice, amount: 1) | ||
|
||
assert {:ok, [alice_merge]} = Transaction.merge(%{address: @alice, currency: @currency_2}) | ||
assert %{inputs: [_, _], outputs: [%{amount: 2, currency: @currency_2, owner: @alice}]} = alice_merge | ||
end | ||
|
||
@tag fixtures: [:phoenix_ecto_sandbox] | ||
test "returns one merge transaction if five UTXOs are available – prioritising the lowest value inputs" do | ||
insert_initial_utxo() | ||
|
||
_ = insert(:txoutput, currency: @currency_1, owner: @alice, amount: 1) | ||
_ = insert(:txoutput, currency: @currency_1, owner: @alice, amount: 2) | ||
_ = insert(:txoutput, currency: @currency_1, owner: @alice, amount: 3) | ||
_ = insert(:txoutput, currency: @currency_1, owner: @alice, amount: 4) | ||
_ = insert(:txoutput, currency: @currency_1, owner: @alice, amount: 5) | ||
|
||
assert {:ok, [merge_tx]} = Transaction.merge(%{address: @alice, currency: @currency_1}) | ||
|
||
assert %{ | ||
inputs: [%{amount: 1}, %{amount: 2}, %{amount: 3}, %{amount: 4}], | ||
outputs: [%{amount: 10, currency: @currency_1, owner: @alice}] | ||
} = merge_tx | ||
end | ||
|
||
@tag fixtures: [:phoenix_ecto_sandbox] | ||
test "merge with address and currency fails on single input" do | ||
insert_initial_utxo() | ||
|
||
_ = insert(:txoutput, currency: @currency_1, owner: @alice, amount: 1) | ||
_ = insert(:txoutput, currency: @currency_2, owner: @alice, amount: 1) | ||
|
||
assert Transaction.merge(%{address: @alice, currency: @currency_1}) == | ||
{:error, :single_input} | ||
end | ||
|
||
@tag fixtures: [:phoenix_ecto_sandbox] | ||
test "returns expected error when no inputs are found" do | ||
assert Transaction.merge(%{address: @alice, currency: @currency_1}) == {:error, :no_inputs_found} | ||
end | ||
end | ||
|
||
describe "merge/1 with utxo_positions parameter" do | ||
@tag fixtures: [:phoenix_ecto_sandbox] | ||
test "given valid `utxo_positions` parameters, forms a merge transaction correctly" do | ||
insert_initial_utxo() | ||
|
||
position_1 = | ||
:txoutput |> insert(owner: @alice, currency: @currency_1, amount: 1) |> encoded_position_from_insert() | ||
|
||
position_2 = | ||
:txoutput |> insert(owner: @alice, currency: @currency_1, amount: 1) |> encoded_position_from_insert() | ||
|
||
position_3 = | ||
:txoutput |> insert(owner: @alice, currency: @currency_1, amount: 1) |> encoded_position_from_insert() | ||
|
||
{:ok, [%{inputs: inputs, outputs: outputs}]} = | ||
Transaction.merge([{:utxo_positions, [position_1, position_2, position_3]}]) | ||
|
||
assert length(inputs) == 3 | ||
assert [%{amount: 3, currency: @currency_1, owner: @alice}] = outputs | ||
end | ||
|
||
@tag fixtures: [:phoenix_ecto_sandbox] | ||
test "returns an error if any duplicate positions are in the list" do | ||
insert_initial_utxo() | ||
|
||
position_1 = :txoutput |> insert() |> encoded_position_from_insert() | ||
|
||
assert Transaction.merge([{:utxo_positions, [position_1, position_1]}]) == | ||
{:error, :duplicate_input_positions} | ||
end | ||
|
||
@tag fixtures: [:phoenix_ecto_sandbox] | ||
test "returns expected error if any position is not found" do | ||
insert_initial_utxo() | ||
|
||
position_1 = :txoutput |> insert() |> encoded_position_from_insert() | ||
position_2 = :txoutput |> insert() |> encoded_position_from_insert() | ||
position_3 = :txoutput |> insert() |> encoded_position_from_insert() | ||
|
||
empty_position = insert(:txoutput) |> Map.update!(:blknum, fn n -> n + 1 end) |> encoded_position_from_insert() | ||
|
||
assert Transaction.merge([{:utxo_positions, [position_1, position_2, position_3, empty_position]}]) == | ||
{:error, :position_not_found} | ||
end | ||
|
||
@tag fixtures: [:phoenix_ecto_sandbox] | ||
test "returns an error if there is more than one owner for the given set of UTXO positions" do | ||
insert_initial_utxo() | ||
|
||
position_1 = :txoutput |> insert(owner: @alice, currency: @currency_1) |> encoded_position_from_insert() | ||
position_2 = :txoutput |> insert(owner: @alice, currency: @currency_1) |> encoded_position_from_insert() | ||
position_3 = :txoutput |> insert(owner: @bob, currency: @currency_1) |> encoded_position_from_insert() | ||
|
||
assert Transaction.merge([{:utxo_positions, [position_1, position_2, position_3]}]) == | ||
{:error, :multiple_input_owners} | ||
end | ||
|
||
@tag fixtures: [:phoenix_ecto_sandbox] | ||
test "returns an error if there is more than one currency for the given set of UTXO positions" do | ||
insert_initial_utxo() | ||
|
||
position_1 = :txoutput |> insert(owner: @alice, currency: @currency_1) |> encoded_position_from_insert() | ||
position_2 = :txoutput |> insert(owner: @alice, currency: @currency_1) |> encoded_position_from_insert() | ||
position_3 = :txoutput |> insert(owner: @alice, currency: @currency_2) |> encoded_position_from_insert() | ||
|
||
assert Transaction.merge([{:utxo_positions, [position_1, position_2, position_3]}]) == | ||
{:error, :multiple_currencies} | ||
end | ||
end | ||
|
||
defp encoded_position_from_insert(%{oindex: oindex, txindex: txindex, blknum: blknum}) do | ||
Position.encode({:utxo_position, blknum, txindex, oindex}) | ||
end | ||
|
||
# This is needed so that UTXOs inserted subsequently can have a proper (non-zero) position | ||
defp insert_initial_utxo() do | ||
insert(:txoutput) | ||
end | ||
end |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
wowowowo, nice!