From a4d023fc292bbd1ea453a00fdc372af8b4baafe5 Mon Sep 17 00:00:00 2001 From: okalouti Date: Fri, 7 Aug 2020 16:48:25 +0700 Subject: [PATCH] refactor: endpoint to return boolean result --- .../lib/omg_watcher/block_validator.ex | 6 +++--- .../test/omg_watcher/block_validator_test.exs | 4 ++-- .../omg_watcher_rpc/lib/web/controllers/block.ex | 11 ++++++++--- .../web/controllers/block_test.exs | 16 +++++----------- 4 files changed, 18 insertions(+), 19 deletions(-) diff --git a/apps/omg_watcher/lib/omg_watcher/block_validator.ex b/apps/omg_watcher/lib/omg_watcher/block_validator.ex index 6819127801..8aae938ef1 100644 --- a/apps/omg_watcher/lib/omg_watcher/block_validator.ex +++ b/apps/omg_watcher/lib/omg_watcher/block_validator.ex @@ -26,11 +26,11 @@ defmodule OMG.Watcher.BlockValidator do - Verifies that transactions are correctly formed. - Verifies that given Merkle root matches reconstructed Merkle root. """ - @spec stateless_validate(Block.t()) :: {:ok, Block.t()} | {:error, atom()} + @spec stateless_validate(Block.t()) :: {:ok, boolean()} | {:error, atom()} def stateless_validate(submitted_block) do with {:ok, recovered_transactions} <- verify_transactions(submitted_block.transactions), - {:ok, block} <- verify_merkle_root(submitted_block, recovered_transactions) do - {:ok, block} + {:ok, _block} <- verify_merkle_root(submitted_block, recovered_transactions) do + {:ok, true} end end diff --git a/apps/omg_watcher/test/omg_watcher/block_validator_test.exs b/apps/omg_watcher/test/omg_watcher/block_validator_test.exs index afab2df742..baa367f656 100644 --- a/apps/omg_watcher/test/omg_watcher/block_validator_test.exs +++ b/apps/omg_watcher/test/omg_watcher/block_validator_test.exs @@ -83,7 +83,7 @@ defmodule OMG.WatcherRPC.Web.Validator.BlockValidatorTest do transactions: [signed_txbytes_1, signed_txbytes_2] } - assert {:ok, block} == BlockValidator.stateless_validate(block) + assert {:ok, true} == BlockValidator.stateless_validate(block) end test "returns error for non-matching Merkle root hash" do @@ -118,7 +118,7 @@ defmodule OMG.WatcherRPC.Web.Validator.BlockValidatorTest do transactions: signed_txbytes } - assert {:ok, block} = BlockValidator.stateless_validate(block) + assert {:ok, true} = BlockValidator.stateless_validate(block) end end end diff --git a/apps/omg_watcher_rpc/lib/web/controllers/block.ex b/apps/omg_watcher_rpc/lib/web/controllers/block.ex index 675b6cde77..1185ab6d1a 100644 --- a/apps/omg_watcher_rpc/lib/web/controllers/block.ex +++ b/apps/omg_watcher_rpc/lib/web/controllers/block.ex @@ -49,9 +49,14 @@ defmodule OMG.WatcherRPC.Web.Controller.Block do Executes stateful and stateless validation of a block. """ def validate_block(conn, params) do - with {:ok, block} <- Validator.BlockConstraints.parse_to_validate(params), - {:ok, _block} <- Watcher.BlockValidator.stateless_validate(block) do - api_response(block, conn, :validate_block) + with {:ok, block} <- Validator.BlockConstraints.parse_to_validate(params) do + case Watcher.BlockValidator.stateless_validate(block) do + {:ok, true} -> + api_response(%{valid: true}, conn, :validate_block) + + {:error, reason} -> + api_response(%{valid: false, error: reason}, conn, :validate_block) + end end end end diff --git a/apps/omg_watcher_rpc/test/omg_watcher_rpc/web/controllers/block_test.exs b/apps/omg_watcher_rpc/test/omg_watcher_rpc/web/controllers/block_test.exs index 69a5efa6c6..140b49c001 100644 --- a/apps/omg_watcher_rpc/test/omg_watcher_rpc/web/controllers/block_test.exs +++ b/apps/omg_watcher_rpc/test/omg_watcher_rpc/web/controllers/block_test.exs @@ -197,9 +197,8 @@ defmodule OMG.WatcherRPC.Web.Controller.BlockTest do %{"data" => data} = WatcherHelper.rpc_call("block.validate", params, 200) assert data == %{ - "code" => "block.validate:invalid_merkle_root", - "description" => "Block hash does not match reconstructed Merkle root.", - "object" => "error" + "valid" => false, + "error" => "invalid_merkle_root" } end @@ -245,9 +244,8 @@ defmodule OMG.WatcherRPC.Web.Controller.BlockTest do %{"data" => data} = WatcherHelper.rpc_call("block.validate", params, 200) assert data == %{ - "code" => "validate_block:duplicate_inputs", - "description" => nil, - "object" => "error" + "error" => "duplicate_inputs", + "valid" => false } end @@ -278,11 +276,7 @@ defmodule OMG.WatcherRPC.Web.Controller.BlockTest do %{"data" => data} = WatcherHelper.rpc_call("block.validate", params, 200) - assert data == %{ - "hash" => params.hash, - "number" => params.number, - "transactions" => params.transactions - } + assert data == %{"valid" => true} end end end