Skip to content

Commit

Permalink
Merge branch 'master' into pgebal/draft-block-forming
Browse files Browse the repository at this point in the history
  • Loading branch information
Mederic committed Oct 1, 2020
2 parents f627117 + 8336bc7 commit 16a708c
Show file tree
Hide file tree
Showing 27 changed files with 760 additions and 546 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ jobs:
_counter=$(grep -ri "child-chain" . | grep -c "child-chain")
echo "Current occurrences of child-chain:"
echo $_counter
if [ $_counter -gt 3 ]; then
if [ $_counter -gt 4 ]; then
echo "Have you been naughty or nice? Find out if Santa knows."
exit 1
fi
Expand Down
16 changes: 3 additions & 13 deletions apps/api/test/api/v1/controllers/block_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,27 +2,17 @@ defmodule API.V1.Controller.BlockControllerTest do
use Engine.DB.DataCase, async: true

alias API.V1.Controller.BlockController
alias Engine.DB.Block
alias Engine.DB.Transaction
alias ExPlasma.Encoding

describe "get_by_hash/1" do
test "it returns a matching block" do
_ = insert(:fee, hash: "22", type: :merged_fees)
%{block: block} = transaction = insert(:payment_v1_transaction, block: insert(:block))

{:ok, %{:new_transaction => %{id: id}}} =
:payment_v1_transaction
|> build()
|> Transaction.insert()

_ = Block.form()
transaction = Transaction |> Repo.get(id) |> Repo.preload(:block)

hash = Encoding.to_hex(transaction.block.hash)
hash = Encoding.to_hex(block.hash)
hex_tx_bytes = [Encoding.to_hex(transaction.tx_bytes)]

assert BlockController.get_by_hash(hash) ==
{:ok, %{blknum: transaction.block.blknum, hash: hash, transactions: hex_tx_bytes}}
{:ok, %{blknum: block.blknum, hash: hash, transactions: hex_tx_bytes}}
end

test "it returns `no_block_matching_hash` for missing blocks" do
Expand Down
67 changes: 27 additions & 40 deletions apps/api/test/api/v1/controllers/fee_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -3,47 +3,10 @@ defmodule API.V1.Controller.FeeControllerTest do

alias API.V1.Controller.FeeController

setup_all do
fee_specs = %{
1 => %{
Base.decode16!("0000000000000000000000000000000000000000") => %{
amount: 1,
subunit_to_unit: 1_000_000_000_000_000_000,
pegged_amount: 1,
pegged_currency: "USD",
pegged_subunit_to_unit: 100,
updated_at: DateTime.from_unix!(1_546_336_800)
},
Base.decode16!("0000000000000000000000000000000000000001") => %{
amount: 2,
subunit_to_unit: 1_000_000_000_000_000_000,
pegged_amount: 1,
pegged_currency: "USD",
pegged_subunit_to_unit: 100,
updated_at: DateTime.from_unix!(1_546_336_800)
}
},
2 => %{
Base.decode16!("0000000000000000000000000000000000000000") => %{
amount: 2,
subunit_to_unit: 1_000_000_000_000_000_000,
pegged_amount: 1,
pegged_currency: "USD",
pegged_subunit_to_unit: 100,
updated_at: DateTime.from_unix!(1_546_336_800)
}
}
}

params = [term: fee_specs, type: :current_fees]

_ = insert(:fee, params)

:ok
end

describe "all/1" do
test "returns fees" do
insert(:current_fee)

assert FeeController.all(%{}) ==
{:ok,
%{
Expand Down Expand Up @@ -81,7 +44,9 @@ defmodule API.V1.Controller.FeeControllerTest do
}}
end

test "filters fees" do
test "filters fees by tx_types" do
insert(:current_fee)

assert FeeController.all(%{"tx_types" => [1]}) == {
:ok,
%{
Expand All @@ -108,5 +73,27 @@ defmodule API.V1.Controller.FeeControllerTest do
}
}
end

test "filters fees by currency" do
insert(:current_fee)

assert FeeController.all(%{"currencies" => ["0x0000000000000000000000000000000000000001"]}) == {
:ok,
%{
"1" => [
%{
amount: 2,
currency: "0x0000000000000000000000000000000000000001",
pegged_amount: 1,
pegged_currency: "USD",
pegged_subunit_to_unit: 100,
subunit_to_unit: 1_000_000_000_000_000_000,
updated_at: ~U[2019-01-01 10:00:00Z]
}
],
"2" => []
}
}
end
end
end
29 changes: 23 additions & 6 deletions apps/api/test/api/v1/controllers/transaction_controller_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,42 @@ defmodule API.V1.Controllere.TransactionControllerTest do
use Engine.DB.DataCase, async: true

alias API.V1.Controller.TransactionController
alias Engine.Support.TestEntity
alias ExPlasma.Builder
alias ExPlasma.Encoding

setup do
_ = insert(:fee, hash: "55", term: :no_fees_required, type: :merged_fees)
insert(:merged_fee)

:ok
end

describe "submit/1" do
test "decodes and inserts a tx_bytes into the DB" do
txn = build(:payment_v1_transaction)
tx_hash = Encoding.to_hex(txn.tx_hash)
tx_bytes = Encoding.to_hex(txn.tx_bytes)
entity = TestEntity.alice()

assert TransactionController.submit(tx_bytes) == {:ok, %{tx_hash: tx_hash, blknum: 1_000, tx_index: 0}}
%{output_id: output_id} = insert(:deposit_output, amount: 10, output_guard: entity.addr)
%{output_data: output_data} = build(:output, output_guard: entity.addr, amount: 9)

transaction =
Builder.new(ExPlasma.payment_v1(), %{
inputs: [ExPlasma.Output.decode_id!(output_id)],
outputs: [ExPlasma.Output.decode!(output_data)]
})

tx_bytes =
transaction
|> Builder.sign!([entity.priv_encoded])
|> ExPlasma.encode!()
|> Encoding.to_hex()

{:ok, tx_hash} = ExPlasma.Transaction.hash(transaction)

assert TransactionController.submit(tx_bytes) ==
{:ok, %{tx_hash: Encoding.to_hex(tx_hash), blknum: 1_000, tx_index: 0}}
end

test "it raises an error if the tranasaction is invalid" do
test "it raises an error if the transaction is invalid" do
invalid_hex_tx_bytes =
ExPlasma.payment_v1()
|> Builder.new()
Expand Down
81 changes: 26 additions & 55 deletions apps/api/test/api/v1/router_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -4,56 +4,14 @@ defmodule API.V1.RouterTest do

alias API.V1.Router
alias Engine.DB.Block
alias Engine.DB.Fee
alias Engine.DB.Transaction
alias Engine.Repo
alias Engine.Support.TestEntity
alias ExPlasma.Builder
alias ExPlasma.Encoding

setup do
fee_specs = %{
1 => %{
Base.decode16!("0000000000000000000000000000000000000000") => %{
amount: 1,
subunit_to_unit: 1_000_000_000_000_000_000,
pegged_amount: 1,
pegged_currency: "USD",
pegged_subunit_to_unit: 10,
updated_at: DateTime.from_unix!(1_546_336_800)
},
Base.decode16!("0000000000000000000000000000000000000001") => %{
amount: 2,
subunit_to_unit: 1_000_000_000_000_000_000,
pegged_amount: 1,
pegged_currency: "USD",
pegged_subunit_to_unit: 10,
updated_at: DateTime.from_unix!(1_546_336_800)
}
},
2 => %{
Base.decode16!("0000000000000000000000000000000000000000") => %{
amount: 2,
subunit_to_unit: 1_000_000_000_000_000_000,
pegged_amount: 1,
pegged_currency: "USD",
pegged_subunit_to_unit: 10,
updated_at: DateTime.from_unix!(1_546_336_800)
}
}
}

params = [
term: fee_specs,
type: :current_fees,
hash:
:sha256
|> :crypto.hash(inspect(fee_specs))
|> Base.encode16(case: :lower),
inserted_at: DateTime.add(DateTime.utc_now(), 10_000_000, :second)
]

_ = insert(:fee, params)

_ = insert(:fee, hash: "11", type: :merged_fees)
_ = insert(:current_fee)

%{
expected_result: %{
Expand All @@ -63,7 +21,7 @@ defmodule API.V1.RouterTest do
"currency" => "0x0000000000000000000000000000000000000000",
"pegged_amount" => 1,
"pegged_currency" => "USD",
"pegged_subunit_to_unit" => 10,
"pegged_subunit_to_unit" => 100,
"subunit_to_unit" => 1_000_000_000_000_000_000,
"updated_at" => "2019-01-01T10:00:00Z"
},
Expand All @@ -72,7 +30,7 @@ defmodule API.V1.RouterTest do
"currency" => "0x0000000000000000000000000000000000000001",
"pegged_amount" => 1,
"pegged_currency" => "USD",
"pegged_subunit_to_unit" => 10,
"pegged_subunit_to_unit" => 100,
"subunit_to_unit" => 1_000_000_000_000_000_000,
"updated_at" => "2019-01-01T10:00:00Z"
}
Expand All @@ -83,7 +41,7 @@ defmodule API.V1.RouterTest do
"currency" => "0x0000000000000000000000000000000000000000",
"pegged_amount" => 1,
"pegged_currency" => "USD",
"pegged_subunit_to_unit" => 10,
"pegged_subunit_to_unit" => 100,
"subunit_to_unit" => 1_000_000_000_000_000_000,
"updated_at" => "2019-01-01T10:00:00Z"
}
Expand Down Expand Up @@ -208,15 +166,28 @@ defmodule API.V1.RouterTest do

describe "transaction.submit" do
test "decodes a transaction and inserts it" do
Repo.delete_all(Fee)
_ = insert(:fee, hash: "77", term: :no_fees_required, type: :merged_fees)
insert(:merged_fee)

entity = TestEntity.alice()
%{output_id: output_id} = insert(:deposit_output, amount: 10, output_guard: entity.addr)
%{output_data: output_data} = build(:output, output_guard: entity.addr, amount: 9)

transaction =
Builder.new(ExPlasma.payment_v1(), %{
inputs: [ExPlasma.Output.decode_id!(output_id)],
outputs: [ExPlasma.Output.decode!(output_data)]
})

tx_bytes =
transaction
|> Builder.sign!([entity.priv_encoded])
|> ExPlasma.encode!()

{:ok, tx_hash} = ExPlasma.Transaction.hash(transaction)

txn = build(:payment_v1_transaction)
tx_bytes = Encoding.to_hex(txn.tx_bytes)
tx_hash = Encoding.to_hex(txn.tx_hash)
{:ok, payload} = post("transaction.submit", %{transaction: tx_bytes})
{:ok, payload} = post("transaction.submit", %{transaction: Encoding.to_hex(tx_bytes)})

assert_payload_data(payload, %{"tx_hash" => tx_hash, "blknum" => 1_000, "tx_index" => 0})
assert_payload_data(payload, %{"tx_hash" => Encoding.to_hex(tx_hash), "blknum" => 1_000, "tx_index" => 0})
end

test "that it returns an error if missing transaction params" do
Expand Down
13 changes: 5 additions & 8 deletions apps/api/test/api/v1/views/transaction_view_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -2,20 +2,17 @@ defmodule API.V1.View.TransactionViewTest do
use Engine.DB.DataCase, async: true

alias API.V1.View.TransactionView
alias Engine.DB.Transaction
alias ExPlasma.Encoding

describe "serialize/1" do
test "serializes a transaction" do
_ = insert(:fee, hash: "55", term: :no_fees_required, type: :merged_fees)

transaction_changeset = build(:payment_v1_transaction)
{:ok, %{:new_transaction => transaction}} = Transaction.insert(transaction_changeset)
test "serialize a transaction" do
block = insert(:block)
transaction = insert(:payment_v1_transaction, %{block: block})

assert TransactionView.serialize(transaction) == %{
tx_hash: Encoding.to_hex(transaction.tx_hash),
blknum: 1_000,
tx_index: 0
blknum: block.blknum,
tx_index: transaction.tx_index
}
end
end
Expand Down
19 changes: 3 additions & 16 deletions apps/engine/lib/engine/callbacks/deposit.ex
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ defmodule Engine.Callbacks.Deposit do
alias Engine.Callback
alias Engine.DB.Output
alias Engine.Repo
alias ExPlasma.Output.Position

@doc """
Inserts deposit events, forming the associated UTXOs.
Expand All @@ -42,22 +41,10 @@ defmodule Engine.Callbacks.Deposit do
defp do_callback(multi, []), do: multi

defp build_deposit(multi, event) do
output_id = Position.new(event.data["blknum"], 0, 0)
deposit_blknum = event.data["blknum"]
changeset = Output.deposit(deposit_blknum, event.data["depositor"], event.data["token"], event.data["amount"])

output_params = %{
state: "confirmed",
output_type: ExPlasma.payment_v1(),
output_data: %{
output_guard: event.data["depositor"],
token: event.data["token"],
amount: event.data["amount"]
},
output_id: output_id
}

output = Output.changeset(%Output{}, output_params)

Ecto.Multi.insert(multi, "deposit-output-#{output_id.position}", output,
Multi.insert(multi, "deposit-#{deposit_blknum}", changeset,
on_conflict: :nothing,
conflict_target: :position
)
Expand Down
7 changes: 1 addition & 6 deletions apps/engine/lib/engine/callbacks/exit_started.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ defmodule Engine.Callbacks.ExitStarted do

use Spandex.Decorators

import Ecto.Query

alias Ecto.Multi
alias Engine.Callback
alias Engine.DB.Output
Expand All @@ -34,8 +32,5 @@ defmodule Engine.Callbacks.ExitStarted do
do_callback(multi, positions ++ [position], tail)
end

defp do_callback(multi, positions, []) do
query = where(Output.usable(), [output], output.position in ^positions)
Multi.update_all(multi, :exiting_outputs, query, set: [state: "exiting", updated_at: NaiveDateTime.utc_now()])
end
defp do_callback(multi, positions, []), do: Output.exit(multi, positions)
end
7 changes: 1 addition & 6 deletions apps/engine/lib/engine/callbacks/in_flight_exit_started.ex
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@ defmodule Engine.Callbacks.InFlightExitStarted do

use Spandex.Decorators

import Ecto.Query

alias Ecto.Multi
alias Engine.Callback
alias Engine.DB.Output
Expand All @@ -34,8 +32,5 @@ defmodule Engine.Callbacks.InFlightExitStarted do
do_callback(multi, positions ++ inputs, tail)
end

defp do_callback(multi, positions, []) do
query = where(Output.usable(), [output], output.position in ^positions)
Multi.update_all(multi, :exiting_outputs, query, set: [state: "exiting", updated_at: NaiveDateTime.utc_now()])
end
defp do_callback(multi, positions, []), do: Output.exit(multi, positions)
end
Loading

0 comments on commit 16a708c

Please sign in to comment.