Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 43 additions & 6 deletions lib/network/rpc.ex
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,21 @@ defmodule Network.Rpc do
end

"dio_traffic" ->
chain_id = Base16.decode_int(hd(params))
chain_id_param =
case params do
[] -> throw(:badrequest)
[nil | _] -> throw(:badrequest)
[param | _] -> param
end
Comment on lines +263 to +268

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

This logic for extracting chain_id_param is duplicated in the dio_tickets handler (lines 313-318). To improve code reuse and maintainability, consider extracting this logic into a private function. For example:

defp get_chain_id_param(params) do
  case params do
    [] -> throw(:badrequest)
    [nil | _] -> throw(:badrequest)
    [param | _] -> param
  end
end

You could then call get_chain_id_param(params) in both places.

        chain_id_param = get_chain_id_param(params)


chain_id = Base16.decode_int(chain_id_param)
peak = RemoteChain.peaknumber(chain_id)
peak_epoch = RemoteChain.epoch(chain_id, peak)

epoch =
case params do
[_chain_id, epoch] -> Base16.decode_int(epoch)
[_chain_id, epoch] when epoch != nil -> Base16.decode_int(epoch)
[_chain_id, _epoch] -> peak_epoch
[_chain_id] -> peak_epoch
end
Comment on lines 275 to 279

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The last two clauses in this case statement produce the same result. They can be combined into a single, more concise clause using a tail match.

          case params do
            [_chain_id, epoch] when epoch != nil -> Base16.decode_int(epoch)
            [_chain_id | _] -> peak_epoch
          end


Expand Down Expand Up @@ -302,14 +310,22 @@ defmodule Network.Rpc do
})

"dio_tickets" ->
chain_id = Base16.decode_int(hd(params))
chain_id_param =
case params do
[] -> throw(:badrequest)
[nil | _] -> throw(:badrequest)
[param | _] -> param
end

chain_id = Base16.decode_int(chain_id_param)
peak = RemoteChain.peaknumber(chain_id)
peak_epoch = RemoteChain.epoch(chain_id, peak)

epoch =
case params do
[_chain_id, epoch] when is_integer(epoch) -> epoch
[_chain_id, epoch] when is_binary(epoch) -> Base16.decode_int(epoch)
[_chain_id, epoch] when epoch == nil -> peak_epoch
[_chain_id] -> peak_epoch
end
Comment on lines 325 to 330

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

Similar to the dio_traffic handler, the last two clauses in this case statement can be combined into a single clause for conciseness.

          case params do
            [_chain_id, epoch] when is_integer(epoch) -> epoch
            [_chain_id, epoch] when is_binary(epoch) -> Base16.decode_int(epoch)
            [_chain_id | _] -> peak_epoch
          end


Expand Down Expand Up @@ -427,9 +443,30 @@ defmodule Network.Rpc do
end

defp handle_proxy(method, params, opts) do
[node | params] = params
node = Base16.decode(node)
server = KademliaLight.find_node_object(node)
case params do
[] -> result(nil, 400)
[nil | _] -> result(nil, 400)
[node | params] ->
# Validate that params don't contain nil values for methods that require them
invalid_params =
method in ["dio_traffic", "dio_tickets"] and
params != [] and
case params do
[nil | _] -> true
_ -> false
end
Comment on lines +451 to +457

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

The logic to check for invalid parameters can be simplified. The params != [] check is redundant because [nil | _] will not match an empty list. You can use match?/2 for a more concise boolean check.

        invalid_params =
          method in ["dio_traffic", "dio_tickets"] and
            match?([nil | _], params)


if invalid_params do
result(nil, 400)
else
node = Base16.decode(node)
server = KademliaLight.find_node_object(node)
handle_proxy_continue(method, params, opts, node, server)
end
end
end

defp handle_proxy_continue(method, params, opts, node, server) do

cond do
method not in [
Expand Down
Loading