From d05224744970fe54c01673408d127c0450ba5210 Mon Sep 17 00:00:00 2001 From: Andrew Thompson Date: Tue, 7 Feb 2023 09:17:41 -0800 Subject: [PATCH 1/2] Add a stub ECC worker to reduce gwrs stalls & increase gwrs check timeout --- src/miner_gateway_port.erl | 2 +- src/miner_gateway_stub_ecc_worker.erl | 53 +++++++++++++++++++++++++++ src/miner_port_services_sup.erl | 9 ++++- 3 files changed, 62 insertions(+), 2 deletions(-) create mode 100644 src/miner_gateway_stub_ecc_worker.erl diff --git a/src/miner_gateway_port.erl b/src/miner_gateway_port.erl index 318314b77..5d869dcaf 100644 --- a/src/miner_gateway_port.erl +++ b/src/miner_gateway_port.erl @@ -30,7 +30,7 @@ -define(CONNECT_RETRY_WAIT, 100). -define(CONNECT_ATTEMPTS, 5). -define(HEALTHCHECK_INTERVAL, 600000). --define(HEALTHCHECK_TIMEOUT, 4000). +-define(HEALTHCHECK_TIMEOUT, 10000). start_link(Options) when is_list(Options) -> gen_server:start_link({local, ?MODULE}, ?MODULE, [Options], []). diff --git a/src/miner_gateway_stub_ecc_worker.erl b/src/miner_gateway_stub_ecc_worker.erl new file mode 100644 index 000000000..6fc0d1b42 --- /dev/null +++ b/src/miner_gateway_stub_ecc_worker.erl @@ -0,0 +1,53 @@ +-module(miner_gateway_stub_ecc_worker). +-include_lib("public_key/include/public_key.hrl"). + +-behaviour(gen_server). + +-export([ + start_link/1, + init/1, + handle_call/3, + handle_cast/2, + handle_info/2, + terminate/2 +]). + + + +-record(state, { + key = libp2p_crypto:generate_keys(ecc_compact) + }). + + +start_link(Options) when is_list(Options) -> + %% register as the miner_gateway_ecc_worker + gen_server:start_link({local, miner_gateway_ecc_worker}, ?MODULE, [Options], []). + +init(_Options) -> + {ok, #state{}}. + +handle_call(pubkey, _From, State = #state{key = #{public := Public}}) -> + {reply, {ok, Public}, State}; +handle_call({sign, Binary}, _From, State = #state{key = #{secret := Secret}}) -> + Reply = public_key:sign(Binary, sha256, Secret), + {reply, {ok, Reply}, State}; +handle_call({ecdh, {ecc_compact, {PubKey, {namedCurve, ?secp256r1}}}}, _From, State = #state{key = #{secret := Secret}}) -> + Reply = public_key:compute_key(PubKey, Secret), + {reply, {ok, Reply}, State}; +handle_call(_Msg, _From, State) -> + lager:debug("unhandled call ~p", [_Msg]), + {reply, ok, State}. + +handle_cast(reconnect, State) -> + {noreply, State}; +handle_cast(_Msg, State) -> + lager:debug("unhandled call ~p", [_Msg]), + {noreply, State}. + +handle_info(_Msg, State) -> + lager:debug("unhandled info ~p", [_Msg]), + {noreply, State}. + +terminate(_Reason, _State) -> + ok. + diff --git a/src/miner_port_services_sup.erl b/src/miner_port_services_sup.erl index 48bb57f50..f168a9861 100644 --- a/src/miner_port_services_sup.erl +++ b/src/miner_port_services_sup.erl @@ -92,10 +92,17 @@ init(_Opts) -> {client_ports, [UdpListenPort + 1, UdpListenPort + 2]} ], + ECCModule = case application:get_env(miner, stub_miner_ecc, true) of + false -> + miner_gateway_ecc_worker; + true -> + miner_gateway_stub_ecc_worker + end, + ChildSpecs = [ ?WORKER(miner_gateway_port, [GatewayPortOpts]), - ?WORKER(miner_gateway_ecc_worker, [GatewayECCWorkerOpts]), + ?WORKER(ECCModule, [GatewayECCWorkerOpts]), ?WORKER(miner_mux_port, [MuxOpts]) ], From 9d1ebb99fddf10adebab3cda8afd7c37f0b84b78 Mon Sep 17 00:00:00 2001 From: Andrew Thompson Date: Tue, 7 Feb 2023 09:39:30 -0800 Subject: [PATCH 2/2] Try again --- src/miner_gateway_ecc_worker.erl | 34 ++++++++++++----- src/miner_gateway_stub_ecc_worker.erl | 53 --------------------------- src/miner_port_services_sup.erl | 9 +---- 3 files changed, 26 insertions(+), 70 deletions(-) delete mode 100644 src/miner_gateway_stub_ecc_worker.erl diff --git a/src/miner_gateway_ecc_worker.erl b/src/miner_gateway_ecc_worker.erl index 034fd09b5..b799dd016 100644 --- a/src/miner_gateway_ecc_worker.erl +++ b/src/miner_gateway_ecc_worker.erl @@ -5,6 +5,8 @@ %%%------------------------------------------------------------------- -module(miner_gateway_ecc_worker). +-include_lib("public_key/include/public_key.hrl"). + -behaviour(gen_server). -export([ @@ -29,7 +31,8 @@ connection_monitor :: reference(), host = "localhost" :: string(), port = 4468 :: integer(), - transport = tcp :: tcp | ssl + transport = tcp :: tcp | ssl, + key = libp2p_crypto:generate_keys(ecc_compact) }). -define(CONNECT_RETRY_WAIT, 100). @@ -91,18 +94,31 @@ handle_call(pubkey, _From, State = #state{connection = Connection}) -> {reply, {ok, Reply}, State}; handle_call({sign, Binary}, _From, State = #state{connection = Connection}) -> Reply = - case rpc(Connection, #{data => Binary}, sign, ?MAX_RETRIES) of - {ok, #{signature := Signature}} -> Signature; - Error -> Error + case application:get_env(miner, stub_miner_ecc, true) of + true -> + #state{key = #{secret := Secret}} = State, + public_key:sign(Binary, sha256, Secret); + _ -> + case rpc(Connection, #{data => Binary}, sign, ?MAX_RETRIES) of + {ok, #{signature := Signature}} -> Signature; + Error -> Error + end end, {reply, {ok, Reply}, State}; handle_call({ecdh, PubKey}, _From, State = #state{connection = Connection}) -> Reply = - case - rpc(Connection, #{address => libp2p_crypto:pubkey_to_bin(PubKey)}, ecdh, ?MAX_RETRIES) - of - {ok, #{secret := Secret}} -> Secret; - Error -> Error + case application:get_env(miner, stub_miner_ecc, true) of + true -> + #state{key = #{secret := Secret}} = State, + {ecc_compact, {PK, {namedCurve, ?secp256r1}}} = PubKey, + public_key:compute_key(PK, Secret); + _ -> + case + rpc(Connection, #{address => libp2p_crypto:pubkey_to_bin(PubKey)}, ecdh, ?MAX_RETRIES) + of + {ok, #{secret := Secret}} -> Secret; + Error -> Error + end end, {reply, {ok, Reply}, State}; handle_call(_Msg, _From, State) -> diff --git a/src/miner_gateway_stub_ecc_worker.erl b/src/miner_gateway_stub_ecc_worker.erl deleted file mode 100644 index 6fc0d1b42..000000000 --- a/src/miner_gateway_stub_ecc_worker.erl +++ /dev/null @@ -1,53 +0,0 @@ --module(miner_gateway_stub_ecc_worker). --include_lib("public_key/include/public_key.hrl"). - --behaviour(gen_server). - --export([ - start_link/1, - init/1, - handle_call/3, - handle_cast/2, - handle_info/2, - terminate/2 -]). - - - --record(state, { - key = libp2p_crypto:generate_keys(ecc_compact) - }). - - -start_link(Options) when is_list(Options) -> - %% register as the miner_gateway_ecc_worker - gen_server:start_link({local, miner_gateway_ecc_worker}, ?MODULE, [Options], []). - -init(_Options) -> - {ok, #state{}}. - -handle_call(pubkey, _From, State = #state{key = #{public := Public}}) -> - {reply, {ok, Public}, State}; -handle_call({sign, Binary}, _From, State = #state{key = #{secret := Secret}}) -> - Reply = public_key:sign(Binary, sha256, Secret), - {reply, {ok, Reply}, State}; -handle_call({ecdh, {ecc_compact, {PubKey, {namedCurve, ?secp256r1}}}}, _From, State = #state{key = #{secret := Secret}}) -> - Reply = public_key:compute_key(PubKey, Secret), - {reply, {ok, Reply}, State}; -handle_call(_Msg, _From, State) -> - lager:debug("unhandled call ~p", [_Msg]), - {reply, ok, State}. - -handle_cast(reconnect, State) -> - {noreply, State}; -handle_cast(_Msg, State) -> - lager:debug("unhandled call ~p", [_Msg]), - {noreply, State}. - -handle_info(_Msg, State) -> - lager:debug("unhandled info ~p", [_Msg]), - {noreply, State}. - -terminate(_Reason, _State) -> - ok. - diff --git a/src/miner_port_services_sup.erl b/src/miner_port_services_sup.erl index f168a9861..48bb57f50 100644 --- a/src/miner_port_services_sup.erl +++ b/src/miner_port_services_sup.erl @@ -92,17 +92,10 @@ init(_Opts) -> {client_ports, [UdpListenPort + 1, UdpListenPort + 2]} ], - ECCModule = case application:get_env(miner, stub_miner_ecc, true) of - false -> - miner_gateway_ecc_worker; - true -> - miner_gateway_stub_ecc_worker - end, - ChildSpecs = [ ?WORKER(miner_gateway_port, [GatewayPortOpts]), - ?WORKER(ECCModule, [GatewayECCWorkerOpts]), + ?WORKER(miner_gateway_ecc_worker, [GatewayECCWorkerOpts]), ?WORKER(miner_mux_port, [MuxOpts]) ],