Skip to content

Commit

Permalink
migrate some tests to new reorg interface
Browse files Browse the repository at this point in the history
  • Loading branch information
ayrat555 committed Jun 24, 2020
1 parent 1ef4b25 commit 2870dac
Show file tree
Hide file tree
Showing 5 changed files with 672 additions and 611 deletions.
20 changes: 17 additions & 3 deletions priv/cabbage/apps/itest/lib/reorg.ex
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,34 @@ defmodule Itest.Reorg do
@pause_seconds 100

def finish_reorg() do
if Application.get_env(:cabbage, :reorg) do
if run_reorg?() do
unpause_container!(@node1)
unpause_container!(@node2)

Process.sleep(60 * 1000)
Process.sleep(30 * 1000)
end
end

def start_reorg() do
if Application.get_env(:cabbage, :reorg) do
if run_reorg?() do
GenServer.cast(__MODULE__, :reorg_step1)
end
end

def trigger_reorg(func) do
start_reorg()

response = func()

finish_reorg()

response
end

defp run_reorg?() do
Application.get_env(:cabbage, :reorg)
end

def start_link() do
print_available_containers()

Expand Down
35 changes: 17 additions & 18 deletions priv/cabbage/apps/itest/test/itest/configuration_api_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,6 @@ defmodule ConfigurationRetrievalTests do
require Logger

setup_all do
Reorg.finish_reorg()
Reorg.start_reorg()

data = ABI.encode("getVersion()", [])

{:ok, response} =
Expand All @@ -47,26 +44,28 @@ defmodule ConfigurationRetrievalTests do
end

defwhen ~r/^Operator deploys "(?<service>[^"]+)"$/, %{service: service}, state do
{:ok, response} =
case service do
"Child Chain" ->
ChildChainAPI.Api.Configuration.configuration_get(ChildChainAPI.Connection.new())
Reorg.start_reorg(fn ->
{:ok, response} =
case service do
"Child Chain" ->
ChildChainAPI.Api.Configuration.configuration_get(ChildChainAPI.Connection.new())

"Watcher" ->
WatcherSecurityCriticalAPI.Api.Configuration.configuration_get(WatcherSecurityCriticalAPI.Connection.new())
"Watcher" ->
WatcherSecurityCriticalAPI.Api.Configuration.configuration_get(WatcherSecurityCriticalAPI.Connection.new())

"Watcher Info" ->
WatcherInfoAPI.Api.Configuration.configuration_get(WatcherInfoAPI.Connection.new())
end
"Watcher Info" ->
WatcherInfoAPI.Api.Configuration.configuration_get(WatcherInfoAPI.Connection.new())
end

body = Jason.decode!(response.body)
body = Jason.decode!(response.body)

new_state =
state
|> Map.put(:service_response, body)
|> Map.put(:service, service)
new_state =
state
|> Map.put(:service_response, body)
|> Map.put(:service, service)

{:ok, new_state}
{:ok, new_state}
end)
end

defthen ~r/^Operator can read its configurational values$/, _, %{service: service} = state do
Expand Down
56 changes: 28 additions & 28 deletions priv/cabbage/apps/itest/test/itest/deposits_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -23,36 +23,34 @@ defmodule DepositsTests do
alias Itest.Transactions.Currency

setup do
Reorg.finish_reorg()

[{alice_account, alice_pkey}, {bob_account, _bob_pkey}] = Account.take_accounts(2)

Reorg.start_reorg()

%{alice_account: alice_account, alice_pkey: alice_pkey, bob_account: bob_account, gas: 0}
end

defwhen ~r/^Alice deposits "(?<amount>[^"]+)" ETH to the root chain$/,
%{amount: amount},
%{alice_account: alice_account} = state do
initial_balance = Itest.Poller.root_chain_get_balance(alice_account)
Reorg.trigger_reorg(fn ->
initial_balance = Itest.Poller.root_chain_get_balance(alice_account)

{:ok, receipt_hash} =
amount
|> Currency.to_wei()
|> Client.deposit(alice_account, Itest.PlasmaFramework.vault(Currency.ether()))
{:ok, receipt_hash} =
amount
|> Currency.to_wei()
|> Client.deposit(alice_account, Itest.PlasmaFramework.vault(Currency.ether()))

gas_used = Client.get_gas_used(receipt_hash)
gas_used = Client.get_gas_used(receipt_hash)

{_, new_state} =
Map.get_and_update!(state, :gas, fn current_gas ->
{current_gas, current_gas + gas_used}
end)
{_, new_state} =
Map.get_and_update!(state, :gas, fn current_gas ->
{current_gas, current_gas + gas_used}
end)

balance_after_deposit = Itest.Poller.root_chain_get_balance(alice_account)
balance_after_deposit = Itest.Poller.root_chain_get_balance(alice_account)

state = Map.put_new(new_state, :alice_ethereum_balance, balance_after_deposit)
{:ok, Map.put_new(state, :alice_initial_balance, initial_balance)}
state = Map.put_new(new_state, :alice_ethereum_balance, balance_after_deposit)
{:ok, Map.put_new(state, :alice_initial_balance, initial_balance)}
end)
end

defthen ~r/^Alice should have "(?<amount>[^"]+)" ETH on the child chain$/,
Expand Down Expand Up @@ -87,17 +85,19 @@ defmodule DepositsTests do
defwhen ~r/^Alice sends Bob "(?<amount>[^"]+)" ETH on the child chain$/,
%{amount: amount},
%{alice_account: alice_account, alice_pkey: alice_pkey, bob_account: bob_account} = state do
{:ok, [sign_hash, typed_data, _txbytes]} =
Client.create_transaction(
Currency.to_wei(amount),
alice_account,
bob_account
)

# Alice needs to sign 2 inputs of 1 Eth, 1 for Bob and 1 for the fees
_ = Client.submit_transaction(typed_data, sign_hash, [alice_pkey, alice_pkey])

{:ok, state}
Reorg.trigger_reorg(fn ->
{:ok, [sign_hash, typed_data, _txbytes]} =
Client.create_transaction(
Currency.to_wei(amount),
alice_account,
bob_account
)

# Alice needs to sign 2 inputs of 1 Eth, 1 for Bob and 1 for the fees
_ = Client.submit_transaction(typed_data, sign_hash, [alice_pkey, alice_pkey])

{:ok, state}
end)
end

defthen ~r/^Bob should have "(?<amount>[^"]+)" ETH on the child chain$/,
Expand Down
50 changes: 25 additions & 25 deletions priv/cabbage/apps/itest/test/itest/fee_claiming_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,8 @@ defmodule FeeClaimingTests do
@expected_fee_rule %{"amount" => 1, "currency" => "0x0000000000000000000000000000000000000000"}

setup do
Reorg.finish_reorg()

[{alice_address, alice_pkey}, {bob_address, bob_pkey}] = Account.take_accounts(2)

Reorg.start_reorg()

initial_balance =
@fee_claimer_address
|> Client.get_balance()
Expand All @@ -57,39 +53,43 @@ defmodule FeeClaimingTests do
defwhen ~r/^"(?<entity>[^"]+)" deposits "(?<amount>[^"]+)" ETH to the root chain$/,
%{entity: entity, amount: amount},
state do
entity_address = state[entity].address
Reorg.trigger_reorg(fn ->
entity_address = state[entity].address

{:ok, receipt_hash} =
amount
|> Currency.to_wei()
|> Client.deposit(entity_address, Itest.PlasmaFramework.vault(Currency.ether()))
{:ok, receipt_hash} =
amount
|> Currency.to_wei()
|> Client.deposit(entity_address, Itest.PlasmaFramework.vault(Currency.ether()))

gas_used = Client.get_gas_used(receipt_hash)
gas_used = Client.get_gas_used(receipt_hash)

{_, new_state} =
Map.get_and_update!(state, :gas, fn current_gas ->
{current_gas, current_gas + gas_used}
end)
{_, new_state} =
Map.get_and_update!(state, :gas, fn current_gas ->
{current_gas, current_gas + gas_used}
end)

{:ok, new_state}
{:ok, new_state}
end)
end

defwhen ~r/^"(?<sender>[^"]+)" sends "(?<receiver>[^"]+)" "(?<amount>[^"]+)" ETH on the child chain$/,
%{sender: sender, receiver: receiver, amount: amount},
state do
sender = state[sender]
receiver = state[receiver]
Reorg.trigger_reorg(fn ->
sender = state[sender]
receiver = state[receiver]

{:ok, [sign_hash, typed_data, _txbytes]} =
Client.create_transaction(
Currency.to_wei(amount),
sender.address,
receiver.address
)
{:ok, [sign_hash, typed_data, _txbytes]} =
Client.create_transaction(
Currency.to_wei(amount),
sender.address,
receiver.address
)

_ = Client.submit_transaction(typed_data, sign_hash, [sender.pkey])
_ = Client.submit_transaction(typed_data, sign_hash, [sender.pkey])

{:ok, state}
{:ok, state}
end)
end

defthen ~r/^"(?<entity>[^"]+)" should have "(?<amount>[^"]+)" ETH on the child chain$/,
Expand Down
Loading

0 comments on commit 2870dac

Please sign in to comment.