Skip to content
This repository has been archived by the owner on Jun 12, 2023. It is now read-only.

Add denylist to cli and jsonrpc #1742

Merged
merged 2 commits into from
Aug 17, 2022
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions rebar.config
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@
{copy, "scripts/extensions/hbbft", "bin/extensions/hbbft"},
{copy, "scripts/extensions/info", "bin/extensions/info"},
{copy, "scripts/extensions/dkg", "bin/extensions/dkg"},
{copy, "scripts/extensions/denylist", "bin/extensions/denylist"},
{copy, "scripts/extensions/authorize", "bin/extensions/authorize"},
{copy, "scripts/extensions/print_keys", "bin/extensions/print_keys"},
{copy, "./_build/default/lib/blockchain/scripts/extensions/peer", "bin/extensions/peer"},
Expand Down Expand Up @@ -108,6 +109,7 @@
{trace, "extensions/trace"},
{txn, "extensions/txn"},
{dkg, "extensions/dkg"},
{denylist, "extensions/denylist"},
{authorize, "extensions/authorize"},
{repair, "extensions/repair"},
{print_keys, "extensions/print_keys"}
Expand Down
21 changes: 21 additions & 0 deletions scripts/extensions/denylist
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#!/bin/sh
if [ -t 0 ] ; then
CLIQUE_COLUMNS=$(stty size 2>/dev/null | cut -d ' ' -f 2)
export CLIQUE_COLUMNS
fi

export RELX_RPC_TIMEOUT=${RELX_RPC_TIMEOUT:-900}

j=1
l=$#
buf="[[\"denylist\","
while [ $j -le $l ]; do
buf="$buf\"$1\","
j=$(( j + 1 ))
shift
done

buf="${buf%?}]]"

relx_nodetool rpc blockchain_console command "$buf"
exit $?
120 changes: 120 additions & 0 deletions src/cli/miner_cli_denylist.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
%%%-------------------------------------------------------------------
%% @doc miner_cli_denylist
%% @end
%%%-------------------------------------------------------------------
-module(miner_cli_denylist).

-behavior(clique_handler).

-export([register_cli/0]).

register_cli() ->
register_all_usage(),
register_all_cmds().

register_all_usage() ->
lists:foreach(fun(Args) ->
apply(clique, register_usage, Args)
end,
[
denylist_usage(),
denylist_status_usage(),
denylist_check_usage()
]).

register_all_cmds() ->
lists:foreach(fun(Cmds) ->
[apply(clique, register_command, Cmd) || Cmd <- Cmds]
end,
[
denylist_cmd(),
denylist_status_cmd(),
denylist_check_cmd()
]).
%%
%% denylist
%%

denylist_usage() ->
[["denylist"],
["miner denylist commands\n\n",
" denylist status - Display status of denylist.\n"
" denylist check <address> - Check if address is on denylist.\n"
]
].

denylist_cmd() ->
[
[["denylist"], [], [], fun(_, _, _) -> usage end]
].


%%
%% denylist status
%%

denylist_status_cmd() ->
[
[["denylist", "status"], [], [], fun denylist_status/3]
].

denylist_status_usage() ->
[["denylist", "status"],
["denylist status\n\n",
" Display status of denylist.\n\n"
]
].

denylist_status(["denylist", "status"], [], []) ->
%% miner_poc_denylist:get_version/0 will throw an error if configuration missing yet return {ok, 0} if
%% configuration set but denylist is not loaded for another reason.
Text = try miner_poc_denylist:get_version() of
{ok, Version} when Version =/= 0 ->
clique_status:text(io_lib:format("Denylist version ~p loaded", [Version]));
{ok, 0} ->
clique_status:text("No denylist loaded. List still loading or error encountered. Try again or check application config.")
catch _:_ ->
clique_status:text("No denylist loaded. Missing or invalid denylist values in application config.")
end,
[Text];
denylist_status([], [], []) ->
usage.


%%
%% denylist check
%%

denylist_check_cmd() ->
[
[["denylist", "check", '*'], [], [], fun denylist_check/3]
].

denylist_check_usage() ->
[["denylist", "check"],
["denylist check <address>\n\n",
" Check if address is on denylist.\n\n"
]
].

denylist_check(["denylist", "check", Address], [], []) ->
%% miner_poc_denylist:check/1 will return false if no list loaded
%% so first check if a denylist is loaded using get_version/0
DenylistLoaded = try miner_poc_denylist:get_version() of
{ok, Version} when Version =/= 0 -> true;
_ -> false
catch _:_ -> false
end,
%% if denylist loaded, check if address is on list
case DenylistLoaded of
true ->
try miner_poc_denylist:check(libp2p_crypto:b58_to_bin(Address)) of
Result ->
[clique_status:text(io_lib:format("~p", [Result]))]
catch _:_ ->
[clique_status:text("Invalid address")]
end;
false -> [clique_status:text("No denylist loaded")]
end;
denylist_check([], [], []) ->
usage.
1 change: 0 additions & 1 deletion src/cli/miner_cli_info.erl
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@

-behavior(clique_handler).


-export([register_cli/0]).

-export([get_info/0]).
Expand Down
1 change: 1 addition & 0 deletions src/cli/miner_cli_registry.erl
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
,miner_cli_genesis
,miner_cli_info
,miner_cli_dkg
,miner_cli_denylist
]).

-export([register_cli/0]).
Expand Down
36 changes: 36 additions & 0 deletions src/jsonrpc/miner_jsonrpc_denylist.erl
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
-module(miner_jsonrpc_denylist).

-include("miner_jsonrpc.hrl").
-behavior(miner_jsonrpc_handler).

-export([handle_rpc/2]).

handle_rpc(<<"denylist_status">>, []) ->
try miner_poc_denylist:get_version() of
{ok, Version} when Version =/= 0 -> #{ loaded => true, version => Version };
{ok, _} -> #{ loaded => false }
catch _:_ ->
#{ loaded => false }
end;
handle_rpc(<<"denylist_status">>, Params) ->
?jsonrpc_error({invalid_params, Params});

handle_rpc(<<"denylist_check">>, #{ <<"address">> := Address}) ->
%% miner_poc_denylist:check/1 will return false if no list loaded
%% so first check if a denylist is loaded using get_version/0
DenylistLoaded = try miner_poc_denylist:get_version() of
{ok, Version} when Version =/= 0 -> true;
_ -> false
catch _:_ -> false
end,
case DenylistLoaded of
true ->
try miner_poc_denylist:check(?B58_TO_BIN(Address)) of
Denied -> #{ denied => Denied }
catch _:Reason ->
?jsonrpc_error({invalid_params, Reason})
end;
false -> ?jsonrpc_error({internal_error, denylist_not_loaded})
end;
handle_rpc(<<"denylist_check">>, Params) ->
?jsonrpc_error({invalid_params, Params}).
2 changes: 2 additions & 0 deletions src/miner_jsonrpc_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ handle_rpc_(<<"account_", _/binary>> = Method, Params) ->
miner_jsonrpc_accounts:handle_rpc(Method, Params);
handle_rpc_(<<"info_", _/binary>> = Method, Params) ->
miner_jsonrpc_info:handle_rpc(Method, Params);
handle_rpc_(<<"denylist_", _/binary>> = Method, Params) ->
miner_jsonrpc_denylist:handle_rpc(Method, Params);
handle_rpc_(<<"dkg_", _/binary>> = Method, Params) ->
miner_jsonrpc_dkg:handle_rpc(Method, Params);
handle_rpc_(<<"hbbft_", _/binary>> = Method, Params) ->
Expand Down