-
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
cabbage test for payment v2 #1578
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3f855d2
feat: test with payment v2 transfer
boolafish 258b1b0
feat: allow /transaction.create to pass in tx_type
boolafish f7ac25c
test: cabbage test for payment v2 transfer
boolafish df74571
fix: cleanup
boolafish 4e8d539
refactor: use Alice and Bob instead
boolafish e7db8b8
fix: credo complain single pipe
boolafish 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
# 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.State.Transaction.Payment.Builder do | ||
@moduledoc """ | ||
Module to build payment tx of different tx types. (eg. v1 and v2) | ||
""" | ||
|
||
alias OMG.Crypto | ||
alias OMG.Output | ||
alias OMG.State.Transaction | ||
alias OMG.State.Transaction.Payment | ||
alias OMG.Utxo | ||
|
||
require Transaction | ||
require Utxo | ||
|
||
@max_inputs 4 | ||
@max_outputs 4 | ||
@zero_metadata <<0::256>> | ||
|
||
@payment_tx_type OMG.WireFormatTypes.tx_type_for(:tx_payment_v1) | ||
@payment_output_type OMG.WireFormatTypes.output_type_for(:output_payment_v1) | ||
|
||
@payment_v2_tx_type OMG.WireFormatTypes.tx_type_for(:tx_payment_v2) | ||
@payment_v2_output_type OMG.WireFormatTypes.output_type_for(:output_payment_v2) | ||
|
||
@output_types %{ | ||
@payment_tx_type => @payment_output_type, | ||
@payment_v2_tx_type => @payment_v2_output_type | ||
} | ||
|
||
@doc """ | ||
Creates a new raw transaction structure from a list of inputs and a list of outputs, given in a succinct tuple form. | ||
|
||
assumptions: | ||
``` | ||
length(inputs) <= @max_inputs | ||
length(outputs) <= @max_outputs | ||
``` | ||
""" | ||
@spec new_payment( | ||
pos_integer, | ||
list({pos_integer, pos_integer, 0..unquote(@max_outputs - 1)}), | ||
list({Crypto.address_t(), Payment.currency(), pos_integer}), | ||
Transaction.metadata() | ||
) :: Payment.t() | ||
def new_payment(tx_type, inputs, outputs, metadata \\ @zero_metadata) | ||
when Transaction.is_metadata(metadata) and length(inputs) <= @max_inputs and length(outputs) <= @max_outputs do | ||
inputs = Enum.map(inputs, &new_input/1) | ||
outputs = Enum.map(outputs, fn output -> new_output(output, @output_types[tx_type]) end) | ||
%Transaction.Payment{tx_type: tx_type, inputs: inputs, outputs: outputs, metadata: metadata} | ||
end | ||
|
||
# `new_input/1` and `new_output/1` are here to just help interpret the short-hand form of inputs outputs when doing | ||
# `new/3` | ||
defp new_input({blknum, txindex, oindex}), do: Utxo.position(blknum, txindex, oindex) | ||
|
||
defp new_output({owner, currency, amount}, output_type) do | ||
%Output{ | ||
owner: owner, | ||
currency: currency, | ||
amount: amount, | ||
output_type: output_type | ||
} | ||
end | ||
end |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -22,6 +22,7 @@ defmodule OMG.WatcherInfo.UtxoSelection do | |
alias OMG.TypedDataHash | ||
alias OMG.WatcherInfo.DB | ||
|
||
require Logger | ||
require Transaction | ||
require Transaction.Payment | ||
|
||
|
@@ -37,6 +38,7 @@ defmodule OMG.WatcherInfo.UtxoSelection do | |
} | ||
|
||
@type order_t() :: %{ | ||
tx_type: non_neg_integer(), | ||
owner: Crypto.address_t(), | ||
payments: nonempty_list(payment_t()), | ||
fee: fee_t(), | ||
|
@@ -71,7 +73,8 @@ defmodule OMG.WatcherInfo.UtxoSelection do | |
TODO: seems unocovered by any tests | ||
""" | ||
@spec create_advice(%{Transaction.Payment.currency() => list(%DB.TxOutput{})}, order_t()) :: advice_t() | ||
def create_advice(utxos, %{owner: owner, payments: payments, fee: fee} = order) do | ||
def create_advice(utxos, order) do | ||
%{tx_type: tx_type, owner: owner, payments: payments, fee: fee} = order | ||
needed_funds = needed_funds(payments, fee) | ||
token_utxo_selection = select_utxo(utxos, needed_funds) | ||
|
||
|
@@ -83,7 +86,7 @@ defmodule OMG.WatcherInfo.UtxoSelection do | |
|
||
if utxo_count <= Transaction.Payment.max_inputs(), | ||
do: create_transaction(funds, order) |> respond(:complete), | ||
else: create_merge(owner, funds) |> respond(:intermediate) | ||
else: create_merge(tx_type, owner, funds) |> respond(:intermediate) | ||
end | ||
end | ||
|
||
|
@@ -139,7 +142,9 @@ defmodule OMG.WatcherInfo.UtxoSelection do | |
else: {:error, {:insufficient_funds, missing_funds}} | ||
end | ||
|
||
defp create_transaction(utxos_per_token, %{owner: owner, payments: payments, metadata: metadata, fee: fee}) do | ||
defp create_transaction(utxos_per_token, order) do | ||
%{tx_type: tx_type, owner: owner, payments: payments, metadata: metadata, fee: fee} = order | ||
|
||
rests = | ||
utxos_per_token | ||
|> Stream.map(fn {token, utxos} -> | ||
|
@@ -165,10 +170,11 @@ defmodule OMG.WatcherInfo.UtxoSelection do | |
{:error, :empty_transaction} | ||
|
||
true -> | ||
raw_tx = create_raw_transaction(inputs, outputs, metadata) | ||
raw_tx = create_raw_transaction(tx_type, inputs, outputs, metadata) | ||
|
||
{:ok, | ||
%{ | ||
tx_type: tx_type, | ||
inputs: inputs, | ||
outputs: outputs, | ||
fee: fee, | ||
|
@@ -179,7 +185,7 @@ defmodule OMG.WatcherInfo.UtxoSelection do | |
end | ||
end | ||
|
||
defp create_merge(owner, utxos_per_token) do | ||
defp create_merge(tx_type, owner, utxos_per_token) do | ||
utxos_per_token | ||
|> Enum.map(fn {token, utxos} -> | ||
Stream.chunk_every(utxos, Transaction.Payment.max_outputs()) | ||
|
@@ -190,6 +196,7 @@ defmodule OMG.WatcherInfo.UtxoSelection do | |
|
||
inputs -> | ||
create_transaction([{token, inputs}], %{ | ||
tx_type: tx_type, | ||
fee: %{amount: 0, currency: token}, | ||
metadata: @empty_metadata, | ||
owner: owner, | ||
|
@@ -201,15 +208,22 @@ defmodule OMG.WatcherInfo.UtxoSelection do | |
|> Enum.map(fn {:ok, tx} -> tx end) | ||
end | ||
|
||
defp create_raw_transaction(inputs, outputs, metadata) do | ||
if Enum.any?(outputs, &(&1.owner == nil)), | ||
do: nil, | ||
else: | ||
Transaction.Payment.new( | ||
defp create_raw_transaction(tx_type, inputs, outputs, metadata) do | ||
# Somehow the API is designed to return nil txbytes instead of claiming that as bad request :( | ||
# So we need to return nil here | ||
# See the test: "test /transaction.create does not return txbytes when spend owner is not provided" | ||
case Enum.any?(outputs, &(&1.owner == nil)) do | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. damn...I used to be on the side of using |
||
true -> | ||
nil | ||
|
||
false -> | ||
Transaction.Payment.Builder.new_payment( | ||
tx_type, | ||
inputs |> Enum.map(&{&1.blknum, &1.txindex, &1.oindex}), | ||
outputs |> Enum.map(&{&1.owner, &1.currency, &1.amount}), | ||
metadata || @empty_metadata | ||
) | ||
end | ||
end | ||
|
||
defp create_txbytes(tx) do | ||
|
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
Feature: Payment V2 transaction | ||
|
||
Scenario: Alice sends Bob ETH with payment v1 and v2 | ||
When Alice deposits "4" ETH to the root chain | ||
Then Alice should have "4" ETH on the child chain | ||
When Alice sends Bob "1" ETH on the child chain with payment v1 | ||
Then Bob should have "1" ETH on the child chain | ||
When Alice sends Bob "2" ETH on the child chain with payment v2 | ||
Then Bob should have "3" ETH on the child chain |
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.
I wonder if different transaction types will have enough shared behaviours to share the same builder? E.g. same number of inputs and outputs, expect the same parameters, etc.
I'm thinking, if not, it might be better to share just the interface (i.e. function signatures) and not the builder implementation (e.g. implying that v1 and v2's inputs can be mapped with
Utxo.position/3
and outputs with%Output{}
)? Otherwise this will be the scary module that everyone's afraid to edit.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.
In the ideal state (of my personal thinking), it will be best if each tx type has its own module. This PR is reusing
Transaction.Payment
that leads to this awkward need of this builder. If each tx type has its own module then the module would be responsible to have its own function like thisnew
and can be interface or behavior-ized. Two payment module can be refactored to share the code between themselves but that is out of the concern of the interface. And new non payment tx type will have nice interface to follow (if everything goes smooth, I know from experience interface breaks often too 😅).So...I did spend a bit time on having PaymentV2 module. I think it breaks a bit on child chain and breaks quite a lot on watcher-info. The code was not designed for ALD (which makes sense) so nothing is dispatching via tx type at this moment.
As the refactor seems a bit too huge, I want to have test covering first to give us faster feedback loop and some confidence. Also, want to focus on the POC now and try to figure a list of things to refactor on master branch to make it ALD friendly.
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.
aight sounds good! Thanks for the detailed info!
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.
can I assume this as an approval of the PR :p