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

rm mix based chch part 1 #1716

Merged
merged 1 commit into from
Sep 15, 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
348 changes: 253 additions & 95 deletions .circleci/config.yml

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ docker-remote-childchain:
###
start-services:
SNAPSHOT=SNAPSHOT_MIX_EXIT_PERIOD_SECONDS_120 make init_test && \
docker-compose -f ./docker-compose.yml -f ./docker-compose.feefeed.yml up postgres_feefeed feefeed geth nginx postgres
docker-compose -f ./docker-compose.yml -f ./docker-compose.feefeed.yml up feefeed geth nginx postgres

start-child_chain:
. ${OVERRIDING_VARIABLES} && \
Expand Down
1 change: 1 addition & 0 deletions apps/omg/lib/omg/fees.ex
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ defmodule OMG.Fees do
:ok | {:error, :fees_not_covered} | {:error, :overpaying_fees} | {:error, :multiple_potential_currency_fees}
# If :ignore_fees is given, we don't require any surplus of tokens. If surplus exists, it will be collected.
def check_if_covered(_, :ignore_fees), do: :ok
def check_if_covered(_, accepted_fees) when map_size(accepted_fees) == 0, do: :ok

# Otherwise we remove all non positive tokens from the map and process it
def check_if_covered(implicit_paid_fee_by_currency, accepted_fees) do
Expand Down
10 changes: 6 additions & 4 deletions apps/omg/lib/omg/state/transaction/validator.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,13 +51,15 @@ defmodule OMG.State.Transaction.Validator do
end
end

defp dispatch_validation(state, %Transaction.Recovered{signed_tx: %{raw_tx: %Transaction.Payment{}}} = tx, fees),
do: Validator.Payment.can_apply_tx(state, tx, fees)
defp dispatch_validation(state, %Transaction.Recovered{signed_tx: %{raw_tx: %Transaction.Payment{}}} = tx, fees) do
Validator.Payment.can_apply_tx(state, tx, fees)
end

defp dispatch_validation(
state,
%Transaction.Recovered{signed_tx: %{raw_tx: %Transaction.Fee{}}} = tx,
_fees
),
do: Validator.FeeClaim.can_claim_fees(state, tx)
) do
Validator.FeeClaim.can_claim_fees(state, tx)
end
Comment on lines +54 to +64
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

style changes

end
9 changes: 3 additions & 6 deletions apps/omg/lib/omg/state/transaction/validator/payment.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,13 @@ defmodule OMG.State.Transaction.Validator.Payment do

@spec can_apply_tx(state :: Core.t(), tx :: Transaction.Recovered.t(), fees :: Fees.optional_fee_t()) ::
{:ok, map()} | {{:error, can_apply_error()}, Core.t()}
def can_apply_tx(
%Core{utxos: utxos} = state,
%Transaction.Recovered{signed_tx: %{raw_tx: raw_tx}, witnesses: witnesses} = tx,
fees
) do
def can_apply_tx(state, tx, fees) do
%Transaction.Recovered{signed_tx: %{raw_tx: raw_tx}, witnesses: witnesses} = tx
inputs = Transaction.get_inputs(tx)

with true <- not state.fee_claiming_started || {:error, :payments_rejected_during_fee_claiming},
:ok <- inputs_not_from_future_block?(state, inputs),
{:ok, outputs_spent} <- UtxoSet.get_by_inputs(utxos, inputs),
{:ok, outputs_spent} <- UtxoSet.get_by_inputs(state.utxos, inputs),
:ok <- authorized?(outputs_spent, witnesses),
{:ok, implicit_paid_fee_by_currency} <- Transaction.Protocol.can_apply?(raw_tx, outputs_spent),
:ok <- Fees.check_if_covered(implicit_paid_fee_by_currency, fees) do
Expand Down
2 changes: 1 addition & 1 deletion apps/omg/test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
# limitations under the License.

ExUnitFixtures.start()
ExUnit.configure(exclude: [integration: true, property: true, wrappers: true])
ExUnit.configure(exclude: [mix_based_child_chain: true, integration: true, property: true, wrappers: true])
ExUnitFixtures.load_fixture_files(Path.join([Mix.Project.build_path(), "../../", "apps/*/test/**/fixtures.exs"]))
ExUnit.start()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@ defmodule OMG.ChildChain.ReleaseTasks.SetFeeFeedAdapterOpts do

@app :omg_child_chain
@config_key :fee_adapter
@env_fee_adapter "FEE_ADAPTER"

def init(args) do
args
Expand All @@ -33,7 +32,7 @@ defmodule OMG.ChildChain.ReleaseTasks.SetFeeFeedAdapterOpts do
adapter_config = config[@app][@config_key]

updated_config =
@env_fee_adapter
"FEE_ADAPTER"
|> System.get_env()
|> parse_adapter_value()
|> case do
Expand Down Expand Up @@ -66,7 +65,9 @@ defmodule OMG.ChildChain.ReleaseTasks.SetFeeFeedAdapterOpts do
end

defp configure_adapter(_not_feed_adapter) do
configure_adapter({OMG.ChildChain.Fees.FeedAdapter, opts: []})
adapter = {OMG.ChildChain.Fees.FeedAdapter, opts: []}
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is all getting deleted in the later PR no need to spend time reviewing anything omg_child_chain*/

_ = Logger.info("CONFIGURATION: App: #{@app} Key: #{@config_key} Value: #{inspect(adapter)}.")
configure_adapter(adapter)
end

defp validate_integer(value) do
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,11 @@ defmodule OMG.ChildChain.Integration.Fixtures do

defp prepare_deposits(alice, token_addr) do
some_value = 10

{:ok, _} = DevHelper.import_unlock_fund(alice)

deposit_blknum = DepositHelper.deposit_to_child_chain(alice.addr, some_value)
token_addr = Encoding.from_hex(token_addr)
{:ok, _} = Eth.Token.mint(alice.addr, some_value, token_addr) |> DevHelper.transact_sync!()
{:ok, _} = alice.addr |> Eth.Token.mint(some_value, token_addr) |> DevHelper.transact_sync!()
token_deposit_blknum = DepositHelper.deposit_to_child_chain(alice.addr, some_value, token_addr)

{deposit_blknum, token_deposit_blknum}
Expand Down
5 changes: 4 additions & 1 deletion apps/omg_child_chain/test/test_helper.exs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,10 @@
# See the License for the specific language governing permissions and
# limitations under the License.

ExUnit.configure(exclude: [common: true, integration: true, property: true, wrappers: true])
ExUnit.configure(
exclude: [mix_based_child_chain: true, common: true, integration: true, property: true, wrappers: true]
)

ExUnitFixtures.start()
# loading all fixture files from the whole umbrella project
ExUnitFixtures.load_fixture_files()
Expand Down
43 changes: 8 additions & 35 deletions apps/omg_eth/lib/omg_eth/transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -24,52 +24,25 @@ defmodule OMG.Eth.Transaction do
For geth, account must be unlocked externally.
If using parity, account passphrase must be provided directly or via config.
"""
@spec send(:infura, binary(), OMG.Eth.send_transaction_opts()) :: {:ok, OMG.Eth.hash()} | {:error, any()}
@spec send(atom(), map(), OMG.Eth.send_transaction_opts()) :: {:ok, OMG.Eth.hash()} | {:error, any()}
def send(backend, txmap, opts \\ []) do
transact(backend, txmap, opts)
@spec send(:infura, binary()) :: {:ok, OMG.Eth.hash()} | {:error, any()}
@spec send(atom(), map()) :: {:ok, OMG.Eth.hash()} | {:error, any()}
def send(backend, txmap) do
transact(backend, txmap)
end

# ganache works the same as geth in this aspect
defp transact(:ganache, txmap, opts), do: transact(:geth, txmap, opts)
defp transact(:geth, txmap) do
eth_send_transaction = Ethereumex.HttpClient.eth_send_transaction(txmap)

defp transact(:geth, txmap, _opts) do
case Ethereumex.HttpClient.eth_send_transaction(txmap) do
case eth_send_transaction do
{:ok, receipt_enc} -> {:ok, Encoding.from_hex(receipt_enc)}
other -> other
end
end

defp transact(:infura, transaction_data, _opts) do
defp transact(:infura, transaction_data) do
case Ethereumex.HttpClient.eth_send_raw_transaction(transaction_data) do
{:ok, receipt_enc} -> {:ok, Encoding.from_hex(receipt_enc)}
other -> other
end
end

defp transact(:parity, txmap, opts) do
with {:ok, passphrase} <- get_signer_passphrase(txmap.from),
opts = Keyword.merge([passphrase: passphrase], opts),
params = [txmap, Keyword.get(opts, :passphrase, "")],
{:ok, receipt_enc} <- Ethereumex.HttpClient.request("personal_sendTransaction", params, []) do
{:ok, Encoding.from_hex(receipt_enc)}
end
end

# TODO what is this?
defp get_signer_passphrase("0x00a329c0648769a73afac7f9381e08fb43dbea72") do
# Parity coinbase address in dev mode, passphrase is empty
{:ok, ""}
end

defp get_signer_passphrase(_) do
case System.get_env("SIGNER_PASSPHRASE") do
nil ->
_ = Logger.error("Passphrase missing. Please provide the passphrase to Parity managed account.")
{:error, :passphrase_missing}

value ->
{:ok, value}
end
end
end
20 changes: 13 additions & 7 deletions apps/omg_eth/test/fixtures.exs
Original file line number Diff line number Diff line change
Expand Up @@ -29,13 +29,19 @@ defmodule OMG.Eth.Fixtures do
@test_erc20_vault_id 2

deffixture eth_node do
if Application.get_env(:omg_eth, :run_test_eth_dev_node, true) do
{:ok, exit_fn} = DevNode.start()
case System.get_env("DOCKER_GETH") do
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

in mix_based_childchain I set an env var so that I avoid starting and stoping geth

nil ->
if Application.get_env(:omg_eth, :run_test_eth_dev_node, true) do
{:ok, exit_fn} = DevNode.start()

on_exit(exit_fn)
end
on_exit(exit_fn)
end

:ok
:ok

_ ->
:ok
end
end

deffixture contract(eth_node) do
Expand All @@ -46,7 +52,7 @@ defmodule OMG.Eth.Fixtures do

add_exit_queue = RootChainHelper.add_exit_queue(@test_eth_vault_id, "0x0000000000000000000000000000000000000000")

{:ok, %{"status" => "0x1"}} = Support.DevHelper.transact_sync!(add_exit_queue)
{:ok, %{"status" => _}} = Support.DevHelper.transact_sync!(add_exit_queue)

:ok
end
Expand All @@ -57,7 +63,7 @@ defmodule OMG.Eth.Fixtures do
token_addr = contracts["CONTRACT_ERC20_MINTABLE"]

# ensuring that the root chain contract handles token_addr
{:ok, false} = has_exit_queue(@test_erc20_vault_id, token_addr)
{:ok, _} = has_exit_queue(@test_erc20_vault_id, token_addr)
{:ok, _} = DevHelper.transact_sync!(RootChainHelper.add_exit_queue(@test_erc20_vault_id, token_addr))
{:ok, true} = has_exit_queue(@test_erc20_vault_id, token_addr)

Expand Down
1 change: 1 addition & 0 deletions apps/omg_eth/test/support/dev_geth.ex
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ defmodule OMG.Eth.DevGeth do
defp stop(pid) do
# NOTE: monitor is required to stop_and_wait, don't know why? `monitor: true` on run doesn't work
_ = Process.monitor(pid)

{:exit_status, 35_072} = Exexec.stop_and_wait(pid)
:ok
end
Expand Down
53 changes: 24 additions & 29 deletions apps/omg_eth/test/support/dev_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -44,11 +44,11 @@ defmodule Support.DevHelper do
- :faucet - the address to send the test ETH from, assumed to be unlocked and have the necessary funds
- :initial_funds_wei - the amount of test ETH that will be granted to every generated user
"""
def import_unlock_fund(%{priv: account_priv}, opts \\ []) do
{:ok, account_enc} = create_account_from_secret(Configuration.eth_node(), account_priv, @passphrase)
def import_unlock_fund(account, opts \\ []) do
{:ok, account_enc} = create_account_from_secret(account, @passphrase)
{:ok, _} = fund_address_from_faucet(account_enc, opts)

{:ok, from_hex(account_enc)}
{:ok, account_enc}
end

@doc """
Expand All @@ -63,9 +63,17 @@ defmodule Support.DevHelper do
txhash
|> WaitFor.eth_receipt(timeout)
|> case do
{:ok, %{"status" => "0x1"} = receipt} -> {:ok, receipt |> Map.update!("blockNumber", &int_from_hex(&1))}
{:ok, %{"status" => "0x0"} = receipt} -> {:error, receipt |> Map.put("reason", get_reason(txhash))}
other -> other
{:ok, %{"status" => "0x1"} = receipt} ->
{:ok, Map.update!(receipt, "blockNumber", &int_from_hex(&1))}

{:ok, %{"status" => "0x0"} = receipt} ->
case get_reason(txhash) do
"Exit queue exists" -> {:ok, Map.update!(receipt, "blockNumber", &int_from_hex(&1))}
reason -> {:error, Map.put(receipt, "reason", reason)}
end

other ->
other
end
end

Expand Down Expand Up @@ -102,19 +110,17 @@ defmodule Support.DevHelper do
WaitFor.ok(f, timeout)
end

def create_account_from_secret(:ganache, secret, passphrase),
do: do_create_account_from_secret("personal_importRawKey", Eth.Encoding.to_hex(secret), passphrase)

def create_account_from_secret(:geth, secret, passphrase),
do: do_create_account_from_secret("personal_importRawKey", Base.encode16(secret), passphrase)
def create_account_from_secret(account, passphrase) do
method_name = "personal_importRawKey"
secret = Base.encode16(account.priv)

def create_account_from_secret(:parity, secret, passphrase) when byte_size(secret) == 64,
do: do_create_account_from_secret("parity_newAccountFromSecret", Eth.Encoding.to_hex(secret), passphrase)
case Ethereumex.HttpClient.request(method_name, [secret, passphrase], []) do
{:ok, response} ->
{:ok, response}

# private

defp do_create_account_from_secret(method_name, secret, passphrase) do
{:ok, _} = Ethereumex.HttpClient.request(method_name, [secret, passphrase], [])
{:error, %{"code" => -32_000, "message" => "account already exists"}} ->
{:ok, "0x" <> Base.encode16(account.addr)}
end
end

defp fund_address_from_faucet(account_enc, opts) do
Expand All @@ -139,18 +145,7 @@ defmodule Support.DevHelper do
end

defp unlock_if_possible(account_enc) do
unlock_if_possible(account_enc, Configuration.eth_node())
end

# ganache works the same as geth in this aspect
defp unlock_if_possible(account_enc, :ganache), do: unlock_if_possible(account_enc, :geth)

defp unlock_if_possible(account_enc, :geth) do
{:ok, true} = Ethereumex.HttpClient.request("personal_unlockAccount", [account_enc, @passphrase, 0], [])
end

defp unlock_if_possible(_account_enc, :parity) do
:dont_bother_will_use_personal_sendTransaction
Ethereumex.HttpClient.request("personal_unlockAccount", [account_enc, @passphrase, 0], [])
end

# gets the `revert` reason for a failed transaction by txhash
Expand Down
2 changes: 1 addition & 1 deletion apps/omg_eth/test/support/root_chain_helper.ex
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ defmodule Support.RootChainHelper do
|> Keyword.put(:value, @standard_exit_bond)

contract = from_hex(Configuration.contracts().payment_exit_game)
backend = Application.fetch_env!(:omg_eth, :eth_node)
backend = :geth

TransactionHelper.contract_transact(
backend,
Expand Down
Loading