From ce3616e4757a1097fdad67969d800295466a5a34 Mon Sep 17 00:00:00 2001 From: Milton Scuderi Date: Fri, 20 May 2022 12:36:13 -0300 Subject: [PATCH 1/7] Test scripts for ethclient.ex --- eth_client/test/eth_client_test.exs | 44 +++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) diff --git a/eth_client/test/eth_client_test.exs b/eth_client/test/eth_client_test.exs index 743cf8b..688ddb4 100644 --- a/eth_client/test/eth_client_test.exs +++ b/eth_client/test/eth_client_test.exs @@ -1,4 +1,48 @@ defmodule EthClientTest do use ExUnit.Case doctest EthClient + alias EthClient.Context + alias EthClient.Account + + @bin "../contracts/src/bin/Storage.bin" + @abi "../contracts/src/bin/Storage.abi" + + describe "Ethereum war tooling functions" do + test "Deploy" do + bin = @bin + abi = @abi + + contract = EthClient.deploy(bin, abi) + assert contract == EthClient.Context.contract() + end + + test "Invoke" do + bin = @bin + abi = @abi + + contract = EthClient.deploy(bin, abi) + {:ok, tx_ans} = EthClient.invoke("store(uint256)", [3], 0) + assert tx_ans != nil + end + + test "Call" do + bin = @bin + abi = @abi + + contract = EthClient.deploy(bin, abi) + {:ok, res} = EthClient.call("retrieve()", []) + assert res == "0x0000000000000000000000000000000000000000000000000000000000000000" + {:ok, res} = EthClient.call("test_function()", []) + assert res == "0x0000000000000000000000000000000000000000000000000000000000000001" + end + + test "Balance" do + bin = @bin + abi = @abi + + contract = EthClient.deploy(bin, abi) + + assert 0.0 == EthClient.get_balance(contract.address) + end + end end From 4148eadaee22b1af3a2b45ab1aa97a7f53ac0f9c Mon Sep 17 00:00:00 2001 From: Milton Scuderi Date: Fri, 20 May 2022 12:48:50 -0300 Subject: [PATCH 2/7] Fix credo warnings --- eth_client/lib/eth_client.ex | 9 --------- eth_client/test/eth_client_test.exs | 2 +- 2 files changed, 1 insertion(+), 10 deletions(-) diff --git a/eth_client/lib/eth_client.ex b/eth_client/lib/eth_client.ex index 9f6c6ea..cc962c3 100644 --- a/eth_client/lib/eth_client.ex +++ b/eth_client/lib/eth_client.ex @@ -13,15 +13,6 @@ defmodule EthClient do 4 => "rinkeby." } - # TODO: - # Modify the code so that the only thing we do in Rust is the EC signature and Keccak hashing - # View the state of a contract (all its variables, etc). This will require parsing the ABI - # Add the ability to check if a transaction is a contract deployment or not - # Check balance - # Fix gas limit - # Change shell text based on context - # Get list of nodes - def deploy(bin_path) do {:ok, data} = File.read(bin_path) data = add_0x(data) diff --git a/eth_client/test/eth_client_test.exs b/eth_client/test/eth_client_test.exs index 688ddb4..cad65ab 100644 --- a/eth_client/test/eth_client_test.exs +++ b/eth_client/test/eth_client_test.exs @@ -1,8 +1,8 @@ defmodule EthClientTest do use ExUnit.Case doctest EthClient - alias EthClient.Context alias EthClient.Account + alias EthClient.Context @bin "../contracts/src/bin/Storage.bin" @abi "../contracts/src/bin/Storage.abi" From 386a90f3cf90903497c0de458612e714b2987e97 Mon Sep 17 00:00:00 2001 From: Milton Scuderi Date: Fri, 20 May 2022 18:26:12 -0300 Subject: [PATCH 3/7] Add further tests --- eth_client/test/eth_client_test.exs | 59 ++++++++++++++++++++--------- 1 file changed, 41 insertions(+), 18 deletions(-) diff --git a/eth_client/test/eth_client_test.exs b/eth_client/test/eth_client_test.exs index cad65ab..0d1fecc 100644 --- a/eth_client/test/eth_client_test.exs +++ b/eth_client/test/eth_client_test.exs @@ -7,42 +7,65 @@ defmodule EthClientTest do @bin "../contracts/src/bin/Storage.bin" @abi "../contracts/src/bin/Storage.abi" - describe "Ethereum war tooling functions" do - test "Deploy" do - bin = @bin - abi = @abi + setup %{bin: bin, abi: abi} do + contract = EthClient.deploy(bin, abi) + {:ok, contract: contract} + end - contract = EthClient.deploy(bin, abi) + describe "deploy/2" do + @tag bin: @bin, abi: @abi + test "[SUCCESS] Succesful deploy", %{contract: contract} do assert contract == EthClient.Context.contract() end + end - test "Invoke" do - bin = @bin - abi = @abi - - contract = EthClient.deploy(bin, abi) + describe "invoke/3" do + @tag bin: @bin, abi: @abi + test "[SUCCESS] invokes a function it returns the transaction hash" do {:ok, tx_ans} = EthClient.invoke("store(uint256)", [3], 0) assert tx_ans != nil end - test "Call" do - bin = @bin - abi = @abi + @tag bin: @bin, abi: @abi + test "[FAILURE] invokes a function with incorrect amount of params" do + assert_raise UndefinedFunctionError, fn -> EthClient.invoke("store(uint256)", []) end + end + + @tag bin: @bin, abi: @abi + test "[FAILURE] invokes a contract function with incorrect params" do + assert_raise FunctionClauseError, fn -> EthClient.invoke("store(uint256)", [], 0) end + end + end - contract = EthClient.deploy(bin, abi) + describe "call/2" do + @tag bin: @bin, abi: @abi + test "[SUCCESS] Call" do {:ok, res} = EthClient.call("retrieve()", []) assert res == "0x0000000000000000000000000000000000000000000000000000000000000000" {:ok, res} = EthClient.call("test_function()", []) assert res == "0x0000000000000000000000000000000000000000000000000000000000000001" end - test "Balance" do - bin = @bin - abi = @abi + @tag bin: @bin, abi: @abi + test "[FAILURE] calls a function with incorrect amount of params" do + assert_raise UndefinedFunctionError, fn -> EthClient.call("retrieve()") end + end - contract = EthClient.deploy(bin, abi) + @tag bin: @bin, abi: @abi + test "[FAILURE] calls a contract function with incorrect params" do + assert_raise FunctionClauseError, fn -> EthClient.call("retrieve()", [12]) end + end + end + describe "balance/1" do + @tag bin: @bin, abi: @abi + test "[SUCCESS] Balance", %{contract: contract} do assert 0.0 == EthClient.get_balance(contract.address) end + + @tag bin: @bin, abi: @abi + test "[FAILURE] unexisting address", %{contract: contract} do + assert_raise FunctionClauseError, fn -> EthClient.get_balance("0x123213b") end + end end end From 3c11c461603ff6922c29f736b0a6f0c582e78ade Mon Sep 17 00:00:00 2001 From: jrigada Date: Mon, 23 May 2022 12:33:27 -0300 Subject: [PATCH 4/7] change type of exception in test --- eth_client/test/eth_client_test.exs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/eth_client/test/eth_client_test.exs b/eth_client/test/eth_client_test.exs index 0d1fecc..8743ef8 100644 --- a/eth_client/test/eth_client_test.exs +++ b/eth_client/test/eth_client_test.exs @@ -28,7 +28,7 @@ defmodule EthClientTest do @tag bin: @bin, abi: @abi test "[FAILURE] invokes a function with incorrect amount of params" do - assert_raise UndefinedFunctionError, fn -> EthClient.invoke("store(uint256)", []) end + assert_raise FunctionClauseError, fn -> EthClient.invoke("store(uint256)", []) end end @tag bin: @bin, abi: @abi From 5d9e2ebb35623561802cfc2bdeaa666850af780e Mon Sep 17 00:00:00 2001 From: jrigada Date: Tue, 24 May 2022 13:58:53 -0300 Subject: [PATCH 5/7] setup all to deploy only once --- eth_client/lib/eth_client.ex | 2 +- eth_client/test/eth_client_test.exs | 10 ++++------ eth_client/test/test_helper.exs | 1 + 3 files changed, 6 insertions(+), 7 deletions(-) diff --git a/eth_client/lib/eth_client.ex b/eth_client/lib/eth_client.ex index 03611c2..2332860 100644 --- a/eth_client/lib/eth_client.ex +++ b/eth_client/lib/eth_client.ex @@ -19,7 +19,7 @@ defmodule EthClient do @local_host_chain_id 1234 @local_host_rpc "http://localhost:8545" - + def deploy(bin_path) do {:ok, data} = File.read(bin_path) data = add_0x(data) diff --git a/eth_client/test/eth_client_test.exs b/eth_client/test/eth_client_test.exs index 8743ef8..45c9c6a 100644 --- a/eth_client/test/eth_client_test.exs +++ b/eth_client/test/eth_client_test.exs @@ -1,14 +1,12 @@ defmodule EthClientTest do use ExUnit.Case doctest EthClient - alias EthClient.Account - alias EthClient.Context @bin "../contracts/src/bin/Storage.bin" @abi "../contracts/src/bin/Storage.abi" - setup %{bin: bin, abi: abi} do - contract = EthClient.deploy(bin, abi) + setup_all do + contract = EthClient.deploy(@bin, @abi) {:ok, contract: contract} end @@ -41,7 +39,7 @@ defmodule EthClientTest do @tag bin: @bin, abi: @abi test "[SUCCESS] Call" do {:ok, res} = EthClient.call("retrieve()", []) - assert res == "0x0000000000000000000000000000000000000000000000000000000000000000" + assert res == "0x0000000000000000000000000000000000000000000000000000000000000003" {:ok, res} = EthClient.call("test_function()", []) assert res == "0x0000000000000000000000000000000000000000000000000000000000000001" end @@ -64,7 +62,7 @@ defmodule EthClientTest do end @tag bin: @bin, abi: @abi - test "[FAILURE] unexisting address", %{contract: contract} do + test "[FAILURE] unexisting address", %{contract: _contract} do assert_raise FunctionClauseError, fn -> EthClient.get_balance("0x123213b") end end end diff --git a/eth_client/test/test_helper.exs b/eth_client/test/test_helper.exs index 869559e..c066466 100644 --- a/eth_client/test/test_helper.exs +++ b/eth_client/test/test_helper.exs @@ -1 +1,2 @@ +ExUnit.configure(seed: 0) ExUnit.start() From a1058c5b462f99fb1e9b9006ddc3e942e575d279 Mon Sep 17 00:00:00 2001 From: jrigada Date: Mon, 30 May 2022 10:07:49 -0300 Subject: [PATCH 6/7] remove tags --- .envrc | 4 ++-- eth_client/test/eth_client_test.exs | 10 +--------- 2 files changed, 3 insertions(+), 11 deletions(-) diff --git a/.envrc b/.envrc index 66ba2b6..b3b58d4 100644 --- a/.envrc +++ b/.envrc @@ -1,6 +1,6 @@ -export ETH_RPC_HOST=http://localhost:8545 +export ETH_RPC_HOST="https://rinkeby.infura.io/v3/8db75325ccd14936a965997dd1469177" export ETH_USER_ADDRESS=0xafb72ccaeb7e22c8a7640f605824b0898424b3da export ETH_USER_PK=e90d75baafee04b3d9941bd8d76abe799b391aec596515dee11a9bd55f05709c export ETH_API_KEY="" export ETH_CONTRACT="" -export ETH_CHAIN_ID="1234" +export ETH_CHAIN_ID="4" diff --git a/eth_client/test/eth_client_test.exs b/eth_client/test/eth_client_test.exs index 45c9c6a..2d6835a 100644 --- a/eth_client/test/eth_client_test.exs +++ b/eth_client/test/eth_client_test.exs @@ -6,37 +6,33 @@ defmodule EthClientTest do @abi "../contracts/src/bin/Storage.abi" setup_all do + IO.inspect(EthClient.Context.all()) contract = EthClient.deploy(@bin, @abi) {:ok, contract: contract} end describe "deploy/2" do - @tag bin: @bin, abi: @abi test "[SUCCESS] Succesful deploy", %{contract: contract} do assert contract == EthClient.Context.contract() end end describe "invoke/3" do - @tag bin: @bin, abi: @abi test "[SUCCESS] invokes a function it returns the transaction hash" do {:ok, tx_ans} = EthClient.invoke("store(uint256)", [3], 0) assert tx_ans != nil end - @tag bin: @bin, abi: @abi test "[FAILURE] invokes a function with incorrect amount of params" do assert_raise FunctionClauseError, fn -> EthClient.invoke("store(uint256)", []) end end - @tag bin: @bin, abi: @abi test "[FAILURE] invokes a contract function with incorrect params" do assert_raise FunctionClauseError, fn -> EthClient.invoke("store(uint256)", [], 0) end end end describe "call/2" do - @tag bin: @bin, abi: @abi test "[SUCCESS] Call" do {:ok, res} = EthClient.call("retrieve()", []) assert res == "0x0000000000000000000000000000000000000000000000000000000000000003" @@ -44,24 +40,20 @@ defmodule EthClientTest do assert res == "0x0000000000000000000000000000000000000000000000000000000000000001" end - @tag bin: @bin, abi: @abi test "[FAILURE] calls a function with incorrect amount of params" do assert_raise UndefinedFunctionError, fn -> EthClient.call("retrieve()") end end - @tag bin: @bin, abi: @abi test "[FAILURE] calls a contract function with incorrect params" do assert_raise FunctionClauseError, fn -> EthClient.call("retrieve()", [12]) end end end describe "balance/1" do - @tag bin: @bin, abi: @abi test "[SUCCESS] Balance", %{contract: contract} do assert 0.0 == EthClient.get_balance(contract.address) end - @tag bin: @bin, abi: @abi test "[FAILURE] unexisting address", %{contract: _contract} do assert_raise FunctionClauseError, fn -> EthClient.get_balance("0x123213b") end end From c527fbc2846acda9a891d94d33f19f1665261e39 Mon Sep 17 00:00:00 2001 From: jrigada Date: Mon, 30 May 2022 10:12:03 -0300 Subject: [PATCH 7/7] remove IO.inspect --- eth_client/test/eth_client_test.exs | 1 - 1 file changed, 1 deletion(-) diff --git a/eth_client/test/eth_client_test.exs b/eth_client/test/eth_client_test.exs index 2d6835a..42bbe77 100644 --- a/eth_client/test/eth_client_test.exs +++ b/eth_client/test/eth_client_test.exs @@ -6,7 +6,6 @@ defmodule EthClientTest do @abi "../contracts/src/bin/Storage.abi" setup_all do - IO.inspect(EthClient.Context.all()) contract = EthClient.deploy(@bin, @abi) {:ok, contract: contract} end