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

Commit

Permalink
Check for a public listen address before submitting heartbeat
Browse files Browse the repository at this point in the history
  • Loading branch information
PaulVMo committed Sep 23, 2021
1 parent 0bb8841 commit 1aaa7d4
Showing 1 changed file with 21 additions and 10 deletions.
31 changes: 21 additions & 10 deletions src/miner_val_heartbeat.erl
Original file line number Diff line number Diff line change
Expand Up @@ -84,16 +84,27 @@ handle_info({blockchain_event, {add_block, Hash, Sync, _Ledger}},
case blockchain_ledger_validator_v1:last_heartbeat(Val) of
N when (N + HBInterval) =< Height
andalso ((not Sync) orelse TimeAgo =< (60 * 30)) ->
%% we need to construct and submit a heartbeat txn
{ok, CBMod} = blockchain_ledger_v1:config(?predicate_callback_mod, Ledger),
{ok, Callback} = blockchain_ledger_v1:config(?predicate_callback_fun, Ledger),
UnsignedTxn =
blockchain_txn_validator_heartbeat_v1:new(Address, Height, CBMod:Callback()),
Txn = blockchain_txn_validator_heartbeat_v1:sign(UnsignedTxn, SigFun),
lager:info("submitting txn ~p for val ~p ~p ~p", [Txn, Val, N, HBInterval]),
Self = self(),
blockchain_worker:submit_txn(Txn, fun(Res) -> Self ! {sub, Res} end),
{noreply, State#state{txn_status = waiting}};
%% it is time to heartbeat, check for a public listening address
SwarmTID = blockchain_swarm:tid(),
PeerBook = libp2p_swarm:peerbook(SwarmTID),
{ok, Peer} = libp2p_peerbook:get(PeerBook, blockchain_swarm:pubkey_bin()),
ListenAddresses = libp2p_peer:listen_addrs(Peer),
case lists:any(fun libp2p_transport_tcp:is_public/1, ListenAddresses) of
true ->
%% we need to construct and submit a heartbeat txn
{ok, CBMod} = blockchain_ledger_v1:config(?predicate_callback_mod, Ledger),
{ok, Callback} = blockchain_ledger_v1:config(?predicate_callback_fun, Ledger),
UnsignedTxn =
blockchain_txn_validator_heartbeat_v1:new(Address, Height, CBMod:Callback()),
Txn = blockchain_txn_validator_heartbeat_v1:sign(UnsignedTxn, SigFun),
lager:info("submitting txn ~p for val ~p ~p ~p", [Txn, Val, N, HBInterval]),
Self = self(),
blockchain_worker:submit_txn(Txn, fun(Res) -> Self ! {sub, Res} end),
{noreply, State#state{txn_status = waiting}};
false ->
lager:warning("skipping heartbeat. no public listen address: ~p", [ListenAddresses]),
{noreply, State}
end;
_ -> {noreply, State}
end;
{error, not_found} ->
Expand Down

0 comments on commit 1aaa7d4

Please sign in to comment.