Skip to content

Commit

Permalink
Almost all tests fixed with major test speed optimizations
Browse files Browse the repository at this point in the history
  • Loading branch information
izelnakri committed Mar 4, 2020
1 parent 6a483ff commit ffb5f20
Show file tree
Hide file tree
Showing 12 changed files with 289 additions and 133 deletions.
2 changes: 1 addition & 1 deletion .circleci/config.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ jobs:
DOCKER_TAG=$(echo eth:${CIRCLE_BRANCH} | tr '/' '_')
docker pull ${HUB_USERNAME}/${DOCKER_TAG}
docker run -t -d --name="eth" ${HUB_USERNAME}/${DOCKER_TAG} /bin/sh
- run: docker exec -it eth mix test test/eth/query_test.exs --seed 0
- run: docker exec -it eth mix test test --seed 0 --trace

workflows:
version: 2
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ RUN apt-get update && apt-get -y install curl make build-essential && curl https

ENV PATH=$PATH:/root/.volta/bin

RUN volta install node@8.9
RUN volta install node@8.17

ADD ["package.json", "package-lock.json"]

Expand Down
15 changes: 10 additions & 5 deletions lib/eth/transaction.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,8 @@ defmodule ETH.Transaction do
|> send_transaction(sender_wallet.private_key)
end

def send_transaction(sender_wallet, receiver_wallet, value, private_key) when is_number(value) do
def send_transaction(sender_wallet, receiver_wallet, value, private_key)
when is_number(value) do
%{from: sender_wallet.eth_address, to: receiver_wallet.eth_address, value: value}
|> send_transaction(private_key)
end
Expand All @@ -59,7 +60,8 @@ defmodule ETH.Transaction do
|> send_transaction(private_key)
end

def send_transaction(sender_wallet, receiver_wallet, params, private_key) when is_list(params) do
def send_transaction(sender_wallet, receiver_wallet, params, private_key)
when is_list(params) do
params
|> Keyword.merge(from: sender_wallet.eth_address, to: receiver_wallet.eth_address)
|> send_transaction(private_key)
Expand Down Expand Up @@ -116,15 +118,17 @@ defmodule ETH.Transaction do
tx_hash
end

def send_transaction!(sender_wallet, receiver_wallet, value, private_key) when is_number(value) do
def send_transaction!(sender_wallet, receiver_wallet, value, private_key)
when is_number(value) do
{:ok, tx_hash} =
%{from: sender_wallet.eth_address, to: receiver_wallet.eth_address, value: value}
|> send_transaction(private_key)

tx_hash
end

def send_transaction!(sender_wallet, receiver_wallet, params, private_key) when is_map(params) do
def send_transaction!(sender_wallet, receiver_wallet, params, private_key)
when is_map(params) do
{:ok, tx_hash} =
params
|> Map.merge(%{from: sender_wallet.eth_address, to: receiver_wallet.eth_address})
Expand All @@ -133,7 +137,8 @@ defmodule ETH.Transaction do
tx_hash
end

def send_transaction!(sender_wallet, receiver_wallet, params, private_key) when is_list(params) do
def send_transaction!(sender_wallet, receiver_wallet, params, private_key)
when is_list(params) do
{:ok, tx_hash} =
params
|> Keyword.merge(from: sender_wallet.eth_address, to: receiver_wallet.eth_address)
Expand Down
34 changes: 30 additions & 4 deletions lib/eth/transaction/builder.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,12 @@ defmodule ETH.Transaction.Builder do
value = Keyword.get(params, :value, 0)
gas_price = Keyword.get(params, :gas_price, ETH.gas_price!())
data = Keyword.get(params, :data, "")

target_data =
if data !== "" && !String.starts_with?(data, "0x"),
do: "0x" <> Hexate.encode(data),
else: data

nonce = Keyword.get(params, :nonce, generate_nonce(Keyword.get(params, :from)))
chain_id = Keyword.get(params, :chain_id, 3)

Expand All @@ -77,20 +83,33 @@ defmodule ETH.Transaction.Builder do
ETH.estimate_gas!(%{
to: to,
value: value,
data: data,
data: target_data,
nonce: nonce,
chain_id: chain_id
})
)

%{nonce: nonce, gas_price: gas_price, gas_limit: gas_limit, to: to, value: value, data: data}
%{
nonce: nonce,
gas_price: gas_price,
gas_limit: gas_limit,
to: to,
value: value,
data: target_data
}
end

defp build_params_from_map(params) do
to = Map.get(params, :to, "")
value = Map.get(params, :value, 0)
gas_price = Map.get(params, :gas_price, ETH.gas_price!())
data = Map.get(params, :data, "")

target_data =
if data !== "" && !String.starts_with?(data, "0x"),
do: "0x" <> Hexate.encode(data),
else: data

nonce = Map.get(params, :nonce, generate_nonce(Map.get(params, :from)))
chain_id = Map.get(params, :chain_id, 3)

Expand All @@ -101,13 +120,20 @@ defmodule ETH.Transaction.Builder do
ETH.estimate_gas!(%{
to: to,
value: value,
data: data,
data: target_data,
nonce: nonce,
chain_id: chain_id
})
)

%{nonce: nonce, gas_price: gas_price, gas_limit: gas_limit, to: to, value: value, data: data}
%{
nonce: nonce,
gas_price: gas_price,
gas_limit: gas_limit,
to: to,
value: value,
data: target_data
}
end

defp generate_nonce(nil), do: 0
Expand Down
12 changes: 7 additions & 5 deletions lib/eth/transaction_queries.ex
Original file line number Diff line number Diff line change
Expand Up @@ -19,28 +19,28 @@ defmodule ETH.TransactionQueries do

def get_block_transaction_count(block_number) when is_number(block_number) do
case HttpClient.eth_get_block_transaction_count_by_number(block_number) do
{:ok, transaction_count} -> {:ok, transaction_count}
{:ok, transaction_count} -> {:ok, convert_to_number(transaction_count)}
error -> error
end
end

def get_block_transaction_count(block_hash) do
case HttpClient.eth_get_block_transaction_count_by_hash(block_hash) do
{:ok, transaction_count} -> {:ok, transaction_count}
{:ok, transaction_count} -> {:ok, convert_to_number(transaction_count)}
error -> error
end
end

def get_block_transaction_count!(block_number) when is_number(block_number) do
{:ok, transaction_count} = HttpClient.eth_get_block_transaction_count_by_number(block_number)

transaction_count
convert_to_number(transaction_count)
end

def get_block_transaction_count!(block_hash) do
{:ok, transaction_count} = HttpClient.eth_get_block_transaction_count_by_hash(block_hash)

transaction_count
convert_to_number(transaction_count)
end

def get_transaction_from_block(block_number, index) when is_number(block_number) do
Expand Down Expand Up @@ -85,7 +85,9 @@ defmodule ETH.TransactionQueries do

def get_transaction_receipt(transaction_hash) do
case HttpClient.eth_get_transaction_receipt(transaction_hash) do
{:ok, nil} -> {:error, nil}
{:ok, nil} ->
{:error, nil}

{:ok, raw_transaction_receipt} ->
{:ok, convert_transaction_receipt(raw_transaction_receipt)}

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@
"ganache-cli": "^6.9.1"
},
"volta": {
"node": "8.9.4"
"node": "8.17.0"
}
}
7 changes: 7 additions & 0 deletions test/eth/query_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ defmodule ETH.Query.Test do
end

test "get_block/1 by number works" do
ETH.TestClient.advance_block_by(1)

target_result = ETH.get_block(2)
target_block = target_result |> elem(1)

Expand Down Expand Up @@ -169,6 +171,8 @@ defmodule ETH.Query.Test do
end

test "get_block!/1 by number works" do
ETH.TestClient.advance_block_by(1)

target_block = ETH.get_block!(2)

assert target_block.number == 2
Expand Down Expand Up @@ -199,6 +203,8 @@ defmodule ETH.Query.Test do
end

test "get_block!/1 by hash works" do
ETH.TestClient.advance_block_by(1)

first_block = ETH.get_block!(1)
second_block = ETH.get_block!(2)

Expand Down Expand Up @@ -313,6 +319,7 @@ defmodule ETH.Query.Test do

first_difference = first_gas_in_ether / second_gas_in_ether
second_difference = first_gas_in_wei / second_gas_in_wei

assert Float.floor(first_difference, 15) == Float.floor(second_difference, 15)
end
end
12 changes: 8 additions & 4 deletions test/eth/transaction/parser_test.exs
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ defmodule ETH.Transaction.Parser.Test do
}

assert Transaction.parse(
@not_signed_transaction_map |> Map.keys()
@not_signed_transaction_map
|> Map.keys()
|> Enum.reduce(%{}, fn key, acc ->
Map.put(acc, key, to_buffer(Map.get(@not_signed_transaction_map, key)))
end)
Expand Down Expand Up @@ -153,7 +154,8 @@ defmodule ETH.Transaction.Parser.Test do
}

assert Transaction.parse(
@signed_transaction_map |> Map.keys()
@signed_transaction_map
|> Map.keys()
|> Enum.reduce(%{}, fn key, acc ->
Map.put(acc, key, to_buffer(Map.get(@signed_transaction_map, key)))
end)
Expand Down Expand Up @@ -198,7 +200,8 @@ defmodule ETH.Transaction.Parser.Test do
]

assert Transaction.to_list(
@signed_transaction_map |> Map.keys()
@signed_transaction_map
|> Map.keys()
|> Enum.reduce(%{}, fn key, acc ->
Map.put(acc, key, to_buffer(Map.get(@signed_transaction_map, key)))
end)
Expand Down Expand Up @@ -229,7 +232,8 @@ defmodule ETH.Transaction.Parser.Test do
]

assert Transaction.to_list(
@not_signed_transaction_map |> Map.keys()
@not_signed_transaction_map
|> Map.keys()
|> Enum.reduce(%{}, fn key, acc ->
Map.put(acc, key, to_buffer(Map.get(@not_signed_transaction_map, key)))
end)
Expand Down
Loading

0 comments on commit ffb5f20

Please sign in to comment.