diff --git a/lib/poa_agent/entity/ethereum/block.ex b/lib/poa_agent/entity/ethereum/block.ex index fbe75b3..8c96dea 100644 --- a/lib/poa_agent/entity/ethereum/block.ex +++ b/lib/poa_agent/entity/ethereum/block.ex @@ -50,6 +50,44 @@ defmodule POAAgent.Entity.Ethereum.Block do :uncles ] + @doc false + def format_block(block) when is_map(block) do + difficulty = POAAgent.Format.Literal.Hex.decimalize(block["difficulty"]) + gas_limit = String.to_integer(POAAgent.Format.Literal.Hex.decimalize(block["gasLimit"])) + gas_used = String.to_integer(POAAgent.Format.Literal.Hex.decimalize(block["gasUsed"])) + number = String.to_integer(POAAgent.Format.Literal.Hex.decimalize(block["number"])) + size = String.to_integer(POAAgent.Format.Literal.Hex.decimalize(block["size"])) + timestamp = String.to_integer(POAAgent.Format.Literal.Hex.decimalize(block["timestamp"])) + total_difficulty = POAAgent.Format.Literal.Hex.decimalize(block["totalDifficulty"]) + + %POAAgent.Entity.Ethereum.Block{ + author: block["author"], + difficulty: difficulty, + extra_data: block["extraData"], + gas_limit: gas_limit, + gas_used: gas_used, + hash: block["hash"], + miner: block["miner"], + number: number, + parent_hash: block["parentHash"], + receipts_root: block["receiptsRoot"], + seal_fields: block["sealFields"], + sha3_uncles: block["sha3Uncles"], + signature: block["signature"], + size: size, + state_root: block["stateRoot"], + step: block["step"], + timestamp: timestamp, + total_difficulty: total_difficulty, + transactions: block["transactions"], + transactions_root: block["transactionsRoot"], + uncles: block["uncles"] + } + end + def format_block(_) do + nil + end + defimpl POAAgent.Entity.NameConvention do def from_elixir_to_node(x) do mapping = [ diff --git a/lib/poa_agent/plugins/collectors/eth/latest_block.ex b/lib/poa_agent/plugins/collectors/eth/latest_block.ex index 0dc0280..489c2d0 100644 --- a/lib/poa_agent/plugins/collectors/eth/latest_block.ex +++ b/lib/poa_agent/plugins/collectors/eth/latest_block.ex @@ -1,6 +1,8 @@ defmodule POAAgent.Plugins.Collectors.Eth.LatestBlock do use POAAgent.Plugins.Collector + alias POAAgent.Entity.Ethereum.Block + @moduledoc """ This is a Collector's Plugin which makes requests to a Ethereum node in order to know if a new block has been added. @@ -28,7 +30,7 @@ defmodule POAAgent.Plugins.Collectors.Eth.LatestBlock do with block_number <- get_latest_block(), {:ok, block} <- Ethereumex.HttpClient.eth_get_block_by_number(block_number, :false) do - block = format_block(block) + block = Block.format_block(block) range = history_range(block, 0) history = history(range) @@ -48,7 +50,7 @@ defmodule POAAgent.Plugins.Collectors.Eth.LatestBlock do {:notransfer, state} block_number -> {:ok, block} = Ethereumex.HttpClient.eth_get_block_by_number(block_number, :false) - {:transfer, format_block(block), %{state | last_block: block_number}} + {:transfer, Block.format_block(block), %{state | last_block: block_number}} end end @@ -58,12 +60,22 @@ defmodule POAAgent.Plugins.Collectors.Eth.LatestBlock do :ok end + @doc false + def history_range(block, last_block) do + max_blocks_history = 40 + + from = Enum.max([block.number - max_blocks_history, last_block + 1]) + to = Enum.max([block.number, 0]) + + from..to + end + @doc false def history(range) do history = for i <- range do block_number = "0x" <> Integer.to_string(i, 16) {:ok, block} = Ethereumex.HttpClient.eth_get_block_by_number(block_number, :false) - format_block(block) + Block.format_block(block) end %POAAgent.Entity.Ethereum.History{ @@ -85,51 +97,4 @@ defmodule POAAgent.Plugins.Collectors.Eth.LatestBlock do end end - @doc false - defp format_block(block) when is_map(block) do - difficulty = POAAgent.Format.Literal.Hex.decimalize(block["difficulty"]) - gas_limit = String.to_integer(POAAgent.Format.Literal.Hex.decimalize(block["gasLimit"])) - gas_used = String.to_integer(POAAgent.Format.Literal.Hex.decimalize(block["gasUsed"])) - number = String.to_integer(POAAgent.Format.Literal.Hex.decimalize(block["number"])) - size = String.to_integer(POAAgent.Format.Literal.Hex.decimalize(block["size"])) - timestamp = String.to_integer(POAAgent.Format.Literal.Hex.decimalize(block["timestamp"])) - total_difficulty = POAAgent.Format.Literal.Hex.decimalize(block["totalDifficulty"]) - - %POAAgent.Entity.Ethereum.Block{ - author: block["author"], - difficulty: difficulty, - extra_data: block["extraData"], - gas_limit: gas_limit, - gas_used: gas_used, - hash: block["hash"], - miner: block["miner"], - number: number, - parent_hash: block["parentHash"], - receipts_root: block["receiptsRoot"], - seal_fields: block["sealFields"], - sha3_uncles: block["sha3Uncles"], - signature: block["signature"], - size: size, - state_root: block["stateRoot"], - step: block["step"], - timestamp: timestamp, - total_difficulty: total_difficulty, - transactions: block["transactions"], - transactions_root: block["transactionsRoot"], - uncles: block["uncles"] - } - end - defp format_block(_) do - nil - end - - defp history_range(block, last_block) do - max_blocks_history = 40 - - from = Enum.max([block.number - max_blocks_history, last_block + 1]) - to = Enum.max([block.number, 0]) - - from..to - end - end \ No newline at end of file diff --git a/lib/poa_agent/plugins/transfers/web_socket/primus.ex b/lib/poa_agent/plugins/transfers/web_socket/primus.ex index 6188415..9343217 100644 --- a/lib/poa_agent/plugins/transfers/web_socket/primus.ex +++ b/lib/poa_agent/plugins/transfers/web_socket/primus.ex @@ -9,6 +9,7 @@ defmodule POAAgent.Plugins.Transfers.WebSocket.Primus do alias POAAgent.Plugins.Transfers.WebSocket.Primus alias POAAgent.Entity.Host.Information + alias POAAgent.Plugins.Collectors.Eth.LatestBlock defmodule State do @moduledoc false @@ -171,6 +172,8 @@ defmodule POAAgent.Plugins.Transfers.WebSocket.Primus do defmodule Client do @moduledoc false + alias POAAgent.Entity.Ethereum.Block + use WebSockex def send(handle, message) do @@ -210,7 +213,7 @@ defmodule POAAgent.Plugins.Transfers.WebSocket.Primus do defp handle_primus_event(["history", %{"max" => max, "min" => min}], state) do context = struct!(Primus.State, Application.get_env(:poa_agent, Primus)) - h = POAAgent.Plugins.Collectors.Eth.LatestBlock.history(min..max) + h = LatestBlock.history(min..max) history = for i <- h.history do Entity.NameConvention.from_elixir_to_node(i) @@ -225,10 +228,11 @@ defmodule POAAgent.Plugins.Transfers.WebSocket.Primus do {:reply, {:text, event}, state} end defp handle_primus_event(["history", false], state) do - epoch = 20 - {:ok, num} = Ethereumex.HttpClient.eth_block_number() - num = String.to_integer(POAAgent.Format.Literal.Hex.decimalize(num)) - {:reply, {:text, event}, state} = handle_primus_event(["history", %{"max" => num, "min" => num - epoch}], state) + {:ok, block_number} = Ethereumex.HttpClient.eth_block_number() + {:ok, block} = Ethereumex.HttpClient.eth_get_block_by_number(block_number, :false) + block = Block.format_block(block) + min..max = LatestBlock.history_range(block, 0) + {:reply, {:text, event}, state} = handle_primus_event(["history", %{"max" => max, "min" => min}], state) {:reply, {:text, event}, state} end defp handle_primus_event(data, state) do